Graph-Native Infrastructure for Context and Accountable AI Systems
Python agent-memoryaiai-governanceai-infrastructureartificial-intelligencecontext-engineeringcontext-graphsdata-engineeringdecision-intelligencedeveloper-toolsexplainable-aigenerative-aigraph-ragknowledge-graphllmontologyprovenancepythonreasoningsemantic-search
security: validate WebSocket Origin against the CORS allowlist (GHSA-4643) (#917)
Mohd Kaiff29c4310a11c
diff --git a/semantica/explorer/app.py b/semantica/explorer/app.pyindex 68bab729..bd9ab042 100644--- a/semantica/explorer/app.py+++ b/semantica/explorer/app.py@@ -193,6 +193,23 @@ def create_app(@app.websocket("/ws/graph-updates")async def websocket_endpoint(websocket: WebSocket):+ # CORSMiddleware doesn't cover WebSocket handshakes (Starlette's+ # CORS support only wraps HTTP), so under SEMANTICA_ALLOW_ANONYMOUS+ # the key check below accepts any origin — loopback binding isn't a+ # boundary against a browser, since any page the operator has open+ # can still reach ws://localhost:.../ws/graph-updates directly.+ # Reject a foreign Origin explicitly here, against the same+ # allowlist CORSMiddleware already enforces for HTTP+ # (GHSA-4643-wpgq-w329). Browsers always send Origin on a+ # cross-origin WebSocket handshake; native/CLI clients omit it+ # entirely, so a missing Origin is allowed through — the browser is+ # the only threat this check is closing.+ origin = websocket.headers.get("origin")+ allowed_origins = app.state.explorer_settings["allowed_origins"]+ if origin is not None and origin not in allowed_origins:+ await websocket.close(code=4403) # forbidden+ return+# Browsers can't set custom headers on a WebSocket handshake, so# accept the key via header (non-browser clients) or query param# (browser clients), same SEMANTICA_API_KEY the REST routes check.diff --git a/tests/explorer/test_explorer_auth.py b/tests/explorer/test_explorer_auth.pyindex 4b7b9c6f..e29461d2 100644--- a/tests/explorer/test_explorer_auth.py+++ b/tests/explorer/test_explorer_auth.py@@ -150,3 +150,62 @@ def test_websocket_accepts_connection_with_header_key(client, monkeypatch):) as websocket:ack = websocket.receive_json()assert ack["event"] == "connection_ack"+++# ---------------------------------------------------------------------------+# WebSocket Origin validation (GHSA-4643-wpgq-w329): CORSMiddleware doesn't+# cover WebSocket handshakes at all, so under SEMANTICA_ALLOW_ANONYMOUS the+# key check alone accepted a handshake from any origin — loopback binding is+# not a boundary against a browser, since any page the operator has open can+# still reach ws://localhost:.../ws/graph-updates. These pin the fix: a+# hostile Origin is refused even in anonymous mode (and even with a correct+# key), a same-origin/allowlisted Origin still works, and a missing Origin+# (native/CLI clients, which never set the header) is still allowed through.+# ---------------------------------------------------------------------------++def test_websocket_rejects_hostile_origin_under_anonymous_mode(client, monkeypatch):+ monkeypatch.setenv("SEMANTICA_ALLOW_ANONYMOUS", "true")+ monkeypatch.delenv("SEMANTICA_API_KEY", raising=False)++ with pytest.raises(Exception):+ with client.websocket_connect(+ "/ws/graph-updates", headers={"Origin": "https://evil.example"}+ ):+ pass+++def test_websocket_rejects_hostile_origin_even_with_correct_key(client, monkeypatch):+ """Defense in depth: Origin is checked before the API key, so a hostile+ page that somehow obtained a valid key still can't hijack the socket."""+ monkeypatch.delenv("SEMANTICA_ALLOW_ANONYMOUS", raising=False)+ monkeypatch.setenv("SEMANTICA_API_KEY", "correct-key")++ with pytest.raises(Exception):+ with client.websocket_connect(+ "/ws/graph-updates",+ headers={"Origin": "https://evil.example", "X-API-Key": "correct-key"},+ ):+ pass+++def test_websocket_accepts_allowlisted_origin_under_anonymous_mode(client, monkeypatch):+ monkeypatch.setenv("SEMANTICA_ALLOW_ANONYMOUS", "true")+ monkeypatch.delenv("SEMANTICA_API_KEY", raising=False)++ with client.websocket_connect(+ "/ws/graph-updates", headers={"Origin": "http://localhost:5173"}+ ) as websocket:+ ack = websocket.receive_json()+ assert ack["event"] == "connection_ack"+++def test_websocket_accepts_missing_origin_under_anonymous_mode(client, monkeypatch):+ """Native/CLI clients never send an Origin header — only browsers do —+ so a missing Origin must still be allowed through; the browser is the+ only threat this check closes."""+ monkeypatch.setenv("SEMANTICA_ALLOW_ANONYMOUS", "true")+ monkeypatch.delenv("SEMANTICA_API_KEY", raising=False)++ with client.websocket_connect("/ws/graph-updates") as websocket:+ ack = websocket.receive_json()+ assert ack["event"] == "connection_ack"