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.

9.2 KB
Raw Blame
import '@js/config/urls.js';
import '@js/services/api.js';
import '@js/security/url-validator.js';
import '@js/security/safe-fetch.js';

const context = 'a'.repeat(64);
const json = (body, status = 200, headers = {}) => new Response(JSON.stringify(body), {
    status, headers: { 'Content-Type': 'application/json', ...headers }
});
const rejected = () => json({ error: 'CSRF token missing or invalid' }, 403, { 'X-LDR-CSRF-Rejected': '1' });
const refreshed = (authContext = context) => json({ csrf_token: 'fresh-token', auth_context: authContext });
const token = () => document.querySelector('meta[name="csrf-token"]').content;

describe('session-bound CSRF recovery', () => {
    const originalLocation = window.location;
    const originalFetch = globalThis.fetch;

    beforeEach(() => {
        delete window.location;
        window.location = { href: 'http://localhost/settings', pathname: '/settings', search: '', hash: '' };
        document.head.replaceChildren();
        for (const [name, content] of [['csrf-token', 'stale-token'], ['auth-context', context]]) {
            const meta = document.createElement('meta');
            meta.name = name;
            meta.content = content;
            document.head.append(meta);
        }
        globalThis.fetch = vi.fn();
    });
    afterEach(() => {
        globalThis.fetch = originalFetch;
        window.location = originalLocation;
        document.head.replaceChildren();
        vi.useRealTimers();
    });

    it('refreshes and retries a rejected JSON write exactly once with the same body', async () => {
        fetch.mockResolvedValueOnce(rejected()).mockResolvedValueOnce(refreshed()).mockResolvedValueOnce(json({ saved: true }));
        const body = JSON.stringify({ value: 'keep this exact payload' });
        await expect(window.api.fetchWithErrorHandling('/settings/api/save', { method: 'POST', body })).resolves.toEqual({ saved: true });
        expect(fetch).toHaveBeenCalledTimes(3);
        expect(fetch.mock.calls.map(([url]) => url)).toEqual(['/settings/api/save', '/auth/csrf-token', '/settings/api/save']);
        expect(fetch.mock.calls[1][1]).toMatchObject({ cache: 'no-store', credentials: 'same-origin', redirect: 'error' });
        const replay = fetch.mock.calls[2][1];
        expect(replay.body).toBe(body);
        expect(new globalThis.Headers(replay.headers).get('X-CSRFToken')).toBe('fresh-token');
        expect(new globalThis.Headers(replay.headers).get('X-LDR-Auth-Context')).toBe(context);
        expect(replay.signal).toBe(fetch.mock.calls[0][1].signal);
        expect(token()).toBe('fresh-token');
    });

    it('supports auth-aware raw responses and replaces either spelling of a caller token', async () => {
        fetch.mockResolvedValueOnce(rejected()).mockResolvedValueOnce(refreshed()).mockResolvedValueOnce(json({ deleted: true }));
        const options = { method: 'DELETE', headers: new globalThis.Headers({ 'X-CSRF-Token': 'old-override', 'X-Trace': 'keep', 'x-ldr-auth-context': 'caller-value' }) };
        const response = await window.safeFetchWithAuth('/library/api/item/1', options);
        expect(await response.json()).toEqual({ deleted: true });
        const sent = new globalThis.Headers(fetch.mock.calls[2][1].headers);
        expect(sent.get('X-CSRFToken')).toBe('fresh-token');
        expect(sent.has('X-CSRF-Token')).toBe(false);
        expect(sent.get('X-Trace')).toBe('keep');
        expect(new globalThis.Headers(fetch.mock.calls[0][1].headers).get('X-LDR-Auth-Context')).toBe(context);
        expect(sent.get('X-LDR-Auth-Context')).toBe(context);
        expect(options.headers.get('X-CSRF-Token')).toBe('old-override');
        expect(options.headers.get('X-LDR-Auth-Context')).toBe('caller-value');
    });

    it.each([
        ['/api/save', { method: 'POST', body: '{}' }, json({ error: 'Permission denied' }, 403)],
        ['/api/save', { method: 'POST', body: '{}' }, json({ error: 'failed' }, 500)],
        ['/api/save', { method: 'GET' }, rejected()],
        ['https://outside.example/save', { method: 'POST', body: '{}' }, rejected()],
        ['//outside.example/save', { method: 'POST', body: '{}' }, rejected()],
        ['/\\outside.example/save', { method: 'POST', body: '{}' }, rejected()],
        ['/\t/outside.example/save', { method: 'POST', body: '{}' }, rejected()],
        ['/auth/login', { method: 'POST', body: '{}' }, rejected()],
        ['/api/save', { method: 'POST', body: new FormData() }, rejected()],
        ['/api/save', { method: 'POST', body: '{}', credentials: 'omit' }, rejected()],
    ])('does not retry an ineligible request or unrelated error (%s)', async (url, options, response) => {
        fetch.mockResolvedValueOnce(response);
        await expect(window.api.fetchWithErrorHandling(url, options)).rejects.toThrow();
        expect(fetch).toHaveBeenCalledTimes(1);
        expect(token()).toBe('stale-token');
    });

    it('does not retry after a transport failure with an unknown write outcome', async () => {
        fetch.mockRejectedValueOnce(new TypeError('Failed to fetch'));
        await expect(window.api.postJSON('/api/save', {})).rejects.toThrow('Failed to fetch');
        expect(fetch).toHaveBeenCalledTimes(1);
    });

    it('does not retry a CSRF rejection reached through a redirect', async () => {
        const response = rejected();
        Object.defineProperty(response, 'redirected', { value: true });
        fetch.mockResolvedValueOnce(response);
        await expect(window.api.postJSON('/api/save', {})).rejects.toThrow('CSRF');
        expect(fetch).toHaveBeenCalledTimes(1);
    });

    it('does not recover without a rendered authenticated context', async () => {
        document.querySelector('meta[name="auth-context"]').remove();
        fetch.mockResolvedValueOnce(rejected());
        await expect(window.api.postJSON('/api/save', {})).rejects.toThrow('CSRF');
        expect(fetch).toHaveBeenCalledTimes(1);
    });

    it('never retries after another login replaced the original one', async () => {
        fetch.mockResolvedValueOnce(rejected()).mockResolvedValueOnce(refreshed('b'.repeat(64)));
        await expect(window.api.postJSON('/api/save', {})).rejects.toThrow(/sign-in.*changed/i);
        expect(fetch).toHaveBeenCalledTimes(2);
        expect(token()).toBe('stale-token');
    });

    it('does not replay if the page context changes while refreshing', async () => {
        fetch.mockResolvedValueOnce(rejected()).mockImplementationOnce(async () => {
            document.querySelector('meta[name="auth-context"]').content = 'b'.repeat(64);
            return refreshed();
        });
        await expect(window.api.postJSON('/api/save', {})).rejects.toThrow(/sign-in.*changed/i);
        expect(fetch).toHaveBeenCalledTimes(2);
        expect(token()).toBe('stale-token');
    });

    it('sends an expired session to login without replaying its write', async () => {
        fetch.mockResolvedValueOnce(rejected()).mockResolvedValueOnce(json({ error: 'Authentication required' }, 401));
        const onRejected = vi.fn();
        window.api.postJSON('/api/save', {}).catch(onRejected);
        await vi.waitFor(() => expect(window.location.href).toBe('/auth/login?next=%2Fsettings'));
        expect(fetch).toHaveBeenCalledTimes(2);
        expect(onRejected).not.toHaveBeenCalled();
    });

    it('stops after the second CSRF rejection', async () => {
        fetch.mockResolvedValueOnce(rejected()).mockResolvedValueOnce(refreshed()).mockResolvedValueOnce(rejected());
        await expect(window.api.postJSON('/api/save', {})).rejects.toThrow('CSRF');
        expect(fetch).toHaveBeenCalledTimes(3);
    });

    it.each([json({ error: 'refresh failed' }, 503), json({ csrf_token: 'fresh-token' }), json({ auth_context: context })])(
        'does not replay when refresh fails or is malformed', async response => {
            fetch.mockResolvedValueOnce(rejected()).mockResolvedValueOnce(response);
            await expect(window.api.postJSON('/api/save', {})).rejects.toThrow();
            expect(fetch).toHaveBeenCalledTimes(2);
            expect(token()).toBe('stale-token');
        }
    );

    it('honors cancellation during refresh and never sends the write again', async () => {
        const controller = new AbortController();
        fetch.mockResolvedValueOnce(rejected()).mockImplementationOnce(async (_url, options) => {
            controller.abort();
            options.signal.throwIfAborted();
        });
        await expect(window.api.fetchWithErrorHandling('/api/save', {
            method: 'POST', body: '{}', signal: controller.signal
        })).rejects.toMatchObject({ name: 'AbortError' });
        expect(fetch).toHaveBeenCalledTimes(2);
    });

    it('keeps the original timeout across refresh', async () => {
        vi.useFakeTimers();
        fetch.mockResolvedValueOnce(rejected()).mockImplementationOnce((_url, options) => new Promise((_resolve, reject) => {
            options.signal.addEventListener('abort', () => reject(new globalThis.DOMException('aborted', 'AbortError')), { once: true });
        }));
        const request = window.api.fetchWithErrorHandling('/api/save', { method: 'POST', body: '{}', timeout: 100 });
        const assertion = expect(request).rejects.toThrow('Request timed out');
        await vi.advanceTimersByTimeAsync(100);
        await assertion;
        expect(fetch).toHaveBeenCalledTimes(2);
    });
});