【问题标题】:how to use policy of owasp-java-html-sanitizer on a jsp page如何在 jsp 页面上使用 owasp-java-html-sanitizer 的策略
【发布时间】:2013-04-08 12:17:06
【问题描述】:

我的任务是防止我们的网站遭受跨站点脚本 (XSS)。这个概念对我来说是新的,我搜索了很多并得到了 owasp-java-html-sanitizer。我用

创建了自己的策略
public static final PolicyFactory POLICY_DEFINITION = new HtmlPolicyBuilder()

通过使用.allowAttributes,我设计了它。 但是现在我一无所知如何使用它......我发现以下代码 sn-p:

System.err.println("[Reading from STDIN]");
    // Fetch the HTML to sanitize.
    String html = CharStreams.toString(new InputStreamReader(System.in,
            Charsets.UTF_8));
    // Set up an output channel to receive the sanitized HTML.
    HtmlStreamRenderer renderer = HtmlStreamRenderer.create(System.out,
    // Receives notifications on a failure to write to the output.
            new Handler<IOException>() {
                public void handle(IOException ex) {
                    Throwables.propagate(ex); // System.out suppresses
                                                // IOExceptions
                }
            },
            // Our HTML parser is very lenient, but this receives
            // notifications on
            // truly bizarre inputs.
            new Handler<String>() {
                public void handle(String x) {
                    throw new AssertionError(x);
                }
            });
    // Use the policy defined above to sanitize the HTML.
    HtmlSanitizer.sanitize(html, POLICY_DEFINITION.apply(renderer));
}

但是我怎样才能将它应用到我的 JSP 中,因为我认为这适用于简单的 HTML。 请帮忙。

【问题讨论】:

    标签: java xss owasp html-sanitizing


    【解决方案1】:

    您可以将渲染器附加到 StringWriter 而不是 System.out,但使用策略的 sanitize convenience method 可能更容易:

    public java.lang.String sanitize(@Nullable
                                     java.lang.String html)
    

    清理 HTML 字符串的便捷函数。

    返回一个 HTML 字符串,可以安全地插入到您的 JSP 页面中。

    【讨论】:

      猜你喜欢
      • 2012-07-07
      • 1970-01-01
      • 2015-12-27
      • 2017-04-09
      • 2014-08-03
      • 2015-05-06
      • 1970-01-01
      • 2022-11-12
      • 2011-11-23
      相关资源
      最近更新 更多