Skip to content

fix: resolve dot segments in directory redirect Location - #249

Open
simpleqt wants to merge 1 commit into
expressjs:masterfrom
simpleqt:fix/redirect-dot-segments
Open

simpleqt wants to merge 1 commit into
expressjs:masterfrom
simpleqt:fix/redirect-dot-segments

Conversation

@simpleqt

@simpleqt simpleqt commented Nov 3, 2026 •

Copy link
Copy Markdown

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/ and pets/):

node -e "const s=require('serve-static')('test/fixtures');const h=require('http').createServer((q,r)=>s(q,r,()=>{r.statusCode=404;r.end()}));h.listen(3000)"
curl -sI http://localhost:3000/users/.   | grep -i location   # Location: /users/./
curl -sI http://localhost:3000/users/..  | grep -i location   # Location: /users/../
curl -sI http://localhost:3000/./users   | grep -i location   # Location: /./users/

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. send had already resolved a different path on disk: it runs path.normalize before fs.stat, so /users/. and /users/.. match the users directory and the root directory. That makes the redirect the only place where the unresolved form survives.

Fix: resolve the pathname with path.posix.normalize so the Location names the directory send actually matched. Leading slashes are collapsed before resolution rather than after, because path.posix.normalize('/users/..' + '/') would otherwise produce a protocol-relative // Location.

A segment spelled only with %2e and dots - %2e, %2e%2e, .%2E, %2e. - is resolved the same way as its literal counterpart, because send decodes the path before resolving it. A %2e inside 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:

npx mocha --reporter spec test/redirect-dot-segments.js

without the change: 3 passing, 6 failing
Error: expected "Location" of "/users/", got "/users/./"

with the change: 9 passing, including the literal, percent-encoded (both %2e%2e and .%2E) and duplicate-leading-slash cases.

Full suite npm test-equivalent run of mocha test: 101 passing. eslint . passes.

Not run: the leak test (npm run test-leak, requires valgrind) and the coverage script test-ci.

Copilot AI balanced review requested due to automatic review settings November 3, 2026 21:42

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 yxshee left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. Percent-encoded dot segments still pass through. send decodes the path before normalizing it, so /users/%2e%2e matches the root directory. But path.posix.normalize doesn't treat %2e as ., so the Location keeps the unresolved segment. The WHATWG URL parser counts %2e and .%2E etc. as dot segments, so a browser will resolve the Location anyway, but raw clients get the same mismatch this PR fixes for .. Mapping %2e/%2E to . inside a segment before normalizing (or saying in a comment that encoded dots are out of scope) would cover it.

  2. 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, so send looks 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 yxshee left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. Percent-encoded dot segments still pass through. send decodes the path before normalizing it, so /users/%2e%2e matches the root directory. But path.posix.normalize doesn't treat %2e as ., so the Location keeps the unresolved segment. The WHATWG URL parser counts %2e and .%2E etc. as dot segments, so a browser will resolve the Location anyway, but raw clients get the same mismatch this PR fixes for .. Mapping %2e/%2E to . inside a segment before normalizing (or saying in a comment that encoded dots are out of scope) would cover it.

  2. 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, so send looks 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>
@simpleqt
simpleqt force-pushed the fix/redirect-dot-segments branch from a0b723c to e3ecea3 Compare November 12, 2026 05:42
@simpleqt

Copy link
Copy Markdown
Author

Both notes were right, and both are fixed in the amended commit (e3ecea3).

Encoded dot segments: a segment spelled only with %2e and dots (%2e, %2e%2e, .%2E, %2e.) is now resolved exactly like its literal counterpart, since send decodes before it resolves. A %2e inside a longer segment name (/users/a%2eb) is left as supplied - decoding it there would name a different file than the one matched on disk. /users/%2e%2e now redirects to /, and .%2E behaves the same.

The loop claim was wrong: I could not reproduce a loop either. /users/.. answers 301 to /users/../, that request has a trailing slash, send looks for an index in the root and 404s here - it does not redirect again. I removed the loop sentence from the description; the wrong Location is the whole defect.

Tests: test/redirect-dot-segments.js is now 9 passing with the change (3 passing / 6 failing without it), full mocha test is 101 passing, eslint . passes. The description now carries those numbers instead of the old ones.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants