An XML parser factory is created without any setting that limits external entities
What it finds
An XML parser factory made with its defaults, as DocumentBuilderFactory.newInstance(), resolves external entities and DTDs: a document can then make the parser read local files or fetch URLs, and send their contents back in the parsed result.
A factory is reported when the code that makes it never calls setFeature, setAttribute, setProperty, setXIncludeAware or setExpandEntityReferences, and does not return it to a caller that may configure it; for a factory made in a field initializer, the whole class is searched for those calls. Covered are the factories of DOM, SAX, StAX, XSLT and schemas in the JDK, and the builders of dom4j and JDOM.
From the rule's source, unhardened_xml_parser.rs.
import javax.xml.XMLConstants;import javax.xml.parsers.DocumentBuilderFactory; class Xml { Object open() throws Exception { return DocumentBuilderFactory.newInstance().newDocumentBuilder();tx0 reports: this DocumentBuilderFactory resolves external entities by default } DocumentBuilderFactory handedOn() { return DocumentBuilderFactory.newInstance(); } DocumentBuilderFactory safe() throws Exception { DocumentBuilderFactory f = DocumentBuilderFactory.newInstance(); f.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); return f; }} class Shared { private final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();tx0 reports: this DocumentBuilderFactory resolves external entities by default} Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/java:unhardened-xml-parser" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/java:unhardened-xml-parser] reason="..."tx0 explain tx0/java:unhardened-xml-parser shows the same in the terminal. Found a false positive? Report it.