我正在使用 antisamy 库来清理应用程序的输入,以防止 XSS。我对嵌套标签有疑问,例如:
<<b>script>alert('xss');<</b>/script>
我的清理方法如下:
public String clean(String input) {
if (input == null) {
return null;
}
input = StringEscapeUtils.unescapeHtml(input);
try {
Policy policy = Policy.getInstance(getClass().getResourceAsStream("/antisamy-textonly-policy.xml"));
AntiSamy antiSamy = new AntiSamy();
CleanResults cleanResults = antiSamy.scan(input, policy);
String cleaned = cleanResults.getCleanHTML();
return StringEscapeUtils.unescapeHtml(cleaned);
} catch (PolicyException e) {
....
} catch (ScanException e) {
....
}
}
我针对此类输入的测试失败:
public void doubleTagTest() {
def cleaned = xss.clean("<<b>script>alert('xss');<</b>/script>");
assert cleaned.isEmpty();
}
与:
Assertion failed: assert cleaned.isEmpty() | | | false alert('xss');
at org.codehaus.groovy.runtime.InvokerHelper.assertFailed(InvokerHelper.java:386)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.assertFailed(ScriptBytecodeAdapter.java:658)
您知道如何在不递归调用 xss.clean()
的情况下处理它吗?
最佳答案
Antisamy 正在产生正确的结果 - 格式错误的标签被删除,留下纯文本 alert('xss');
.
考虑以下因素
<b<i>>Hello World!</b</i>>
粗体和斜体标签不知何故变得困惑 - antisamy 正确地去除了损坏的标签,留下文本 Hello World!
哪个是对的。在您的原始测试中存在看起来像 javascript 的纯文本,这并不重要 - 有害的 <script>
标签已被删除。
关于java - XSS 清理嵌套 html 标签输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19512376/