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.

4.4 KB
Raw Blame
/**
 * Authentication rate-limit check against a fresh server using the shipped
 * defaults, with rate limiting enabled. All submissions fail form validation.
 *
 * Usage: BASE_URL=http://127.0.0.1:5000 node tests/ui_tests/test_rate_limiting_settings.js
 */

const assert = require('node:assert/strict');
const puppeteer = require('puppeteer');
const { getPuppeteerLaunchOptions } = require('./puppeteer_config');

async function postAuthForm(page, endpoint, data) {
    return page.evaluate(async (path, formData) => {
        const csrfResponse = await fetch('/auth/csrf-token');
        if (!csrfResponse.ok) {
            throw new Error(`CSRF token request failed: ${csrfResponse.status}`);
        }
        const { csrf_token: csrfToken } = await csrfResponse.json();
        if (!csrfToken) {
            throw new Error('CSRF token response is missing its token');
        }

        const response = await fetch(path, {
            method: 'POST',
            credentials: 'same-origin',
            redirect: 'manual',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
                'X-CSRFToken': csrfToken
            },
            body: new URLSearchParams(formData).toString()
        });
        return {
            status: response.status,
            headers: {
                limit: response.headers.get('X-RateLimit-Limit'),
                remaining: response.headers.get('X-RateLimit-Remaining'),
                reset: response.headers.get('X-RateLimit-Reset'),
                retryAfter: response.headers.get('Retry-After')
            },
            body: response.status === 429 ? await response.json() : null
        };
    }, endpoint, data);
}

function assertRateLimited(response, limit) {
    assert.equal(response.status, 429, 'Expected a rate-limit response');
    assert.equal(response.headers.limit, String(limit));
    assert.equal(response.headers.remaining, '0');
    assert.match(response.headers.reset, /^\d+(\.\d+)?$/);
    assert.match(response.headers.retryAfter, /^\d+$/);
    assert.equal(response.body.error, 'Too many requests');
    assert.equal(response.body.message, 'Too many attempts. Please try again later.');
}

async function exhaustBucket(page, endpoint, data, allowedAttempts) {
    for (let attempt = 1; attempt <= allowedAttempts; attempt++) {
        const response = await postAuthForm(page, endpoint, data);
        assert.equal(response.status, 400,
            `${endpoint}: attempt ${attempt}/${allowedAttempts} must reach form validation`);
    }
    const response = await postAuthForm(page, endpoint, data);
    assertRateLimited(response, allowedAttempts);
    console.log(`${endpoint}: ${allowedAttempts} validation failures allowed; next attempt returned 429`);
}

async function testRateLimiting() {
    // CI servers normally disable limiting; an explicit opt-in is required.
    if (process.env.CI && process.env.ENABLE_RATE_LIMITING !== 'true') {
        console.log('Skipping rate limiting check in CI (requires ENABLE_RATE_LIMITING=true)');
        return;
    }

    const baseUrl = (process.env.BASE_URL || 'http://127.0.0.1:5000').replace(/\/+$/, '');
    const browser = await puppeteer.launch(getPuppeteerLaunchOptions());
    try {
        const page = await browser.newPage();
        await page.goto(`${baseUrl}/auth/login`, { waitUntil: 'domcontentloaded' });
        const invalidLogin = { username: '', password: '' };
        await exhaustBucket(page, '/auth/login', invalidLogin, 5);

        // Use the same browser and client IP: spending the login quota must
        // leave the independent registration bucket usable.
        await page.goto(`${baseUrl}/auth/register`, { waitUntil: 'domcontentloaded' });
        await exhaustBucket(page, '/auth/register', {
            username: 'x',
            password: '',
            confirm_password: '',
            acknowledge: 'false'
        }, 10);

        // The login bucket must still be exhausted after registration checks.
        assertRateLimited(await postAuthForm(page, '/auth/login', invalidLogin), 5);
        console.log('Authentication rate limits, separate buckets, 429 bodies and headers passed');
    } finally {
        await browser.close();
    }
}

if (require.main === module) {
    testRateLimiting()
        .then(() => process.exit(0))
        .catch(error => {
            console.error('Rate limiting check failed:', error);
            process.exit(1);
        });
}

module.exports = { testRateLimiting };