fix: handle paths with whitespaces in staticFiles (#3659)
Fix #3657 Static files with special characters in their names (spaces, `#`, unicode, etc.) fail to load due to path encoding issues. ## Root cause `prepareStaticFile()` uses `new URL(pathname, base)` to normalize paths, but `new URL` doesn't properly encode characters that are already percent-encoded in the filesystem path. For example, a file literally named `test %20encodeUri` becomes `test%20%20encodeUri` (the space and literal `%20` are indistinguishable), instead of the correct `test%20%2520encodeUri`. --------- Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com> Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Bartek Iwańczuk
Claude Opus 4.6
parent
4b4e495194
commit
c863097779
@@ -0,0 +1 @@
|
||||
space it works
|
||||
@@ -0,0 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Document</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>ok</h1>
|
||||
</body>
|
||||
</html>
|
||||
@@ -43,8 +43,10 @@ export function devServer(): Plugin[] {
|
||||
return next();
|
||||
}
|
||||
|
||||
const decodedPathname = decodeURIComponent(url.pathname.slice(1));
|
||||
|
||||
// Check if it's a static file first
|
||||
const staticFilePath = path.join(publicDir, url.pathname.slice(1));
|
||||
const staticFilePath = path.join(publicDir, decodedPathname);
|
||||
try {
|
||||
const stat = await Deno.stat(staticFilePath);
|
||||
if (stat.isFile) {
|
||||
@@ -57,7 +59,7 @@ export function devServer(): Plugin[] {
|
||||
// Check if it's a static/index.html file
|
||||
const staticFilePathIndex = path.join(
|
||||
publicDir,
|
||||
url.pathname.slice(1),
|
||||
decodedPathname,
|
||||
"index.html",
|
||||
);
|
||||
try {
|
||||
|
||||
@@ -53,6 +53,12 @@ Deno.test({
|
||||
const res = await fetch(`${address}/test_static/foo.txt`);
|
||||
const text = await res.text();
|
||||
expect(text).toEqual("it works");
|
||||
|
||||
const resWithSpace = await fetch(
|
||||
`${address}/test%20%2520encodeUri/foo%20%2520encodeUri.txt`,
|
||||
);
|
||||
const textWithSpace = await resWithSpace.text();
|
||||
expect(textWithSpace).toEqual("space it works");
|
||||
},
|
||||
);
|
||||
},
|
||||
@@ -482,6 +488,10 @@ Deno.test({
|
||||
const res = await fetch(`${address}/test_static/foo`);
|
||||
const text = await res.text();
|
||||
expect(text).toContain("<h1>ok</h1>");
|
||||
|
||||
const resWithSpace = await fetch(`${address}/test%20%2520encodeUri`);
|
||||
const textWithSpace = await resWithSpace.text();
|
||||
expect(textWithSpace).toContain("<h1>ok</h1>");
|
||||
},
|
||||
);
|
||||
},
|
||||
|
||||
@@ -37,6 +37,12 @@ Deno.test({
|
||||
const res = await fetch(`${demoServer.address()}/test_static/foo.txt`);
|
||||
const text = await res.text();
|
||||
expect(text).toContain("it works");
|
||||
|
||||
const resWithSpace = await fetch(
|
||||
`${demoServer.address()}/test%20%2520encodeUri/foo%20%2520encodeUri.txt`,
|
||||
);
|
||||
const textWithSpace = await resWithSpace.text();
|
||||
expect(textWithSpace).toContain("space it works");
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
@@ -481,6 +487,12 @@ Deno.test({
|
||||
const res = await fetch(`${demoServer.address()}/test_static/foo`);
|
||||
const text = await res.text();
|
||||
expect(text).toContain("<h1>ok</h1>");
|
||||
|
||||
const resWithSpace = await fetch(
|
||||
`${demoServer.address()}/test%20%2520encodeUri/`,
|
||||
);
|
||||
const textWithSpace = await resWithSpace.text();
|
||||
expect(textWithSpace).toContain("<h1>ok</h1>");
|
||||
},
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
|
||||
Reference in New Issue
Block a user