简体   繁体   English

如何在jsp页面上使用owasp-java-html-sanitizer的策略

[英]how to use policy of owasp-java-html-sanitizer on a jsp page

I have been given a task to prevent our website from Cross-site Scripting (XSS). 我负有防止我们的网站跨站点脚本(XSS)的任务。 The concept is new to me and I googled a lot and got owasp-java-html-sanitizer. 这个概念对我来说并不陌生,我在Google上搜索了很多,并获得了owasp-java-html-sanitizer。 I created my own policy with 我制定了自己的政策

public static final PolicyFactory POLICY_DEFINITION = new HtmlPolicyBuilder()

by using .allowAttributes , I designed it . 通过使用.allowAttributes ,我对其进行了设计。 But now I am clueless how to use it ...I found following code snippet: 但是现在我不知道如何使用它...我发现了以下代码片段:

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));
}

but how can I apply this to my JSP because I think this is for simple HTML. 但是如何将其应用于我的JSP,因为我认为这是针对简单的HTML。 Please help. 请帮忙。

You could attach the renderer to a StringWriter instead of System.out , but it's probably easier to just use the policy's sanitize convenience method : 您可以将渲染器附加到StringWriter而不是System.out ,但是仅使用策略的sanitize方便方法可能会更容易:

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

A convenience function that sanitizes a string of HTML. 方便功能,用于清理HTML字符串。

which returns a string of HTML that is safe to interpolate into your JSP page. 它返回安全插入到您的JSP页面中的HTML字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM