当 JSTL escapeXml 为 false 时如何防止 JavaScript 注入 (XSS)

发布于 2024-10-03 04:02:21 字数 289 浏览 4 评论 0原文

我有一个表格,人们可以添加他们的东西。然而,在这种形式下,如果他们输入 JavaScript 而不仅仅是文本,他们就可以轻松地注入他们想做的任何事情。为了防止这种情况,我可以将 escapeXml 设置为 true,但是普通的 HTML 也会被转义。

<td><c:out value="${item.textValue}" escapeXml="true" /></td>

除了将其设置为 true 之外,还有其他方法可以防止 JavaScript 注入吗?

I have a form that people can add their stuff. However, in that form, if they enter JavaScript instead of only text, they can easily inject whatever they want to do. In order to prevent it, I can set escapeXml to true, but then normal HTML would be escaped as well.

<td><c:out value="${item.textValue}" escapeXml="true" /></td>

Is there any other way to prevent JavaScript injection rather than setting this to true?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

慢慢从新开始 2024-10-10 04:02:21

我建议使用 Jsoup 。以下是来自其网站的相关摘录。

清理不受信任的 HTML

问题

您希望允许不受信任的用户提供 HTML 以在您的网站上输出(例如作为评论提交)。您需要清理此 HTML 以避免跨站脚本 (XSS) 攻击。< /p>

解决方案

使用 jsoup HTML Cleaner 使用 白名单.

字符串不安全 = 
      "

链接

"; String safe = Jsoup.clean(unsafe, Whitelist.basic()); // 现在:

链接

因此,在处理提交的文本期间,您基本上需要做的就是以下操作:

String text = request.getParameter("text");
String safe = Jsoup.clean(text, Whitelist.basic());
// Persist 'safe' in DB instead.

Jsoup 还提供了更多优势。另请参阅 HTML 解析器的优点和缺点在Java中

I'd recommend using Jsoup for this. Here's an extract of relevance from its site.

Sanitize untrusted HTML

Problem

You want to allow untrusted users to supply HTML for output on your website (e.g. as comment submission). You need to clean this HTML to avoid cross-site scripting (XSS) attacks.

Solution

Use the jsoup HTML Cleaner with a configuration specified by a Whitelist.

String unsafe = 
      "<p><a href='http://example.com/' onclick='stealCookies()'>Link</a></p>";
String safe = Jsoup.clean(unsafe, Whitelist.basic());
      // now: <p><a href="http://example.com/" rel="nofollow">Link</a></p>

So, all you basically need to do is the the following during processing the submitted text:

String text = request.getParameter("text");
String safe = Jsoup.clean(text, Whitelist.basic());
// Persist 'safe' in DB instead.

Jsoup offers more advantages than that as well. See also Pros and Cons of HTML parsers in Java.

余厌 2024-10-10 04:02:21

您需要将服务器上的 HTML 文本解析为 XML,然后丢弃不在严格白名单中的任何标记和属性。
(并检查 hrefsrc 属性中的 URL)

You need to parse the HTML text on the server as XML, then throw out any tags and attributes that aren't in a strict whitelist.
(And check the URLs in href and src attributes)

陌伤浅笑 2024-10-10 04:02:21

这正是 OWASP AntiSamy 项目 的目的。

OWASP AntiSamy 项目有很多内容。从技术上讲,它是一个 API,用于确保用户提供的 HTML/CSS 符合应用程序的规则。另一种说法可能是:它是一个 API,可以帮助您确保客户端不会在他们为个人资料、评论等提供的 HTML 中提供恶意货物代码,这些代码会保留在服务器上。关于 Web 应用程序的术语“恶意代码”通常指的是“JavaScript”。级联样式表仅在调用 JavaScript 引擎时才被视为恶意。然而,在很多情况下,“正常”的 HTML 和 CSS 可能会被恶意使用。所以我们也会处理这个问题。

另一种选择是 OWASP HTMLSanitizer 项目。它速度更快,依赖性更少,并且目前得到了项目负责人的积极支持。我认为它尚未经历任何 GA/稳定版本,因此您在评估此库时应该考虑这一点。

This is exactly the intent of the OWASP AntiSamy project.

The OWASP AntiSamy project is a few things. Technically, it is an API for ensuring user-supplied HTML/CSS is in compliance within an application's rules. Another way of saying that could be: It's an API that helps you make sure that clients don't supply malicious cargo code in the HTML they supply for their profile, comments, etc., that get persisted on the server. The term "malicious code" in regards to web applications usually mean "JavaScript." Cascading Stylesheets are only considered malicious when they invoke the JavaScript engine. However, there are many situations where "normal" HTML and CSS can be used in a malicious manner. So we take care of that too.

Another alternative is the OWASP HTMLSanitizer project. It is faster, has less dependencies and actively supported by the project lead as of now. I don’t think it has gone through any GA/Stable release yet so you should consider that when evaluating this library.

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