Sanitizing Data
You've seen how to validate input that comes from the outside world to your Express app.
There's one thing you quickly learn when you run a public-facing server: never trust the input.
Even if you sanitize and make sure that people can't enter weird things using client-side code, you'll still be subject to people using tools (even just the browser devtools) to POST directly to your endpoints.
Or bots trying every possible combination of exploit known to humans.
What you need to do is sanitize your input.
The express-validator
package you already use to validate input can also conveniently be used to perform sanitization.
Say you have a POST endpoint that accepts the name, email, and age parameters:
You might validate it using:
You can add sanitization by piping the sanitization methods after the validation ones:
Here I used the methods:
trim()
trims characters (whitespace by default) at the beginning and at the end of a stringescape()
replaces<
,>
,&
,'
,"
and/
with their corresponding HTML entitiesnormalizeEmail()
canonicalizes an email address. Accepts several options to lowercase email addresses or subaddresses (for exampleflavio+newsletters@gmail.com
)
Other sanitization methods:
blacklist()
removes characters that appear in the blacklistwhitelist()
removes characters that do not appear in the whitelistunescape()
replaces HTML encoded entities with<
,>
,&
,'
,"
and/
ltrim()
like trim(), but only trims characters at the start of the stringrtrim()
like trim(), but only trims characters at the end of the stringstripLow()
removes ASCII control characters, which are normally invisible
Force conversion to a format:
toBoolean()
converts the input string to a boolean. Everything except for '0', 'false' and '' returns true. In strict mode only '1' and 'true' return true.toDate()
converts the input string to a date, or null if the input is not a datetoFloat()
converts the input string to a float, or NaN if the input is not a floattoInt()
converts the input string to an integer, or NaN if the input is not an integer
Like with custom validators, you can create a custom sanitizer.
In the callback function you just return the sanitized value:
Last updated
Was this helpful?