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.

10.1 KB
Raw Blame
# allow: no-sut-import — black-box HTTP test; drives real chat routes
"""Request-boundary SSRF pre-flight on the chat custom LLM endpoint.

``send_message`` and ``retry_attempt`` read ``llm.openai_endpoint.url``
from the caller's settings snapshot and hand it to
``start_research_process``. Every sibling research-start boundary gates
the identical read with ``is_safe_custom_llm_endpoint`` BEFORE any
research row is committed or worker spawned (research.py, followup.py,
news_flask_api.py); the chat routes must too. The provider's
``assert_base_url_safe`` still re-validates at client construction —
the boundary buys a clean 400 instead of a committed IN_PROGRESS row
plus a spawned worker that dies at construction.
"""

import json
from unittest.mock import patch

import pytest

# Endpoint shapes is_safe_custom_llm_endpoint refuses (defence-in-depth
# class already shipped at the three sibling boundary sites). Inert
# markers only — nothing is ever fetched: the spawn is mocked, and with
# the gate in place the request never reaches it.
POISONED_ENDPOINTS = [
    # Cloud-instance metadata literal — blocked in every mode and scope.
    "http://169.254.169.254/v1",
    # Link-local (non-literal) — blocked via block_link_local=True.
    "http://169.254.0.1:8000/v1",
]

# Legitimate endpoints an authenticated user could configure; they must
# pass the boundary gate (spawn proceeds, mocked here). The self-hosted
# shape is DNS-independent (localhost needs no resolver); the public
# shape mirrors the sibling hardening suite's control.
SAFE_ENDPOINTS = [
    "http://127.0.0.1:11434/v1",
    "https://api.openai.com/v1",
]


def _snapshot(url_value):
    """A chat-route settings snapshot selecting the openai_endpoint provider."""
    return {
        "llm.provider": {"value": "openai_endpoint"},
        "llm.model": {"value": "test-model"},
        "llm.openai_endpoint.url": {"value": url_value},
    }


def _create_session(client):
    create_response = client.post(
        "/api/chat/sessions",
        json={"initial_query": "Test query"},
        content_type="application/json",
    )
    assert create_response.status_code == 200
    return json.loads(create_response.data)["session_id"]


def _send(client, session_id):
    return client.post(
        f"/api/chat/sessions/{session_id}/messages",
        json={
            "content": "What is quantum computing?",
            "trigger_research": True,
        },
        content_type="application/json",
    )


class TestSendMessageCustomEndpointGate:
    def test_poisoned_endpoint_returns_400_and_never_spawns(
        self, authenticated_client
    ):
        """A metadata / link-local endpoint is refused at the request
        boundary, before the atomic write commits the research row and
        before start_research_process is ever called."""
        from local_deep_research.web.routers import chat as chat_routes

        session_id = _create_session(authenticated_client)
        for url in POISONED_ENDPOINTS:
            with (
                patch.object(
                    chat_routes,
                    "_load_settings",
                    return_value=_snapshot(url),
                ),
                patch.object(chat_routes, "start_research_process") as spawn,
            ):
                response = _send(authenticated_client, session_id)

            assert response.status_code == 400, (
                f"endpoint {url!r} must be refused with 400, got "
                f"{response.status_code}"
            )
            data = json.loads(response.data)
            assert data["success"] is False
            assert data["error"] == "Invalid custom endpoint URL"
            assert not spawn.called, (
                "no worker may be spawned for a poisoned endpoint"
            )

    @pytest.mark.parametrize("url", SAFE_ENDPOINTS)
    def test_safe_endpoint_proceeds_to_spawn(self, authenticated_client, url):
        """Pass-through guard: a legitimate endpoint still reaches
        start_research_process (the boundary gate must not over-block)."""
        from local_deep_research.web.routers import chat as chat_routes

        session_id = _create_session(authenticated_client)
        with (
            patch.object(
                chat_routes,
                "_load_settings",
                return_value=_snapshot(url),
            ),
            patch.object(chat_routes, "start_research_process") as spawn,
        ):
            response = _send(authenticated_client, session_id)

        assert response.status_code == 200
        assert json.loads(response.data)["research_id"] is not None
        assert spawn.called

    def test_unset_endpoint_for_openai_endpoint_provider_proceeds(
        self, authenticated_client
    ):
        """Pass-through guard: provider openai_endpoint with no URL
        configured (defaults apply downstream) is not blocked here."""
        from local_deep_research.web.routers import chat as chat_routes

        session_id = _create_session(authenticated_client)
        with (
            patch.object(
                chat_routes,
                "_load_settings",
                return_value=_snapshot(None),
            ),
            patch.object(chat_routes, "start_research_process") as spawn,
        ):
            response = _send(authenticated_client, session_id)

        assert response.status_code == 200
        assert spawn.called

    def test_other_provider_ignores_endpoint_setting(
        self, authenticated_client
    ):
        """Pass-through guard: for any other provider the URL setting is
        not the LLM endpoint (chat only forwards it for
        openai_endpoint), so the gate must not interfere."""
        from local_deep_research.web.routers import chat as chat_routes

        session_id = _create_session(authenticated_client)
        snapshot = {
            "llm.provider": {"value": "ollama"},
            "llm.model": {"value": "llama3"},
            "llm.openai_endpoint.url": {
                "value": POISONED_ENDPOINTS[0]
            },  # irrelevant for ollama
        }
        with (
            patch.object(chat_routes, "_load_settings", return_value=snapshot),
            patch.object(chat_routes, "start_research_process") as spawn,
        ):
            response = _send(authenticated_client, session_id)

        assert response.status_code == 200
        assert spawn.called


class TestRetryAttemptCustomEndpointGate:
    def test_retry_with_poisoned_endpoint_returns_400_and_never_spawns(
        self, authenticated_client
    ):
        """The retry boundary applies the same pre-flight: the original
        attempt is only created here (its spawn is mocked); the retry is
        then refused at the boundary instead of deleting the attempt and
        spawning a doomed replacement."""
        from local_deep_research.web.routers import chat as chat_routes

        session_id = _create_session(authenticated_client)

        # Create the original attempt with a SAFE endpoint so the first
        # send succeeds (spawn mocked).
        with (
            patch.object(
                chat_routes,
                "_load_settings",
                return_value=_snapshot(SAFE_ENDPOINTS[0]),
            ),
            patch.object(chat_routes, "start_research_process"),
        ):
            first = _send(authenticated_client, session_id)
        assert first.status_code == 200
        research_id = json.loads(first.data)["research_id"]
        assert research_id is not None

        # Retry under a poisoned endpoint snapshot: must 400 at the
        # boundary, before any spawn of the replacement run. The slot
        # cap is short-circuited (the first run's spawn is mocked, so
        # its registration never clears) — admission is a different
        # seam with its own 409 contract suite.
        with (
            patch.object(
                chat_routes,
                "_load_settings",
                return_value=_snapshot(POISONED_ENDPOINTS[0]),
            ),
            patch.object(
                chat_routes,
                "_check_chat_research_slot_locked",
                return_value=None,
            ),
            patch.object(chat_routes, "start_research_process") as spawn,
        ):
            response = authenticated_client.post(
                f"/api/chat/sessions/{session_id}/attempts/{research_id}/retry",
                content_type="application/json",
            )

        assert response.status_code == 400, (
            f"retry with a poisoned endpoint must be refused with 400, "
            f"got {response.status_code}"
        )
        data = json.loads(response.data)
        assert data["success"] is False
        assert data["error"] == "Invalid custom endpoint URL"
        assert not spawn.called, (
            "no replacement worker may be spawned for a poisoned endpoint"
        )

    def test_retry_with_safe_endpoint_proceeds_to_spawn(
        self, authenticated_client
    ):
        """Pass-through guard: retry under a safe endpoint still spawns
        the replacement run."""
        from local_deep_research.web.routers import chat as chat_routes

        session_id = _create_session(authenticated_client)
        with (
            patch.object(
                chat_routes,
                "_load_settings",
                return_value=_snapshot(SAFE_ENDPOINTS[0]),
            ),
            patch.object(chat_routes, "start_research_process"),
        ):
            first = _send(authenticated_client, session_id)
        research_id = json.loads(first.data)["research_id"]

        with (
            patch.object(
                chat_routes,
                "_load_settings",
                return_value=_snapshot(SAFE_ENDPOINTS[0]),
            ),
            patch.object(
                chat_routes,
                "_check_chat_research_slot_locked",
                return_value=None,
            ),
            patch.object(chat_routes, "start_research_process") as spawn,
        ):
            response = authenticated_client.post(
                f"/api/chat/sessions/{session_id}/attempts/{research_id}/retry",
                content_type="application/json",
            )

        assert response.status_code == 200
        assert spawn.called