encodeForHtml()
(CF10 中的新功能)vs htmlEditFormat()
,它们有什么不同?
2 回答
我认为它与 java 的 OWASP ESAPI 中的 encodeForHTML 函数相同。更安全地避免 XSS 攻击以使用 HTML 中的内容。
<cfsavecontent variable="htmlcontent">
<html>
<head>
<script>function hello() {alert('hello')}</script>
</head>
<body>
<a href="#bookmark">Book Mark & Anchor</a><br/>
<div class="xyz">Div contains & here.</div>
<IMG SRC=javascript:alert(&# x27XSS')>
<IMG SRC=javascript:alert('XSS')>
</body>
</html></cfsavecontent>
<cfoutput>#htmleditformat(htmlcontent)#</cfoutput>
<br />
<cfoutput>#encodeforhtml(htmlcontent)#</cfoutput>
EncodeFor* 函数基于 OWASP ESAPI 库。主要区别在于 HTMLEditFormat() 仅替换“坏”字符串,如&
,<
并>
用好的字符串,如&
,<
而>
EncodeForHTML() 更智能,一个优点是它可以识别已经编码的内容,而不是对其进行双重编码.
例如,如果用户向您的网站提交了以下内容:
<div>
Here is <i>test</i> html content includes<br/>
<script>alert('hello')</script>
Notice how & rendered with both functions.
</div>
HTMLEditFormat() 和 EncodeForHTML() 都可以正确地转义 '<' 和 '>' 字符。但是 HTMLEditFormat() 会盲目地&
再次编码,这样你的输出看起来像:
... how &amp; rendered ...
否则它会像 encodeForHTML() 一样:
... how & 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/