Repository navigation
fix(serve-static): normalize all backslashes in file paths, not just the first - #4962
Merged
yusukebe merged 1 commit intoMay 24, 2026
Conversation
…the first `getFilePathWithoutDefaultDocument` converts backslash separators to slashes so the same path resolves consistently regardless of the separator style of the incoming request. The replace used a non-global regex (`/\/`), so only the FIRST backslash was converted: a path with multiple segments like `foo\bar\baz.txt` became `foo/bar\baz.txt` — mixed separators that resolve to the wrong file (or 404) on platforms that don't treat `\` as a separator. The adjacent comment (`foo\bar.txt => foo/bar.txt`) shows the intent was to normalize all of them. Fix: add the `g` flag (`/\/g`). Behaviour is unchanged for any path with zero or one backslash, so all existing tests pass; added a regression test covering a multi-segment path (with and without `root`) that returns the broken `foo/bar\baz.txt` before this change. tsc --noEmit clean; eslint + prettier clean.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4962 +/- ##
=======================================
Coverage 78.77% 78.77%
=======================================
Files 154 154
Lines 10571 10571
Branches 2211 2211
=======================================
Hits 8327 8327
Misses 2244 2244 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Member
|
It actually needs |
1 task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
getFilePathWithoutDefaultDocumentinsrc/utils/filepath.tsnormalizes backslash path separators to forward slashes so a requested path resolves consistently regardless of separator style. The replacement uses a non-global regex:Without the
gflag, only the first backslash is converted, so a path with more than one segment is left with mixed separators:foo\bar\baz.txtfoo/bar\baz.txtfoo/bar/baz.txtThe remaining
\then resolves to the wrong file (or a 404) on back-ends that don't treat\as a path separator. The adjacent comment (foo\bar.txt => foo/bar.txt) shows the intent was to normalize all of them.Fix
Add the
gflag:Behaviour is unchanged for any path with zero or one backslash, so all existing tests pass. I also added a regression test covering a multi-segment path (with and without
root), which returns the brokenfoo/bar\baz.txtbefore this change.Why it was easy to miss
Every existing
slashToBackslash(...)test case contains only a single backslash after the leading-separator strip, so the suite stayed green and the missing flag was invisible on a skim.Checks
vitest run src/utils/filepath.test.ts— green (incl. new regression test)tsc --noEmit— cleaneslint+prettier— clean