← Java / Spring · Contents

JAVA-030 / Advanced

Allowing XML external entities (XXE)

A05:2021 Security Misconfiguration · CWE-611 XXE

Why is this a problem?

Reading file:///etc/passwd and SSRF.

How to fix it

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);

Further reading