In PHP to filter inputs data I use functions htmlspecialchars and mysql_real_escape_string. Is there any functions like this in nodejs?
I need to check all inputs in my rounter functions to prevent hacker attacks like xss. Thanks!
node-validator is the perfect library for this, it has many functions for both validation and sanitation / filtering, for example:
entityDecode() //Decode HTML entities
entityEncode()
xss() //Remove common XSS attack vectors from text (default)
xss(true) //Remove common XSS attack vectors from images
or
contains(str)
notContains(str)
regex(pattern, modifiers) //Usage: regex(/[a-z]/i) or regex('[a-z]','i')
notRegex(pattern, modifiers)
len(min, max) //max is optional
isUUID(version) //Version can be 3 or 4 or empty, see http://en.wikipedia.org/wiki/Universally_unique_identifier
isDate() //Uses Date.parse() - regex is probably a better choice
isAfter(date) //Argument is optional and defaults to today
isBefore(date) //Argument is optional and defaults to today
isIn(options) //Accepts an array or string
There is a NodeJS package for the Google Caja HTML sanitizer. Or you use the answer provided here:
function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
For SQL it depends on what library you are using, but most of them will escape parameterized queries.