Repository navigation
fix(server): enforce CORS origin policy on /v1/realtime upgrade - #2047
Merged
Merged
Conversation
inureyes
added a commit
that referenced
this pull request
Sep 29, 2026
Browsers do not apply CORS to WebSocket upgrades, so a page on any site could open /v1/realtime, take the single session slot, and read every event even when --cors-origins or --allowed-origins restricted browser access. CorsPolicy::permits_websocket_origin checks a present Origin against the configured rule (any under *, localhost hosts under localhost, byte equality under a literal, membership under --allowed-origins) and accepts an absent Origin, which only non-browser clients send. realtime_router now takes the policy, and the handler answers 403 with an invalid_request_error body before the upgrade, so no reservation is taken. The origin check runs before upgrade validation; the API-key layer stays outside, so failing both checks is still 401. The check lives in the route, so router-mode sub-apps built without the CORS layer enforce it too. Tests: websocket_origin_matrix covers every policy against absent, allowed, disallowed, null and non-UTF-8 origins; an app-level test covers create_app and create_app_without_cors including the 401 ordering; tests/realtime_ws.rs gains start_server_with_policy and end-to-end handshake tests. Closes #2042
inureyes
force-pushed
the
fix/issue-2042-realtime-ws-origin
branch
from
September 29, 2026 15:29
987fa59 to
2fbdee6
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
/v1/realtimenow checks the upgrade'sOriginagainst the--cors-origins/--allowed-originspolicy (CorsPolicy::permits_websocket_origin). A disallowed origin gets403with{"error":{"message":"origin not allowed for /v1/realtime","type":"invalid_request_error"}}before the upgrade, so no engine reservation is taken. An absentOriginis accepted; the default*policy accepts any origin, so default behavior is unchanged.realtime_routertakesArc<CorsPolicy>;build_routespasses the server's policy. The check lives in the route, so router-mode sub-apps (built without the CORS layer) enforce it too. The API-key layer stays outside: failing both checks is 401.docs/nemotron-voicechat.mdnext to the--api-keynote.Deviation from the issue
The issue shows the handler taking
ws: WebSocketUpgrade. The handler instead takesResult<WebSocketUpgrade, WebSocketUpgradeRejection>as its last extractor, so the origin check runs before upgrade validation. A disallowed origin is refused even on a malformed handshake, and the in-process app test can reach the 403 throughcreate_app/create_app_without_corswithout a hyper upgrade handle.Test plan
cargo test --release --lib server::cors(newwebsocket_origin_matrix: every policy against absent, allowed, disallowed,null, non-UTF-8)cargo test --release --lib server::routes::realtime(new test over both app builds, including 401 ordering; existing mount/401 test)cargo test --release --test realtime_ws(newstart_server_with_policy,disallowed_origin_is_rejected_before_session, localhost/default policy test; all existing tests)--lib --tests --examples -D warnings), contract tests, andcargo test --release -p mlxcel --lib server::pass locally; GitHub CI not awaited (GB10 runner down)Closes #2042