这是清理 XML 的有效 cfscript 代码,有两种方法,一种清除较高的国际字符,另一种仅清除破坏 XML 的较低 ASCII 字符,如果发现更多字符,只需扩展过滤规则。
<cfscript>
function cleanHighAscii(text){
var buffer = createObject("java", "java.lang.StringBuffer").init();
var pattern = createObject("java", "java.util.regex.Pattern").compile(javaCast( "string", "[^\x00-\x7F]" ));
var matcher = pattern.Matcher(javaCast( "string", text));
while(matcher.find()){
var value = matcher.group();
var asciiValue = asc(value);
if ((asciiValue == 8220) OR (asciiValue == 8221))
value = """";
else if ((asciiValue == 8216) || (asciiValue == 8217))
value = "'";
else if (asciiValue == 8230)
value = "...";
else
value = "&###asciiValue#;";
matcher.AppendReplacement(buffer, javaCast( "string", value ));
}
matcher.AppendTail(buffer);
return buffer.ToString();
}
function removeSubAscii(text){
return rereplaceNoCase(text, "\x1A","&###26#;", "all");
}
function XMLSafe(text){
text = cleanHighAscii(text);
text = removeSubAscii(text);
return text;
}
</cfscript>
其他可能性是用户 CF10 函数 encodeForXML():
https://learn.adobe.com/wiki/display/coldfusionen/EncodeForXML
或者直接使用 CF10 附带的 ESAPI,或者从 OWASP 站点https://www.owasp.org/index.php/ESAPI_Overview将 ESAPI jar 添加到您的旧 CF :
var esapi = createObject("java", "org.owasp.esapi.ESAPI");
var esapiEncoder = esapi.encoder();
return esapiEncoder.encodeForXML(text);