fix(router): route dot segments the same way on every runtime

The same bytes on the wire reached different routes depending on where
the app ran. `GET /public/../admin` hit a `/admin` route under Deno, Bun
and workerd, and 404'd under Node — 200 vs 404 for one request.

The fetch-based runtimes resolve dot segments inside the `Request`
constructor, before green-tea is handed anything: `new Request(
'http://x/public/../admin').url` is already `http://x/admin`. Node's raw
target has no such step. Rejecting outright — the stricter policy, and
what this module already does for `//` — is not implementable on three of
the four runtimes, because the raw target is unrecoverable there. So Node
converges on the rule the other three cannot opt out of.

`%2e` is a dot to the URL parser, so the encoded spelling resolves too;
leaving it out would keep it as a way to reach a route the plain spelling
resolves away from.

Cost is gated: paths with no `.` and no `%` pay two indexOf scans
(20.0 -> 21.7 ns for `/hello`), a literal dot pays the regex (35.5 ns),
and only real dot segments pay the split (103.6 ns).

The existing parity suite could not have caught this — its `both()`
helper goes through `fetch()`, which normalizes the URL before it reaches
the wire. The new cases write the request line to a socket by hand.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-08-16 23:35:26 -05:00
1 parent e625d5c881
commit 0c9f8a545e
3 files changed
+116 -1

No files matched your search

+54 -1
View File
@@ -166,11 +166,64 @@ export function compilePattern(pattern: string): CompiledPattern {
return compiled;
}
/** Matches a `.` or `..` segment, percent-encoded or not, sitting alone between separators. */
const DOT_SEGMENT = /(?:^|\/)(?:\.|%2e){1,2}(?:\/|$)/i;
/**
* Classifies one path segment as `.`, `..`, or neither.
*
* `%2e` is a dot as far as the URL spec is concerned, so `/a/%2e%2e/b` and `/a/../b` are the same
* path. Skipping the encoded spelling would leave the encoded form as a way to reach a route the
* plain form resolves away from.
*/
function dotSegment(segment: string): '.' | '..' | undefined {
if (segment === '.' || segment === '..') return segment;
if (segment.length > 6 || !segment.includes('%')) return undefined;
const decoded = segment.toLowerCase().replace(/%2e/g, '.');
return decoded === '.' || decoded === '..' ? decoded : undefined;
}
/**
* Resolves `.` and `..` segments, the way RFC 3986 §5.2.4 and the WHATWG URL parser both do.
*
* This runs for the *Node* adapter's benefit. Every fetch-based runtime — Deno, Bun, workerd —
* resolves dot segments inside the `Request` constructor, before green-tea is handed anything:
* `new Request('http://x/public/../admin').url` is already `http://x/admin`. The raw target is
* simply not recoverable there, so rejecting dot segments outright — which is what this module
* does for `//`, and would be the stricter policy — cannot be implemented on three of the four
* runtimes. Resolving is the only rule all four can agree on.
*
* Before this, the same bytes on the wire routed differently per runtime: `GET /public/../admin`
* reached a `/admin` route under Deno/Bun/edge and 404'd under Node.
*/
function resolveDotSegments(path: string): string {
const out: string[] = [];
for (const segment of path.split('/').slice(1)) {
const dots = dotSegment(segment);
if (dots === '.') continue;
if (dots === '..') {
out.pop();
continue;
}
out.push(segment);
}
return `/${out.join('/')}`;
}
/** 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;
// Two `indexOf` scans gate the regex, and the regex gates the split. A path with no `.` and no
// `%` at all — which is most of them — pays only the scans.
const resolved =
(path.includes('.') || path.includes('%')) && DOT_SEGMENT.test(path) ? resolveDotSegments(path) : path;
const normalized = resolved.length > 1 && resolved.endsWith('/') ? resolved.slice(0, -1) : resolved;
// Validation only — the decoded value is deliberately discarded, matching is done on the raw
// path. So the work is skipped entirely unless there is something to validate.