# OWASP Java HTML Sanitizer
[](https://github.com/OWASP/java-html-sanitizer/actions/workflows/build.yml) [](https://www.bestpractices.dev/projects/2602) [](https://search.maven.org/artifact/com.googlecode.owasp-java-html-sanitizer/owasp-java-html-sanitizer)
一个用 Java 编写、快速且易于配置的 HTML Sanitizer,让你可以在 Web 应用中包含第三方编写的 HTML,同时防御 XSS。
该 sanitizer JAR 没有运行时依赖。它唯一的编译时依赖是 `spotbugs-annotations`(provided 作用域,仅注解);其他 jar 仅测试套件需要。
此代码在编写时遵循安全最佳实践,拥有广泛的测试套件,并经过了[对抗性安全审查](https://github.com/owasp/java-html-sanitizer/blob/main/docs/attack_review_ground_rules.md)。
## 目录
* [快速开始](#getting-started)
* [仓库布局](#repository-layout)
* [预打包策略](#prepackaged-policies)
* [编写策略](#crafting-a-policy)
* [自定义策略](#custom-policies)
* [预处理器](#preprocessors)
* [遥测](#telemetry)
* [问题\?](#questions)
* [贡献](#contributing)
* [许可证](#license)
* [致谢](#credits)
## 快速开始
[快速开始](https://github.com/owasp/java-html-sanitizer/blob/main/docs/getting_started.md) 包含有关如何在使用或不使用 Maven 的情况下入门的说明。
## 仓库布局
构建需要 JDK 11 或更高版本。从仓库根目录,构建所有 Maven 模块并运行其测试,使用:
```sh
./mvnw clean verify
```
Maven wrapper 固定了所需的 Maven 版本。如果你使用 `-pl` 选择模块,请同时使用 `-am`,以便 Maven 构建它所依赖的模块。
* [owasp-java-html-sanitizer/](https://github.com/owasp/java-html-sanitizer/blob/main/owasp-java-html-sanitizer/README.md) 包含已发布的库、其测试以及其 Java 模块描述符。
* [java8-shim/](https://github.com/owasp/java-html-sanitizer/blob/main/java8-shim/README.md) 和
[java10-shim/](https://github.com/owasp/java-html-sanitizer/blob/main/java10-shim/README.md) 实现了
打包进库 JAR 的兼容性代码。
* [examples/](https://github.com/owasp/java-html-sanitizer/blob/main/examples/README.md) 包含可供阅读和复制的示例策略;
它会被测试,但不会被发布。
* [empiricism/](https://github.com/owasp/java-html-sanitizer/blob/main/empiricism/README.md) 包含用于
生成 sanitizer 的 HTML 元素表的浏览器实验。
* [docs/](https://github.com/owasp/java-html-sanitizer/blob/main/docs/README.md) 包含用户、安全和历史文档。
## 预打包策略
你可以使用
[预打包策略](https://www.javadoc.io/doc/com.googlecode.owasp-java-html-sanitizer/owasp-java-html-sanitizer/latest/org/owasp/html/Sanitizers.html):
```Java
PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);
String safeHTML = policy.sanitize(untrustedHTML);
```
## 制定策略
这些
[测试](https://github.com/OWASP/java-html-sanitizer/blob/main/owasp-java-html-sanitizer/src/test/java/org/owasp/html/HtmlPolicyBuilderTest.java)
展示了如何配置你自己的
[策略](https://www.javadoc.io/doc/com.googlecode.owasp-java-html-sanitizer/owasp-java-html-sanitizer/latest/org/owasp/html/HtmlPolicyBuilder.html):
```Java
PolicyFactory policy = new HtmlPolicyBuilder()
.allowElements("a")
.allowUrlProtocols("https")
.allowAttributes("href").onElements("a")
.requireRelNofollowOnLinks()
.toFactory();
String safeHTML = policy.sanitize(untrustedHTML);
```
## 自定义策略
你可以编写
[自定义策略](https://www.javadoc.io/doc/com.googlecode.owasp-java-html-sanitizer/owasp-java-html-sanitizer/latest/org/owasp/html/ElementPolicy.html)
来实现诸如将 `h1` 改为带有特定 class 的 `div` 之类的操作:
```Java
PolicyFactory policy = new HtmlPolicyBuilder()
.allowElements("p")
.allowElements(
(String elementName, List<String> attrs) -> {
// Add a class attribute.
attrs.add("class");
attrs.add("header-" + elementName);
// Return elementName to include, null to drop.
return "div";
}, "h1", "h2", "h3", "h4", "h5", "h6")
.toFactory();
String safeHTML = policy.sanitize(untrustedHTML);
```
请注意,如果你希望这些元素在不包含任何属性的情况下也能通过过滤器,就需要使用 `allowWithoutAttributes()` 方法将 "a"、"font"、"img"、"input" 和 "span" 这些元素显式加入白名单。
[属性策略](https://www.javadoc.io/doc/com.googlecode.owasp-java-html-sanitizer/owasp-java-html-sanitizer/latest/org/owasp/html/AttributePolicy.html) 也允许运行自定义代码。添加属性策略不会削弱任何默认策略,例如 `style` 或 URL 属性检查。
```Java
PolicyFactory myPolicy = new HtmlPolicyBuilder()
.allowElements("div", "span")
.allowAttributes("data-foo")
.matching(
(String elementName, String attributeName, String value) -> {
// Return value for the attribute or null to drop.
return value;
})
.onElements("div", "span")
.toFactory();
```
## 预处理器
预处理器允许插入文本以及进行大规模的结构性更改。
```Java
PolicyFactory myPolicy = new HtmlPolicyBuilder()
.withPreprocessor(
(HtmlStreamEventReceiver r) -> {
// Provide user with info about links before they click.
// Before: <a href="https://example.com/...">
// After: (https://example.com) <a href="https://example.com/...">
return new HtmlStreamEventReceiverWrapper(r) {
@Override public void openTag(String elementName, List<String> attrs) {
if ("a".equals(elementName)) {
for (int i = 0, n = attrs.size(); i < n; i += 2) {
if ("href".equals(attrs.get(i))) {
String url = attrs.get(i + 1);
String origin;
try {
URI uri = new URI(url);
String scheme = uri.getScheme();
String authority = uri.getRawAuthority();
if (scheme == null && authority == null) {
origin = null;
} else {
origin = (scheme != null ? scheme + ":" : "")
+ (authority != null ? "//" + authority : "");
}
} catch (URISyntaxException ex) {
origin = "about:invalid";
}
if (origin != null) {
text(" (" + origin + ") ");
}
}
}
}
super.openTag(elementName, attrs);
}
};
})
.allowElements("a")
.allowAttributes("href").onElements("a")
.allowStandardUrlProtocols()
...
.toFactory();
```
预处理在应用策略之前进行,因此不会影响输出的安全性。
## 遥测
当策略拒绝某个元素或属性时,它会通知 [HtmlChangeListener](https://www.javadoc.io/doc/com.googlecode.owasp-java-html-sanitizer/owasp-java-html-sanitizer/latest/org/owasp/html/HtmlChangeListener.html)。
你可以利用这一点来跟踪策略违规趋势,并发现何时有人试图突破你的安全防护。
```Java
PolicyFactory myPolicyFactory = ...;
// If you need to associate reports with some context, you can do so.
MyContextClass myContext = ...;
String sanitizedHtml = myPolicyFactory.sanitize(
unsanitizedHtml,
new HtmlChangeListener<MyContextClass>() {
@Override
public void discardedTag(MyContextClass context, String elementName) {
// ...
}
@Override
public void discardedAttributes(
MyContextClass context, String elementName, String... attributeNames) {
// ...
}
},
myContext);
```
`discardedAttributes` 也会对因元素被丢弃(因其已无剩余属性)而被拒绝的属性触发,例如一个链接的唯一 `href` 被拒绝的情况。有两个默认方法为覆盖它们的监听器提供更多细节:`discardedAttribute` 接收每个被拒绝属性的值,`discardedText` 接收策略从保留的字面内容元素中移除的类标签内容,以及渲染器无法从其中输出的内容。
**注意**:如果字符串经过净化后没有任何变更通知,并不意味着输入字符串一定可以安全使用。只能使用净化器的输出。
净化器确保输出属于常用 HTML 解析器对其含义能达成一致的 HTML 子集,但没有通知并不意味着输入属于这样的子集,只意味着它不包含被移除的结构性内容。
关于此主题的更多内容,请参阅 ["Why sanitize when you can validate"](https://github.com/OWASP/java-html-sanitizer/blob/main/docs/html-validation.md)。
## 有问题?
如果您希望报告漏洞,请参阅
[安全策略](https://github.com/owasp/java-html-sanitizer/blob/main/SECURITY.md) 和
[攻击审查基本规则](https://github.com/owasp/java-html-sanitizer/blob/main/docs/attack_review_ground_rules.md)。
订阅
[邮件列表](https://groups.google.com/g/owasp-java-html-sanitizer-support)
或关注本仓库的
[发布](https://github.com/OWASP/java-html-sanitizer/releases) 和
[安全公告](https://github.com/OWASP/java-html-sanitizer/security/advisories)
以获取已知 [漏洞](https://github.com/owasp/java-html-sanitizer/blob/main/docs/vulnerabilities.md) 和重要更新的通知。
## 贡献
本项目由 [Jim Manico](https://github.com/jmanico) 领导。发布
管理与维护由以下人员共同负责:
[Abhishek](https://github.com/mrabhishek)、
[Andres Almiray](https://github.com/aalmiray)、
[Ben Evans](https://github.com/kittylyst)、
[Erik Costlow](https://github.com/erikcostlow) 和
[Brian Fox](https://github.com/brianf)。
[Mike Samuel](https://github.com/mikesamuel) 创立了本项目并编写了
最初的净化器;他不再参与日常维护。
如果您想贡献,请提交
[issue](https://github.com/OWASP/java-html-sanitizer/issues) —— 这是
联系维护者的最佳方式。
我们欢迎 [issue 报告](https://github.com/OWASP/java-html-sanitizer/issues) 和 PR。
改变行为或添加功能的 PR 应同时包含正向和
[负向测试](https://www.guru99.com/negative-testing.html)。
请注意,贡献受项目双重许可证约束:
`Apache-2.0 OR BSD-2-Clause`,由接收方选择。请参阅
[COPYING](https://github.com/OWASP/java-html-sanitizer/blob/main/COPYING)。
## 许可证
双重许可:**`Apache-2.0 OR BSD-2-Clause`**。您可以根据自己的选择,在
[Apache License, Version 2.0](https://github.com/owasp/java-html-sanitizer/blob/main/LICENSE) 或 BSD 2-Clause
许可证下使用本软件 —— 您无需同时遵守两者。
[COPYING](https://github.com/owasp/java-html-sanitizer/blob/main/COPYING) 是授权声明的权威表述,包含
两个许可证的完整文本。`LICENSE` 仅包含 Apache-2.0 分支,以便
理解单一许可证文件的自动化工具能够检测到一个;
它并不缩小 `COPYING` 所提供的选择范围。
每个源文件都带有 SPDX 标识符。两个源自 AntiSamy 的测试
文件是 `BSD-3-Clause` 下的第三方代码,列于
`COPYING` 中的 THIRD-PARTY CODE 部分;它们不会被编译进已发布的
构件中。
## 致谢
[感谢所有提供批评和代码帮助的人](https://github.com/owasp/java-html-sanitizer/blob/main/docs/credits.md)
[安全名人堂](https://github.com/owasp/java-html-sanitizer/blob/main/docs/hall_of_fame.md):私下报告
漏洞的研究人员