HTML-Entity转义以防止XSS [英] HTML-Entity escaping to prevent XSS

查看:1437
本文介绍了HTML-Entity转义以防止XSS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些用户输入。在我的代码中,我确保以下歌曲可以被转义:

 & - > &放大器;放大器; 
< - > &安培; LT;
> - > &安培; GT;

OWASP 我读过,还有更多的字符需要转义。



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

 & - > &放大器;放大器; 
- >& quot;

确保所有属性都是封闭的按。这使我确信我的html属性,但不是关于HTML本身。



我想知道,如果我的转义就足够了。我已阅读此帖子,但我仍然不确定我的担忧。



(JavaScripts会被OWASP-Library转义)

解决方案

我也使用OWASP库( ESAPI )库来逃脱字符串用于不同类型的显示,请使用:

  String html = ESAPI.encoder()。encodeForHTML(hello< how> ; 你是'); 
String html_attr = ESAPI.encoder()。encodeForHTMLAttribute(你好&how'是'你');
String js = ESAPI.encoder()。encodeForJavaScript(你好&how'是'你');

HTML(假设jsp)

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

更新 2017



由于ESAPI编码器被认为是 legacy ,一个更好的选择已经创建并且正在积极维护,我强烈建议使用 ESAPI ,那么请使用<编码器> rel =noreferrer> OWASP Java Encoder 。 c>,集成已被添加,这将允许您使用此库进行编码。



用法在其维基页面,但为了完成,这是我们如何才能做到的它可以对数据进行上下文编码:

  // HTML上下文
字符串html = Encoder.forHtml(u< ntrus> te'd');

// HTML属性上下文
String htmlAttr = Encoder.forHtmlAttribute(u< ntrus> te'd');

// Javascript属性上下文
String jsAttr = Encoder.forJavaScriptAttribute(u< ntrus> te'd');

HTML(假设jsp)

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

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


I have some user-input. Within my code I ensure that the following sings are escaped:

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

At OWASP I read, that there are more chars to be escaped.

For attributes I do another kind of escaping:

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

It is ensured, 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)

解决方案

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

这篇关于HTML-Entity转义以防止XSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆