Description
In libxmljs and libxmljs2, noent: true substitutes entity values. Access to external files and network resources, as well as expansion limits, depends on the bundled libxml2 version, build and loaders.
Potential impact
File contents can reach responses or logs, and the parser may contact internal services. Excessive entity expansion can exhaust CPU or memory.
Remediation
- Set
noent: falsefor untrusted XML and reject unnecessary DTDs. Check other parser options and custom loaders for file or network access, and restrict the process’s permissions. - Restrict accepted formats and set limits on input size, expansion, memory and processing time. Handle parse errors. The parser resolves predefined references such as
&and<, so use the parsed text without decoding HTML entities again.
Examples
Before
javascript
const express = require('express');
const libxml = require('libxmljs');
const app = express();
app.use(express.text({ type: '*/*' }));
// Parse externally supplied XML with noent: true
app.post('/api/parse-xml', (req, res) => {
const xmlPayload = String(req.body || '');
// noent: true may expand external entities, enabling file access, SSRF or DoS
const doc = libxml.parseXmlString(xmlPayload, { noent: true });
res.json({ root: doc.root().name() });
});
app.listen(3000);
After
javascript
const express = require('express');
const libxml = require('libxmljs');
const app = express();
app.use(express.text({ type: '*/*' }));
// Disable noent and reject DTDs
app.post('/api/parse-xml', (req, res) => {
const xmlPayload = String(req.body || '');
// Reject DTD declarations, including external entity declarations
if (/<!DOCTYPE/i.test(xmlPayload)) {
return res.status(400).send('DTD is not allowed');
}
// Keep noent disabled explicitly
const doc = libxml.parseXmlString(xmlPayload, { noent: false });
// Do not decode the parser output a second time
const textContent = doc.root().text();
res.json({ root: doc.root().name(), text: textContent.slice(0, 200) });
});
app.listen(3000);
The second handler checks the string decoded by Express for a DTD declaration and disables noent. After parsing, it applies slice(0, 200) only to the JSON text field and returns the full root name. Limit the input while reading it and handle malformed XML errors.