为了防止跨站点脚本(XSS),我使用了 OWASP 推荐的ESAPI(企业安全 API)。 esapi.jar 文件已包含在 ColdFusion 的早期版本中,但在 CF10 中,您现在可以轻松调用其中一些有用的函数:encodeForJavascript()
、encodeForHTML()
、encodeForURL()
、encodeForCSS()
和encodeForHTMLAttribute()
.
我遇到了麻烦encodeForJavascript()
,我失去了反斜杠......
<cfoutput>
<cfif isDefined("url.name")>
<!--- Here is the problem, this is identical to the original ascii32to126 string except for one char is missing, the backslash between the brackets ...Z[]... --->
#url.name#
<cfabort>
</cfif>
<!---
ASCII 32 thru 126
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
In the line below I double up on the double-quotes and pounds in order to get the cfset to work
--->
<cfset ascii32to126 = "!""##$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~">
<script>
function locateTo(value)
{
window.location='thisPage.cfm?name='+encodeURIComponent(value);
//alert('thisPage.cfm?name='+encodeURIComponent(value));
}
locateTo('#encodeForJavaScript(ascii32to126)#');
</script>
</cfoutput>
我第一次打电话encodeForJavaScript()
是因为我们在 JavaScript 上下文中。
然后我打电话encodeURIComponent()
以确保正确构建 URL。
一切正常,但在结果页面上我丢失了反斜杠\
。我在这里想念什么?
(是的,我知道我还必须保护我的输出位置#url.name#
。对于这个实验,我没有这样做,因为我需要查看源以查看字符串是否与原始字符串匹配。)
**更新** - 我正在运行 ColdFusion 10,并应用了所有最新的补丁。问题似乎出在encodeForJavaScript()
.
也失败了JSStringFormat()
。这样做表明两者都缺少反斜杠......
<cfset ascii32to126 = "!""##$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~">
<cfoutput>
#encodeForHTML(encodeForJavaScript(ascii32to126))#
<br><br>
#encodeForHTML(JSStringFormat(ascii32to126))#
</cfoutput>