← Java / Spring · 目录

JAVA-030 / 高级

允许 XML 外部实体(XXE)

A05:2021 Security Misconfiguration · CWE-611 XXE

为什么存在问题?

读取 file:///etc/passwd 或导致 SSRF。

如何修复

java
DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
f.setFeature("https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html", true);
f.setFeature("https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html", false);
f.setFeature("https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html", false);
f.setXIncludeAware(false); f.setExpandEntityReferences(false);

延伸阅读