问题描述
<u href="javascript:toggleTable('*****')" onMouSEOver="this.style.color='red'" onMouSEOut="this.style.color='#000000'">*****</u>
清理 href 属性后 th 标签下的元素没有出现,如下所示:
<u onmouSEOver="this.style.color='red'" onmouSEOut="this.style.color='#000000'">*****</u>
PolicyFactory html = new HtmlPolicyBuilder()
.allowElements("u")
.allowAttributes("href","onMouSEOver","onMouSEOut").onElements("u")
.allowTextIn("u")
.allowCommonBlockElements()
.allowCommonInlineFormattingElements()
.allowStandardUrlProtocols()
.allowUrlProtocols("href")
.allowStyling()
.requireRelnofollowOnLinks()
.allowAttributes("href").globally()
.toFactory();
我在 table 和 tr 元素上也面临同样的问题。样式属性也不在 table 和 tr 元素下。
任何建议/帮助表示赞赏。 提前致谢。
解决方法
href
属性被删除,因为它没有允许的协议。如果 .allowUrlProtocols("href")
更改为 .allowUrlProtocols("javascript")
,则将允许。
允许 javascript
URL 或 onMouseOver
和 onMouseOut
属性将允许脚本执行但容易受到 XSS 攻击。
相反,您可以更改设计。鼠标悬停样式不需要 JavaScript。可以通过 CSS :hover
选择器来完成。
作为上面的答案,在 javascript
中添加 .allowUrlProtocols("javascript")
将允许通过,但是您仍然会在 "("
中获得 UTF 编码的 ")"
和 javascript:toggleTable('*****')
作为"("
& ")"
进入特殊字符。
您的输出看起来像 javascript:toggleTable0028'*****'0029
为了让它工作,你必须用字符串函数替换来替换它。
sanitizer.sanitize(HTML).replace("0028","(").replace("0029",")");