防止 Node.js/服务器端 javascript 中的 XSS

发布于 2024-09-19 07:15:42 字数 124 浏览 8 评论 0原文

知道如何防止对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

随波逐流 2024-09-26 07:15:43

如果还有人在看这个。
似乎 xss 包很受欢迎并得到维护

从文档中:

var xss = require("xss");
var html = xss('<script>alert("xss");</script>');
console.log(html);

If someone is still watching this.
It seems like the xss package to be popular and maintained

From the docs:

var xss = require("xss");
var html = xss('<script>alert("xss");</script>');
console.log(html);

薆情海 2024-09-26 07:15:43

你应该尝试库 npm“insane”。
https://github.com/bevacqua/insane

我在生产中尝试过,效果很好。大小非常小(压缩后大约 3kb)。

  • 清理 html
  • 删除所有评估 js 的属性或标签
  • 您可以允许不需要清理的属性或标签

该文档非常容易阅读和理解。
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).

  • Sanitize html
  • Remove all attributes or tags who evaluate js
  • You can allow attributes or tags that you don't want sanitize

The documentation is very easy to read and understand.
https://github.com/bevacqua/insane

掐死时间 2024-09-26 07:15:42

我创建了一个捆绑 Caja HTML Sanitizer

npm install 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

npm install sanitizer

http://github.com/theSmaw/Caja-HTML-Sanitizer

https://www.npmjs.com/package/sanitizer

Any feedback appreciated.

她如夕阳 2024-09-26 07:15:42

在客户端清理/重写 HTML 的答案之一建议从 Google Caja 借用了 JS 中基于白名单的 HTML 清理程序,据我通过快速滚动可以看出,它实现了 HTML SAX 解析器,而不依赖于浏览器的 DOM。

更新: 另外,请记住,Caja 消毒程序显然已经接受了全面、专业的安全审查,而正则表达式则因很容易以危及安全的方式出现拼写错误而闻名。

更新2017-09-24:现在还有DOMPurify。我还没有使用过它,但看起来它满足或超过了我所寻找的每一点:

  • 尽可能依赖运行时环境提供的功能。 (通过尽可能依赖经过充分测试的成熟实现,对于性能和最大限度地提高安全性都很重要。)

    • 依赖于浏览器的 DOM 或 Node.JS 的 jsdom
  • 默认配置旨在尽可能少地剥离,同时仍保证删除 JavaScript。

    • 支持 HTML、MathML 和 SVG
    • 在 IE8 和 IE9 下回退到 Microsoft 专有的、不可配置的 toStaticHTML
  • 高度可配置,使其适合对可以包含任意 HTML 的输入实施限制,例如所见即所得或 Markdown 注释字段。 (事实上​​,这是这里的顶部)

    • 支持常用的标签/属性白名单/黑名单和 URL 正则表达式白名单
    • 具有特殊选项,可进一步清理某些常见类型的 HTML 模板元字符。
  • 他们非常重视兼容性和可靠性

    • 在 16 种不同的浏览器以及 Node.JS 的三个不同主要版本上运行的自动化测试。
    • 为了确保开发者和 CI 主机都在同一页面上,我们发布了锁定文件。

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.)

    • Relies on either a browser's DOM or jsdom for Node.JS.
  • Default configuration designed to strip as little as possible while still guaranteeing removal of javascript.

    • Supports HTML, MathML, and SVG
    • Falls back to Microsoft's proprietary, un-configurable 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)

    • Supports the usual tag/attribute whitelisting/blacklisting and URL regex whitelisting
    • Has special options to sanitize further for certain common types of HTML template metacharacters.
  • They're serious about compatibility and reliability

    • Automated tests running on 16 different browsers as well as three diffferent major versions of Node.JS.
    • To ensure developers and CI hosts are all on the same page, lock files are published.
逆夏时光 2024-09-26 07:15:42

所有常用技术也适用于 node.js 输出,这意味着:

  • 黑名单不起作用。
  • 您不应该为了保护 HTML 输出而过滤输入。它不会起作用,或者会通过不必要地扭曲数据而起作用。
  • 您应该对 HTML 输出中的文本进行 HTML 转义。

我不确定 Node.js 是否为此提供了一些内置功能,但类似的东西应该可以完成这项工作:

function htmlEscape(text) {
   return text.replace(/&/g, '&').
     replace(/</g, '<').  // it's not neccessary to escape >
     replace(/"/g, '"').
     replace(/'/g, ''');
}

All usual techniques apply to node.js output as well, which means:

  • Blacklists will not work.
  • You're not supposed to filter input in order to protect HTML output. It will not work or will work by needlessly malforming the data.
  • You're supposed to HTML-escape text in HTML output.

I'm not sure if node.js comes with some built-in for this, but something like that should do the job:

function htmlEscape(text) {
   return text.replace(/&/g, '&').
     replace(/</g, '<').  // it's not neccessary to escape >
     replace(/"/g, '"').
     replace(/'/g, ''');
}
走野 2024-09-26 07:15:42

我最近发现 node-validatornode-validator “ rel="nofollow">克里索。

示例

get('/', function (req, res) {

  //Sanitize user input
  req.sanitize('textarea').xss(); // No longer supported
  req.sanitize('foo').toBoolean();

});

XSS 函数弃用

XSS 函数在此库中不再可用。

https://github.com/chriso/validator.js#deprecations

I recently discovered node-validator by chriso.

Example

get('/', function (req, res) {

  //Sanitize user input
  req.sanitize('textarea').xss(); // No longer supported
  req.sanitize('foo').toBoolean();

});

XSS Function Deprecation

The XSS function is no longer available in this library.

https://github.com/chriso/validator.js#deprecations

写给空气的情书 2024-09-26 07:15:42

您还可以查看 ESAPI。该库有一个javascript 版本。它非常坚固。

You can also look at ESAPI. There is a javascript version of the library. It's pretty sturdy.

朮生 2024-09-26 07:15:42

在较新版本的 validator 模块中,您可以使用以下脚本来防止 XSS 攻击:

  var validator = require('validator');

  var escaped_string = validator.escape(someString);

In newer versions of validator module you can use the following script to prevent XSS attack:

  var validator = require('validator');

  var escaped_string = validator.escape(someString);
冷情 2024-09-26 07:15:42

尝试一下 npm 模块 strip-js。它执行以下操作:

  • 清理 HTML
  • 删除脚本标签
  • 删除包含 JavaScript 代码的属性,例如“onclick”、“onerror”等
  • 删除包含 JavaScript 代码的“href”属性

https://www.npmjs.com/package/strip-js

Try out the npm module strip-js. It performs the following actions:

  • Sanitizes HTML
  • Removes script tags
  • Removes attributes such as "onclick", "onerror", etc. which contain JavaScript code
  • Removes "href" attributes which contain JavaScript code

https://www.npmjs.com/package/strip-js

蓝眸 2024-09-26 07:15:42

更新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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文