Skip to content

Commit 82dad62

Browse files
francisjohnjohnston-webProdia Evolution Worker
andauthored
fix(serve-static): normalize all backslashes in file paths, not just the first (#4962)
`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. Co-authored-by: Prodia Evolution Worker <evo@prodia.dev>
1 parent 2f01b77 commit 82dad62

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

src/utils/filepath.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ describe('getFilePathWithoutDefaultDocument', () => {
4343
expect(
4444
getFilePathWithoutDefaultDocument({ filename: slashToBackslash('./..foo../bar.txt') })
4545
).toBe('..foo../bar.txt')
46+
47+
// Multiple path segments: every backslash must be normalized, not just the first
48+
expect(
49+
getFilePathWithoutDefaultDocument({ filename: slashToBackslash('foo/bar/baz.txt') })
50+
).toBe('foo/bar/baz.txt')
51+
expect(
52+
getFilePathWithoutDefaultDocument({
53+
filename: slashToBackslash('foo/bar/baz.txt'),
54+
root: 'assets',
55+
})
56+
).toBe('assets/foo/bar/baz.txt')
4657
})
4758
})
4859

src/utils/filepath.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export const getFilePathWithoutDefaultDocument = (
4343
filename = filename.replace(/^\.?[\/\\]/, '')
4444

4545
// foo\bar.txt => foo/bar.txt
46-
filename = filename.replace(/\\/, '/')
46+
filename = filename.replace(/\\/g, '/')
4747

4848
// assets/ => assets
4949
root = root.replace(/\/$/, '')

0 commit comments

Comments
 (0)