9

encodeForHtml()(CF10 中的新功能)vs htmlEditFormat(),它们有什么不同?

4

2 回答 2

10

我认为它与 java 的 OWASP ESAPI 中的 encodeForHTML 函数相同。更安全地避免 XSS 攻击以使用 HTML 中的内容。

<cfsavecontent variable="htmlcontent">
<html>
    <head>
        <script>function hello() {alert('hello')}</script>
    </head>
    <body>
        <a href="#bookmark">Book Mark &amp; Anchor</a><br/>
        <div class="xyz">Div contains & here.</div>
        <IMG     SRC=&#x6A&#x61&#x76&#x61&#x73&#x63&#x72&#x69&#x70&#x74&#x3A&#x61&#x6C&#x65&#x72&#x74&#x28&#    x27&#x58&#x53&#x53&#x27&#x29>
    <IMG SRC=&#0000106&#0000097&#0000118&#0000097&#0000115&#0000099&#0000114&#0000105&#0000112&#0000116&#0000058&#0000097&#0000108&#0000101&#0000114&#0000116&#0000040&#0000039&#0000088&#0000083&#0000083&#0000039&#0000041>
</body>
</html></cfsavecontent>

<cfoutput>#htmleditformat(htmlcontent)#</cfoutput>
<br />
<cfoutput>#encodeforhtml(htmlcontent)#</cfoutput>
于 2012-05-15T09:35:11.817 回答
5

EncodeFor* 函数基于 OWASP ESAPI 库。主要区别在于 HTMLEditFormat() 仅替换“坏”字符串,如&<>用好的字符串,如&amp;&lt;&gt;EncodeForHTML() 更智能,一个优点是它可以识别已经编码的内容,而不是对其进行双重编码.

例如,如果用户向您的网站提交了以下内容:

<div>
Here is <i>test</i> html content includes<br/>
<script>alert('hello')</script>
Notice how &amp; rendered with both functions.
</div>

HTMLEditFormat() 和 EncodeForHTML() 都可以正确地转义 '<' 和 '>' 字符。但是 HTMLEditFormat() 会盲目地&再次编码,这样你的输出看起来像:

... how &amp;amp; rendered ...

否则它会像 encodeForHTML() 一样:

... how &amp; rendered ...

HTMLEditFormat() 无法判断与符号已经编码,因此它再次重新编码。这是一个简单的示例,但它演示了 ESAPI 库如何更智能,因此更安全。

底线,没有理由在 CF10+ 中使用 HTMLEditFormat()。为获得最大程度的保护,您应该将 Format 函数替换为 Encode 函数。

上面的完整示例和更多背景信息位于 isummation:http ://www.isummation.com/blog/day-2-avoid-cross-site-scripting-xss-using-coldfusion-10-part-1/

于 2016-04-06T00:21:50.310 回答