diff --git a/headroom/proxy/handlers/openai.py b/headroom/proxy/handlers/openai.py index 831bddb4a..38a0c5fdf 100644 --- a/headroom/proxy/handlers/openai.py +++ b/headroom/proxy/handlers/openai.py @@ -640,7 +640,11 @@ def _allowed_ws_origins_from_env() -> list[str] | None: raw = os.environ.get(_CORS_ALLOWED_ORIGINS_ENV) if raw is None or not raw.strip(): return None - return [origin.strip() for origin in raw.split(",") if origin.strip()] + # "*" is a CORS answer (which pages may read a response). It never waives + # the Origin check, which decides whether the proxy acts on the request at + # all, so it is dropped here; a list left empty means the loopback default. + origins = [origin.strip() for origin in raw.split(",") if origin.strip()] + return [origin for origin in origins if origin != "*"] or None def _is_loopback_ws_origin(origin: str) -> bool: @@ -657,7 +661,7 @@ def _is_allowed_websocket_origin(headers: dict[str, str]) -> bool: Native clients commonly omit Origin, so absence is allowed. When Origin is present, default to loopback-only and allow explicit configured origins via - HEADROOM_WS_ORIGINS or HEADROOM_CORS_ORIGINS. + HEADROOM_WS_ORIGINS or HEADROOM_CORS_ORIGINS. A ``*`` entry matches nothing. """ origin = _header_get(headers, "origin") if not origin: @@ -666,8 +670,6 @@ def _is_allowed_websocket_origin(headers: dict[str, str]) -> bool: allowed_origins = _allowed_ws_origins_from_env() if allowed_origins is None: return _is_loopback_ws_origin(origin) - if "*" in allowed_origins: - return True normalized_origin = _normalize_origin(origin) if normalized_origin is None: diff --git a/tests/test_openai_codex_routing.py b/tests/test_openai_codex_routing.py index bd919d252..c36d0bccf 100644 --- a/tests/test_openai_codex_routing.py +++ b/tests/test_openai_codex_routing.py @@ -953,6 +953,46 @@ def test_websocket_origin_policy_can_be_pinned_with_env(monkeypatch): assert _is_allowed_websocket_origin({"origin": "http://localhost:3000"}) is False +@pytest.mark.parametrize( + ("ws_origins", "cors_origins"), + [(None, "*"), ("*", None), (None, "*, https://dash.example.com")], +) +def test_websocket_origin_policy_ignores_wildcard(monkeypatch, ws_origins, cors_origins): + for name, value in ( + ("HEADROOM_WS_ORIGINS", ws_origins), + ("HEADROOM_CORS_ORIGINS", cors_origins), + ): + if value is None: + monkeypatch.delenv(name, raising=False) + else: + monkeypatch.setenv(name, value) + + assert _is_allowed_websocket_origin({"origin": "https://remote.example"}) is False + # A bare "*" leaves the loopback default in place. + if "dash.example.com" not in (cors_origins or ""): + assert _is_allowed_websocket_origin({"origin": "http://127.0.0.1:8787"}) is True + else: + assert _is_allowed_websocket_origin({"origin": "https://dash.example.com"}) is True + + +def test_handle_openai_responses_ws_closes_foreign_origin_under_wildcard_cors(monkeypatch): + monkeypatch.delenv("HEADROOM_WS_ORIGINS", raising=False) + monkeypatch.setenv("HEADROOM_CORS_ORIGINS", "*") + handler = _DummyOpenAIHandler() + websocket = _DummyWebSocket({"origin": "https://remote.example"}) + + with patch.dict(sys.modules, {"websockets": MagicMock()}): + with patch( + "headroom.proxy.handlers.openai._resolve_codex_routing_headers", + side_effect=AssertionError("routing should not run"), + ): + anyio.run(handler.handle_openai_responses_ws, websocket) + + assert websocket.closed is True + assert websocket.close_code == 1008 + assert websocket.close_reason == "origin not allowed" + + def test_handle_openai_responses_ws_resolves_codex_routing_headers(): class SentinelError(RuntimeError): pass diff --git a/tests/test_proxy_cors.py b/tests/test_proxy_cors.py index 384fc7876..8fa0cb80c 100644 --- a/tests/test_proxy_cors.py +++ b/tests/test_proxy_cors.py @@ -94,8 +94,20 @@ def test_explicit_allowlist_override(monkeypatch: pytest.MonkeyPatch) -> None: def test_wildcard_optback(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setenv("HEADROOM_CORS_ORIGINS", "*") - resp = _preflight(_make_client(), "http://evil.com") - assert resp.status_code == 200 - assert resp.headers.get("access-control-allow-origin") == "*" + monkeypatch.delenv("HEADROOM_WS_ORIGINS", raising=False) + client = _make_client() + + # "*" widens which pages may read a response; it does not let a foreign + # page drive the proxy, so the security gate refuses it before CORS runs. + foreign = _preflight(client, "http://evil.com") + assert foreign.status_code == 403 + assert foreign.headers.get("access-control-allow-origin") is None + refused_post = client.post("/v1/messages", content="{}", headers={"Origin": "http://evil.com"}) + assert refused_post.status_code == 403 + + # A page the proxy trusts still gets the wildcard answer. + local = _preflight(client, "http://127.0.0.1:8787") + assert local.status_code == 200 + assert local.headers.get("access-control-allow-origin") == "*" # Wildcard opt-back must not silently re-enable credentialed reads. - assert resp.headers.get("access-control-allow-credentials") != "true" + assert local.headers.get("access-control-allow-credentials") != "true"