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.

8.5 KB
Raw Blame
"""Contracts for the WebSocket origin allowlist resolution (never ``[]``).

engine.io's origin check is ``if self.cors_allowed_origins != []``: an
empty **list** disables origin validation entirely, while ``None``
derives the same-origin allowlist from Host/X-Forwarded-Proto. The
module-level resolution documents this as load-bearing — but nothing
pinned the semantics:

- unset env must resolve to ``None`` (never ``[]``),
- an empty string must resolve the same way,
- ``*`` must require the explicit env opt-in, including when it appears as
  one entry inside a comma-separated list rather than alone,
- whitespace-only input must resolve to exactly ``None``, not merely to
  something that happens not to be ``[]``.

The comma-edge (``","``) previously resolved to ``["", ""]`` — a list
of empty-string origins that is neither the documented ``[]`` footgun
nor a sane allowlist (engine.io rejects every handshake carrying an
Origin header against it); it now resolves to ``None``, i.e. the
documented same-origin default. This is a deliberate loosening to that
default, not a fail-closed outcome.

The import-time value never being the validation-disabling ``[]`` is
pinned in ``tests/security/test_socket_ownership_edges_fastapi.py``
(``TestLiveServerOriginPolicy``), not re-litigated here.

The startup-ordering suite pins the resolution's module-scope freeze;
these pin what it actually resolves.
"""

from __future__ import annotations

import local_deep_research.web.services.socketio_asgi as prod_module


class TestOriginResolutionFailsClosed:
    def test_unset_env_resolves_to_none(self):
        assert prod_module._resolve_socketio_cors(None) is None, (
            "unset env must resolve to same-origin (None), never [] "
            "(engine.io skips origin validation for [])"
        )

    def test_empty_string_resolves_to_none(self):
        assert prod_module._resolve_socketio_cors("") is None

    def test_comma_edge_resolves_to_none(self):
        """',' splits into empty-string origins — resolve to the same-origin
        default (None), not a junk allowlist."""
        assert prod_module._resolve_socketio_cors(",") is None
        assert prod_module._resolve_socketio_cors(" , ") is None

    def test_star_requires_explicit_env(self):
        assert prod_module._resolve_socketio_cors("*") == "*"

    def test_star_within_a_list_is_the_explicit_allow_all_opt_in(self):
        """engine.io has no "allow-all plus named origins" mode: a list
        containing ``*`` already accepts every Origin, so it must resolve
        the same as a bare ``*`` rather than as a list that behaves as
        allow-all while the startup log claims a finite allowlist."""
        assert prod_module._resolve_socketio_cors("https://a,*") == "*"
        assert prod_module._resolve_socketio_cors("*, https://a") == "*"

    def test_origin_list_is_split_stripped_and_empties_dropped(self):
        assert prod_module._resolve_socketio_cors(
            "https://a.example, https://b.example"
        ) == ["https://a.example", "https://b.example"]
        assert prod_module._resolve_socketio_cors(
            " , https://x.example , "
        ) == ["https://x.example"]

    def test_resolution_never_returns_an_empty_list(self):
        for hostile in (None, "", " ", ",", " , , "):
            result = prod_module._resolve_socketio_cors(hostile)
            assert result is None or (
                isinstance(result, (str, list)) and result != []
            ), f"{hostile!r} resolved to the validation-disabling {result!r}"

    def test_whitespace_only_input_resolves_to_none_not_merely_non_empty(self):
        """Stronger than ``!= []``: a mutant that returned ``"*"`` (or any
        other truthy non-list value) for whitespace-only input would still
        satisfy ``result != []`` above, so this pins the exact value --
        whitespace-only input carries no origins and must resolve to
        the same-origin default, never a permissive fallback."""
        for whitespace_only in (" ", "\t", "\xa0", " , , "):
            result = prod_module._resolve_socketio_cors(whitespace_only)
            assert result is None, (
                f"{whitespace_only!r} resolved to {result!r}, not the "
                "same-origin default (None)"
            )


class TestAllowAllPolicyIsLoggedVisibly:
    """The allow-all posture must be visible at the default INFO sink."""

    @staticmethod
    def _capture(cors, env_value):
        from loguru import logger

        records = []
        # local_deep_research/__init__.py disables the package namespace;
        # re-enable it so this capture works when run on its own, not only
        # after some other test happened to enable it.
        logger.enable("local_deep_research")
        sink_id = logger.add(
            lambda m: records.append(
                (m.record["level"].name, m.record["message"])
            ),
            level="INFO",
        )
        try:
            prod_module._log_socketio_cors_policy(cors, env_value)
        finally:
            logger.remove(sink_id)
            logger.disable("local_deep_research")
        return records

    def test_mixed_star_and_origin_logs_warning(self):
        env = "https://a.example,*"
        records = self._capture(prod_module._resolve_socketio_cors(env), env)
        assert len(records) == 1
        level, message = records[0]
        assert level == "WARNING"
        assert "all origins allowed" in message
        assert "https://a.example" in message

    def test_bare_star_logs_info_not_hidden(self):
        records = self._capture("*", "*")
        assert [lvl for lvl, _ in records] == ["INFO"]
        assert "all origins allowed" in records[0][1]


class TestPolicyLogRunsAfterLoggingIsConfigured:
    """The policy log must be emitted from the lifespan startup, not at
    import.

    ``socketio_asgi`` is imported at the top of ``utilities/log_utils.py``,
    i.e. while ``logger.disable("local_deep_research")`` from the package
    ``__init__`` is still in effect and before ``config_logger()``
    re-enables it, so a module-scope log call is silently dropped in
    production. Source-level pins: loading the FastAPI app here would pull
    the whole application.
    """

    @staticmethod
    def _module_ast(module):
        import ast
        import inspect

        return ast.parse(inspect.getsource(module))

    @staticmethod
    def _called_names(node):
        import ast

        names = set()
        for sub in ast.walk(node):
            if isinstance(sub, ast.Call):
                func = sub.func
                if isinstance(func, ast.Name):
                    names.add(func.id)
                elif isinstance(func, ast.Attribute):
                    names.add(func.attr)
        return names

    def test_policy_is_not_logged_at_module_scope(self):
        import ast

        tree = self._module_ast(prod_module)
        module_scope_calls = set()
        for stmt in tree.body:
            if isinstance(
                stmt, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)
            ):
                continue
            module_scope_calls |= self._called_names(stmt)
        assert not module_scope_calls & {
            "_log_socketio_cors_policy",
            "log_socketio_cors_policy",
        }, (
            "the WebSocket origin policy is logged at import time, while "
            "package logging is still disabled -- the line is lost"
        )

    def test_lifespan_startup_logs_the_policy(self):
        import ast
        from pathlib import Path

        fastapi_app_path = (
            Path(prod_module.__file__).resolve().parents[1] / "fastapi_app.py"
        )
        tree = ast.parse(fastapi_app_path.read_text(encoding="utf-8"))
        lifespans = [
            node
            for node in tree.body
            if isinstance(node, ast.AsyncFunctionDef)
            and node.name == "lifespan"
        ]
        assert len(lifespans) == 1, "premise: fastapi_app defines lifespan()"
        assert "log_socketio_cors_policy" in self._called_names(lifespans[0]), (
            "the lifespan startup no longer logs the WebSocket origin policy"
        )

    def test_public_entry_point_logs_the_resolved_policy(self):
        from loguru import logger

        records = []
        logger.enable("local_deep_research")
        sink_id = logger.add(
            lambda m: records.append(m.record["message"]), level="INFO"
        )
        try:
            prod_module.log_socketio_cors_policy()
        finally:
            logger.remove(sink_id)
            logger.disable("local_deep_research")
        assert len(records) == 1
        assert records[0].startswith("Socket.IO CORS:")