diff --git a/src/web/middleware/auth.ts b/src/web/middleware/auth.ts index f0459e52..4ba9546d 100644 --- a/src/web/middleware/auth.ts +++ b/src/web/middleware/auth.ts @@ -401,8 +401,12 @@ export function isLostWebviewRootFrame(req: FastifyRequest): boolean { * * `rawUrl` has been through `stripBasePath()`, which removes the mount prefix only in its * literal spelling, so under `basePath` a re-spelled prefix (`/CODEMAN/webview/x`, - * `/%63odeman/...`, `//codeman/...`, `/x/../codeman/...`) is still in front. After the - * normalisation, the lower-cased base path is removed once when the path starts with it. + * `/%63odeman/...`, `//codeman/...`, `/x/../codeman/...`) is still in front. So the + * normalised path is judged as it is first, and only when that is no capability path and + * it starts with the lower-cased base path, once more with the base path removed once. + * Either match counts. As it is comes first because the literal prefix may already be + * gone: under a mount named `/webview`, `/webview/webview/x` arrives as `/webview/x`, and + * removing the base again would lose the capability prefix itself. */ export function capabilityPrefixOf(rawUrl: string, basePath = ''): 'webview' | 'html-view' | null { let path = rawUrl.split('?')[0]; @@ -418,8 +422,13 @@ export function capabilityPrefixOf(rawUrl: string, basePath = ''): 'webview' | ' .normalize(path) .replace(/\u0130/g, 'i') .toLowerCase(); + const asIs = capabilityPrefixOfNormalised(path); + if (asIs) return asIs; const base = basePath.toLowerCase(); - if (base && path.startsWith(`${base}/`)) path = path.slice(base.length); + return base && path.startsWith(`${base}/`) ? capabilityPrefixOfNormalised(path.slice(base.length)) : null; +} + +function capabilityPrefixOfNormalised(path: string): 'webview' | 'html-view' | null { if (path.startsWith(`${WEBVIEW_PROXY_PREFIX}/`)) return 'webview'; if (path.startsWith(`${HTML_VIEW_PREFIX}/`)) return 'html-view'; return null; diff --git a/test/trusted-proxy-auth.test.ts b/test/trusted-proxy-auth.test.ts index 0b3cc0ee..a6dad713 100644 --- a/test/trusted-proxy-auth.test.ts +++ b/test/trusted-proxy-auth.test.ts @@ -352,6 +352,13 @@ describe('capabilityPrefixOf judges a path the way a proxy matcher does', () => ['/other/webview/x', '/codeman', null], ['/codeman', '/codeman', null], ['/CODEMAN/webview/x', '', null], // root mount: no prefix to remove + // A mount named like a capability prefix: the server has already stripped the literal + // mount, so what arrives must also be judged as it is. + ['/webview/x', '/webview', 'webview'], // from /webview/webview/x + ['/html-view/x', '/html-view', 'html-view'], // from /html-view/html-view/x + ['/webview/nested/x', '/webview/nested', 'webview'], // from /webview/nested/webview/nested/x + ['/WEBVIEW/webview/x', '/webview', 'webview'], + ['/api/me', '/webview', null], ] as const)('%s under "%s" -> %s', (url, basePath, expected) => { expect(capabilityPrefixOf(url, basePath)).toBe(expected); }); @@ -955,6 +962,22 @@ describe('under a base path, every spelling of the mount prefix is refused like const ok = await rawGet(port, '/codeman/api/me', { authorization: basic('bob', 'bobpass123') }); expect(ok.status).toBe(200); }); + + it('keeps the capability prefix on a mount named like it', async () => { + useSecret({ inline: SECRET }); + const s = new WebServer(0, false, true, '127.0.0.1', undefined, false, '/webview'); + await s.start(); + extraServers.push(s); + const port = s.boundPort; + // The server strips the literal mount, so this arrives as /webview/x. + for (let i = 0; i < 60; i++) { + const res = await rawGet(port, '/webview/webview/x'); + expect(res.status).toBe(403); + expect(res.challenge).toBeUndefined(); + } + const ok = await rawGet(port, '/webview/api/me', { authorization: basic('bob', 'bobpass123') }); + expect(ok.status).toBe(200); + }); }); describe('trusted-proxy sign-in is off without its preconditions', () => {