From c86309777974e88d92c78f8f284b01e5a9b0b2e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B9=85=E4=BC=B4?= <942101897@qq.com> Date: Thu, 26 Mar 2026 03:00:48 +0800 Subject: [PATCH] fix: handle paths with whitespaces in staticFiles (#3659) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Co-authored-by: Claude Opus 4.6 (1M context) --- packages/fresh/src/dev/dev_build_cache.ts | 10 +++++++-- .../fresh/src/middlewares/static_files.ts | 2 +- .../src/middlewares/static_files_test.ts | 22 +++++++++++++------ .../test %20encodeUri/foo %20encodeUri.txt | 1 + .../demo/static/test %20encodeUri/index.html | 11 ++++++++++ .../plugin-vite/src/plugins/dev_server.ts | 6 +++-- packages/plugin-vite/tests/build_test.ts | 10 +++++++++ packages/plugin-vite/tests/dev_server_test.ts | 12 ++++++++++ 8 files changed, 62 insertions(+), 12 deletions(-) create mode 100644 packages/plugin-vite/demo/static/test %20encodeUri/foo %20encodeUri.txt create mode 100644 packages/plugin-vite/demo/static/test %20encodeUri/index.html diff --git a/packages/fresh/src/dev/dev_build_cache.ts b/packages/fresh/src/dev/dev_build_cache.ts index 7378c5f1..24148862 100644 --- a/packages/fresh/src/dev/dev_build_cache.ts +++ b/packages/fresh/src/dev/dev_build_cache.ts @@ -529,6 +529,12 @@ ${serializedFsRoutes} `.replaceAll(/\n[\n]+/g, "\n\n"); } +export function systemPathToUrlEncoded(systemPath: string): string { + const normalized = systemPath.replaceAll(WINDOWS_SEPARATOR, "/"); + const components = normalized.split("/").filter((comp) => comp); + return "/" + components.map((comp) => encodeURIComponent(comp)).join("/"); +} + export async function prepareStaticFile( item: PendingStaticFile, outDir: string, @@ -537,10 +543,10 @@ export async function prepareStaticFile( > { const file = await Deno.open(item.filePath); const hash = item.hash ? item.hash : await hashContent(file.readable); - const url = new URL(item.pathname, "http://localhost"); + const encodedPathname = systemPathToUrlEncoded(item.pathname); return { - name: url.pathname, + name: encodedPathname, hash, filePath: path.isAbsolute(item.filePath) ? path.relative(outDir, item.filePath) diff --git a/packages/fresh/src/middlewares/static_files.ts b/packages/fresh/src/middlewares/static_files.ts index d295060a..fce149aa 100644 --- a/packages/fresh/src/middlewares/static_files.ts +++ b/packages/fresh/src/middlewares/static_files.ts @@ -18,7 +18,7 @@ export function staticFiles(): Middleware { const buildCache = getBuildCache(ctx); if (buildCache === null) return await ctx.next(); - let pathname = decodeURIComponent(url.pathname); + let pathname = url.pathname; if (config.basePath) { pathname = pathname !== config.basePath ? pathname.slice(config.basePath.length) diff --git a/packages/fresh/src/middlewares/static_files_test.ts b/packages/fresh/src/middlewares/static_files_test.ts index b0733c53..84a8ba79 100644 --- a/packages/fresh/src/middlewares/static_files_test.ts +++ b/packages/fresh/src/middlewares/static_files_test.ts @@ -6,7 +6,10 @@ import { ASSET_CACHE_BUST_KEY } from "../constants.ts"; import { BUILD_ID } from "@fresh/build-id"; import type { Command } from "../commands.ts"; import type { ServerIslandRegistry } from "../context.ts"; -import { getContentType } from "../dev/dev_build_cache.ts"; +import { + getContentType, + systemPathToUrlEncoded, +} from "../dev/dev_build_cache.ts"; class MockBuildCache implements BuildCache { root = ""; @@ -223,12 +226,17 @@ Deno.test("static files - enables caching in production", async () => { ); }); -Deno.test("static files - decoded pathname", async () => { - const buildCache = new MockBuildCache({ - "C#.svg": { content: "body {}", hash: null }, - "西安市.png": { content: "body {}", hash: null }, - "인천.avif": { content: "body {}", hash: null }, - }); +Deno.test("static files - encoded pathname", async () => { + // Build cache stores URL-encoded paths (matching what prepareStaticFile produces) + const fileKeys = ["C#.svg", "西安市.png", "인천.avif"]; + const entries = Object.fromEntries( + fileKeys.map((key) => [ + systemPathToUrlEncoded(key), + { content: "body {}", hash: null }, + ]), + ); + + const buildCache = new MockBuildCache(entries); const server = serveMiddleware( staticFiles(), { buildCache }, diff --git a/packages/plugin-vite/demo/static/test %20encodeUri/foo %20encodeUri.txt b/packages/plugin-vite/demo/static/test %20encodeUri/foo %20encodeUri.txt new file mode 100644 index 00000000..7d0e8d91 --- /dev/null +++ b/packages/plugin-vite/demo/static/test %20encodeUri/foo %20encodeUri.txt @@ -0,0 +1 @@ +space it works \ No newline at end of file diff --git a/packages/plugin-vite/demo/static/test %20encodeUri/index.html b/packages/plugin-vite/demo/static/test %20encodeUri/index.html new file mode 100644 index 00000000..ed10d490 --- /dev/null +++ b/packages/plugin-vite/demo/static/test %20encodeUri/index.html @@ -0,0 +1,11 @@ + + + + + + Document + + +

ok

+ + diff --git a/packages/plugin-vite/src/plugins/dev_server.ts b/packages/plugin-vite/src/plugins/dev_server.ts index 729594ea..2a7e6da8 100644 --- a/packages/plugin-vite/src/plugins/dev_server.ts +++ b/packages/plugin-vite/src/plugins/dev_server.ts @@ -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 { diff --git a/packages/plugin-vite/tests/build_test.ts b/packages/plugin-vite/tests/build_test.ts index 86feb781..73c650fb 100644 --- a/packages/plugin-vite/tests/build_test.ts +++ b/packages/plugin-vite/tests/build_test.ts @@ -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("

ok

"); + + const resWithSpace = await fetch(`${address}/test%20%2520encodeUri`); + const textWithSpace = await resWithSpace.text(); + expect(textWithSpace).toContain("

ok

"); }, ); }, diff --git a/packages/plugin-vite/tests/dev_server_test.ts b/packages/plugin-vite/tests/dev_server_test.ts index b1baefcf..e55e4f73 100644 --- a/packages/plugin-vite/tests/dev_server_test.ts +++ b/packages/plugin-vite/tests/dev_server_test.ts @@ -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("

ok

"); + + const resWithSpace = await fetch( + `${demoServer.address()}/test%20%2520encodeUri/`, + ); + const textWithSpace = await resWithSpace.text(); + expect(textWithSpace).toContain("

ok

"); }, sanitizeOps: false, sanitizeResources: false,