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.

7.4 KB
Raw Blame
"""Cross-user subscription isolation on the live FastAPI news router.

Each handler opens the authenticated user's encrypted database, so foreign
and absent subscription IDs share the same not-found response. GET, PUT,
DELETE and history return the complete curated NewsAPIException envelope,
including the caller-supplied ID. The run route returns a flat not-found
body. Owner-success and survival controls distinguish isolation from a
broken route; research startup is stubbed to avoid external work.
"""

# allow: no-sut-import — requests exercise the live FastAPI app through
# authenticated clients. Only research startup is replaced with a local stub.
from typing import Final
from uuid import UUID, uuid4

import pytest

from .auth_fixtures import AuthenticatedUser

pytestmark = pytest.mark.real_session_check


QUERY_SENTINEL: Final = "cross-user-news-7c315fc0-9ef0-4d77-b48c-f4cc07fe8955"


def _create_subscription(user: AuthenticatedUser) -> str:
    response = user.client.post(
        "/news/api/subscribe",
        json={
            "name": "Cross-user security subscription",
            "query": QUERY_SENTINEL,
            "subscription_type": "search",
            "refresh_minutes": 240,
        },
    )
    assert response.status_code == 200, response.text[:300]
    subscription_id = response.json()["subscription_id"]
    _ = UUID(subscription_id)
    return subscription_id


def _assert_subscription_not_found(response, subscription_id: str) -> None:
    """Only the caller-supplied ID may vary in the curated error envelope."""
    assert response.status_code == 404, response.text[:300]
    assert response.json() == {
        "error": f"Subscription not found: {subscription_id}",
        "error_code": "SUBSCRIPTION_NOT_FOUND",
        "status_code": 404,
        "details": {"subscription_id": subscription_id},
    }


@pytest.mark.parametrize(
    ("method", "json_body"),
    [
        pytest.param("GET", None, id="get"),
        pytest.param(
            "PUT",
            {"name": "attacker overwrite"},
            id="update",
        ),
        pytest.param("DELETE", None, id="delete"),
    ],
)
def test_foreign_and_absent_subscription_actions_are_indistinguishable(
    two_authenticated_users: tuple[AuthenticatedUser, AuthenticatedUser],
    method: str,
    json_body: dict[str, str] | None,
) -> None:
    # Given
    user_a, user_b = two_authenticated_users
    subscription_id = _create_subscription(user_a)
    absent_subscription_id = str(uuid4())

    # When
    foreign = user_b.client.request(
        method,
        f"/news/api/subscriptions/{subscription_id}",
        json=json_body,
    )
    absent = user_b.client.request(
        method,
        f"/news/api/subscriptions/{absent_subscription_id}",
        json=json_body,
    )

    _assert_subscription_not_found(foreign, subscription_id)
    _assert_subscription_not_found(absent, absent_subscription_id)

    survivor = user_a.client.get(f"/news/api/subscriptions/{subscription_id}")
    assert survivor.status_code == 200
    survivor_data = survivor.json()
    assert survivor_data["id"] == subscription_id
    assert survivor_data["query_or_topic"] == QUERY_SENTINEL
    assert survivor_data["name"] == "Cross-user security subscription"


def test_foreign_subscription_is_absent_from_list(
    two_authenticated_users: tuple[AuthenticatedUser, AuthenticatedUser],
) -> None:
    # Given
    user_a, user_b = two_authenticated_users
    subscription_id = _create_subscription(user_a)

    # When
    response = user_b.client.get("/news/api/subscriptions/current")

    # Then
    assert response.status_code == 200
    subscriptions = response.json()["subscriptions"]
    assert all(item["id"] != subscription_id for item in subscriptions)
    assert all(item["query"] != QUERY_SENTINEL for item in subscriptions)

    # Then
    # Owner-positive control: without it the two ``all(...)`` assertions above
    # hold vacuously over user B's empty list, so a listing endpoint that
    # returned no subscriptions to anyone would still pass.
    survivor = user_a.client.get("/news/api/subscriptions/current")
    assert survivor.status_code == 200
    survivor_subscriptions = survivor.json()["subscriptions"]
    assert any(item["id"] == subscription_id for item in survivor_subscriptions)
    assert any(
        item["query"] == QUERY_SENTINEL for item in survivor_subscriptions
    )


def test_foreign_subscription_cannot_be_run_and_survives(
    two_authenticated_users: tuple[AuthenticatedUser, AuthenticatedUser],
    monkeypatch,
) -> None:
    """A foreign call starts nothing; the owner can start the same subscription."""
    starts = []
    research_id = str(uuid4())

    def start_research(_request, data, username):
        starts.append((username, data))
        return {"status": "success", "research_id": research_id}

    monkeypatch.setattr(
        "local_deep_research.web.routers.news_flask_api._start_research_in_process",
        start_research,
    )

    # Given
    user_a, user_b = two_authenticated_users
    subscription_id = _create_subscription(user_a)

    # When
    denied = user_b.client.post(
        f"/news/api/subscriptions/{subscription_id}/run"
    )

    # Then
    # The body matters as much as the status: an unregistered or misspelled
    # route also answers 404, so status alone would pass against a URL that
    # reaches no handler at all. The JSON payload only comes from the route's
    # own not-found branch, which -- unlike GET/PUT/DELETE/history -- does not
    # go through the NewsAPIException machinery and so does not echo the id.
    assert denied.status_code == 404
    assert denied.json() == {"error": "Subscription not found"}
    assert starts == []

    survivor = user_a.client.get(f"/news/api/subscriptions/{subscription_id}")
    assert survivor.status_code == 200
    assert survivor.json()["query_or_topic"] == QUERY_SENTINEL

    owner_run = user_a.client.post(
        f"/news/api/subscriptions/{subscription_id}/run"
    )
    assert owner_run.status_code == 200, owner_run.text[:300]
    assert owner_run.json()["status"] == "success"
    assert owner_run.json()["research_id"] == research_id
    assert len(starts) == 1
    username, request_data = starts[0]
    assert username == user_a.username
    assert request_data["query"] == QUERY_SENTINEL
    assert request_data["metadata"]["subscription_id"] == subscription_id


def test_foreign_and_absent_subscription_history_are_indistinguishable(
    two_authenticated_users: tuple[AuthenticatedUser, AuthenticatedUser],
) -> None:
    # Given
    user_a, user_b = two_authenticated_users
    subscription_id = _create_subscription(user_a)
    absent_subscription_id = str(uuid4())

    # When
    foreign = user_b.client.get(
        f"/news/api/subscriptions/{subscription_id}/history"
    )
    absent = user_b.client.get(
        f"/news/api/subscriptions/{absent_subscription_id}/history"
    )

    _assert_subscription_not_found(foreign, subscription_id)
    _assert_subscription_not_found(absent, absent_subscription_id)

    # Then
    # Owner-positive control: without it the equality above holds just as well
    # when ``/history`` 404s for everyone, so it could not tell "foreigners are
    # refused" apart from "the route is broken".
    survivor = user_a.client.get(
        f"/news/api/subscriptions/{subscription_id}/history"
    )
    assert survivor.status_code == 200
    survivor_data = survivor.json()
    assert survivor_data["subscription"]["id"] == subscription_id
    assert survivor_data["subscription"]["query_or_topic"] == QUERY_SENTINEL