检查输入并将其转换为适当的类型以防止 XSS
Check input and convert it to appropriate type to prevent XSS
假设我从用户那里得到 "name",他输入 <script>alert("you got xss!");</script>
但是在我转换的服务器上:
name= name_from_frontend.toString();
在将其保存到数据库之前。
同样,我检查 int using parseInt()
float using parseFloat()
boolean using typeof(variable_name)
等等。
是否还需要转义<
、&
等OWASP提到的字符来防止XSS?
对字符串类型调用 toString()
没有帮助。您可能需要去除邪恶的标签,例如 script
等
大多数服务器端语言,例如 python (https://pypi.python.org/pypi/bleach)、php (strip_tags()
) 等,都有专注于清除恶意标记的函数库,因此您可以稍后安全地使用该输入。
如果您想要一个 JS 解决方案,您应该检查 https://www.npmjs.com/package/sanitize-html,它可以完成这项工作,但旨在与 Node.js 一起使用。示例:
clean = sanitizeHtml(dirty, {
allowedTags: [ 'b', 'i', 'em', 'strong', 'a' ],
allowedAttributes: {
'a': [ 'href' ]
}
});
Enapupe 大体上是正确的,但他的回答 You probably need to strip evil tags such as script, etc.
部分不是很清楚。
正确的说法是 - 您绝对必须使用 OWASP guidelines 删除 XSS,转换为字符串或不转换为字符串都没有区别!
假设我从用户那里得到 "name",他输入 <script>alert("you got xss!");</script>
但是在我转换的服务器上:
name= name_from_frontend.toString();
在将其保存到数据库之前。
同样,我检查 int using parseInt()
float using parseFloat()
boolean using typeof(variable_name)
等等。
是否还需要转义<
、&
等OWASP提到的字符来防止XSS?
对字符串类型调用 toString()
没有帮助。您可能需要去除邪恶的标签,例如 script
等
大多数服务器端语言,例如 python (https://pypi.python.org/pypi/bleach)、php (strip_tags()
) 等,都有专注于清除恶意标记的函数库,因此您可以稍后安全地使用该输入。
如果您想要一个 JS 解决方案,您应该检查 https://www.npmjs.com/package/sanitize-html,它可以完成这项工作,但旨在与 Node.js 一起使用。示例:
clean = sanitizeHtml(dirty, {
allowedTags: [ 'b', 'i', 'em', 'strong', 'a' ],
allowedAttributes: {
'a': [ 'href' ]
}
});
Enapupe 大体上是正确的,但他的回答 You probably need to strip evil tags such as script, etc.
部分不是很清楚。
正确的说法是 - 您绝对必须使用 OWASP guidelines 删除 XSS,转换为字符串或不转换为字符串都没有区别!