【发布时间】:2012-02-07 00:36:02
【问题描述】:
在 PHP 中过滤输入数据我使用函数 htmlspecialchars 和 mysql_real_escape_string。 nodejs中有这样的功能吗?
我需要检查我的路由器函数中的所有输入,以防止像 xss 这样的黑客攻击。 谢谢!
【问题讨论】:
在 PHP 中过滤输入数据我使用函数 htmlspecialchars 和 mysql_real_escape_string。 nodejs中有这样的功能吗?
我需要检查我的路由器函数中的所有输入,以防止像 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
【讨论】:
function strip_tags(oldString) { return oldString.replace(/(<([^>]+)>)/ig,""); }
Google Caja HTML sanitizer 有一个 NodeJS 包。或者你使用here提供的答案:
function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
对于 SQL,这取决于您使用的库,但其中大多数会转义参数化查询。
【讨论】: