Is it advisable to clean user inputs in express js?

After utilizing express-validator's escape() function to sanitize user inputs and storing the escaped data in the database using parameterized queries, I encountered an issue when rendering the input from the database with the EJS view engine. The escaped characters show up as ' instead of '. Do I need to unescape them during rendering, or should I avoid using the sanitize function altogether?

Answer №1

It is recommended to always save the original data as it is received from the user. For example, if the user inputs "<h1>", store that exact string in your database. Before displaying this data on the browser, make sure to sanitize the input either on the client or server side to prevent cross-site scripting (XSS) attacks. Instead of escaping the string using escape() in your express app, opt for storing the raw text and sanitizing it before rendering.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Halting execution: Trying to use the keyword 'import' which is not allowed here

0. April 2023: error cannot be reproduced anymore see here The error is no longer replicable due to bug fixes in react-scripts 5.0.1. 1 Even though the error is gone, the question and my self-answer still seem relevant to Angular users and others as we ...

Matching the cookie and header in express.js CSURF results in a 403 error code

My express server setup is quite simple: app.use(bodyParser.json()); app.use(cookieParser()); app.use(csurf({ cookie: true })); // routes app.use(Routes imported from another file); Currently, the client side consists of a basic form in react. ...

Retrieving modified object values in Node.js - A comprehensive guide

How can I retrieve the modified value of an object? The file index.js is executed before index2.js and here's what my code looks like: var object = { size:'5' }; var setSize = function(size) { object.size = size; } exports.object ...

Route.get() is expecting a callback function, however it received an object of undefined instead

In my nodejs application using typescript, I am working on separating the routing by introducing interfaces and controllers to handle the logic. app.ts const countryRoutes = require('./routes/countryroute') app.use('/countries', count ...

The dependency that was installed in the node_modules directory is now showing as missing the

I have encountered an issue with 2 TS packages. The first package, project-1, is installed as a dependency in the second package, project-2. While I am able to import and access all type definitions of project-1 in project-2, the dependencies (node_modules ...

Utilize i18next Localization in a Function Instead of App.js

Currently, I am utilizing ExpressJS and i18next. Within app.js var express = require('express') , i18n = require('i18next') , user = require('./routes/user') ... //internationalization i18n.init({ lng: 'en-US&apos ...

The procedure for deleting npm and node package on a Mac system

Currently, I am struggling to completely remove the npm and node packages that I initially installed in order to replace them with the Homebrew version. Can someone provide me with guidance on how to successfully eliminate the package versions of node.js a ...

Sanitizing form fields in node.js: Best practices and techniques

I recently installed the express-validator package to help me sanitize form fields. However, when I tried using it, I encountered an error: TypeError: req.sanitize is not a function. var express = require('express'); var router = express.Router() ...

Node.js v14.4.0 is not compatible with npm library

Hello there, fellow tech enthusiasts! I've encountered a peculiar issue while upgrading Node.js and npm on my Ubuntu 18.04.6 server. Following the installation guides, I initially used the commands: curl -fsSL https://deb.nodesource.com/setup_17.x | ...

Is it possible for a redis client to function without having a redis datastore installed?

Currently in my node web server, I am utilizing the npm module known as redis. Upon executing my code... const client = redis.createClient(); client.on("error", function (err) { console.log("Error " + err); }); client.hmset(["key", "test keys 1", "t ...

Encoding a string in JSON that contains the "#" symbol along with other special characters

The client side javascript code I have is as follows: <html> <script type="text/javascript" src="js/jquery.min.js"></script> <script> $(document).ready(function() { //var parameters = "a=" + JSON.stringify( ...

What steps should I take to create code that can generate a JWT token for user authentication and authorization?

I'm struggling to get this working. I have a dilemma with two files: permissionCtrl.js and tokenCtrl.js. My tech stack includes nJWT, Node.js/Express.js, Sequelize & Postgres. The permission file contains a function called hasPermission that is linke ...

Dealing with Unauthorized Errors (401) in an Axios React-Redux Application

I'm looking to handle the 401 unauthorized error that my server may output by dispatching an action. I've noticed many people using axios interceptors for this purpose. Could someone explain what interceptors are and guide me through implementing ...

Steps for extracting URL parameters from AWS API Gateway and passing them to a lambda function

After successfully setting up my API gateway and connecting it to my lambda function, I specified the URL as {id} with the intention of passing this parameter into the lambda. Despite numerous attempts using both the default template and a custom one for ...

How can a response header be included for a redirect in Express?

In my NodeJS Express application, I am attempting to redirect users to a different URL and then include a specific header in the response. Is it feasible to achieve this functionality? For instance, let's say a request is redirected to https://example ...

Encountered an issue while attempting to launch the express

I've encountered an error while attempting to launch my express server, and despite my best efforts, I can't seem to figure out the cause. It was operational for a period of time before suddenly ceasing function, though I'm unable to pinpoin ...

Having trouble trying to update information using Sequelize and MySQL

I am in the process of developing an application that involves updating posts from the past. A dedicated page has been created for this purpose along with a corresponding route. app.post("/posts/:id/update", function(req, res){ Post.findAll({ ...

Mongoose Exception: Question does not have a constructor

After coming across numerous questions on stack overflow related to this error without finding a solution that works for me, I've decided to ask my own question. (probably due to my limited understanding of javascript/node/mongoose) The modules I am ...

What are the recommended events for initiating the spawning of a child process in order to guarantee a callback is always executed?

Utilizing node to encapsulate an executable, I'm utilizing the spawn event emitter. For additional information, you can refer to the documentation. There are various events that can be subscribed to. child = spawn("path/to/exe", args) child.on(' ...

Unable to retrieve an image from various sources

My setup includes an Express server with a designated folder for images. app.use(express.static("files")); When attempting to access an image from the "files" folder at localhost:3000/test, everything functions properly. However, when trying to ...