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.

15.1 KB
Raw Blame
/**
 * Late-DOMPurify contracts for the tabnabbing hook.
 *
 * ``xss-protection.js`` registers an ``afterSanitizeAttributes`` hook that
 * forces ``rel="noopener noreferrer"`` on ``<a>`` (including SVG ``<a>``),
 * ``<area>`` and ``<form>`` elements whose target can open a new browsing
 * context. In the shipped base.html the app.js module (which binds
 * DOMPurify) executes before this deferred classic script, so the load-time
 * registration succeeds; the runtime suite pins that favorable order.
 *
 * These contracts pin the adverse order: the module loads with no DOMPurify
 * in scope and the sanitizer binds afterwards. Each sanitize entry point is
 * exercised on a freshly evaluated module as the FIRST sanitize call, so each
 * one must register the hook on its own rather than inherit a registration
 * made by an earlier test. The hook is also bound to the DOMPurify instance,
 * so a replaced instance must get it too. The hook reads and writes through
 * Element.prototype, so a <form> control that shadows a built-in on the
 * form instance cannot switch it off, and it merges into an existing rel
 * rather than replacing it.
 */

const EXPORTED_GLOBALS = [
    'XSSProtection', 'escapeHtml', 'escapeHtmlAttribute', 'safeSetInnerHTML',
    'safeCreateElement', 'safeSetTextContent', 'createSafeAlertElement',
    'sanitizeUserInput', 'sanitizeHtml', 'safeUpdateButton',
    'createSafeLoadingOverlay', 'safeSetStyles', 'showSafeAlert',
];

/**
 * Subset of DOMPurify's default ALLOWED_ATTR that these fixtures use. Like
 * the real default list it does NOT contain `target`, so a config without
 * ALLOWED_ATTR/ADD_ATTR naming `target` strips it, as production does.
 */
const DEFAULT_ALLOWED_ATTR = [
    'href', 'title', 'class', 'id', 'rel', 'action', 'method', 'name',
    'alt', 'shape', 'coords',
];

/**
 * Built-ins a descendant form control can shadow on its <form> instance.
 * Browsers expose a form's controls as named properties that override
 * same-named built-ins ([LegacyOverrideBuiltIns]); happy-dom does not, so
 * the stand-in below emulates it while the hooks run.
 */
const FORM_SHADOWABLE = ['tagName', 'getAttribute', 'setAttribute'];
const FORM_CONTROLS = 'input,button,select,textarea,fieldset,output,object';

/**
 * Run `fn` with `el`'s shadowable built-ins overridden, as a browser does
 * for a <form> holding a control named or id'd like one. Returns how many
 * properties were shadowed so a test can prove the emulation engaged.
 */
const withFormNamedProps = (el, fn) => {
    const shadowed = [];
    if (el.localName === 'form') {
        const controls = Array.from(el.querySelectorAll(FORM_CONTROLS));
        FORM_SHADOWABLE.forEach((prop) => {
            const control = controls.find((c) =>
                c.getAttribute('name') === prop || c.getAttribute('id') === prop
            );
            if (control) {
                Object.defineProperty(el, prop, {
                    configurable: true,
                    get: () => control,
                });
                shadowed.push(prop);
            }
        });
    }
    try {
        fn();
    } finally {
        shadowed.forEach((prop) => delete el[prop]);
    }
    return shadowed.length;
};

/**
 * Minimal DOMPurify stand-in that honours the parts of the config the
 * sanitize paths under test rely on: ALLOWED_TAGS / FORBID_TAGS (dropped
 * elements keep their content), ALLOWED_ATTR (else the default list above)
 * plus ADD_ATTR, and RETURN_DOM_FRAGMENT. Like DOMPurify it runs the
 * afterSanitizeAttributes hooks on every surviving element after its
 * attributes were filtered, with a browser form's named-property
 * shadowing emulated (`shadowedCount` counts the shadowed properties).
 */
const makePurifier = () => {
    const hooks = {};
    const stats = { shadowedCount: 0 };
    const lower = (list) => (list || []).map((x) => String(x).toLowerCase());
    const purifier = {
        isSupported: true,
        addHook: (name, cb) => {
            hooks[name] ||= [];
            hooks[name].push(cb);
        },
        sanitize: (dirty, config = {}) => {
            const template = document.createElement('template');
            // eslint-disable-next-line no-unsanitized/property -- test harness stand-in: the input is this file's own fixture
            template.innerHTML = String(dirty);
            const allowedTags = config.ALLOWED_TAGS
                ? new Set(lower(config.ALLOWED_TAGS))
                : null;
            const forbiddenTags = new Set(lower(config.FORBID_TAGS));
            const allowedAttr = new Set([
                ...lower(config.ALLOWED_ATTR || DEFAULT_ALLOWED_ATTR),
                ...lower(config.ADD_ATTR),
            ]);
            Array.from(template.content.querySelectorAll('*')).forEach((el) => {
                const name = String(el.localName).toLowerCase();
                if ((allowedTags && !allowedTags.has(name)) ||
                    forbiddenTags.has(name)) {
                    el.replaceWith(...Array.from(el.childNodes));
                    return;
                }
                Array.from(el.attributes).forEach((attr) => {
                    if (!allowedAttr.has(attr.name.toLowerCase())) {
                        el.removeAttribute(attr.name);
                    }
                });
                stats.shadowedCount += withFormNamedProps(el, () => {
                    (hooks.afterSanitizeAttributes || []).forEach((cb) => cb(el));
                });
            });
            if (config.RETURN_DOM_FRAGMENT) {
                return template.content;
            }
            return template.innerHTML;
        },
    };
    return { purifier, hooks, stats };
};

const hookCount = (hooks) => (hooks.afterSanitizeAttributes || []).length;

const firstAnchor = (html) =>
    new window.DOMParser().parseFromString(html, 'text/html').querySelector('a');

const anchorHtmlWithTarget = (target) => {
    const a = document.createElement('a');
    a.setAttribute('href', 'https://example.test');
    a.setAttribute('target', target);
    a.textContent = 'x';
    return a.outerHTML;
};

// <area> and <form> also navigate via `target` and accept rel keywords.
const elementHtmlWithTarget = (tag, target) => {
    const el = document.createElement(tag);
    el.setAttribute(tag === 'form' ? 'action' : 'href', 'https://example.test');
    el.setAttribute('target', target);
    return el.outerHTML;
};

const firstElement = (html, tag) =>
    new window.DOMParser().parseFromString(html, 'text/html').querySelector(tag);

/**
 * Evaluate a fresh copy of xss-protection.js with NO DOMPurify in scope,
 * then bind a stand-in afterwards, as a late-binding page would.
 */
const loadThenLateBind = async () => {
    delete globalThis.DOMPurify;
    vi.resetModules();
    await import('@js/security/xss-protection.js');
    const late = makePurifier();
    // eslint-disable-next-line require-atomic-updates -- deliberate late binding of the deferred-module global
    globalThis.DOMPurify = late.purifier;
    return late;
};

describe('tabnabbing hook under late-bound DOMPurify', () => {
    afterEach(() => {
        delete globalThis.DOMPurify;
        EXPORTED_GLOBALS.forEach((name) => delete window[name]);
    });

    it('safeSetInnerHTML registers the hook on its first call', async () => {
        const { hooks } = await loadThenLateBind();
        expect(hookCount(hooks)).toBe(0);

        const el = document.createElement('div');
        window.safeSetInnerHTML(el, anchorHtmlWithTarget('_blank'), true);

        expect(hookCount(hooks)).toBe(1);
        expect(el.querySelector('a').getAttribute('rel')).toBe(
            'noopener noreferrer'
        );
    });

    it('sanitizeHtml registers the hook on its first call', async () => {
        const { hooks } = await loadThenLateBind();
        expect(hookCount(hooks)).toBe(0);

        const out = window.sanitizeHtml(anchorHtmlWithTarget('_blank'));

        expect(hookCount(hooks)).toBe(1);
        expect(firstAnchor(out).getAttribute('rel')).toBe(
            'noopener noreferrer'
        );
    });

    it('safeCreateElement registers the hook on its first call', async () => {
        const { hooks } = await loadThenLateBind();
        expect(hookCount(hooks)).toBe(0);

        // Its config (ALLOWED_TAGS only) keeps DOMPurify's default attribute
        // list, which has no `target`, so production strips the target and
        // the hook has nothing to act on; only the registration is pinned.
        const anchor = window.safeCreateElement('a', 'x', {
            href: 'https://example.test',
        });

        expect(hookCount(hooks)).toBe(1);
        expect(anchor.tagName).toBe('A');
    });

    it('the exported ensure registers without any sanitize call', async () => {
        const { hooks } = await loadThenLateBind();
        window.XSSProtection.ensureTabnabbingHook();
        expect(hookCount(hooks)).toBe(1);
    });

    it('registration is idempotent across sanitize calls', async () => {
        const { hooks } = await loadThenLateBind();
        for (let i = 0; i < 3; i++) {
            window.sanitizeHtml('<p>again</p>');
        }
        window.XSSProtection.ensureTabnabbingHook();
        expect(hookCount(hooks)).toBe(1);
    });

    it('a replaced DOMPurify instance gets the hook too', async () => {
        const first = await loadThenLateBind();
        window.sanitizeHtml('<p>warm</p>');
        expect(hookCount(first.hooks)).toBe(1);

        const second = makePurifier();
        globalThis.DOMPurify = second.purifier;
        const out = window.sanitizeHtml(anchorHtmlWithTarget('_blank'));
        expect(hookCount(second.hooks)).toBe(1);
        expect(firstAnchor(out).getAttribute('rel')).toBe(
            'noopener noreferrer'
        );

        // Switching back must not register a duplicate on the first one.
        globalThis.DOMPurify = first.purifier;
        window.sanitizeHtml('<p>again</p>');
        expect(hookCount(first.hooks)).toBe(1);
    });

    it('a DOMPurify without addHook is tolerated and warned about once', async () => {
        delete globalThis.DOMPurify;
        vi.resetModules();
        await import('@js/security/xss-protection.js');
        const warn = vi.spyOn(globalThis.SafeLogger, 'warn');
        try {
            globalThis.DOMPurify = { isSupported: true, sanitize: (dirty) => String(dirty) };

            expect(() => window.sanitizeHtml('<p>x</p>')).not.toThrow();
            window.sanitizeHtml('<p>y</p>');
            window.XSSProtection.ensureTabnabbingHook();

            const hookWarnings = warn.mock.calls.filter((args) =>
                String(args[0]).includes('addHook')
            );
            expect(hookWarnings).toHaveLength(1);
        } finally {
            warn.mockRestore();
        }
    });

    it('a SafeLogger without a warn function does not break sanitizing', async () => {
        delete globalThis.DOMPurify;
        vi.resetModules();
        await import('@js/security/xss-protection.js');
        const savedLogger = globalThis.SafeLogger;
        try {
            globalThis.SafeLogger = {};
            globalThis.DOMPurify = { isSupported: true, sanitize: (dirty) => String(dirty) };
            expect(() => window.sanitizeHtml('<p>x</p>')).not.toThrow();
        } finally {
            globalThis.SafeLogger = savedLogger;
        }
    });

    it.each([
        '_blank', '_BLANK', ' _blank ', 'popup', '_new',
        // Padded keywords are not the keyword: " _top " names a new window.
        ' _top ', '_self ',
    ])('target %j can open a new context and gains rel', async (target) => {
        await loadThenLateBind();
        const out = window.sanitizeHtml(anchorHtmlWithTarget(target));
        expect(firstAnchor(out).getAttribute('rel')).toBe(
            'noopener noreferrer'
        );
    });

    it.each([
        '_self', '_SELF', '_parent', '_top', '',
    ])('target %j stays in the same context and is left alone', async (target) => {
        await loadThenLateBind();
        const out = window.sanitizeHtml(anchorHtmlWithTarget(target));
        expect(firstAnchor(out).hasAttribute('rel')).toBe(false);
    });

    it.each([
        ['area', '_blank'], ['area', 'popup'],
        ['form', '_blank'], ['form', 'popup'],
    ])('<%s target=%j> can open a new context and gains rel', async (tag, target) => {
        await loadThenLateBind();
        const out = window.sanitizeHtml(elementHtmlWithTarget(tag, target), {
            ALLOWED_TAGS: [tag],
            FORBID_TAGS: [],
        });
        const el = firstElement(out, tag);
        expect(el).not.toBeNull();
        expect(el.getAttribute('rel')).toBe('noopener noreferrer');
    });

    it.each(['area', 'form'])('<%s target=_self> is left alone', async (tag) => {
        await loadThenLateBind();
        const out = window.sanitizeHtml(elementHtmlWithTarget(tag, '_self'), {
            ALLOWED_TAGS: [tag],
            FORBID_TAGS: [],
        });
        expect(firstElement(out, tag).hasAttribute('rel')).toBe(false);
    });

    it('an SVG <a target=_blank> (lowercase tagName) gains rel', async () => {
        await loadThenLateBind();
        const out = window.sanitizeHtml(
            '<svg><a href="https://example.test" target="_blank">x</a></svg>',
            { ALLOWED_TAGS: ['svg', 'a'] }
        );
        const anchor = firstElement(out, 'svg a');
        expect(anchor).not.toBeNull();
        expect(anchor.tagName).toBe('a');
        expect(anchor.getAttribute('rel')).toBe('noopener noreferrer');
    });

    it.each([
        ['name', 'tagName'], ['id', 'tagName'],
        ['name', 'getAttribute'], ['id', 'getAttribute'],
        ['name', 'setAttribute'], ['id', 'setAttribute'],
    ])('a <form> whose control %s=%j shadows the built-in still gains rel', async (attr, prop) => {
        const { stats } = await loadThenLateBind();
        const form = document.createElement('form');
        form.setAttribute('action', 'https://example.test');
        form.setAttribute('target', 'popup');
        const control = document.createElement('input');
        control.setAttribute(attr, prop);
        form.appendChild(control);

        const out = window.sanitizeHtml(form.outerHTML, {
            ALLOWED_TAGS: ['form', 'input'],
            ALLOWED_ATTR: ['action', 'target', 'rel', 'name', 'id'],
            FORBID_TAGS: [],
        });

        // The emulated override was live while the hook ran...
        expect(stats.shadowedCount).toBeGreaterThanOrEqual(1);
        // ...and the hook read and wrote through Element.prototype anyway.
        const el = firstElement(out, 'form');
        expect(el).not.toBeNull();
        expect(el.getAttribute('rel')).toBe('noopener noreferrer');
    });

    it('existing rel tokens are kept, opener is dropped, forced ones added once', async () => {
        await loadThenLateBind();
        const a = document.createElement('a');
        a.setAttribute('href', 'https://example.test');
        a.setAttribute('target', '_blank');
        a.setAttribute('rel', 'nofollow UGC opener NoOpener nofollow');
        a.textContent = 'x';

        const out = window.sanitizeHtml(a.outerHTML);
        const rel = firstAnchor(out).getAttribute('rel');
        const tokens = rel.split(/\s+/);

        expect(tokens).toEqual(['nofollow', 'UGC', 'noopener', 'noreferrer']);
        expect(tokens.map((t) => t.toLowerCase())).not.toContain('opener');
    });
});