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.7 KB
Raw Blame
"""Exercise #5787 through real nginx, uvicorn, and the production login route.

Run with LDR_TESTING_WITH_MOCKS=false and nginx on PATH. No provider, Redis,
or existing user database is needed. Both servers bind only to loopback and
the application stores all state below pytest's temporary directory.
"""

# allow: no-sut-import — black-box HTTP test launches the production web application

from contextlib import contextmanager
import itertools
import os
from pathlib import Path
import shutil
import socket
import subprocess
import sys
import time
from uuid import uuid4

import httpx
import pytest


pytestmark = pytest.mark.integration
REPO_ROOT = Path(__file__).resolve().parents[2]
LOGIN_BUDGET = 5
ACCOUNT_BUDGET = 3
MODES = ("overwrite", "append", "separate-lines", "real-ip")


def _unused_port():
    with socket.socket() as listener:
        listener.bind(("127.0.0.1", 0))
        return listener.getsockname()[1]


@contextmanager
def _server(command, log_path, ready_url, *, env=None):
    """Fail on startup errors and reap the process even if an assertion fails."""
    with log_path.open("w") as log:
        process = subprocess.Popen(
            command,
            cwd=REPO_ROOT,
            env=env,
            stdout=log,
            stderr=subprocess.STDOUT,
        )
        try:
            deadline = time.monotonic() + 90
            with httpx.Client(trust_env=False, timeout=1) as client:
                while time.monotonic() < deadline:
                    if process.poll() is not None:
                        pytest.fail(f"Server exited:\n{log_path.read_text()}")
                    try:
                        response = client.get(ready_url)
                        if response.status_code == 200:
                            break
                    except httpx.TransportError:
                        pass  # Server is still starting; the deadline bounds retries.
                    time.sleep(0.1)
                else:
                    pytest.fail(
                        f"Server did not start:\n{log_path.read_text()}"
                    )
            yield
        finally:
            process.terminate()
            try:
                process.wait(timeout=15)
            except subprocess.TimeoutExpired:
                process.kill()
                process.wait(timeout=5)


@pytest.fixture(
    scope="module", params=[False, True], ids=["trust-off", "trust-on"]
)
def proxy(request, tmp_path_factory):
    # Module fixtures run before the function-scoped integration skip.
    if os.environ.get("LDR_TESTING_WITH_MOCKS", "true").lower() == "true":
        pytest.skip(
            "Integration test skipped in mock mode "
            "(set LDR_TESTING_WITH_MOCKS=false to run)"
        )
    nginx = shutil.which("nginx")
    if nginx is None:
        pytest.fail("This integration test requires nginx on PATH")
    root = tmp_path_factory.mktemp(f"login-proxy-{request.param}")
    app_port, proxy_port = _unused_port(), _unused_port()
    while proxy_port == app_port:
        proxy_port = _unused_port()

    # Do not inherit a real data directory, disabled throttling, external
    # storage, or pytest's production-cookie bypass into the child process.
    env = {
        key: value
        for key, value in os.environ.items()
        if not key.startswith("LDR_")
        and key
        not in {
            "PYTEST_CURRENT_TEST",
            "TRUST_PROXY_HEADERS",
            "DISABLE_RATE_LIMITING",
            "RATE_LIMIT_STORAGE_URI",
            "RATELIMIT_STORAGE_URL",
        }
    }
    env.update(
        PYTHONPATH=os.pathsep.join(
            [str(REPO_ROOT / "src"), env.get("PYTHONPATH", "")]
        ),
        LDR_DATA_DIR=str(root / "data"),
        LDR_TEST_MODE="false",
        LDR_DISABLE_RATE_LIMITING="false",
        LDR_SECURITY_RATE_LIMIT_LOGIN=f"{LOGIN_BUDGET} per 15 minutes",
        LDR_SECURITY_ACCOUNT_LOCKOUT_THRESHOLD=str(ACCOUNT_BUDGET),
        LDR_NEWS_SCHEDULER_ENABLED="false",
        RATE_LIMIT_STORAGE_URI="memory://",
        TRUST_PROXY_HEADERS=str(request.param).lower(),
    )
    # Use the production launcher so this includes uvicorn's actual
    # proxy_headers/forwarded_allow_ips wiring, not a TestClient simulation.
    command = [
        sys.executable,
        "-c",
        "from local_deep_research.web.app import _run_with_uvicorn; "
        f"_run_with_uvicorn('127.0.0.1', {app_port}, False)",
    ]
    headers = {
        "overwrite": """
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_set_header X-Real-IP $remote_addr;
        """,
        "append": """
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        """,
        "separate-lines": """
            proxy_set_header X-Forwarded-For $http_x_forwarded_for;
            proxy_set_header X-Forwarded-For $remote_addr;
        """,
        # A proxy using only X-Real-IP must clear the untrusted XFF header
        # (#6849). This covers the supported fallback, not that open defect.
        "real-ip": """
            proxy_set_header X-Forwarded-For "";
            proxy_set_header X-Real-IP $remote_addr;
        """,
    }
    locations = "\n".join(
        f"""location /{mode}/ {{
            proxy_pass http://127.0.0.1:{app_port}/;
            proxy_set_header Host $http_host;
            proxy_set_header X-Forwarded-Proto $scheme;
            {directives}
        }}"""
        for mode, directives in headers.items()
    )
    config = root / "nginx.conf"
    config.write_text(
        f"""daemon off;
master_process off;
error_log stderr;
pid "{root / "nginx.pid"}";
events {{ worker_connections 64; }}
http {{
    access_log off;
    client_body_temp_path "{root / "client-body"}";
    proxy_temp_path "{root / "proxy-temp"}";
    fastcgi_temp_path "{root / "fastcgi-temp"}";
    uwsgi_temp_path "{root / "uwsgi-temp"}";
    scgi_temp_path "{root / "scgi-temp"}";
    server {{
        listen 127.0.0.1:{proxy_port};
        location = /ready {{ return 200 'ready'; }}
        {locations}
    }}
}}
"""
    )
    with (
        _server(
            command,
            root / "app.log",
            f"http://127.0.0.1:{app_port}/auth/csrf-token",
            env=env,
        ),
        _server(
            [nginx, "-p", str(root), "-c", str(config), "-e", "stderr"],
            root / "nginx.log",
            f"http://127.0.0.1:{proxy_port}/ready",
        ),
    ):
        yield f"http://127.0.0.1:{proxy_port}", itertools.count(2)


@contextmanager
def _client(proxy, mode):
    address, addresses = proxy
    # Actual socket source addresses give each scenario an independent bucket
    # without resetting the limiter or letting a request header pick its key.
    source = f"127.0.0.{next(addresses)}"
    with httpx.Client(
        base_url=f"{address}/{mode}/",
        transport=httpx.HTTPTransport(local_address=source),
        trust_env=False,
        timeout=10,
    ) as client:
        response = client.get("auth/csrf-token")
        assert response.status_code == 200, response.text
        client.headers["X-CSRFToken"] = response.json()["csrf_token"]
        yield client


def _login(client, username, attempt, forged):
    headers = []
    if forged:
        headers = [
            ("X-Forwarded-For", f"9.9.9.{attempt + 1}, 8.8.8.8"),
            ("X-Forwarded-For", f"1.1.1.{attempt + 1}"),
            ("X-Real-IP", f"4.4.4.{attempt + 1}"),
        ]
    return client.post(
        "auth/login",
        data={"username": username, "password": "incorrect-password"},
        headers=headers,
    )


def _assert_ip_limit(response):
    # Account lockout is HTML; CSRF is 403. Require the production IP-limit
    # handler's JSON and headers so neither can give a false positive.
    assert response.status_code == 429, response.text
    assert response.json() == {
        "error": "Too many requests",
        "message": "Too many attempts. Please try again later.",
    }
    assert int(response.headers["Retry-After"]) > 0
    assert int(response.headers["X-RateLimit-Limit"]) == LOGIN_BUDGET
    assert int(response.headers["X-RateLimit-Remaining"]) == 0


@pytest.mark.parametrize("mode", MODES)
@pytest.mark.parametrize("forged", [False, True], ids=["control", "forged"])
def test_password_spray_hits_ip_limit(proxy, mode, forged):
    prefix = uuid4().hex[:12]
    with _client(proxy, mode) as client:
        for attempt in range(LOGIN_BUDGET + 2):
            # A different username per request prevents account lockout from
            # hiding a bypass. Keep usernames unique across scenarios too.
            username = f"spray-{prefix}-{attempt}"
            response = _login(client, username, attempt, forged)
            if attempt < LOGIN_BUDGET:
                assert response.status_code == 401, response.text
            else:
                _assert_ip_limit(response)

        # A fresh client must still get its own budget: simply collapsing
        # everyone into the nginx loopback address would also stop the spray.
        with _client(proxy, mode) as independent:
            response = _login(independent, f"independent-{prefix}", 0, forged)
            assert response.status_code == 401, response.text
        _assert_ip_limit(_login(client, f"still-blocked-{prefix}", 8, forged))


def test_account_lockout_remains_separate(proxy):
    with _client(proxy, "append") as client:
        username = f"same-account-{uuid4().hex[:12]}"
        for attempt in range(LOGIN_BUDGET + 1):
            response = _login(client, username, attempt, True)
            if attempt < ACCOUNT_BUDGET:
                assert response.status_code == 401, response.text
            elif attempt < LOGIN_BUDGET:
                assert response.status_code == 429, response.text
                assert "temporarily locked" in response.text
                assert "Retry-After" not in response.headers
            else:
                _assert_ip_limit(response)