XML external entity (XXE) vulnerability

XML external entity references and parser protection

Description

Restrict DTDs and external resources before parsing untrusted XML. On the JDK, explicitly enabling XMLConstants.FEATURE_SECURE_PROCESSING sets processing limits and restricts external access. Check the deployed parser and runtime settings. For new code use SAXParserFactory. In older code using XMLReaderFactory, deprecated since Java 9, review the actual configuration.

Potential impact

The parser may expose files it can read or send requests to internal services. Entity expansion may exhaust CPU and memory.

Remediation

  1. If you do not need DTDs, set jdk.xml.dtd.support=deny in the JDK 25 JAXP configuration or as a JVM system property. To configure an individual parser, set http://apache.org/xml/features/disallow-doctype-decl to true.
  2. Enable XMLConstants.FEATURE_SECURE_PROCESSING. Set XMLConstants.ACCESS_EXTERNAL_DTD and XMLConstants.ACCESS_EXTERNAL_SCHEMA to empty strings to block external protocols.
  3. If you need DTDs, disable external general entities, external parameter entities and external DTD loading. Supply only approved resources through a local XML catalog or a reviewed resolver, and keep finite processing limits.

Examples

Before

java
import java.io.InputStream;
import javax.xml.XMLConstants;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.helpers.DefaultHandler;

public final class UnsafeXXE {
    static void parse(InputStream xml) throws Exception {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, false);

        SAXParser parser = factory.newSAXParser();
        parser.parse(xml, new DefaultHandler());
    }
}

This parser explicitly turns off secure processing and leaves DTDs and external access unrestricted in the code.

java
import jakarta.servlet.http.HttpServletRequest;
import javax.xml.parsers.DocumentBuilderFactory;

public final class InlineServletXXE {
    static void parse(HttpServletRequest request) throws Exception {
        DocumentBuilderFactory.newInstance()
            .newDocumentBuilder()
            .parse(request.getInputStream());
    }
}

The inline call sets no local restrictions on DocumentBuilderFactory. Check whether the deployment’s JAXP settings restrict DTDs or external access.

After

java
import java.io.InputStream;
import javax.xml.XMLConstants;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.helpers.DefaultHandler;

public final class SafeXXE {
    static void parse(InputStream xml) throws Exception {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
        factory.setFeature(
            "http://apache.org/xml/features/disallow-doctype-decl",
            true
        );

        SAXParser parser = factory.newSAXParser();
        parser.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "");
        parser.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
        parser.parse(xml, new DefaultHandler());
    }
}

This SAX parser is for documents that do not need DTDs. It rejects DOCTYPE and blocks external DTD and schema access before parsing.

References