Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

5.3 KB
Raw Blame
/**
 * happy-dom repairs required for DOMPurify to behave like a browser.
 *
 * happy-dom has two defects that DOMPurify trips over (first seen on
 * 20.11.12; both still reproduce on the pinned 20.14.5):
 *
 *   1. `document.createNodeIterator` truncates traversal as soon as the
 *      current node is removed from the tree. DOMPurify removes nodes while
 *      iterating, so only the nodes up to the first removal are ever
 *      inspected.
 *   2. The BASE `Node.prototype` `nodeName` getter returns "". DOMPurify
 *      caches that getter at import time (anti-clobbering hardening added in
 *      3.4.8) and reads every node's name through it, so the first node it
 *      looks at is seen as an unknown tag: elements are unwrapped (tags
 *      dropped, text kept) and text nodes are dropped outright.
 *
 * Together these make `DOMPurify.sanitize` destroy exactly one node per call
 * and, crucially, NOT be idempotent -- `sanitize(sanitize(x)) !== sanitize(x)`
 * for any markup with more than one node. That breaks
 * `XSSProtection.sanitizeStable`, whose whole contract is to iterate to a
 * fixpoint: instead of converging it erodes the document one node per pass
 * and then falls closed to escaped text. In a real browser DOMPurify is
 * idempotent and `sanitizeStable` returns on its second pass.
 *
 * WHAT THE createNodeIterator REPAIR ITSELF DIVERGES ON. The replacement is
 * not a NodeIterator: it walks the tree once, up front, and then replays that
 * snapshot. A real NodeIterator is live -- it tracks the tree as it changes,
 * so a node DOMPurify has already removed is never handed back, and nodes
 * inserted during traversal are seen. Here the opposite holds: every node
 * present at creation time is yielded, including ones removed mid-pass, and
 * nodes added mid-pass are never yielded. For DOMPurify that is safe in the
 * direction that matters -- it means the sanitizer inspects MORE nodes than a
 * browser would, never fewer, so nothing hostile is skipped -- but a test
 * that asserts on *how many* nodes were visited, or that relies on a removal
 * cutting traversal short, is measuring this shim rather than the browser.
 *
 * Neither repair changes what `Element.prototype.namespaceURI` reports:
 * happy-dom's own HTML parser already assigns SVG namespace correctly by
 * tree position (see `NamespaceURI.svg` handling in
 * html-parser/HTMLParser.ts), and this file does not touch that. It also
 * does NOT fabricate a MathML namespace from tag name -- happy-dom has no
 * MathML foreign-content support at all (no tokenizer/tree-construction
 * branch for it), so no getter override can make `<math>` subtrees behave
 * like a real browser's; a getter that returned MathML for any element whose
 * tagName happens to be in the MathML tag list, regardless of where that
 * element actually sits in the tree, would fabricate a namespace-confusion
 * invariant no test could ever fail. Suites that need parsed `<math>`
 * content to survive DOMPurify for a LEGITIMATE reason (asserting on KaTeX's
 * accessibility subtree, not testing namespace-confusion security) opt into
 * that approximation explicitly and locally via
 * `tests/js/happy-dom-mathml-namespace-approximation.js` -- see that file's
 * header for what it does and does not model. Suites asserting a namespace-
 * confusion / mXSS invariant (tests/js/security/mxss-reparse-invariant.test.js)
 * must NOT install it.
 *
 * This runs as a Vitest setup file, BEFORE any test module (and therefore
 * before any `import 'dompurify'`), because DOMPurify captures these
 * primitives at import time. Several suites under tests/js/security/ install
 * their own copy of the same two repairs (plus their own local MathML
 * namespace approximation) in module scope; re-installing is harmless (every
 * descriptor is `configurable` and this file's repairs do not touch
 * namespaceURI at all).
 *
 * Upstream: https://github.com/capricorn86/happy-dom/issues/2182
 */

export function installDomPurifyHappyDomShims() {
    document.createNodeIterator = function(root, whatToShow) {
        const mask = whatToShow === undefined || whatToShow === null
            ? 0xffffffff
            : whatToShow;
        const nodes = [];
        (function collect(node) {
            if (mask & (1 << (node.nodeType - 1))) {
                nodes.push(node);
            }
            for (let i = 0; i < node.childNodes.length; i++) {
                collect(node.childNodes[i]);
            }
        })(root);
        let index = 0;
        return {
            nextNode() {
                return index < nodes.length ? nodes[index++] : null;
            },
            previousNode() {
                return index > 0 ? nodes[--index] : null;
            },
        };
    };

    Object.defineProperty(window.Node.prototype, 'nodeName', {
        configurable: true,
        get() {
            switch (this.nodeType) {
                case 1: return this.tagName;
                case 2: return this.name;
                case 3: return '#text';
                case 4: return '#cdata-section';
                case 7: return this.target;
                case 8: return '#comment';
                case 9: return '#document';
                case 10: return '#document-type';
                case 11: return '#document-fragment';
                default: return '';
            }
        },
    });
}

installDomPurifyHappyDomShims();