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.7 KB
Raw Blame
"""Request-scoped template authorization and the served browser policy."""

import concurrent.futures
import re
from pathlib import Path

from bs4 import BeautifulSoup
from fastapi.testclient import TestClient
from starlette.requests import Request

import local_deep_research
from local_deep_research.web.template_config import templates
from local_deep_research.web.utils.csp import template_security_context


def _nonce(response):
    policy = response.headers["content-security-policy"]
    matches = re.findall(r"'nonce-([A-Za-z0-9_-]{43})'", policy)
    assert len(matches) == 2 and matches[0] == matches[1]
    return matches[0]


def test_each_response_authorizes_its_own_scripts_and_styles(app):
    client = TestClient(app, raise_server_exceptions=False)
    seen = set()
    for path, status in (("/auth/login", 200), ("/missing-template-page", 404)):
        for _ in range(2):
            response = client.get(path)
            assert response.status_code == status
            nonce = _nonce(response)
            assert nonce not in seen
            seen.add(nonce)
            tags = BeautifulSoup(response.text, "html.parser")
            executable = [
                tag
                for tag in tags.find_all(["script", "style"])
                if tag.name == "style" or not tag.has_attr("src")
            ]
            assert executable
            assert all(tag.get("nonce") == nonce for tag in executable), {
                "path": path,
                "missing": sum(not tag.get("nonce") for tag in executable),
                "mismatched": sum(
                    bool(tag.get("nonce")) and tag.get("nonce") != nonce
                    for tag in executable
                ),
            }
            helper = tags.find(
                "script", src="/static/js/security/template-events.js"
            )
            assert helper is not None
            assert helper["nonce"] == nonce
            token = helper["data-template-token"]
            assert re.fullmatch(r"[A-Za-z0-9_-]{43}", token)
            assert token != nonce
            for tag in tags.find_all(True):
                assert not any(name.startswith("on") for name in tag.attrs)
                if any(name.startswith("data-ldr-on") for name in tag.attrs):
                    assert tag["data-ldr-template-token"] == token


def test_template_context_is_copied_and_caller_values_cannot_set_authorizations(
    app,
):
    shared = {
        "csp_nonce": "caller-value",
        "template_event_token": "caller-value",
    }
    original = dict(shared)
    authorizations = []
    for legacy in (False, True):
        request = Request(
            {
                "type": "http",
                "method": "GET",
                "path": "/auth/login",
                "root_path": "",
                "scheme": "http",
                "headers": [],
                "query_string": b"",
                "session": {},
                "app": app,
                "router": app.router,
                "server": ("testserver", 80),
            }
        )
        if legacy:
            response = templates.TemplateResponse(
                "auth/login.html", shared, request=request
            )
        else:
            response = templates.TemplateResponse(
                request, "auth/login.html", shared
            )
        security = template_security_context(request.scope)
        assert response.context["csp_nonce"] == security.nonce
        assert response.context["template_event_token"] == security.event_token
        assert response.context is not shared
        assert shared == original
        authorizations.append(security)
    assert authorizations[0] != authorizations[1]


def test_independent_scopes_remain_independent_under_concurrent_render_preparation():
    def prepare(_):
        scope = {}
        first = template_security_context(scope)
        assert template_security_context(scope) is first
        return first

    with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:
        results = list(executor.map(prepare, range(16)))
    assert len({value.nonce for value in results}) == 16
    assert len({value.event_token for value in results}) == 16
    assert all(value.nonce != value.event_token for value in results)


def test_checked_in_html_keeps_event_code_out_of_attributes():
    web = Path(local_deep_research.__file__).parent / "web"
    paths = [
        *(web / "templates").rglob("*.html"),
        *(web / "static/templates").rglob("*.html"),
    ]
    assert paths
    for path in paths:
        tags = BeautifulSoup(path.read_text(encoding="utf-8"), "html.parser")
        assert not any(
            name.startswith("on")
            for tag in tags.find_all(True)
            for name in tag.attrs
        ), path.name