fix(router): reject invalid and ambiguous paths

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-08-02 20:39:09 -05:00
1 parent 790db661e5
commit 8e38fffd77
6 files changed
+150 -9

No files matched your search

+36
View File
@@ -583,3 +583,39 @@ test('per-route maxBodyBytes overrides the server default', async () => {
server.close();
});
describe('route table validation', () => {
it('rejects routes with the same method and effective pattern', () => {
@Route('/users')
class DuplicateCtl {
@Get('/:id') byId() {
return {};
}
@Get('/:name') byName() {
return {};
}
}
@Module({ mountpoint: '/api', controllers: [DuplicateCtl] })
class DuplicateModule {}
expect(() => createApp({ modules: [DuplicateModule] })).toThrow(
/ambiguous route GET.*\/api\/users\/:name.*DuplicateCtl\.byName.*DuplicateCtl\.byId/,
);
});
it('allows the same effective pattern under different methods', () => {
@Route('/users')
class MethodsCtl {
@Get('/:id') get() {
return {};
}
@Post('/:name') post() {
return {};
}
}
@Module({ mountpoint: '/api', controllers: [MethodsCtl] })
class MethodsModule {}
expect(() => createApp({ modules: [MethodsModule] })).not.toThrow();
});
});
+41
View File
@@ -35,6 +35,26 @@ class DupCtl {
@Module({ mountpoint: '/api', controllers: [DupCtl] })
class DupTeapotModule {}
@Route('/shape')
class ParamIdCtl {
@Get('/:id', { export: true })
get() {
return { from: 'id' };
}
}
@Module({ mountpoint: '/api', controllers: [ParamIdCtl] })
class ParamIdTeapotModule {}
@Route('/shape')
class ParamNameCtl {
@Get('/:name', { export: true })
get() {
return { from: 'name' };
}
}
@Module({ mountpoint: '/api', controllers: [ParamNameCtl] })
class ParamNameTeapotModule {}
@Route('/c')
class PlainCtl {
@Get('/ok')
@@ -112,6 +132,27 @@ describe('mesh skeleton integration', () => {
bServer.close();
});
it('refuses ambiguous remote routes that differ only by parameter name', async () => {
const a = createApp({ modules: [ParamIdTeapotModule], experimental: true, mesh: { secret: SECRET } });
const b = createApp({ modules: [ParamNameTeapotModule], experimental: true, mesh: { secret: SECRET } });
const aServer = await a.listen(0);
const bServer = await b.listen(0);
const url = (s: any) => `ws://127.0.0.1:${(s.address() as any).port}/__mesh__/control`;
const teacup = createApp({
modules: [PlainTeacupModule],
experimental: true,
mesh: { teapots: [{ url: url(aServer), secret: SECRET }, { url: url(bServer), secret: SECRET }] },
});
try {
await expect(teacup.ready()).rejects.toThrow(/ambiguous remote route.*GET \/api\/shape\/:name/i);
} finally {
await teacup.close();
aServer.close();
bServer.close();
}
});
it('accepts the same route from a single teapot (regression: no false positive)', async () => {
const a = createApp({ modules: [DupTeapotModule], experimental: true, mesh: { secret: SECRET } });
const aServer = await a.listen(0);
+14 -1
View File
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest';
import { compilePattern, matchPattern, matchRoute, allowedMethods } from '../src/http/router';
import { compilePattern, matchPattern, matchRoute, allowedMethods, normalizeRequestPath } from '../src/http/router';
import type { RouteDef } from '../src/http/types';
const route = (method: string, pattern: string): RouteDef => ({
@@ -53,6 +53,19 @@ describe('compilePattern', () => {
});
});
describe('normalizeRequestPath', () => {
it('treats one trailing slash as equivalent and preserves root', () => {
expect(normalizeRequestPath('/users')).toBe('/users');
expect(normalizeRequestPath('/users/')).toBe('/users');
expect(normalizeRequestPath('/')).toBe('/');
});
it('rejects repeated slashes and malformed percent encoding', () => {
expect(() => normalizeRequestPath('/users//42')).toThrow(/repeated slash/);
expect(() => normalizeRequestPath('/users/%E0%A4%A')).toThrow(/malformed path encoding/);
});
});
describe('matchRoute precedence', () => {
it('static beats :param beats catch-all regardless of registration order', () => {
const routes = [route('GET', '/a/:rest*'), route('GET', '/a/:id'), route('GET', '/a/b')];