Skip to content

fix: enforce route-scoped auth for realtime RPC - #123

Merged
nakasyou merged 2 commits into
mainfrom
codex/fix-vulnerability-in-realtime-endpoint-cmw8df
Apr 28, 2026
Merged

fix: enforce route-scoped auth for realtime RPC#123
nakasyou merged 2 commits into
mainfrom
codex/fix-vulnerability-in-realtime-endpoint-cmw8df

Conversation

@nakasyou

Copy link
Copy Markdown
Member

Motivation

  • Realtime WebSocket endpoints were callable by ID alone and bypassed the route-bound authorization, route middleware, and same-origin RPC checks that protect actions/loaders.
  • Because realtime handlers are collected globally, any client that knows an ID could invoke privileged server logic for routes that expect middleware-based protections.

Description

  • Add realtimeIds to the route server access metadata and include realtimes when building routeServerAccessEntries so realtime handlers are scoped to reachable route files.
  • Enforce RPC route resolution with getRpcCurrentRoute(...) and check membership of the requested realtime id against routeAccess.realtimeIds in both the build-time app (packages/eclipsa/vite/build/mod.ts) and dev app (packages/eclipsa/vite/dev-app/mod.ts).
  • Execute realtime handlers through composeRouteMiddlewares(...) so route middlewares and server hooks run before executeRealtime(...).
  • Cache the pre-upgrade route match for the websocket upgrade using a per-request WeakMap and reuse it during the websocket handler execution to keep checks consistent across the upgrade boundary.
  • Update and add tests to assert emitted build source includes realtime auth checks and to verify dev realtime requests are permitted only when the RPC route context authorizes the realtime ID.

Testing

  • Ran bunx tsc -p tsconfig.json --noEmit and it completed successfully.
  • Ran bun run test vite/build/mod.test.ts vite/dev-app/mod.test.ts (from packages/eclipsa) and the updated test suite passed (48 tests passed across both files).

Codex Task

Co-authored-by: codex <codex@openai.com>
Copilot AI review requested due to automatic review settings April 28, 2026 03:44
@github-actions

github-actions Bot commented Apr 28, 2026

Copy link
Copy Markdown
Contributor

Website preview: https://pr-123-eclipsa.xiarenda61.workers.dev

Commit: 6e0dedc

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1166918490

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/eclipsa/vite/build/mod.ts Outdated
Comment on lines +1585 to +1587
const routeMatch = getRpcCurrentRoute(appHooks, c);
if (!routeMatch) {
return c.text("Bad Request", 400);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Accept browser realtime connects without RPC header

This new pre-upgrade check makes realtime upgrades fail whenever getRpcCurrentRoute(...) is null, but browser clients cannot provide x-eclipsa-route-url during new WebSocket(...) connects (the runtime client in packages/eclipsa/core/realtime.ts opens a socket URL only). In practice, normal realtime().connect() calls will now hit 400 Bad Request for production (and the same logic exists in dev), so this change breaks legitimate browser realtime usage instead of only blocking cross-route ID probing.

Useful? React with 👍 / 👎.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR closes an authorization gap where realtime WebSocket RPC handlers could be invoked globally by id, bypassing route-scoped access checks and route middleware protections.

Changes:

  • Adds realtimeIds to route server access metadata and scopes realtime handlers to the reachable route graph (dev + build).
  • Enforces realtime RPC route resolution via getRpcCurrentRoute(...) and validates the requested realtime id against route access.
  • Runs realtime execution through composeRouteMiddlewares(...), and caches pre-upgrade route matches via a per-request WeakMap to keep upgrade checks consistent.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
packages/eclipsa/vite/dev-app/mod.ts Adds route-scoped realtime access IDs, enforces current-route resolution for realtime, caches route matches, and runs realtime via route middleware composition.
packages/eclipsa/vite/dev-app/mod.test.ts Updates websocket adapter test to include route RPC header; adds test asserting realtime calls are blocked outside the current route graph.
packages/eclipsa/vite/build/mod.ts Extends build-time route server access entries with realtime IDs and emits runtime checks + middleware composition for realtime WS routes.
packages/eclipsa/vite/build/mod.test.ts Asserts emitted server bundle source includes realtime auth/middleware checks and route-match caching.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread packages/eclipsa/vite/dev-app/mod.ts Outdated
Comment on lines +1250 to +1258
return composeRouteMiddlewares(
routeMatch.route,
requestContext,
routeMatch.params,
async () => {
await executeRealtime(id, requestContext, socket)
return requestContext.body(null, 204)
},
) as Promise<Response>

Copilot AI Apr 28, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

createRealtimeHonoUpgradeHandler ignores the return value of its connect callback (see packages/eclipsa/core/realtime.ts:404-439), so the Response produced by resolveRequest(...)/composeRouteMiddlewares(...) here is never sent to the client. If a route middleware short-circuits by returning a Response (e.g. auth failure), the WebSocket upgrade will still succeed and the connection may remain open but inert. Consider running the route middlewares/auth checks in the pre-upgrade middleware (before await next()), or have the connect path explicitly close the socket (e.g. 1008) when middlewares short-circuit (use a non-Response sentinel return from the handler to detect success).

Copilot uses AI. Check for mistakes.
Comment thread packages/eclipsa/vite/build/mod.ts Outdated
Comment on lines +1604 to +1618
await resolveRequest(c, async (requestContext, appHooks) => {
const id = requestContext.req.param("id");
await executeRealtime(id, requestContext, socket);
return requestContext.body(null, 204);
const routeMatch = realtimeRouteMatches.get(requestContext.req.raw) ?? getRpcCurrentRoute(appHooks, requestContext);
if (!routeMatch) {
return requestContext.text("Bad Request", 400);
}
return composeRouteMiddlewares(
routeMatch.route,
requestContext,
routeMatch.params,
async () => {
await executeRealtime(id, requestContext, socket);
return requestContext.body(null, 204);
},
);

Copilot AI Apr 28, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

createRealtimeHonoUpgradeHandler does not use the Response returned by its connect callback (see packages/eclipsa/core/realtime.ts:404-439), so the Response returned from resolveRequest(...)/composeRouteMiddlewares(...) here is effectively discarded. This means route middlewares that deny access by returning a Response won't prevent the WebSocket upgrade and may leave an idle connection open. Consider moving middleware execution/auth denial into the pre-upgrade middleware (before await next()), or explicitly close the socket when middlewares short-circuit (use a non-Response sentinel from the handler so you can distinguish “authorized” vs “middleware returned a Response”).

Copilot uses AI. Check for mistakes.
Carry browser route context through realtime WebSocket URLs and authorize route-scoped realtime handlers before the WebSocket upgrade proceeds.

Co-authored-by: codex <codex@openai.com>
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 18.98734% with 128 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
packages/eclipsa/vite/build/mod.ts 2.59% 75 Missing ⚠️
packages/eclipsa/vite/dev-app/mod.ts 30.26% 53 Missing ⚠️
Files with missing lines Coverage Δ
packages/eclipsa/core/realtime.ts 60.26% <100.00%> (-1.37%) ⬇️
packages/eclipsa/core/router-shared.ts 90.90% <100.00%> (+18.68%) ⬆️
packages/eclipsa/vite/dev-app/mod.ts 25.48% <30.26%> (-0.15%) ⬇️
packages/eclipsa/vite/build/mod.ts 8.42% <2.59%> (-0.09%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@nakasyou
nakasyou merged commit 6acfc0d into main Apr 28, 2026
5 checks passed
@nakasyou
nakasyou deleted the codex/fix-vulnerability-in-realtime-endpoint-cmw8df branch April 28, 2026 14:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants