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.

13.3 KB
Raw Blame
"""SSRF-guard tests for ``POST /api/start_research`` (FastAPI port).

Ports main's Flask-era ``TestStartResearchCustomEndpointSSRF`` suite
(``tests/web/routes/test_research_routes_start_research_coverage.py``,
visible via ``git show origin/main:...``) to the FastAPI route in
``web/routers/research.py``.

The user-supplied ``custom_endpoint`` is later handed to the OpenAI
client (httpx) as ``base_url`` with no SafeSession wrapping, so the
route layer is the only place to reject cloud-metadata / link-local /
non-HTTP targets before any research thread spawns. Private IPs and
localhost must pass because local LLM backends (Ollama / LM Studio /
vLLM) live there.

Contract under test (main parity):
* malicious ``custom_endpoint`` + ``model_provider=openai_endpoint``
  -> 400 ``{"status": "error", ...}``, and the research thread is NEVER
  spawned — including when the malicious URL comes from the saved
  ``llm.openai_endpoint.url`` setting instead of the request body;
* ``custom_endpoint`` is dropped (not validated, not forwarded) for
  every other provider;
* safe endpoints (localhost / private IPs) are accepted and forwarded
  to the spawned research thread.

Auth is satisfied by overriding the ``require_auth`` dependency (same
pattern as test_metrics_star_reviews.py); the per-user DB, settings
manager, and thread spawn are mocked at their import seams so no real
research starts and no encrypted DB is created.
"""

from contextlib import ExitStack, contextmanager
from unittest.mock import MagicMock, patch

import pytest
from fastapi.testclient import TestClient

ROUTER = "local_deep_research.web.routers.research"

# Local (inside-function) imports — patch at their source modules.
_SETTINGS_MANAGER = "local_deep_research.settings.SettingsManager"
_SAVE_STRATEGY = (
    "local_deep_research.web.services.research_service.save_research_strategy"
)
_RECLAIM_STALE = (
    "local_deep_research.web.routes.globals.reclaim_stale_user_active_research"
)

START_URL = "/api/start_research"

AWS_METADATA = (
    "http://169.254.169.254/latest/meta-data/iam/security-credentials/"
)
GCP_METADATA = "http://metadata.google.internal/computeMetadata/v1/"


# ---------------------------------------------------------------------------
# Fixtures / helpers
# ---------------------------------------------------------------------------


@pytest.fixture
def client():
    """FastAPI test client authenticated as ``testuser``.

    Overrides the ``require_auth`` dependency instead of doing a real
    register/login because every DB touch is patched out — a real auth
    flow would create a real encrypted DB this suite doesn't need.
    """
    from local_deep_research.web.fastapi_app import app
    from local_deep_research.web.dependencies.auth import require_auth

    app.dependency_overrides[require_auth] = lambda: "testuser"
    try:
        yield TestClient(app, raise_server_exceptions=False)
    finally:
        app.dependency_overrides.pop(require_auth, None)


def _csrf_headers(client):
    """CSRF header for state-changing requests (middleware runs pre-route)."""
    token = client.get("/auth/csrf-token").json()["csrf_token"]
    return {"X-CSRFToken": token}


def _make_settings_manager(provider="ollama", model="gpt-4", **extra):
    """SettingsManager mock backed by a lookup table (Flask-suite idiom)."""
    sm = MagicMock()
    lookup = {
        "llm.provider": provider,
        "llm.model": model,
        "llm.ollama.url": "http://localhost:11434",
        "llm.openai_endpoint.url": extra.get("openai_url", None),
        "search.tool": "searxng",
        "search.iterations": 5,
        "search.questions_per_iteration": 5,
        "search.search_strategy": "source-based",
        "app.max_concurrent_researches": 3,
    }
    sm.get_setting.side_effect = lambda key, default=None: lookup.get(
        key, default
    )
    sm.get_all_settings.return_value = {"setting_key": "setting_val"}
    # NOT a dict -> _precheck_engine_policy skips (documented behavior for
    # test doubles); the egress-policy precheck has its own suite.
    sm.get_settings_snapshot.return_value = MagicMock()
    return sm


def _mock_db_session():
    """MagicMock standing in for the per-user SQLAlchemy session."""
    ms = MagicMock()
    chain = ms.query.return_value.filter_by.return_value
    chain.count.return_value = 0  # active researches -> never queue
    chain.first.return_value = MagicMock()
    chain.scalar.return_value = 0
    return ms


@contextmanager
def _start_research_mocks(sm):
    """Patch every seam of _start_research_sync except the SSRF guard.

    The SSRF validators (``validate_url`` / ``is_safe_custom_llm_endpoint``)
    run REAL — that is the behavior under test. Yields the mock for
    ``start_research_process`` so tests can assert whether a research
    thread would have spawned and with which ``custom_endpoint``.
    """
    ms = _mock_db_session()

    @contextmanager
    def _session_ctx(*args, **kwargs):
        yield ms

    fake_thread = MagicMock()
    fake_thread.ident = 99

    with ExitStack() as stack:
        stack.enter_context(
            patch(f"{ROUTER}.get_user_db_session", _session_ctx)
        )
        stack.enter_context(patch(_SETTINGS_MANAGER, return_value=sm))
        spawn = stack.enter_context(
            patch(f"{ROUTER}.start_research_process", return_value=fake_thread)
        )
        stack.enter_context(
            patch(f"{ROUTER}.resolve_user_password", return_value=("pw", False))
        )
        stack.enter_context(patch(f"{ROUTER}.log_settings"))
        stack.enter_context(patch(f"{ROUTER}.ResearchHistory"))
        stack.enter_context(patch(f"{ROUTER}.UserActiveResearch"))
        stack.enter_context(patch(_SAVE_STRATEGY))
        stack.enter_context(patch(_RECLAIM_STALE, return_value=False))
        yield spawn


def _post_start(client, payload):
    return client.post(START_URL, json=payload, headers=_csrf_headers(client))


# ---------------------------------------------------------------------------
# Malicious custom_endpoint -> 400, no research thread spawned
# ---------------------------------------------------------------------------


class TestMaliciousCustomEndpointRejected:
    @pytest.mark.parametrize(
        "endpoint",
        [
            pytest.param(AWS_METADATA, id="aws-metadata-ip"),
            pytest.param(GCP_METADATA, id="gcp-metadata-hostname"),
            pytest.param("file:///etc/passwd", id="file-scheme"),
            pytest.param("not-a-url", id="garbage-url"),
        ],
    )
    def test_rejected_with_400_and_no_spawn(self, client, endpoint):
        sm = _make_settings_manager()
        with _start_research_mocks(sm) as spawn:
            resp = _post_start(
                client,
                {
                    "query": "anything",
                    "model": "gpt-4",
                    "model_provider": "openai_endpoint",
                    "custom_endpoint": endpoint,
                },
            )

        assert resp.status_code == 400, resp.text
        data = resp.json()
        assert data["status"] == "error"
        spawn.assert_not_called()

    def test_uppercase_provider_still_validated(self, client):
        """Main fix #3348: the provider is normalized to lowercase before
        the SSRF gate, so an UPPERCASE provider string must not sneak a
        metadata endpoint past the ``== "openai_endpoint"`` comparison."""
        sm = _make_settings_manager()
        with _start_research_mocks(sm) as spawn:
            resp = _post_start(
                client,
                {
                    "query": "anything",
                    "model": "gpt-4",
                    "model_provider": "OPENAI_ENDPOINT",
                    "custom_endpoint": AWS_METADATA,
                },
            )

        assert resp.status_code == 400, resp.text
        spawn.assert_not_called()

    def test_malicious_endpoint_from_settings_rejected(self, client):
        """The guard must also validate an endpoint resolved from the
        saved ``llm.openai_endpoint.url`` setting — a poisoned settings
        DB (or an earlier unvalidated save) must not become an SSRF
        vector just because the request body omitted the field."""
        sm = _make_settings_manager(openai_url=AWS_METADATA)
        with _start_research_mocks(sm) as spawn:
            resp = _post_start(
                client,
                {
                    "query": "anything",
                    "model": "gpt-4",
                    "model_provider": "openai_endpoint",
                },
            )

        assert resp.status_code == 400, resp.text
        spawn.assert_not_called()

    def test_missing_endpoint_for_openai_endpoint_provider_400(self, client):
        """No endpoint in the request AND none configured -> clean 400
        (required-field check), not a spawn with a None base_url."""
        sm = _make_settings_manager(openai_url=None)
        with _start_research_mocks(sm) as spawn:
            resp = _post_start(
                client,
                {
                    "query": "anything",
                    "model": "gpt-4",
                    "model_provider": "openai_endpoint",
                },
            )

        assert resp.status_code == 400, resp.text
        data = resp.json()
        assert data["status"] == "error"
        assert "endpoint" in data["message"].lower()
        spawn.assert_not_called()


# ---------------------------------------------------------------------------
# Safe endpoints accepted (local LLM backends must keep working)
# ---------------------------------------------------------------------------


class TestSafeCustomEndpointAccepted:
    @pytest.mark.parametrize(
        "endpoint",
        [
            pytest.param("http://localhost:11434/v1", id="localhost"),
            pytest.param("http://192.168.1.50:8000/v1", id="private-ip"),
        ],
    )
    def test_accepted_and_forwarded_to_spawn(self, client, endpoint):
        sm = _make_settings_manager()
        with _start_research_mocks(sm) as spawn:
            resp = _post_start(
                client,
                {
                    "query": "anything",
                    "model": "gpt-4",
                    "model_provider": "openai_endpoint",
                    "custom_endpoint": endpoint,
                },
            )

        assert resp.status_code == 200, resp.text
        data = resp.json()
        assert data["status"] == "success"
        assert data["research_id"]
        spawn.assert_called_once()
        assert spawn.call_args.kwargs["custom_endpoint"] == endpoint

    def test_schemeless_local_endpoint_accepted(self, client):
        """Regression: the inline guard must normalize before validating.

        A bare ``localhost:11434`` is what the OpenAI-compatible provider
        itself accepts (see is_safe_custom_llm_endpoint), so a local Ollama
        / LM Studio backend configured without a scheme must not be
        rejected at the request boundary.
        """
        sm = _make_settings_manager()
        with _start_research_mocks(sm) as spawn:
            resp = _post_start(
                client,
                {
                    "query": "anything",
                    "model": "gpt-4",
                    "model_provider": "openai_endpoint",
                    "custom_endpoint": "localhost:11434",
                },
            )

        assert resp.status_code == 200, resp.text
        spawn.assert_called_once()


# ---------------------------------------------------------------------------
# custom_endpoint is dropped for non-openai_endpoint providers
# ---------------------------------------------------------------------------


class TestCustomEndpointIgnoredForOtherProviders:
    def test_unrelated_endpoint_dropped_for_lmstudio(self, client):
        """A stale/malicious custom_endpoint must not block (or reach) an
        LM Studio run: the run succeeds, the endpoint validator is never
        invoked, and the spawned thread receives custom_endpoint=None."""
        sm = _make_settings_manager(provider="lmstudio", model="local-model")
        with (
            _start_research_mocks(sm) as spawn,
            patch(f"{ROUTER}.is_safe_custom_llm_endpoint") as validator,
        ):
            resp = _post_start(
                client,
                {
                    "query": "anything",
                    "model": "local-model",
                    "model_provider": "LMSTUDIO",
                    "custom_endpoint": AWS_METADATA,
                },
            )

        assert resp.status_code == 200, resp.text
        assert resp.json()["status"] == "success"
        validator.assert_not_called()
        spawn.assert_called_once()
        assert spawn.call_args.kwargs["custom_endpoint"] is None

    def test_ollama_run_unaffected_by_malicious_endpoint(self, client):
        """Same guarantee for the default provider: the metadata URL is
        dropped at parameter extraction, never validated, never spawned."""
        sm = _make_settings_manager(provider="ollama", model="llama3")
        with _start_research_mocks(sm) as spawn:
            resp = _post_start(
                client,
                {
                    "query": "anything",
                    "model": "llama3",
                    "model_provider": "ollama",
                    "custom_endpoint": "file:///etc/passwd",
                },
            )

        assert resp.status_code == 200, resp.text
        spawn.assert_called_once()
        assert spawn.call_args.kwargs["custom_endpoint"] is None