HTML-Entity 转义以防止 XSS
我有一些用户输入。在我的代码中,我确保转义以下符号:
& -> &
< -> <
> -> >
OWASP 指出还有更多要转义的字符。
对于属性,我做了另一种转义:
& -> &
" -> "
这确保所有属性都被 " 括起来。这让我确定我的 html 属性,但不是 HTML 本身。
我想知道我的转义是否足够。我读过 这个帖子,但我仍然不确定我的担忧
(JavaScript 是通过 OWASP-Library 进行转义的)。
I have some user input. Within my code, I ensure that the following symbols are escaped:
& -> &
< -> <
> -> >
OWASP states that there are more chars to be escaped.
For attributes, I do another kind of escaping:
& -> &
" -> "
This ensures that all attributes are enclosed by ". This makes me sure about my html-attributes, but not about HTML itself.
I wonder if my escaping is sufficient. I've read this post, but I'm still not sure about my concern.
(JavaScripts are escaped with the OWASP-Library)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我还使用 OWASP (ESAPI) 库来逃避对于不同类型的显示,请使用:
HTML(假设jsp)
更新(2017)
由于ESAPI编码器被视为遗留,更好的替代方案是创建并正在积极维护,我强烈建议使用 OWASP Java 编码器。
如果您的项目已使用
ESAPI
,则 集成 已添加,允许您使用此库进行编码。他们的 wiki 页面 上解释了其用法,但为了完整起见,这就是您如何使用它对数据进行上下文编码:
HTML(假设 jsp)
PS:存在更多上下文并受库支持
I use the OWASP (ESAPI) library as well, to escape strings for different types of display, use :
HTML (assume jsp)
Update (2017)
As ESAPI Encoders are considered legacy, a better alternative has been created and is actively being maintained, I would strongly recommend using the OWASP Java Encoder instead.
If your project already uses
ESAPI
, an integration has been added that will allow you to use this library for encoding instead.The usage is explained on their wiki page, but for the sake of completion, this is how you can use it to contextually encode your data:
HTML (assume jsp)
PS: more contexts exist and are supported by the library
我建议您使用 Appache Common Lang 库来转义字符串,例如转义 HTML:
该库有许多有用的方法来转义 HTML、XML、Javascript。
I recommend you to use Appache Common Lang library to escape strings, for exmaple to escape HTML:
the library has many useful methods to escape in HTML, XML, Javascript.
对于 Spring Boot 用户:
有一个内置方法:
For Spring Boot users:
There's a built-in method: