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.

24.9 KB
Raw Blame
"""The live news router must validate custom_endpoint and subscription_id.

Ported from ``tests/security/test_news_live_blueprint_ssrf.py`` on
``origin/main`` (#5603), which the FastAPI migration deleted. The module it
guards moved ``news/flask_api.py`` -> ``web/routers/news_flask_api.py`` and
the blueprint became an ``APIRouter``; the guards themselves survived, and
the merge that produced this branch had to *re-add* the third write path's
guard (``update_subscription_folder``) after an earlier state of the port
dropped it. So the guard has already been lost once here, silently, with no
test to say so.

At the time of writing nothing on this branch references
``_reject_custom_endpoint``, ``_is_valid_uuid`` or ``NO_SUBSCRIPTION_FILTER``:
deleting all three call sites from the write routes turns no test red.
``tests/security/test_llm_endpoint_link_local_hardening.py`` unit-tests the
shared ``is_safe_custom_llm_endpoint`` predicate, and
``tests/web/routers/test_start_research_ssrf.py`` pins it on a *different*
route — neither notices if the news routes stop calling it. That is precisely
the failure shape #5603 existed to prevent (a working guard on a copy nobody
routes to), so as on main the wiring is pinned by driving real HTTP through
the router the app actually mounts, not by inspecting source.

Deviations from the original, all forced by the runtime:
* ``_reject_custom_endpoint`` returns a ``JSONResponse`` (or ``None``) rather
  than Flask's ``(body, status)`` tuple, and needs no app context.
* Authentication is ``Depends(require_auth)``; the FastAPI-native way to say
  "this client is logged in" is ``dependency_overrides``, replacing main's
  ``session_transaction()`` poke.
* ``TestClient`` follows redirects by default where Flask's client did not,
  so every request below passes ``follow_redirects=False``.
"""

import uuid as _uuid
from types import SimpleNamespace
from unittest.mock import MagicMock, patch

import pytest
from fastapi import FastAPI
from starlette.testclient import TestClient

from local_deep_research.web.dependencies.auth import require_auth
from local_deep_research.web.routers import news_flask_api
from local_deep_research.web.routers.news_flask_api import (
    _is_valid_uuid,
    _reject_custom_endpoint,
    router as news_api_router,
)

# A syntactically valid subscription id, for cases where the UUID guard is
# not the thing under test.
A_UUID = "11111111-1111-4111-8111-111111111111"

# The canonical SSRF target: EC2 / GCP / Azure instance metadata.
METADATA_URL = "http://169.254.169.254/"


class TestCustomEndpointSSRF:
    @pytest.mark.parametrize(
        "endpoint",
        [
            "http://169.254.169.254/latest/meta-data/",  # cloud metadata
            "http://[fd00:ec2::254]/",  # IPv6 metadata
            "file:///etc/passwd",  # non-HTTP scheme
            "gopher://example.com/",
        ],
    )
    def test_internal_and_non_http_targets_are_rejected(self, endpoint):
        rejected = _reject_custom_endpoint(endpoint)

        assert rejected is not None, (
            f"{endpoint!r} was accepted as a custom LLM endpoint -- the "
            "server would fetch it, which is SSRF"
        )
        assert rejected.status_code == 400

    def test_absent_endpoint_is_allowed(self):
        """custom_endpoint is optional; omitting it must not 400."""
        assert _reject_custom_endpoint(None) is None

    @pytest.mark.parametrize(
        "endpoint",
        [
            "https://api.openai.com/v1",
            "http://127.0.0.1:11434",  # Ollama
            "http://192.168.1.10:8000",  # LM Studio / vLLM on the LAN
            "localhost:11434",  # scheme-less, as the provider normalises it
        ],
    )
    def test_local_llm_backends_stay_allowed(self, endpoint):
        """The guard must not break the feature it protects.

        Private IPs and localhost are allowed DELIBERATELY -- that is how
        people point this at Ollama / LM Studio / vLLM. The guard is a
        metadata / non-HTTP-scheme denylist, not a private-IP allowlist.
        """
        assert _reject_custom_endpoint(endpoint) is None

    @pytest.mark.parametrize("endpoint", [123, b"http://x", [], {}, 1.5])
    def test_non_string_endpoint_is_rejected_not_crashed(self, endpoint):
        """A JSON body can carry any type, so the guard must not assume str.

        Before this was handled, ``123`` raised ``AttributeError`` and
        ``b"..."`` a ``TypeError`` inside the validator (surfacing as a 500),
        while ``[]`` and ``{}`` were treated as "unset" and persisted.
        """
        rejected = _reject_custom_endpoint(endpoint)

        assert rejected is not None
        assert rejected.status_code == 400


class TestSubscriptionIdValidation:
    @pytest.mark.parametrize(
        "value",
        ["%", "_", "%%", "a%b", "' OR 1=1 --", "", "not-a-uuid", None, 123],
    )
    def test_non_uuid_values_are_rejected(self, value):
        assert _is_valid_uuid(value) is False

    def test_real_uuid_is_accepted(self):
        assert _is_valid_uuid(str(_uuid.uuid4())) is True


class TestMaskSensitiveUrlNeverRaises:
    """The masker is what makes logging an unexpected value safe, so it must
    not be the thing that raises on one."""

    @pytest.mark.parametrize("value", [12345, None, object(), b"bytes", []])
    def test_non_string_input_is_masked_not_raised(self, value):
        from local_deep_research.security.url_builder import mask_sensitive_url

        masked = mask_sensitive_url(value)

        assert isinstance(masked, str)
        assert "***" in masked

    def test_password_is_still_masked(self):
        from local_deep_research.security.url_builder import mask_sensitive_url

        masked = mask_sensitive_url("https://user:[email protected]/hook")

        assert "hunter2" not in masked


# ---------------------------------------------------------------------------
# The guards must be wired into the router the app actually mounts.
#
# These go over HTTP through the real routes, real dependencies and real
# guards; only the service layer below the guards is stubbed, so nothing
# reaches a network or a database. Deleting either guard's call -- the exact
# regression this branch already made once -- fails these.
# ---------------------------------------------------------------------------


@pytest.fixture
def news_app():
    """A FastAPI app with the live news router at its real mount point.

    ``require_auth`` is overridden rather than driven through a real login:
    the subject here is the SSRF/UUID guards, and a genuine login would drag
    in SQLCipher and the whole auth stack for no added signal.
    ``tests/security/test_unauthenticated_reachability_census.py`` is what
    proves these routes carry ``require_auth`` at all.
    """
    app = FastAPI()
    app.include_router(news_api_router)
    app.dependency_overrides[require_auth] = lambda: "testuser"
    return app


@pytest.fixture
def live(news_app):
    """Client over the live router with the service layer stubbed.

    A request that gets past the guards must SUCCEED, so that a non-400
    response proves the guard let the value through rather than that
    something else happened to fail.
    """
    client = TestClient(news_app, raise_server_exceptions=False)

    with (
        patch(
            "local_deep_research.web.routers.news_flask_api.get_user_db_session"
        ) as mock_db_session,
        patch(
            "local_deep_research.web.routers.news_flask_api.get_settings_manager"
        ) as mock_settings,
        patch("local_deep_research.web.routers.news_flask_api.api") as mock_api,
    ):
        mock_db_session.return_value.__enter__.return_value = MagicMock()
        mock_settings.return_value.get_setting.return_value = 20
        mock_api.get_news_feed.return_value = {"news_items": [], "total": 0}
        mock_api.create_subscription.return_value = {
            "id": A_UUID,
            "status": "active",
        }
        mock_api.update_subscription.return_value = {
            "status": "success",
            "subscription": {"id": A_UUID},
        }
        mock_api.get_subscription_history.return_value = {
            "subscription_id": A_UUID,
            "history": [],
        }
        yield SimpleNamespace(
            client=client, api=mock_api, db_session=mock_db_session
        )


class TestSubscribeRouteRejectsSSRF:
    """POST /news/api/subscribe -- the route the frontend really calls."""

    def test_metadata_endpoint_returns_400(self, live):
        response = live.client.post(
            "/news/api/subscribe",
            json={"query": "AI news", "custom_endpoint": METADATA_URL},
            follow_redirects=False,
        )

        assert response.status_code == 400, (
            f"POST /news/api/subscribe accepted {METADATA_URL!r} "
            f"(HTTP {response.status_code}) -- the server would fetch cloud "
            "metadata on the subscription's behalf"
        )
        live.api.create_subscription.assert_not_called()

    def test_ollama_endpoint_is_not_rejected(self, live):
        """The guard must not break pointing a subscription at a local LLM."""
        response = live.client.post(
            "/news/api/subscribe",
            json={
                "query": "AI news",
                "custom_endpoint": "http://localhost:11434/v1",
            },
            follow_redirects=False,
        )

        assert response.status_code != 400
        live.api.create_subscription.assert_called_once()

    def test_non_string_endpoint_returns_400_not_500(self, live):
        """A wrong-typed endpoint is a client error, not a server crash."""
        response = live.client.post(
            "/news/api/subscribe",
            json={"query": "AI news", "custom_endpoint": 123},
            follow_redirects=False,
        )

        assert response.status_code == 400
        live.api.create_subscription.assert_not_called()


class TestUpdateSubscriptionRouteRejectsSSRF:
    """PUT /news/api/subscriptions/<id> is the second write path."""

    def test_metadata_endpoint_returns_400(self, live):
        response = live.client.put(
            f"/news/api/subscriptions/{A_UUID}",
            json={"custom_endpoint": METADATA_URL},
            follow_redirects=False,
        )

        assert response.status_code == 400, (
            "PUT /news/api/subscriptions/<id> accepted a metadata URL -- an "
            "existing subscription can be repointed at it"
        )
        live.api.update_subscription.assert_not_called()

    def test_ollama_endpoint_is_not_rejected(self, live):
        response = live.client.put(
            f"/news/api/subscriptions/{A_UUID}",
            json={"custom_endpoint": "http://localhost:11434/v1"},
            follow_redirects=False,
        )

        assert response.status_code != 400
        live.api.update_subscription.assert_called_once()


class TestUpdateSubscriptionFolderRouteRejectsSSRF:
    """PUT /news/api/subscription/subscriptions/<id> is the third write path.

    Unlike the other two it persists through a ``setattr`` loop straight to a
    DB session rather than through the ``news.api`` module, so it is stubbed
    at ``get_user_db_session`` instead of the ``api`` mock. It is also the
    one the FastAPI port dropped and the merge had to restore: deleting its
    guard call leaves every other test in this file green.

    Its guard is no longer the SSRF check the other two write paths use --
    ``normalize_compact_subscription_update`` restricts this route to an
    explicit field allowlist (``folder``, ``folder_id``, ``notes``,
    ``refresh_interval_minutes``, ``is_active``, ``status``), so
    ``custom_endpoint`` is rejected as an unsupported field before
    ``_reject_custom_endpoint`` ever runs.
    """

    @staticmethod
    def _stub_subscription():
        return SimpleNamespace(
            id=A_UUID,
            name="test",
            status="active",
            folder_id=None,
            refresh_interval_minutes=60,
            next_refresh=None,
            last_refresh=None,
            custom_endpoint=None,
            updated_at=None,
        )

    def test_metadata_endpoint_returns_400(self, live):
        live.db_session.reset_mock()
        response = live.client.put(
            f"/news/api/subscription/subscriptions/{A_UUID}",
            json={"custom_endpoint": METADATA_URL},
            follow_redirects=False,
        )

        assert response.status_code == 400, (
            "PUT /news/api/subscription/subscriptions/<id> accepted a "
            f"metadata URL (HTTP {response.status_code}) -- custom_endpoint "
            "must be rejected on this route (now as an unsupported field, "
            "before it could ever reach an SSRF check)"
        )
        live.db_session.assert_not_called()

    def test_custom_endpoint_is_rejected_as_unsupported_field(self, live):
        """custom_endpoint is not on this route's allowlist at all.

        This used to send a *valid* local-LLM endpoint and assert the SSRF
        guard let it through, the way ``update_subscription`` still does.
        This route now enforces an explicit field allowlist (``folder``,
        ``folder_id``, ``notes``, ``refresh_interval_minutes``,
        ``is_active``, ``status``) via
        ``normalize_compact_subscription_update``, so even a valid endpoint
        never reaches the SSRF check -- it 400s as an unsupported field
        instead. That is a stronger guarantee than the old SSRF check: the
        field can never be written through this route, safe or not.
        """
        live.db_session.reset_mock()
        response = live.client.put(
            f"/news/api/subscription/subscriptions/{A_UUID}",
            json={"custom_endpoint": "http://localhost:11434/v1"},
            follow_redirects=False,
        )

        assert response.status_code == 400
        assert "custom_endpoint" in response.json()["error"]
        live.db_session.assert_not_called()

    def test_allowlisted_field_is_still_accepted(self, live):
        """The allowlist narrows the route, it does not break it.

        A field the allowlist does carry (``folder``) must still reach the
        DB session, so the rejection above is proven to be about
        ``custom_endpoint`` specifically and not the route 400ing on
        everything.
        """
        sub = self._stub_subscription()
        mock_session = MagicMock()
        mock_session.query.return_value.filter_by.return_value.first.return_value = sub
        live.db_session.reset_mock()
        live.db_session.return_value.__enter__.return_value = mock_session

        response = live.client.put(
            f"/news/api/subscription/subscriptions/{A_UUID}",
            json={"folder": "AI News"},
            follow_redirects=False,
        )

        assert response.status_code == 200, (
            f"PUT /news/api/subscription/subscriptions/<id> rejected an "
            f"allowlisted field (HTTP {response.status_code}) -- "
            f"{response.text[:200]}"
        )
        live.db_session.assert_called_once()


class TestUpdateFolderRouteRejectsMassAssignment:
    """PUT /news/api/subscription/folders/<id> -- the folder-update sibling
    of ``update_subscription_folder`` above.

    ``FolderManager.update_folder`` used to run a blind ``setattr`` loop
    gated only by ``hasattr(folder, key)`` on the live SQLAlchemy instance.
    That is true for real columns, but also for ORM internals
    (``_sa_instance_state``) and the bound ``to_dict`` method, so a request
    body naming either one corrupted the instance or broke the route's own
    ``folder.to_dict()`` call on the same request (500, not a clean 400).
    ``normalize_folder_update`` closes this the same way
    ``normalize_compact_subscription_update`` closed the subscription
    route's equivalent hole: an explicit field allowlist checked before the
    DB session is ever opened.
    """

    def test_to_dict_field_is_rejected_as_unsupported(self, live):
        live.db_session.reset_mock()
        response = live.client.put(
            "/news/api/subscription/folders/folder-1",
            json={"to_dict": "x"},
            follow_redirects=False,
        )

        assert response.status_code == 400, (
            "PUT /news/api/subscription/folders/<id> accepted a body "
            f"naming to_dict (HTTP {response.status_code}) -- this used to "
            "shadow the bound to_dict method on the instance and 500 the "
            "route's own folder.to_dict() call"
        )
        assert "to_dict" in response.json()["error"]
        live.db_session.assert_not_called()

    def test_sa_instance_state_field_is_rejected_as_unsupported(self, live):
        live.db_session.reset_mock()
        response = live.client.put(
            "/news/api/subscription/folders/folder-1",
            json={"_sa_instance_state": 1},
            follow_redirects=False,
        )

        assert response.status_code == 400, (
            "PUT /news/api/subscription/folders/<id> accepted a body "
            f"naming _sa_instance_state (HTTP {response.status_code}) -- "
            "this used to corrupt SQLAlchemy's ORM identity tracking for "
            "the instance"
        )
        assert "_sa_instance_state" in response.json()["error"]
        live.db_session.assert_not_called()

    def test_invalid_body_is_rejected_before_offload(self, live):
        """The route validates before dispatching to the DB thread.

        ``_impl`` re-validates too, so dropping the route-level check would
        still 400 -- but only after handing an invalid body to
        ``run_db_sync``. Pin the early rejection, as the compact
        subscription route's tests do.
        """
        with patch.object(news_flask_api, "run_db_sync") as offload:
            response = live.client.put(
                "/news/api/subscription/folders/folder-1",
                json={"to_dict": "x"},
                follow_redirects=False,
            )

        assert response.status_code == 400
        assert response.json() == {
            "error": "Unsupported folder update fields: to_dict"
        }
        offload.assert_not_called()

    def test_allowlisted_field_is_still_accepted(self, live):
        """The allowlist narrows the route, it does not break it.

        A field the allowlist does carry (``color``) must still reach the
        DB session, so the rejections above are proven to be about the
        specific unsupported fields and not the route 400ing on everything.
        """
        folder = SimpleNamespace(
            id="folder-1",
            name="Test Folder",
            description=None,
            color="#000000",
            icon=None,
            is_default=False,
            sort_order=0,
            created_at=None,
            updated_at=None,
            to_dict=lambda: {"id": "folder-1", "color": "#ffffff"},
        )
        mock_session = MagicMock()
        mock_session.get.return_value = folder
        live.db_session.reset_mock()
        live.db_session.return_value.__enter__.return_value = mock_session

        response = live.client.put(
            "/news/api/subscription/folders/folder-1",
            json={"color": "#ffffff"},
            follow_redirects=False,
        )

        assert response.status_code == 200, (
            f"PUT /news/api/subscription/folders/<id> rejected an "
            f"allowlisted field (HTTP {response.status_code}) -- "
            f"{response.text[:200]}"
        )
        live.db_session.assert_called_once()

    @pytest.mark.parametrize(
        "field,value",
        [
            ("name", 123),
            ("name", None),
            ("description", 123),
            ("color", 123),
            ("icon", 123),
            ("is_default", "not-a-bool"),
            ("is_default", 1),
            ("sort_order", "abc"),
            ("sort_order", True),
            ("sort_order", 1.5),
        ],
    )
    def test_wrong_typed_field_is_rejected_with_400(self, live, field, value):
        """Field-name allowlisting alone is not enough: a wrong-typed value
        for an allowlisted field used to reach ``FolderManager.update_folder``'s
        blind ``setattr``/``session.commit()`` unchecked -- an opaque 500,
        or a silently miscoerced value thanks to SQLite's dynamic column
        typing. ``normalize_folder_update`` must reject it with a clean 400
        naming the field before the DB session is ever opened, the same
        contract as the field-name checks above.

        ``is_default=1`` and ``sort_order=True`` cover a bool masquerading
        as an int/not-quite-bool -- ``type(x) is`` (not ``isinstance``)
        checks must not accidentally accept these.
        """
        live.db_session.reset_mock()
        response = live.client.put(
            "/news/api/subscription/folders/folder-1",
            json={field: value},
            follow_redirects=False,
        )

        assert response.status_code == 400, (
            f"PUT /news/api/subscription/folders/<id> accepted "
            f"{field}={value!r} (HTTP {response.status_code}) instead of "
            "rejecting the wrong type before the session was opened"
        )
        assert field in response.json()["error"]
        live.db_session.assert_not_called()


class TestFeedRouteValidatesSubscriptionId:
    """GET /news/api/feed takes subscription_id from the query string."""

    def test_non_uuid_returns_400(self, live):
        response = live.client.get(
            "/news/api/feed?subscription_id=not-a-uuid",
            follow_redirects=False,
        )

        assert response.status_code == 400, (
            "GET /news/api/feed accepted a non-UUID subscription_id "
            f"(HTTP {response.status_code}) -- the value reaches the query "
            "layer unvalidated"
        )
        live.api.get_news_feed.assert_not_called()

    def test_like_wildcard_returns_400(self, live):
        response = live.client.get(
            "/news/api/feed?subscription_id=%25", follow_redirects=False
        )

        assert response.status_code == 400
        live.api.get_news_feed.assert_not_called()

    def test_uuid_is_accepted(self, live):
        response = live.client.get(
            f"/news/api/feed?subscription_id={A_UUID}", follow_redirects=False
        )

        assert response.status_code == 200
        assert (
            live.api.get_news_feed.call_args.kwargs["subscription_id"] == A_UUID
        )

    def test_all_sentinel_is_accepted(self, live):
        """``news/api.py`` documents "all" as "no subscription filter".

        It is not a UUID, so the guard has to allow it explicitly or every
        external client using the documented sentinel starts getting 400s.
        """
        response = live.client.get(
            "/news/api/feed?subscription_id=all", follow_redirects=False
        )

        assert response.status_code == 200
        assert (
            live.api.get_news_feed.call_args.kwargs["subscription_id"] == "all"
        )

    def test_omitted_subscription_id_is_accepted(self, live):
        response = live.client.get("/news/api/feed", follow_redirects=False)

        assert response.status_code == 200
        assert (
            live.api.get_news_feed.call_args.kwargs["subscription_id"] is None
        )


class TestHistoryRouteValidatesSubscriptionId:
    """GET /news/api/subscriptions/<id>/history takes its id from the URL
    path, but that id still reaches a LIKE rather than an equality filter.

    ``api.get_subscription_history`` interpolates it into a
    ``%"subscription_id": "<id>"%`` LIKE pattern. The value is
    ``escape_like``-wrapped, so this was never exploitable -- but it is the
    same shape as the feed query parameter and gets the same guard.
    """

    def test_non_uuid_returns_400(self, live):
        response = live.client.get(
            "/news/api/subscriptions/not-a-uuid/history",
            follow_redirects=False,
        )

        assert response.status_code == 400, (
            "GET /news/api/subscriptions/<id>/history accepted a non-UUID "
            f"path id (HTTP {response.status_code}) -- the value reaches a "
            "LIKE pattern unvalidated"
        )
        live.api.get_subscription_history.assert_not_called()

    def test_like_wildcard_returns_400(self, live):
        response = live.client.get(
            "/news/api/subscriptions/%25/history", follow_redirects=False
        )

        assert response.status_code == 400
        live.api.get_subscription_history.assert_not_called()

    def test_all_sentinel_is_not_a_history_target(self, live):
        """``all`` is the feed's "no filter" sentinel, not a subscription.

        The history route resolves one subscription, so it takes no sentinel
        and the plain UUID check applies.
        """
        response = live.client.get(
            "/news/api/subscriptions/all/history", follow_redirects=False
        )

        assert response.status_code == 400
        live.api.get_subscription_history.assert_not_called()

    def test_uuid_is_accepted(self, live):
        response = live.client.get(
            f"/news/api/subscriptions/{A_UUID}/history",
            follow_redirects=False,
        )

        assert response.status_code == 200
        assert live.api.get_subscription_history.call_args.args[0] == A_UUID