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.

5.5 KB
Raw Blame
"""Runtime positive controls for the companion isolation census (#7097)."""

import ast

import pytest
from fastapi.testclient import TestClient

from tests.security import test_cross_user_isolation_census as census

# allow: no-sut-import — the census reads the production package's AST; the
# runtime controls below import and construct the real NoteService through
# the same source text supplied to that analyzer. No database is accessed.
_HEADER = """
from __future__ import annotations
import typing
import typing as t
from typing import Annotated, ForwardRef, ForwardRef as FR
from fastapi import Depends, FastAPI
from local_deep_research.research_library.notes.services.note_service import NoteService

app = FastAPI()

def require_auth() -> str:
    return "alice"
"""
_MODULE = "local_deep_research.web.routers.control"


def _probe_source(declaration):
    return (
        _HEADER
        + f"""
@app.get("/probe")
def probe(*, {declaration}, auth_username: Annotated[str, Depends(require_auth)]):
    return {{"authenticated_username": auth_username, "service_username": svc.username}}
"""
    )


@pytest.mark.parametrize(
    "declaration",
    [
        "svc: Annotated[NoteService, Depends()]",
        "svc: \"Annotated[ForwardRef('NoteService'), Depends()]\"",
        "svc: \"Annotated[typing.ForwardRef('NoteService'), Depends()]\"",
        "svc: \"Annotated[t.ForwardRef('NoteService'), Depends()]\"",
        "svc: \"Annotated[FR('NoteService'), Depends()]\"",
        "svc: \"ForwardRef('NoteService')\" = Depends()",
        "svc: \"FR('NoteService')\" = Depends()",
        "svc: \"Annotated[ForwardRef(arg='NoteService'), Depends()]\"",
    ],
)
def test_runtime_valid_forward_refs_are_reported(declaration):
    source = _probe_source(declaration)
    namespace = {"__name__": __name__}
    # Every source fragment is a fixed local test fixture, never user input.
    exec(compile(source, "<census-forward-ref-control>", "exec"), namespace)  # noqa: S102

    # Prove these annotations actually construct the identity-taking service
    # from request input, rather than merely looking suspicious to the AST.
    with TestClient(namespace["app"]) as client:
        response = client.get("/probe", params={"username": "mallory"})
    assert response.status_code == 200
    assert response.json() == {
        "authenticated_username": "alice",
        "service_username": "mallory",
    }
    assert {
        target
        for _, target in census.injected_identity_callables(
            ast.parse(source), _MODULE
        )
    } == {"NoteService"}


def test_auth_derived_forward_ref_remains_scoped():
    source = (
        _HEADER
        + """
def authenticated_identity(username: Annotated[str, Depends(require_auth)]) -> str:
    return username

@app.get("/probe")
def probe(username: "Annotated[FR('authenticated_identity'), Depends()]"):
    return {"username": username}
"""
    )
    namespace = {"__name__": __name__}
    # Every source fragment is a fixed local test fixture, never user input.
    exec(compile(source, "<census-auth-control>", "exec"), namespace)  # noqa: S102
    with TestClient(namespace["app"]) as client:
        response = client.get("/probe", params={"username": "mallory"})
    assert response.status_code == 200
    assert response.json() == {"username": "alice"}

    (route,) = census._control_routes(source)
    assert route.auth_params == {"username"}
    assert census.identity_from_request_input(route) == []
    assert census.injected_identity_callables(ast.parse(source), _MODULE) == []


@pytest.mark.parametrize(
    "declaration",
    [
        "svc: Annotated[str, \"ForwardRef('NoteService')\"]",
        "svc: Annotated[str, ForwardRef('NoteService')]",
        "svc: Annotated[str, {'arg': 'NoteService'}]",
        "svc: str = 'ForwardRef(\"NoteService\")'",
        "svc: Annotated[ForwardRef('str', module='NoteService'), Depends()]",
    ],
)
def test_annotation_metadata_and_value_strings_remain_data(declaration):
    assert (
        census.injected_identity_callables(
            ast.parse(_probe_source(declaration)), _MODULE
        )
        == []
    )


@pytest.mark.parametrize(
    "imports, expression",
    [
        ("", "ForwardRef('NoteService')"),
        ("from unrelated import ForwardRef", "ForwardRef('NoteService')"),
        ("import unrelated as typing", "typing.ForwardRef('NoteService')"),
        ("from unrelated import ForwardRef as FR", "FR('NoteService')"),
    ],
)
def test_unrelated_forward_ref_names_are_not_evaluated(imports, expression):
    source = f"""
from fastapi import Depends
from local_deep_research.research_library.notes.services.note_service import NoteService
{imports}
def probe(svc: {expression} = Depends()):
    return svc
"""
    assert census.injected_identity_callables(ast.parse(source), _MODULE) == []


@pytest.mark.parametrize(
    "argument",
    ["target_name", "42", "'['", "''", "'NoteService' + suffix"],
)
def test_nonliteral_or_unparseable_references_remain_unresolved(argument):
    source = _probe_source(f"svc: Annotated[FR({argument}), Depends()]")
    assert census.injected_identity_callables(ast.parse(source), _MODULE) == []


def test_nested_forward_references_are_bounded():
    expression = "NoteService"
    for _ in range(census._MAX_QUOTE_DEPTH + 1):
        expression = f"FR({expression!r})"
    node = ast.parse(expression, mode="eval").body
    unresolved = census._unquote(node, {"FR": ("typing", "ForwardRef")})
    assert isinstance(unresolved, ast.Call)
    assert census._dependency_name(unresolved) is None