fix(auth): keep the capability prefix on a mount named like it

The server strips the literal mount prefix before the auth hook runs, so
under a base path named /webview, /html-view or starting with one of
them, /webview/webview/x arrives as /webview/x. Removing the base path
again in capabilityPrefixOf() then took the capability prefix itself,
and such paths reached the Basic check and its failure count.

The normalised path is now judged as it is first, and only when that is
no capability path, once more with the base path removed once. Either
match counts, so this can only add refusals, never an exemption.
This commit is contained in:
13768039+zomby1 committed 2026-11-25 10:36:36 +01:00
1 parent bb68915384
commit aabd71ee98
2 files changed
+35 -3

No files matched your search

+12 -3
View File
@@ -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;
+23
View File
@@ -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', () => {