"use strict";
// Adversarial test suite for the parse-and-rebuild SVG sanitizer.
//
// The previous sanitizer filtered raw text with regexes; this one parses with
// a vetted XML parser (@xmldom/xmldom), validates every node against
// whitelists, and RE-SERIALIZES the output from validated names/values. The
// tests below cover both the classic payload set and the mangled-input class
// that motivated the rewrite: anything the strict parser cannot read is
// rejected outright, and everything that survives is re-emitted with
// attacker-controlled bytes escaped or dropped.
//
// The structural guarantee locked here: sanitized output must itself parse as
// well-formed XML under the same strict parser (no undeclared prefixes, no
// broken tags), so a "sanitized" file can never end up as a parse-error
// document on the serving side.
const test = require("node:test");
const assert = require("node:assert");
const { DOMParser } = require("@xmldom/xmldom");
const { sanitizeSvg, validateSvgBuffer, ALLOWED_TAGS, DANGEROUS_BLOCK_TAGS } = require("./svg-sanitizer.js");
// Re-parse sanitized output with the same strict rules the sanitizer uses.
function assertWellFormedXml(output, label) {
const errors = [];
const parser = new DOMParser({ onError: (severity, message) => errors.push(`${severity}: ${message}`) });
let doc = null;
try {
doc = parser.parseFromString(output, "image/svg+xml");
} catch (err) {
assert.fail(`${label}: sanitized output is not well-formed XML: ${err.message}`);
}
assert.ok(doc && doc.documentElement, `${label}: sanitized output has a document element`);
assert.deepEqual(errors, [], `${label}: sanitized output produced no parser diagnostics`);
}
function sanitizeOk(input) {
const res = sanitizeSvg(input);
assert.equal(res.valid, true, `expected valid, got: ${res.error || "?"}`);
assert.ok(Buffer.isBuffer(res.sanitized), "sanitized output is a Buffer");
return res.sanitized.toString("utf8");
}
function sanitizeRejected(input, errorHint) {
const res = sanitizeSvg(input);
assert.equal(res.valid, false, `expected rejection, got output: ${res.sanitized && res.sanitized.toString("utf8").slice(0, 120)}`);
if (errorHint) {
assert.ok(new RegExp(errorHint, "i").test(String(res.error || "")), `error "${res.error}" should match /${errorHint}/i`);
}
return res.error;
}
const SVG_OPEN = '`);
assert.ok(!/ -->`);
assert.ok(!out.includes("ipt>alert"));
assert.ok(!out.includes("alert(1)"));
assert.ok(out.includes(" {
sanitizeRejected(``, "not well-formed|parse");
sanitizeRejected(``, "not well-formed|parse");
});
test("unquoted and backtick-quoted attributes cannot smuggle attribute boundaries", () => {
// In XML, unquoted attribute values are illegal — the old regex parser
// accepted and re-emitted them; the strict parse rejects the document.
sanitizeRejected('', "not well-formed|parse");
sanitizeRejected('', "not well-formed|parse");
});
test("undefined entities are rejected", () => {
sanitizeRejected('', "parse errors");
});
test("multiple root elements are rejected", () => {
sanitizeRejected('', "not well-formed");
});
test("non-SVG root element is rejected", () => {
sanitizeRejected('hi', "root");
sanitizeRejected("just some text, no markup at all", "root|parse|well-formed");
});
test("namespaced dangerous tags are dropped: svg:script, xhtml:script, case games", () => {
const out = sanitizeOk(``);
assert.ok(!out.toLowerCase().includes("script"));
assert.ok(!out.includes("evil.com"));
assert.ok(out.includes(" {
const out = sanitizeOk(`${SVG_OPEN}
]]>
`);
assert.ok(!out.includes(""; }`, "not well-formed|parse");
// The well-formed way to carry those characters: entity references. They
// must survive as ESCAPED text so the emitted markup cannot be broken out of.
const out = sanitizeOk(`${SVG_OPEN}`);
assert.ok(!out.includes("'));
// Script-only content: subtree dropped leaves an empty svg root, still valid.
assert.equal(badRes.valid, true);
assert.ok(!badRes.data.toString("utf8").includes("script"));
const notBuffer = validateSvgBuffer("");
assert.equal(notBuffer.valid, false);
});
test("final output never contains prohibited tokens even when input games every rule", () => {
const payload = `${SVG_OPEN}
x
`;
const out = sanitizeOk(payload);
for (const bad of [/