SKY-D236 · Security in code
XML external entity (XXE)
An XML document can declare a DTD with "external entities": names that the parser replaces with the contents of a file or a URL. SKY-D236 reports Java and Kotlin XML parsers that are created and used without turning DTDs or external entities off, which the JDK parsers allow by default, TypeScript/JavaScript libxml parses that turn entity substitution on (libxmljs noent: true, the libxml2 XML_PARSE_NOENT option), and Python parse calls using a parser explicitly configured to resolve external entities. In C#, it reports a System.Xml.XmlReader that receives settings enabling DTD parsing and a URL resolver.
- Languages
- Java, Kotlin, TypeScript/JavaScript, Python, C#
- Quality gate
- counted toward the gate limits; a CRITICAL finding fails the gate on its own (full and diff scans). Only findings on request data are CRITICAL and fail the gate on their own; the others are counted.
- Finding type
- review
Why it matters
A visitor who uploads XML with <!ENTITY x SYSTEM "file:///etc/passwd"> gets the file's contents back wherever &x; is echoed. The same trick makes the server fetch internal URLs, and nested entities can exhaust its memory.
What Skylos flags
package com.example; import java.io.IOException;import javax.servlet.http.HttpServletRequest;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RestController;import org.w3c.dom.Document;import org.xml.sax.SAXException; @RestControllerpublic class InvoiceController { @PostMapping("/invoices") public String upload(HttpServletRequest request) throws IOException, ParserConfigurationException, SAXException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(request.getInputStream()); return doc.getDocumentElement().getAttribute("id"); }}What Skylos leaves alone
package com.example; import java.io.IOException;import javax.servlet.http.HttpServletRequest;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RestController;import org.w3c.dom.Document;import org.xml.sax.SAXException; @RestControllerpublic class InvoiceController { private static final String NO_DOCTYPE = "http://apache.org/xml/features/disallow-doctype-decl"; @PostMapping("/invoices") public String upload(HttpServletRequest request) throws IOException, ParserConfigurationException, SAXException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setFeature(NO_DOCTYPE, true); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(request.getInputStream()); return doc.getDocumentElement().getAttribute("id"); }}Why Skylos leaves it alone
disallow-doctype-decl makes the parser reject any document that has a DTD, so no entity can be declared. Skylos reads the setting on the same factory variable before newDocumentBuilder(), including through a string constant of the class.
More languages
Kotlin
Flagged
package com.example import javax.xml.parsers.DocumentBuilderFactoryimport org.springframework.web.bind.annotation.PostMappingimport org.springframework.web.bind.annotation.RequestBodyimport org.springframework.web.bind.annotation.RestController @RestControllerclass InvoiceController { @PostMapping("/invoices") fun upload(@RequestBody body: String): String { val factory = DocumentBuilderFactory.newInstance() val builder = factory.newDocumentBuilder() val doc = builder.parse(body.byteInputStream()) return doc.documentElement.getAttribute("id") }}Not flagged
package com.example import javax.xml.XMLConstantsimport javax.xml.parsers.DocumentBuilderFactoryimport org.springframework.web.bind.annotation.PostMappingimport org.springframework.web.bind.annotation.RequestBodyimport org.springframework.web.bind.annotation.RestController @RestControllerclass InvoiceController { @PostMapping("/invoices") fun upload(@RequestBody body: String): String { val factory = DocumentBuilderFactory.newInstance() factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "") factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "") val builder = factory.newDocumentBuilder() val doc = builder.parse(body.byteInputStream()) return doc.documentElement.getAttribute("id") }}Why Skylos leaves it alone
Setting ACCESS_EXTERNAL_DTD and ACCESS_EXTERNAL_SCHEMA to an empty string allows no protocol for external DTDs and schemas, so the parser cannot load outside files or URLs. Skylos accepts this pair for DocumentBuilderFactory.
How Skylos decides
- Tracks each XML factory variable (
DocumentBuilderFactory,SAXParserFactory,XMLReader,XMLInputFactory,TransformerFactory,SchemaFactory, CommonsDigester, JDOMSAXBuilder, dom4jSAXReader) and the settings applied to it in statement order, up to the parser it creates. - At a parse call, checks those settings per parser, following CodeQL's rules: for example
disallow-doctype-declset to true, both external entity features set to false, orACCESS_EXTERNAL_DTDandACCESS_EXTERNAL_SCHEMAset to""for aDocumentBuilderFactory.FEATURE_SECURE_PROCESSINGalone does not count, and a setting made after the parser was created does not protect it. - Skips parsing a classpath resource or a literal path bundled with the app, building a new document, and DOM serialization through a
Transformer. - Reports CRITICAL when request data reaches the parse call and HIGH otherwise. dom4j
DocumentHelper.parseText()is MEDIUM, or HIGH with request data. - In TypeScript and JavaScript, reports libxmljs
parseXml/parseXmlStringcalled withnoent: trueand any use of the libxml2XML_PARSE_NOENTparse option (not& ~XML_PARSE_NOENT): HIGH, or CRITICAL when request data is parsed in a handler. - In C#, resolves
System.Xmlnames and follows trackedXmlReaderSettingstoXmlReader.Create. BothDtdProcessing.Parseand a resolvedXmlUrlResolvermust be effective at the call. Prohibiting DTDs or keeping the resolver null clears this check.
How to fix it
Turn DTDs off on the factory before you create the parser. If you need DTDs, at least turn off external general entities, external parameter entities and loading external DTDs. In C#, explicitly prohibit DTDs or keep XmlResolver null before creating the reader.
package com.example; import java.io.IOException;import javax.servlet.http.HttpServletRequest;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RestController;import org.w3c.dom.Document;import org.xml.sax.SAXException; @RestControllerpublic class InvoiceController {Added: private static final String NO_DOCTYPE =Added: "http://apache.org/xml/features/disallow-doctype-decl"; @PostMapping("/invoices") public String upload(HttpServletRequest request) throws IOException, ParserConfigurationException, SAXException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();Added: factory.setFeature(NO_DOCTYPE, true);Added: factory.setXIncludeAware(false); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(request.getInputStream()); return doc.getDocumentElement().getAttribute("id"); }}When it can be wrong
- A parser that only reads a trusted file whose path is not a literal, such as a config path from settings, is still reported as HIGH.
- Hardening applied in only one branch of an
ifdoes not count, so a parser that is safe in practice can still be reported.
Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.
How to silence it
Mark a reviewed line and say why. A bare
//comment without the rule ID does not hide the finding.Java Document doc = builder.parse(in); // skylos: ignore[SKY-D236] hardened in XmlConfigAccept the findings you already have with
skylos baseline ., then scan with--baselineso only new ones are listed.[tool.skylos] ignore = ["SKY-D236"]turns the rule off for the whole project.