nodejs过滤输入

问题描述:

在PHP中过滤输入数据我使用函数 htmlspecialchars和mysql_real_escape_string。在nodejs中有这样的功能吗?nodejs过滤输入

我需要检查我的rounter函数中的所有输入以防止像xss这样的黑客攻击。 谢谢!

node-validator为这个完美的库,它有两种验证和卫生/过滤许多功能,例如:

entityDecode()     //Decode HTML entities 
entityEncode() 
xss()       //Remove common XSS attack vectors from text (default) 
xss(true)      //Remove common XSS attack vectors from images 

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 
+0

感谢。该库中是否有类似php的strip_tags功能? – Erik 2012-01-10 16:11:43

+0

我没有看到它,但是在这里它是函数strip_tags(oldString){return} oldString.replace(/()+)>)/ ig,“”); }' – alessioalex 2012-01-10 16:21:28

Google Caja HTML sanitizer有一个NodeJS包。或者你使用的答案提供here

function escapeHtml(unsafe) { 
    return unsafe 
     .replace(/&/g, "&") 
     .replace(/</g, "&lt;") 
     .replace(/>/g, "&gt;") 
     .replace(/"/g, "&quot;") 
     .replace(/'/g, "&#039;"); 
} 

对于SQL这取决于你使用的库,但其中大部分将难逃参数化查询。