我正在构建一个web应用程序,用户可以在其中发布由html文档组成的帖子。例如,用户可以通过dev工具更改文档。因此,文档由任意用户输入组成。我很担心XSS,因为我在这方面没有经验。为了防止XSS注入,我使用jsoup来解析内容。我做了一个定制的白名单,但我不确定(嗯,我怀疑)我什么都想过了。
我保存的内容如下:
public void setContent(String content){
content = Jsoup.clean(content, WhiteList4jsoup.getWhiteList());
this.content = content;
}我的汤汁白名单是这样的:
public static Whitelist getWhiteList() {
Whitelist whiteList = Whitelist.basic();
whiteList.addAttributes("h1", "style");
whiteList.addAttributes("h2", "style");
whiteList.addAttributes("h3", "style");
whiteList.addAttributes("h4", "style");
whiteList.addAttributes("h5", "style");
whiteList.addAttributes("h6", "style");
whiteList.addAttributes("h7", "style");
whiteList.addAttributes("a", "href");
whiteList.addAttributes("img", "src", "style");
whiteList.addAttributes("div", "style", "aria-label");
whiteList.addAttributes("table", "style", "rules", "border");
whiteList.addAttributes("thead", "style");
whiteList.addAttributes("th", "style");
whiteList.addAttributes("tr", "style");
whiteList.addAttributes("td", "style");
// Those are customs tags that I insert in the document when user
// clicks on a button. I Let them in so when I display the message content I can grab them tags with javascript and do some business with it.
whiteList.addAttributes("customContainer", "type", "title", "measured");
whiteList.addAttributes("columnCustom", "title", "value");
return whiteList;
}这些都有潜在的危险吗?
实际上,我在iframe中是一个wysiwyg编辑器,所以我已经控制了它。因为用户可以创建图表,表格和这样的结果是相当混乱的标记,我自己。这就是为什么我想要一个wysiwyg但是,如果太危险,我将走与堆栈溢出相同的路线,并在底部进行预览。我已经完成了我的编辑,但我认为预览不会太费劲。
如果是这样的话,我不打算使用任何可用的标记,比如bbcode,因为我的内容太多定制了(图片、链接、google的图表、表格)。如果我要建一个,有什么需要我注意的吗?
发布于 2015-09-22 23:14:29
你想做的事情本质上是相当危险的。我强烈建议使用类似于BBCode或降价之类的内容,因为它们专门设计为允许相当丰富的内容而不存在脚本注入风险(尽管有时会发现其中的bug)。
尽管如此,我在这里看到的只有两个非常明显的脚本风险。
<a href="javascript:alert('XSS!')">Click here!</a>将创建一些看起来像“正常”超链接的东西,但当单击时,它将在站点原点(如XSS)内执行代码。您可能会注意到,这在StackExchange (它使用Markdown)上不起作用。expression值,这只是简单的嵌入式脚本,但也有一个叫做"CSS行为“的东西,它允许将外部指定的脚本附加到元素中,这实际上与在页面中嵌入脚本标记相同。除此之外,允许CSS在一般情况下是危险的。使用CSS,恶意用户可以完全覆盖页面,更改或隐藏某些内容,甚至使页面看起来与其完全不同。
https://security.stackexchange.com/questions/100916
复制相似问题