Repository navigation
Conversation
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
yxshee
left a comment
There was a problem hiding this comment.
I ran this branch (98 passing) against the fixtures with raw requests:
| request | master | this PR |
|---|---|---|
/users/. |
/users/./ |
/users/ |
/users/..?x=1 |
/users/../?x=1 |
/?x=1 |
/users/../users |
/users/../users/ |
/users/ |
/users/%2e%2e |
/users/%2e%2e/ |
/users/%2e%2e/ (unchanged) |
/users/%2E, /users/.%2e, /%2e/users |
kept as-is | kept as-is |
So the literal dot segments are fixed, and the query string survives.
Two notes:
-
Percent-encoded dot segments still pass through.
senddecodes the path before normalizing it, so/users/%2e%2ematches the root directory. Butpath.posix.normalizedoesn't treat%2eas., so the Location keeps the unresolved segment. The WHATWG URL parser counts%2eand.%2Eetc. as dot segments, so a browser will resolve the Location anyway, but raw clients get the same mismatch this PR fixes for.. Mapping%2e/%2Eto.inside a segment before normalizing (or saying in a comment that encoded dots are out of scope) would cover it. -
The description says
/users/..loops, but I couldn't reproduce a loop on master.GET /users/..answers 301 to/users/../. That request has a trailing slash, sosendlooks for an index in the root and serves it (here it falls through to 404, since the fixtures root has no index.html). It doesn't redirect again. The wrong Location is still worth fixing, but it might be better to drop the loop claim so the changelog entry is accurate.
yxshee
left a comment
There was a problem hiding this comment.
I ran this branch (98 passing) against the fixtures with raw requests:
| request | master | this PR |
|---|---|---|
/users/. |
/users/./ |
/users/ |
/users/..?x=1 |
/users/../?x=1 |
/?x=1 |
/users/../users |
/users/../users/ |
/users/ |
/users/%2e%2e |
/users/%2e%2e/ |
/users/%2e%2e/ (unchanged) |
/users/%2E, /users/.%2e, /%2e/users |
kept as-is | kept as-is |
So the literal dot segments are fixed, and the query string survives.
Two notes:
-
Percent-encoded dot segments still pass through.
senddecodes the path before normalizing it, so/users/%2e%2ematches the root directory. Butpath.posix.normalizedoesn't treat%2eas., so the Location keeps the unresolved segment. The WHATWG URL parser counts%2eand.%2Eetc. as dot segments, so a browser will resolve the Location anyway, but raw clients get the same mismatch this PR fixes for.. Mapping%2e/%2Eto.inside a segment before normalizing (or saying in a comment that encoded dots are out of scope) would cover it. -
The description says
/users/..loops, but I couldn't reproduce a loop on master.GET /users/..answers 301 to/users/../. That request has a trailing slash, sosendlooks for an index in the root and serves it (here it falls through to 404, since the fixtures root has no index.html). It doesn't redirect again. The wrong Location is still worth fixing, but it might be better to drop the loop claim so the changelog entry is accurate.
The directory listener built the trailing-slash redirect target from the raw request pathname, so a request for a directory reached through a dot segment got a Location that still contained it: "/users/." redirected to "/users/./" instead of "/users/", and "/users/.." to "/users/../". send had already resolved the path on disk (it normalizes before stat'ing), so the redirect was the only place where the unresolved form survived. Resolve the pathname with path.posix.normalize before formatting the Location. Leading slashes are collapsed first, so resolving "/users/.." cannot produce a protocol-relative "//" Location. A segment spelled only with %2e and dots is treated as the dot segment WHATWG URL defines, so the Location matches the path send decoded before it resolved it; a %2e inside a longer segment name is left alone. Adds test/redirect-dot-segments.js. Signed-off-by: simpleqt <2015900818@qq.com>
a0b723c to
e3ecea3
Compare
|
Both notes were right, and both are fixed in the amended commit ( Encoded dot segments: a segment spelled only with The loop claim was wrong: I could not reproduce a loop either. Tests: |
A request for a directory that is reached through a dot segment gets a Location header that still contains that dot segment.
Repro (fixtures directory contains
users/andpets/):Expected
Location: /users/,/and/users/.Cause: the directory listener in index.js built the redirect target as
collapseLeadingSlashes(originalUrl.pathname + '/'), so it kept the request path verbatim.sendhad already resolved a different path on disk: it runspath.normalizebeforefs.stat, so/users/.and/users/..match theusersdirectory and the root directory. That makes the redirect the only place where the unresolved form survives.Fix: resolve the pathname with
path.posix.normalizeso the Location names the directorysendactually matched. Leading slashes are collapsed before resolution rather than after, becausepath.posix.normalize('/users/..' + '/')would otherwise produce a protocol-relative//Location.A segment spelled only with
%2eand dots -%2e,%2e%2e,.%2E,%2e.- is resolved the same way as its literal counterpart, becausesenddecodes the path before resolving it. A%2einside a longer segment name (/users/a%2eb) is left as supplied, since decoding it there would name a different file than the one matched on disk.Test evidence:
without the change:
3 passing,6 failingError: expected "Location" of "/users/", got "/users/./"with the change:
9 passing, including the literal, percent-encoded (both%2e%2eand.%2E) and duplicate-leading-slash cases.Full suite
npm test-equivalent run ofmocha test: 101 passing.eslint .passes.Not run: the leak test (
npm run test-leak, requires valgrind) and the coverage scripttest-ci.