Skip to content

XXE in Node.js XML parsers

Node.js XXE prevention is mostly about parser choice: the popular pure-JavaScript parsers (fast-xml-parser, xml2js) do not process DTDs or external entities at all, so they are safe by default — the real exposure is libxmljs, a binding to libxml2, when it is told to load DTDs or substitute entities.

Unlike Java or PHP, most Node.js XML libraries never implemented external entity resolution, so the typical stack is XXE-safe without configuration. The exception is any library backed by native libxml2 (libxmljs) or expat with entity handling. Pick a safe parser and avoid enabling DTD/entity options on the native ones.

Secure parser configuration

fast-xml-parser (recommended)

javascript
import { XMLParser } from 'fast-xml-parser';

const parser = new XMLParser();
const obj = parser.parse(untrustedXml);
// fast-xml-parser does not resolve DTDs or external entities — XXE-safe.

No DTD/entity support exists, so there is nothing to disable.

xml2js

javascript
import { parseStringPromise } from 'xml2js';

const result = await parseStringPromise(untrustedXml);
// xml2js (built on sax) does not expand external entities.

libxmljs (native libxml2 — be careful)

javascript
import libxml from 'libxmljs2';

// Parse with external entities and DTD loading OFF (defaults), network OFF:
const doc = libxml.parseXml(untrustedXml, {
  noent: false,   // do NOT substitute entities
  dtdload: false, // do NOT load external DTDs
  nonet: true,    // no network access
});

Never set noent:true or dtdload:true on untrusted input; keep nonet:true.

Common pitfalls that re-introduce XXE

  • Choosing a native libxml2 binding and enabling noent/dtdload options.
  • Assuming every parser is safe without checking the specific library.
  • Processing SVG/Office uploads with a native parser that has entities on.
  • Shelling out to an external XML tool that does resolve entities.

Verifying the fix

Feed a classic file-read payload to the endpoint. With fast-xml-parser or xml2js the entity is never expanded and &xxe; is returned literally or ignored. For libxmljs, confirm noent/dtdload are false and that a blind OOB payload triggers no outbound request.

Techniques these settings prevent

References