fix(router): drop pending walk frames when a segment fails to decode
CI / dco (pull_request) Successful in 5s
CI / runtimes (bun) (pull_request) Successful in 15s
CI / build (pull_request) Successful in 26s
CI / runtimes (deno) (pull_request) Successful in 19s
CI / runtimes (edge) (pull_request) Successful in 14s

The walk stack is shared across lookups. A malformed % escape threw out
of decodeSegment with frames still pushed, so repeated direct matcher
calls on such paths retained them. Truncate both arrays back to the
entry length in a finally block, and check it with a test.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-10-12 13:47:53 -05:00
1 parent 4de9f32e31
commit a2b71e2472
2 files changed
+19

No files matched your search

+9
View File
@@ -552,6 +552,9 @@ const stackNodes: RadixNode[] = [];
const stackDepths: number[] = []; const stackDepths: number[] = [];
const POST = -1; const POST = -1;
/** @internal Pending frames left on the shared walk stack; zero between lookups. For tests. */
export const walkStackSize = (): number => stackNodes.length;
/** /**
* Depth-first walk with backtracking: a static branch that dead-ends deeper falls back to its param, * Depth-first walk with backtracking: a static branch that dead-ends deeper falls back to its param,
* constrained and catch-all siblings. Params are decoded after splitting, so `%2F` stays inside one * constrained and catch-all siblings. Params are decoded after splitting, so `%2F` stays inside one
@@ -569,6 +572,7 @@ function walk(root: RadixNode, search: Search, segs: string[]): void {
stackNodes.push(root); stackNodes.push(root);
stackDepths.push(0); stackDepths.push(0);
try {
while (stackNodes.length > base) { while (stackNodes.length > base) {
const node = stackNodes.pop()!; const node = stackNodes.pop()!;
const depth = stackDepths.pop()!; const depth = stackDepths.pop()!;
@@ -608,6 +612,11 @@ function walk(root: RadixNode, search: Search, segs: string[]): void {
stackDepths.push(depth + 1); stackDepths.push(depth + 1);
} }
} }
} finally {
// A malformed `%` escape throws out of `decodeSegment` with frames still pending; drop them.
stackNodes.length = base;
stackDepths.length = base;
}
} }
function searchTree( function searchTree(
+10
View File
@@ -126,6 +126,16 @@ describe('radix index: deep paths', () => {
}); });
}); });
describe('radix index: malformed paths', () => {
it('leaves no frames on the shared walk stack when a segment fails to decode', () => {
// `/:rest*` pushes a pending catch-all frame before `/:id` decodes `%GG` and throws.
const routes = [route('GET', '/:id'), route('GET', '/:rest*')];
for (let i = 0; i < 3; i++) expect(() => live.matchRoute(routes, 'GET', '/%GG')).toThrow(URIError);
expect(live.walkStackSize()).toBe(0);
expect(live.matchRoute(routes, 'GET', '/a')?.params).toEqual({ id: 'a' });
});
});
describe('radix index: lifetime', () => { describe('radix index: lifetime', () => {
it('rebuilds when the array it was built from changes length', () => { it('rebuilds when the array it was built from changes length', () => {
const routes = [route('GET', '/a')]; const routes = [route('GET', '/a')];