XXE by switching JSON endpoints to XML
The JSON to XML XXE content-type attack exploits APIs that advertise JSON but will also accept XML: by changing the Content-Type header to application/xml and resubmitting the body as XML, you can reach an underlying XML parser that the JSON interface hid, and trigger external entity injection against it.
- Target formats
- REST APIsPlain XML
- Affected parsers
- Java frameworks (Jackson XML, JAXB).NET model bindersSpring content negotiation
- Impact
- Local file disclosureSSRFBlind exfiltration
How it works
Many frameworks support content negotiation: the same endpoint deserialises JSON or XML depending on the request's Content-Type. Developers test and secure the JSON path, unaware that the XML path is wired up too.
By taking a working JSON request, setting Content-Type: application/xml and rewriting the body as an equivalent XML document with a malicious DOCTYPE, you reach the XML deserialiser — which may resolve external entities even though the JSON front door never exposed one.
Which parser configuration makes it work
Requires a framework that will deserialise XML on the same route (content negotiation or a permissive model binder) using a parser that allows DOCTYPE/external entities. The JSON-only appearance is the lure, not a protection.
Representative payloads
Replace callback hosts and target paths with your own authorized test values.
POST /api/user HTTP/1.1
Content-Type: application/xml
<?xml version="1.0"?>
<!DOCTYPE foo [
<!ENTITY xxe SYSTEM "file:///etc/passwd">
]>
<user><name>&xxe;</name></user>Mirror the JSON structure as XML elements the deserialiser expects.
Detection
As a tester, flip the Content-Type on JSON endpoints to application/xml and probe for XXE. As a defender, enumerate which content types each route actually accepts, and alert on XML bodies arriving at endpoints that are documented as JSON-only — that mismatch is the signature of this attack.
- application/xml bodies on routes documented as JSON
- DOCTYPE/ENTITY declarations in API request bodies
- XML deserialiser invoked by content negotiation on untrusted input
Remediation
Restrict each endpoint to the content types it actually needs — if a route is JSON-only, reject application/xml and other XML media types outright. Where XML must be accepted, configure the XML deserialiser (Jackson XML, JAXB, the framework's model binder) with a hardened parser that disables DOCTYPE/DTD processing and external entities. Do not assume a JSON API is safe from XXE simply because its documented interface is JSON.
Fix checklist
- Restrict routes to the content types they require; reject unexpected XML
- Harden the XML deserialiser (disable DOCTYPE/external entities) where XML is allowed
- Audit framework content-negotiation defaults
- Do not treat a JSON-facing API as automatically XXE-safe