如何防止人们在 Spring MVC 中进行 XSS?
Spring MVC 中如何防止 XSS?现在,我只是将输出用户文本的所有位置放入 JSTL
标记或 fn:escapeXml()
函数中,但这似乎很容易出错,因为我可能会错过一个地方。
有没有一种简单的系统方法来防止这种情况发生?也许像过滤器什么的?我通过在控制器方法上指定 @RequestParam
参数来收集输入。
What should I do to prevent XSS in Spring MVC? Right now I am just putting all places where I output user text into JSTL <c:out>
tags or fn:escapeXml()
functions, but this seems error prone as I might miss a place.
Is there an easy systematic way to prevent this? Maybe like a filter or something? I'm collecting input by specifying @RequestParam
parameters on my controller methods.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
不应仅依赖
,还应该使用 antixss 库,它不仅可以编码,还可以清理输入中的恶意脚本。 OWASP Antisamy 是最好的库之一,它非常灵活并且可以配置(使用 xml 策略文件)根据要求。例如,如果应用程序仅支持文本输入,则最通用的 策略文件 来清理和删除大部分 html 标签。同样,如果应用程序支持需要各种 html 标签的 html 编辑器(例如 tinymce),则可以使用更灵活的策略,例如 eBay 政策文件
Instead of relying only on
<c:out />
, an antixss library should also be used, which will not only encode but also sanitize malicious script in input. One of the best library available is OWASP Antisamy, it's highly flexible and can be configured(using xml policy files) as per requirement.For e.g. if an application supports only text input then most generic policy file provided by OWASP can be used which sanitizes and removes most of the html tags. Similarly if application support html editors(such as tinymce) which need all kind of html tags, a more flexible policy can be use such as ebay policy file
名为RequestWrapper.java的第二个类的代码是:
package com.filter;
/* 表示对于浏览器发出的每个请求,它将调用
CrossScriptingFilter 类。它将解析来自请求的所有组件/元素
将用空字符串 ie 替换黑客放置的所有 javascript 标签
The code second class named RequestWrapper.java is :
package com.filter;
The /* indicates that for every request made from browser, it will call
CrossScriptingFilter class. Which will parse all the components/elements came from the request &
will replace all the javascript tags put by hacker with empty string i.e
在 Spring 中,您可以转义
对于
web.xml
文件中的整个应用程序:对于文件本身中给定页面上的所有表单:
对于每种形式:
In Spring you can escape the html from JSP pages generated by
<form>
tags. This closes off a lot avenues for XSS attacks, and can be done automatically in three ways:For the entire application in the
web.xml
file:For all forms on a given page in the file itself:
For each form:
我通过
@Valid
对所有输入对象使用 Hibernate Validator(绑定和@RequestBody
json,请参阅 https://dzone.com/articles/spring-31-valid-requestbody)。所以@org.hibernate.validator.constraints.SafeHtml
对我来说是一个很好的解决方案。Hibernate
SafeHtmlValidator
依赖于org.jsoup
,因此需要再添加一个项目依赖项:For bean
User
with fieldfor update attempts with value < code>在控制器中
,或者
对于绑定抛出
BindException
,对于@RequestBody< 抛出
MethodArgumentNotValidException
/code> 带有默认消息:验证器也适用于绑定,就像之前的持久化一样。
应用程序可以在 http://topjava.herokuapp.com/ 进行测试
更新:另请参阅@GuyT 的评论
CVE-2019-10219 和 @SafeHtml 的状态
我自己的简历:它是安全的并且可以使用,直到找到更好的解决方案。
更新:由于从
hibernate.validator
中删除@SafeHtml/SafeHtmlValidator
使用自己的NoHtmlValidator
,请参阅 https://stackoverflow.com/a/68888601/548473I use Hibernate Validator via
@Valid
for all input objects (binding and@RequestBody
json, see https://dzone.com/articles/spring-31-valid-requestbody). So@org.hibernate.validator.constraints.SafeHtml
is a good solution for me.Hibernate
SafeHtmlValidator
depends onorg.jsoup
, so it's needed to add one more project dependencies:For bean
User
with fieldfor update attempt with value
<script>alert(123)</script>
in controlleror
is thrown
BindException
for binding andMethodArgumentNotValidException
for@RequestBody
with default message:Validator works as well for binding, as before persisting.
Apps could be tested at http://topjava.herokuapp.com/
UPDATE: see also comment from @GuyT
CVE-2019-10219 and status of @SafeHtml
Resume for myself: it is safe and could be used, until solution better be found.
UPDATE: due to remove
@SafeHtml/SafeHtmlValidator
fromhibernate.validator
use ownNoHtmlValidator
, see https://stackoverflow.com/a/68888601/548473尝试XSSFilter。
Try XSSFilter.
当您尝试防止 XSS 时,考虑上下文非常重要。例如,如果您在 JavaScript 代码片段中的变量内输出数据,而不是在 HTML 标记或 HTML 属性中输出数据,那么如何转义以及转义什么内容是非常不同的。
我有这里的一个例子。另请查看OWASP XSS 预防备忘单。
所以简短的答案是,确保按照 Tendayi Mawushe 的建议转义输出,但在 HTML 属性或 JavaScript 中输出数据时要特别小心。
When you are trying to prevent XSS, it's important to think of the context. As an example how and what to escape is very different if you are ouputting data inside a variable in a JavaScript snippet as opposed to outputting data in an HTML tag or an HTML attribute.
I have an example of this here. Also checkout the OWASP XSS Prevention Cheat Sheet.
So the short answer is, make sure you escape output like suggested by Tendayi Mawushe, but take special care when you are outputting data in HTML attributes or JavaScript.
您首先如何收集用户输入?如果您使用的是
FormController
,此问题/答案可能会有所帮助:Spring:绑定到命令时转义输入
How are you collecting user input in the first place? This question / answer may assist if you're using a
FormController
:Spring: escaping input when binding to command
始终手动检查您使用的方法、标签,并确保它们最终总是逃逸(一次)。框架在这方面有很多错误和差异。
概述:http://www.gablog.eu/online/node/91
Always check manually the methods, tags you use, and make sure that they always escape (once) in the end. Frameworks have many bugs and differences in this aspect.
An overview: http://www.gablog.eu/online/node/91