0

我们正在向我们的网站添加富文本编辑,包括添加 youtube 视频的功能。但另一方面,我们希望保持安全并防止 XSS/HTML 注入。以前我们使用以下代码来转义数据:

ESAPI.encoder().encodeForHTML
ESAPI.encoder().encodeForJavaScript

现在我们必须添加某种允许标签的白名单。有没有办法实现这个功能?

4

3 回答 3

3

我们决定在 ESAPI 上使用 Jsoup。现在代码如下所示:

protected String encodeHtml(String html) {
    return Jsoup.clean(html, getWhitelist());
}

private Whitelist getWhitelist() {
    return new Whitelist()
            .addTags("a", "b", "blockquote", "br", "caption", "cite", "code", "col", "colgroup", "dd", "div", "dl",
                    "dt", "em", "h1", "h2", "h3", "h4", "h5", "h6", "i", "img", "li", "ol", "p", "pre", "q",
                    "small", "strike", "strong", "sub", "sup", "table", "tbody", "td", "tfoot", "th", "thead",
                    "tr", "u", "ul", "iframe")

            .addAttributes("a", "href", "title").addAttributes("blockquote", "cite")
            .addAttributes("col", "span", "width").addAttributes("colgroup", "span", "width")
            .addAttributes("img", "align", "alt", "height", "src", "title", "width")
            .addAttributes("ol", "start", "type").addAttributes("q", "cite")
            .addAttributes("table", "summary", "width")
            .addAttributes("td", "abbr", "axis", "colspan", "rowspan", "width")
            .addAttributes("th", "abbr", "axis", "colspan", "rowspan", "scope", "width")
            .addAttributes("ul", "type")

            .addProtocols("a", "href", "ftp", "http", "https", "mailto")
            .addProtocols("blockquote", "cite", "http", "https").addProtocols("img", "src", "http", "https")
            .addProtocols("q", "cite", "http", "https");
}
于 2014-05-23T10:32:01.527 回答
1

不是开箱即用。我看到的唯一选择是扩展库。看看org.owasp.esapi.codecs.HTMLEntityCodec具体的方法mkCharacterToEntityMap()

这是编解码器告诉编码器类什么要转义,什么不能转义的地方。我会定义你自己的Codec.

然后您可能必须添加一个方法来/扩展DefaultEncoder该类,以便您可以在您想使用它的地方使用您的编解码器。也许像DefaultEncoder.encodeForHTML(String input, Codec codec)

如果白名单需要更加可配置,那么您可能需要更改它,以便您可以发送正则表达式,"input1|input2|input3"这样编解码器就会知道要忽略什么。然后,您可能希望通过配置此白名单,esapi.properties以便您的技术支持可以在生产中更改它,而无需完全重新部署。

于 2014-05-22T19:31:19.240 回答
0

ESAPI Validator.getValidSafeHTML()接近于给你想要的东西。它目前在后台使用 AntiSamy。您也不妨试试 OWASP Java HTML Sanitizer。它的重量很轻,几乎没有(可能没有)依赖关系,并且维护得很好。

-凯文

于 2014-05-28T03:07:17.160 回答