簡體   English   中英

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

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

我負有防止我們的網站跨站點腳本(XSS)的任務。 這個概念對我來說並不陌生,我在Google上搜索了很多,並獲得了owasp-java-html-sanitizer。 我制定了自己的政策

public static final PolicyFactory POLICY_DEFINITION = new HtmlPolicyBuilder()

通過使用.allowAttributes ,我對其進行了設計。 但是現在我不知道如何使用它...我發現了以下代碼片段:

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。 請幫忙。

您可以將渲染器附加到StringWriter而不是System.out ,但是僅使用策略的sanitize方便方法可能會更容易:

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

方便功能,用於清理HTML字符串。

它返回安全插入到您的JSP頁面中的HTML字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM