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.

8.8 KB
Raw Blame
import '@js/security/url-validator.js';

const { URLValidator } = window;
const baseUrl = window.location.href;

describe('URLValidator adversarial inputs', () => {
    describe('scheme smuggling', () => {
        it.each([
            'java\tscript:alert(1)',
            'java\nscript:alert(1)',
            'java\rscript:alert(1)',
            ' \tjavascript:alert(1)',
        ])('rejects tab/newline-smuggled JavaScript schemes: %j', (url) => {
            expect(URLValidator.isSafeUrl(url)).toBe(false);
        });

        it.each(['java\x01script:alert(1)', 'java\x0Bscript:alert(1)'])(
            'treats non-stripped C0 lookalikes as same-origin relative paths: %j',
            (url) => {
                const parsed = new URL(url, baseUrl);

                expect(parsed.protocol).toBe('http:');
                expect(URLValidator.isSafeUrl(url)).toBe(true);
            }
        );
    });

    describe('entity, fullwidth, and NUL lookalikes', () => {
        it.each([
            'javascript:alert(1)',
            'javascript:alert(1)',
            'java\x00script:alert(1)',
            '%6Aavascript:alert(1)',
        ])('resolves %j as a relative path rather than decoding a JavaScript scheme', (url) => {
            const parsed = new URL(url, baseUrl);

            expect(parsed.protocol).toBe('http:');
            expect(parsed.hostname).toBe('localhost');
            expect(URLValidator.isSafeUrl(url)).toBe(true);
        });
    });

    describe('direct unsafe schemes', () => {
        it.each([
            'data:text/html,<script>alert(1)</script>',
            'vbscript:msgbox(1)',
            'file:///etc/passwd',
            'about:blank',
            'blob:https://example.com/uuid',
            'DATA:text/html,<script>alert(1)</script>',
            'VBScript:msgbox(1)',
            'File:///etc/passwd',
            'About:blank',
        ])('rejects %j', (url) => {
            expect(URLValidator.isSafeUrl(url)).toBe(false);
        });
    });

    describe('protocol-relative and backslash URLs', () => {
        it('recognizes protocol-relative URLs as allowed HTTP URLs without a trusted-domain policy', () => {
            expect(new URL('//evil.com', baseUrl).href).toBe('http://evil.com/');
            expect(URLValidator.isSafeUrl('//evil.com')).toBe(true);
        });

        it('normalizes double-backslash URLs to protocol-relative HTTP URLs', () => {
            expect(new URL('\\\\evil.com', baseUrl).href).toBe('http://evil.com/');
            expect(URLValidator.isSafeUrl('\\\\evil.com')).toBe(true);
        });

        it('normalizes slash-backslash URLs to protocol-relative HTTP URLs', () => {
            expect(new URL('/\\evil.com', baseUrl).href).toBe('http://evil.com/');
            expect(URLValidator.isSafeUrl('/\\evil.com')).toBe(true);
        });
    });

    describe('trusted-domain matching', () => {
        const options = { trustedDomains: ['example.com'] };
        const cyrillicHomoglyph = 'https://еxample.com';

        it('rejects an IDN homoglyph rather than treating it as a trusted domain', () => {
            expect(new URL(cyrillicHomoglyph).hostname).toBe('xn--xample-2of.com');
            expect(URLValidator.isSafeUrl(cyrillicHomoglyph, options)).toBe(false);
        });

        it.each([
            'https://example.com。attacker.com',
            'https://[email protected]',
            'https://evil.com#@example.com',
            'https://example.com.evil.com',
            'https://notexample.com',
        ])('rejects trusted-domain confusion URL %j', (url) => {
            expect(URLValidator.isSafeUrl(url, options)).toBe(false);
        });

        it('allows the trusted apex and a real subdomain', () => {
            expect(URLValidator.isSafeUrl('https://example.com', options)).toBe(true);
            expect(URLValidator.isSafeUrl('https://sub.example.com/path', options)).toBe(true);
        });
    });

    describe('benign URLs', () => {
        it.each([
            'settings/general',
            '/news/subscriptions',
            'http://example.com/path',
            'https://example.com/path?q=1',
            'ftp://files.example.com/pub',
            'ftps://files.example.com/pub',
        ])('allows %j', (url) => {
            expect(URLValidator.isSafeUrl(url)).toBe(true);
        });

        it('rejects tab-smuggled JavaScript while sanitizing', () => {
            expect(URLValidator.sanitizeUrl('java\tscript:alert(1)')).toBeNull();
        });
    });

    describe('safeAssign carve-outs', () => {
        it.each([
            ' data:text/html,<script>alert(1)</script>',
            'DATA:text/html,<script>alert(1)</script>',
            'da\tta:text/html,<script>alert(1)</script>',
        ])('rejects a non-exact data: prefix: %j', (url) => {
            const element = {};

            expect(URLValidator.safeAssign(element, 'href', url)).toBe(false);
            expect(element.href).toBeUndefined();
        });

        it('applies trusted-domain validation to double-backslash URLs that miss the internal-path carve-out', () => {
            const element = {};

            expect(URLValidator.safeAssign(element, 'href', '\\\\evil.com', {
                trustedDomains: ['example.com'],
            })).toBe(false);
            expect(element.href).toBeUndefined();
        });

        it('blocks data:text/html assignment', () => {
            const element = {};
            const payload = 'data:text/html,<script>alert(1)</script>';

            expect(URLValidator.safeAssign(element, 'href', payload)).toBe(false);
            expect(element.href).toBeUndefined();
        });

        // An SVG data: document can carry <script>/event-handler content, so
        // safeAssign's data: carve-out must refuse it even for an <img src>,
        // the target where a raster data: image IS accepted. That carve-out
        // checks the MIME type with its own regex, not SAFE_DATA_MIME_TYPES
        // (see the note above its data: branch in url-validator.js). The
        // target is a real <img> so only the MIME check can reject the
        // value: a plain object fails the target check first and would
        // reject even an allowed PNG.
        it('blocks data:image/svg+xml assignment to an img src', () => {
            const element = document.createElement('img');
            const payload = 'data:image/svg+xml;base64,PHN2ZyBvbmxvYWQ9ImFsZXJ0KDEpIi8+';

            expect(URLValidator.safeAssign(element, 'src', payload)).toBe(false);
            expect(element.getAttribute('src')).toBeNull();
        });

        it('does not allow protocol-relative URLs to bypass trustedDomains', () => {
            const element = {};

            expect(URLValidator.safeAssign(element, 'href', '//evil.com', {
                trustedDomains: ['example.com'],
            })).toBe(false);
            expect(element.href).toBeUndefined();
        });

        it('does not allow slash-backslash URLs to bypass trustedDomains', () => {
            const element = {};

            expect(URLValidator.safeAssign(element, 'href', '/\\evil.com', {
                trustedDomains: ['example.com'],
            })).toBe(false);
            expect(element.href).toBeUndefined();
        });

        // Regression: the internal-path carve-out used to compare the RAW
        // string, so a C0 control character at index 1 kept `url[1]` from
        // being '/' while the browser still stripped it and parsed the value
        // as protocol-relative. Every one of these resolved to
        // https://evil.com/ without ever reaching isSafeUrl.
        it.each([
            ['tab', '/\t/evil.com'],
            ['line feed', '/\n/evil.com'],
            ['carriage return', '/\r/evil.com'],
            ['tab before a backslash', '/\t\\evil.com'],
        ])('does not let a %s-smuggled protocol-relative URL skip validation', (_label, url) => {
            const element = {};

            expect(URLValidator.safeAssign(element, 'href', url, {
                trustedDomains: ['example.com'],
            })).toBe(false);
            expect(element.href).toBeUndefined();
        });

        it('still allows trusted carve-outs after the tightening', () => {
            const blobElement = document.createElement('a');
            expect(URLValidator.safeAssign(
                blobElement, 'href', 'blob:https://example.com/uuid'
            )).toBe(true);
            expect(blobElement.href).toBe('blob:https://example.com/uuid');

            const dataElement = document.createElement('img');
            const dataUrl = 'data:image/png;base64,iVBORw0KGgo=';
            expect(URLValidator.safeAssign(dataElement, 'src', dataUrl)).toBe(true);
            expect(dataElement.src).toBe(dataUrl);

            const internalElement = {};
            expect(URLValidator.safeAssign(internalElement, 'href', '/settings/general')).toBe(true);
            expect(internalElement.href).toBe('/settings/general');
        });
    });
});