HTML-Entity 转义以防止 XSS

发布于 2024-12-27 10:12:03 字数 706 浏览 0 评论 0原文

我有一些用户输入。在我的代码中,我确保转义以下符号:

& -> & 
< -> &lt; 
> -> &gt;

OWASP 指出还有更多要转义的字符。

对于属性,我做了另一种转义:

& -> &amp; 
" -> &quot;

这确保所有属性都被 " 括起来。这让我确定我的 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 技术交流群。

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

发布评论

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

评论(3

终难遇 2025-01-03 10:12:03

我还使用 OWASP (ESAPI) 库来逃避对于不同类型的显示,请使用:

String html = ESAPI.encoder().encodeForHTML("hello < how > are 'you'");
String html_attr = ESAPI.encoder().encodeForHTMLAttribute("hello < how > are 'you'");
String js = ESAPI.encoder().encodeForJavaScript("hello < how > are 'you'");

HTML(假设jsp)

<tag attr="<%= html_attr %>" onclick="alert('<%= js %>')"><%= html %></tag>

更新2017

由于ESAPI编码器被视为遗留,更好的替代方案是创建并正在积极维护,我强烈建议使用 OWASP Java 编码器

如果您的项目已使用 ESAPI,则 集成 已添加,允许您使用此库进行编码。

他们的 wiki 页面 上解释了其用法,但为了完整起见,这就是您如何使用它对数据进行上下文编码:

// HTML Context
String html = Encoder.forHtml("u<ntrus>te'd'");

// HTML Attribute Context
String htmlAttr = Encoder.forHtmlAttribute("u<ntrus>te'd'");

// Javascript Attribute Context
String jsAttr = Encoder.forJavaScriptAttribute("u<ntrus>te'd'");

HTML(假设 jsp)

<div data-attr="<%= htmlAttr %>" onclick="alert('<%= jsAttr %>')">
    <%= html %>
</div>

PS:存在更多上下文并受库支持

I use the OWASP (ESAPI) library as well, to escape strings for different types of display, use :

String html = ESAPI.encoder().encodeForHTML("hello < how > are 'you'");
String html_attr = ESAPI.encoder().encodeForHTMLAttribute("hello < how > are 'you'");
String js = ESAPI.encoder().encodeForJavaScript("hello < how > are 'you'");

HTML (assume jsp)

<tag attr="<%= html_attr %>" onclick="alert('<%= js %>')"><%= html %></tag>

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 Context
String html = Encoder.forHtml("u<ntrus>te'd'");

// HTML Attribute Context
String htmlAttr = Encoder.forHtmlAttribute("u<ntrus>te'd'");

// Javascript Attribute Context
String jsAttr = Encoder.forJavaScriptAttribute("u<ntrus>te'd'");

HTML (assume jsp)

<div data-attr="<%= htmlAttr %>" onclick="alert('<%= jsAttr %>')">
    <%= html %>
</div>

PS: more contexts exist and are supported by the library

抽个烟儿 2025-01-03 10:12:03

我建议您使用 Appache Common Lang 库来转义字符串,例如转义 HTML:

String escapedString = org.apache.commons.lang.StringEscapeUtils.escapeHtml(String str);

该库有许多有用的方法来转义 HTML、XML、Javascript。

I recommend you to use Appache Common Lang library to escape strings, for exmaple to escape HTML:

String escapedString = org.apache.commons.lang.StringEscapeUtils.escapeHtml(String str);

the library has many useful methods to escape in HTML, XML, Javascript.

卸妝后依然美 2025-01-03 10:12:03

对于 Spring Boot 用户:

有一个内置方法:

org.springframework.web.util.HtmlUtils.htmlEscape

For Spring Boot users:

There's a built-in method:

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