Skip to main content

SKY-D236 · Security in code

XML external entity (XXE)

High

On the JVM, CRITICAL when request data is parsed, HIGH for other input. Python findings for explicitly enabled external entity processing and C# XML reader configuration findings are HIGH. dom4j DocumentHelper.parseText() is MEDIUM, or HIGH with request data.

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
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

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

src/main/java/com/example/InvoiceController.java · JavaReported on line 21
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

src/main/java/com/example/InvoiceController.java · Java
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

src/main/kotlin/com/example/InvoiceController.kt · KotlinReported on line 15
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

src/main/kotlin/com/example/InvoiceController.kt · Kotlin
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

  1. Tracks each XML factory variable (DocumentBuilderFactory, SAXParserFactory, XMLReader, XMLInputFactory, TransformerFactory, SchemaFactory, Commons Digester, JDOM SAXBuilder, dom4j SAXReader) and the settings applied to it in statement order, up to the parser it creates.
  2. At a parse call, checks those settings per parser, following CodeQL's rules: for example disallow-doctype-decl set to true, both external entity features set to false, or ACCESS_EXTERNAL_DTD and ACCESS_EXTERNAL_SCHEMA set to "" for a DocumentBuilderFactory. FEATURE_SECURE_PROCESSING alone does not count, and a setting made after the parser was created does not protect it.
  3. Skips parsing a classpath resource or a literal path bundled with the app, building a new document, and DOM serialization through a Transformer.
  4. Reports CRITICAL when request data reaches the parse call and HIGH otherwise. dom4j DocumentHelper.parseText() is MEDIUM, or HIGH with request data.
  5. In TypeScript and JavaScript, reports libxmljs parseXml/ parseXmlString called with noent: true and any use of the libxml2 XML_PARSE_NOENT parse option (not & ~XML_PARSE_NOENT): HIGH, or CRITICAL when request data is parsed in a handler.
  6. In C#, resolves System.Xml names and follows tracked XmlReaderSettings to XmlReader.Create. Both DtdProcessing.Parse and a resolved XmlUrlResolver must 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.

src/main/java/com/example/InvoiceController.java · Java · the fix, against the flagged file
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 if does 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 XmlConfig
  • Accept the findings you already have with skylos baseline ., then scan with --baseline so only new ones are listed.

  • [tool.skylos] ignore = ["SKY-D236"] turns the rule off for the whole project.

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.