# SAML响应解析易受XXE攻击，允许任意文件读取

- **ID:** `security/saml-xml-external-entity-xxe`
- **领域:** security
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

用于处理SAML断言的XML解析器配置为启用外部实体解析，允许攻击者注入恶意DOCTYPE来读取本地文件或执行SSRF。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Shibboleth 3.4 | active | — | — |
| OpenSAML 4.2 | active | — | — |
| SimpleSAMLphp 1.19 | active | — | — |

## 解决方案

1. ```
   在XML解析器中显式禁用外部实体。在Java中使用DocumentBuilderFactory：factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
   ```
2. ```
   使用默认拒绝DTD的安全XML解析器，例如OWASP XML安全库提供的解析器。
   ```
3. ```
   在解析之前，根据XSD模式验证SAML响应，并拒绝任何包含DOCTYPE声明的文档。
   ```

## 无效尝试

- **** — Disabling DTD processing globally may break legitimate SAML responses that use DTDs for signature validation. (40% 失败率)
- **** — Using a regex to filter out DOCTYPE declarations is unreliable and can be bypassed with encoding tricks. (70% 失败率)
- **** — Upgrading the SAML library without checking its XML parser configuration may not fix the issue if the library uses a vulnerable parser by default. (50% 失败率)
