From 0b78459309975af39a9b813d4d1f6c65acbea7da Mon Sep 17 00:00:00 2001 From: clibequilibrium <105135724+clibequilibrium@users.noreply.github.com> Date: Tue, 17 Nov 2026 18:19:04 -0500 Subject: [PATCH] console: classify the decoded path the router dispatches on The console guard split the raw, still-percent-encoded url.pathname to decide whether a request was public or operator-only. SvelteKit's router, though, decodes each segment before matching a route. The two disagreed: /%61pi/registry/projects has first segment %61pi, which matches none of the guard's protected prefixes, so classifyAccess returned open and the request sailed through with no session - yet the router decoded it to api and ran the real /api/registry/projects handler. Signed out, that exposed every operator endpoint under /api/* to anyone: listing and creating registry projects, /api/console/me, and the project-scoped proxies behind them. Confirmed against a live dev stack - GET /%61pi/registry/projects returned 200 before, 401 after. Fix it at the one place that matters: decode each segment the way the router does and classify that. The same transform feeds the service-key surface check, so authentication and authorization see one path. Per segment and never re-split, so an encoded slash in a catch-all can't fan out into segments the classifier would read as structure; a malformed escape names no route and is left as-is. Adds an e2e test that attacks the encoded prefix. --- e2e/console-guard.api.spec.ts | 19 +++++++++++++++++ src/hooks.server.ts | 40 +++++++++++++++++++++++++++++++---- 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/e2e/console-guard.api.spec.ts b/e2e/console-guard.api.spec.ts index 89c9feba..37615d98 100644 --- a/e2e/console-guard.api.spec.ts +++ b/e2e/console-guard.api.spec.ts @@ -48,6 +48,25 @@ test.describe('console guard', () => { expect(response.status()).toBe(401); }); + test('a percent-encoded path prefix does not slip past the guard', async ({ request }) => { + // SvelteKit's router decodes `%61pi` to `api` and runs the `/api/...` + // handler, but the guard used to classify the still-encoded path: it saw + // `%61pi`, matched no protected prefix, and waved the request through as + // public. So `/%61pi/registry/projects` reached every operator endpoint + // with no session. The guard must now see the same decoded path the + // router dispatches on, GET and POST alike. + const listed = await request.get('/%61pi/registry/projects'); + expect(listed.status(), 'encoded /api must stay operator-only').toBe(401); + + const me = await request.get('/%61pi/console/me'); + expect(me.status()).toBe(401); + + const created = await request.post('/%61pi/registry/projects', { + data: { id: 'guard-bypass-probe', name: 'probe' } + }); + expect(created.status(), 'encoded /api must not create a project').toBe(401); + }); + test('the chat endpoint is not a free anonymous AI proxy', async ({ request }) => { const response = await request.post(chatPath(SEED_PROJECT), { data: { question: 'who are the users?' } diff --git a/src/hooks.server.ts b/src/hooks.server.ts index 657c1d4c..179911ca 100644 --- a/src/hooks.server.ts +++ b/src/hooks.server.ts @@ -237,9 +237,35 @@ type Access = * The agent workers have no public route (`workers_dev` and `preview_urls` * are both false), so every external request to them arrives through here. */ -function classifyAccess(pathname: string): Access { - const segments = pathname.split('/').filter(Boolean); +/** + * The path SvelteKit's router actually dispatches on: each segment decoded + * exactly once, the way the router decodes it before matching a route. + * + * The guard MUST classify THIS and never the raw `url.pathname`. The router + * decodes `%61pi` to `api` and runs the `/api/...` handler, but a raw-path + * check sees the literal `%61pi`, matches no protected prefix, and classifies + * the request `open` - so an anonymous caller reached every operator endpoint + * through `/%61pi/...`. Decoding each segment here puts the guard and the + * dispatcher back on one path. Per segment, and never re-split afterwards: an + * encoded slash (`%2F`) in a catch-all stays inside its segment for routing + * and must not fan out into new segments the classifier would read as + * structure. A malformed escape can't name a real route, so a segment that + * won't decode is left as-is. + */ +function routedSegments(pathname: string): string[] { + return pathname + .split('/') + .filter(Boolean) + .map((segment) => { + try { + return decodeURIComponent(segment); + } catch { + return segment; + } + }); +} +function classifyAccess(segments: string[]): Access { // /agents/// if (segments[0] === 'agents') { const entry = agentByWorkerSegment(segments[1] ?? ''); @@ -379,7 +405,13 @@ const consoleGuardHandle: Handle = async ({ event, resolve }) => { event.locals.consoleIdentity = null; event.locals.serviceKey = null; - const access = classifyAccess(event.url.pathname); + // Classify the path the router will dispatch on, not the raw URL: a + // percent-encoded prefix (`/%61pi/...`) otherwise slips past the guard and + // still reaches its decoded handler. routedPath is that same decoded path, + // reused below so the service-key surface check agrees with it. + const segments = routedSegments(event.url.pathname); + const routedPath = `/${segments.join('/')}`; + const access = classifyAccess(segments); if (access.scope === 'open') { // Public still has to mean "public surface of a project that exists". // The agents provision a Durable Object on first touch, so a sign-up @@ -441,7 +473,7 @@ const consoleGuardHandle: Handle = async ({ event, resolve }) => { if ( access.projectId && !origin && - isServiceKeySurface(event.url.pathname, access.projectId) && + isServiceKeySurface(routedPath, access.projectId) && !isDemoProjectId(access.projectId) ) { const grant = await verifyServiceKey(event.platform, serviceBearer.toLowerCase());