防止 Node.js/服务器端 javascript 中的 XSS
知道如何防止对 Node.js 应用程序的 XSS 攻击吗?任何处理删除 href、onclick 属性等中的 javascript 的库。来自发布的数据?
我不想为此编写正则表达式:)
有什么建议吗?
Any idea how one would go about preventing XSS attacks on a node.js app? Any libs out there that handle removing javascript in hrefs, onclick attributes,etc. from POSTed data?
I don't want to have to write a regex for all that :)
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
如果还有人在看这个。
似乎 xss 包很受欢迎并得到维护
从文档中:
If someone is still watching this.
It seems like the xss package to be popular and maintained
From the docs:
你应该尝试库 npm“insane”。
https://github.com/bevacqua/insane
我在生产中尝试过,效果很好。大小非常小(压缩后大约 3kb)。
该文档非常容易阅读和理解。
https://github.com/bevacqua/insane
You should try library npm "insane".
https://github.com/bevacqua/insane
I try in production, it works well. Size is very small (around ~3kb gzipped).
The documentation is very easy to read and understand.
https://github.com/bevacqua/insane
我创建了一个捆绑 Caja HTML Sanitizer
http://github.com/theSmaw/Caja 的模块-HTML-Sanitizer
https://www.npmjs.com/package/sanitizer
任何反馈表示赞赏。
I've created a module that bundles the Caja HTML Sanitizer
http://github.com/theSmaw/Caja-HTML-Sanitizer
https://www.npmjs.com/package/sanitizer
Any feedback appreciated.
在客户端清理/重写 HTML 的答案之一建议从 Google Caja 借用了 JS 中基于白名单的 HTML 清理程序,据我通过快速滚动可以看出,它实现了 HTML SAX 解析器,而不依赖于浏览器的 DOM。
更新: 另外,请记住,Caja 消毒程序显然已经接受了全面、专业的安全审查,而正则表达式则因很容易以危及安全的方式出现拼写错误而闻名。
更新2017-09-24:现在还有DOMPurify。我还没有使用过它,但看起来它满足或超过了我所寻找的每一点:
尽可能依赖运行时环境提供的功能。 (通过尽可能依赖经过充分测试的成熟实现,对于性能和最大限度地提高安全性都很重要。)
默认配置旨在尽可能少地剥离,同时仍保证删除 JavaScript。
toStaticHTML
。高度可配置,使其适合对可以包含任意 HTML 的输入实施限制,例如所见即所得或 Markdown 注释字段。 (事实上,这是这里的顶部)
他们非常重视兼容性和可靠性
One of the answers to Sanitize/Rewrite HTML on the Client Side suggests borrowing the whitelist-based HTML sanitizer in JS from Google Caja which, as far as I can tell from a quick scroll-through, implements an HTML SAX parser without relying on the browser's DOM.
Update: Also, keep in mind that the Caja sanitizer has apparently been given a full, professional security review while regexes are known for being very easy to typo in security-compromising ways.
Update 2017-09-24: There is also now DOMPurify. I haven't used it yet, but it looks like it meets or exceeds every point I look for:
Relies on functionality provided by the runtime environment wherever possible. (Important both for performance and to maximize security by relying on well-tested, mature implementations as much as possible.)
Default configuration designed to strip as little as possible while still guaranteeing removal of javascript.
toStaticHTML
under IE8 and IE9.Highly configurable, making it suitable for enforcing limitations on an input which can contain arbitrary HTML, such as a WYSIWYG or Markdown comment field. (In fact, it's the top of the pile here)
They're serious about compatibility and reliability
所有常用技术也适用于 node.js 输出,这意味着:
我不确定 Node.js 是否为此提供了一些内置功能,但类似的东西应该可以完成这项工作:
All usual techniques apply to node.js output as well, which means:
I'm not sure if node.js comes with some built-in for this, but something like that should do the job:
我最近发现 node-validator 由 node-validator “ rel="nofollow">克里索。
示例
XSS 函数弃用
XSS 函数在此库中不再可用。
https://github.com/chriso/validator.js#deprecations
I recently discovered node-validator by chriso.
Example
XSS Function Deprecation
The XSS function is no longer available in this library.
https://github.com/chriso/validator.js#deprecations
您还可以查看 ESAPI。该库有一个javascript 版本。它非常坚固。
You can also look at ESAPI. There is a javascript version of the library. It's pretty sturdy.
在较新版本的
validator
模块中,您可以使用以下脚本来防止 XSS 攻击:In newer versions of
validator
module you can use the following script to prevent XSS attack:尝试一下 npm 模块
strip-js
。它执行以下操作:https://www.npmjs.com/package/strip-js
Try out the npm module
strip-js
. It performs the following actions:https://www.npmjs.com/package/strip-js
更新2021-04-16:xss是一个用于过滤用户输入以防止XSS攻击的模块。
使用白名单指定的配置清理不受信任的 HTML(以防止 XSS)。
访问 https://www.npmjs.com/package/xss
项目主页:http://jsxss.com
Update 2021-04-16: xss is a module used to filter input from users to prevent XSS attacks.
Sanitize untrusted HTML (to prevent XSS) with a configuration specified by a Whitelist.
Visit https://www.npmjs.com/package/xss
Project Homepage: http://jsxss.com