【发布时间】:2012-07-02 18:39:18
【问题描述】:
目前我正在将此方法与 jQuery 解决方案一起使用,以从可能的 XSS 攻击中清除字符串。
sanitize:function(str) {
// return htmlentities(str,'ENT_QUOTES');
return $('<div></div>').text(str).html().replace(/"/gi,'"').replace(/'/gi,''');
}
但我觉得这还不够安全。我错过了什么吗?
我在这里尝试了 phpjs 项目中的 htmlentities: http://phpjs.org/functions/htmlentities:425/
但它有点错误并返回一些额外的特殊符号。可能是旧版本?
例如:
htmlentities('test"','ENT_QUOTES');
生产:
test&quot;
但应该是:
test"
你是如何通过 javascript 处理这个的?
【问题讨论】:
-
你打算如何使用“净化”的字符串?
-
以文本形式插入到 html 文档中。作为 href="sanitized" 或 src="sanitized" 或sanitized
-
从哪里触发插入?您想使用 Javascript 将字符串动态插入到已打开的页面中,还是使用 PHP 将字符串插入到服务器生成的 HTML 文档中?
-
是动态使用javascript。字符串来自不受信任的来源。
-
使用 Caja 的 html_sanitize.js。 stackoverflow.com/questions/12253686/…
标签: javascript xss html-sanitizing