← Java / Spring · Contents

JAVA-032 / Advanced

Processing untrusted XML with JAXB/Unmarshaller

A08:2021 Software and Data Integrity Failures · CWE-611 / CWE-502

Why is this a problem?

Default settings may resolve external entities or expose object-creation paths.

How to fix it

java
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setFeature("https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html", true);
XMLReader r = spf.newSAXParser().getXMLReader();
Unmarshaller u = JAXBContext.newInstance(Order.class).createUnmarshaller();
u.unmarshal(new SAXSource(r, new InputSource(req.getInputStream())));

Further reading