简体   繁体   English

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

[英]How to prevent JavaScript injection (XSS) when JSTL escapeXml is false

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. 但是,以这种形式,如果他们输入JavaScript而不是仅输入文本,则他们可以轻松地注入他们想要执行的操作。 In order to prevent it, I can set escapeXml to true, but then normal HTML would be escaped as well. 为了防止这种情况,我可以将escapeXml设置为true,但是普通的HTML也会被转义。

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

Is there any other way to prevent JavaScript injection rather than setting this to true? 还有其他方法可以防止JavaScript注入,而不是将其设置为true?

I'd recommend using Jsoup for this. 我建议为此使用Jsoup Here's an extract of relevance from its site . 这是其网站的相关摘录。

Sanitize untrusted HTML 清理不受信任的HTML

Problem 问题

You want to allow untrusted users to supply HTML for output on your website (eg as comment submission). 您想允许不受信任的用户提供HTML以在您的网站上输出(例如,作为评论提交)。 You need to clean this HTML to avoid cross-site scripting (XSS) attacks. 您需要清除此HTML,以避免跨站点脚本 (XSS)攻击。

Solution

Use the jsoup HTML Cleaner with a configuration specified by a Whitelist . 将jsoup HTML CleanerWhitelist指定的配置一起使用。

 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. Jsoup还提供了更多的优势。 See also Pros and Cons of HTML parsers in Java . 另请参见Java中HTML解析器的优缺点

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. 您需要将服务器上的HTML文本解析为XML,然后丢弃所有不在严格白名单中的标签和属性。
(And check the URLs in href and src attributes) (并检查hrefsrc属性中的URL)

This is exactly the intent of the OWASP AntiSamy project . 这恰恰是OWASP AntiSamy项目的目的。

The OWASP AntiSamy project is a few things. OWASP AntiSamy项目是几件事。 Technically, it is an API for ensuring user-supplied HTML/CSS is in compliance within an application's rules. 从技术上讲,它是一个API,用于确保用户提供的HTML / CSS符合应用程序规则。 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. 换句话说,它是一个API,可帮助您确保客户端不会在其提供给个人资料,注释等的HTML中提供恶意货物代码,这些代码会持久保存在服务器上。 The term "malicious code" in regards to web applications usually mean "JavaScript." 关于Web应用程序的术语“恶意代码”通常表示“ JavaScript”。 Cascading Stylesheets are only considered malicious when they invoke the JavaScript engine. 级联样式表仅在调用JavaScript引擎时才被认为是恶意的。 However, there are many situations where "normal" HTML and CSS can be used in a malicious manner. 但是,在许多情况下,“正常” HTML和CSS可能以恶意方式使用。 So we take care of that too. 因此,我们也要注意这一点。

Another alternative is the OWASP HTMLSanitizer project. 另一个选择是OWASP HTMLSanitizer项目。 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. 我认为它尚未通过任何GA / Stable版本,因此在评估该库时应考虑到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM