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

+23
View File
@@ -19,6 +19,14 @@ const compiledCache = new Map<string, CompiledPattern>();
const PARAM_NAME = /^[A-Za-z_][A-Za-z0-9_]*$/;
const MAX_CONSTRAINT_LENGTH = 128;
/** Request-path syntax error. Adapters translate this to the standard 400 response envelope. */
export class InvalidRequestPathError extends Error {
constructor(message: string) {
super(message);
this.name = 'InvalidRequestPathError';
}
}
interface ConstraintToken {
length: number;
unbounded: boolean;
@@ -155,6 +163,21 @@ export function compilePattern(pattern: string): CompiledPattern {
return compiled;
}
/** Applies the public trailing-slash policy and validates every segment's percent encoding. */
export function normalizeRequestPath(path: string): string {
if (!path.startsWith('/')) throw new InvalidRequestPathError('invalid request path: path must start with slash');
if (path.includes('//')) throw new InvalidRequestPathError('invalid request path: repeated slash');
const normalized = path.length > 1 && path.endsWith('/') ? path.slice(0, -1) : path;
try {
for (const segment of normalized.split('/').slice(1)) decodeURIComponent(segment);
} catch {
throw new InvalidRequestPathError('invalid request path: malformed path encoding');
}
return normalized;
}
function decodeSegment(value: string): string {
return decodeURIComponent(value);
}