From 49c4be1ac60603174bad1c6e3c13bd88602c51bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien?= <78741537+SebF33@users.noreply.github.com> Date: Wed, 22 Apr 2026 10:24:48 +0200 Subject: [PATCH] fix: handle commas in static file paths (#3770) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit After #3659, static file handling correctly supports spaces and encoded paths, but filenames containing commas are not resolved. Example: - File: `foo,bar.css` - Stored in cache as: `/foo%2Cbar.css` - Request: `/foo,bar.css` --------- Co-authored-by: Bartek IwaƄczuk --- .../fresh/src/middlewares/static_files.ts | 15 +++++++++++ .../src/middlewares/static_files_test.ts | 26 +++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/packages/fresh/src/middlewares/static_files.ts b/packages/fresh/src/middlewares/static_files.ts index ee06b0f7..288f9a98 100644 --- a/packages/fresh/src/middlewares/static_files.ts +++ b/packages/fresh/src/middlewares/static_files.ts @@ -4,6 +4,14 @@ import { BUILD_ID } from "@fresh/build-id"; import { tracer } from "../otel.ts"; import { getBuildCache } from "../context.ts"; +/** Decode and re-encode each path segment so that characters like commas + * are percent-encoded consistently with how `prepareStaticFile` stores + * entries in the build cache. */ +function normalizePathname(pathname: string): string { + return "/" + + pathname.split("/").filter(Boolean).map(encodeURIComponent).join("/"); +} + /** * Fresh middleware to serve static files from the `static/` directory. * ```ts @@ -25,6 +33,13 @@ export function staticFiles(): Middleware { : "/"; } + try { + pathname = normalizePathname(decodeURIComponent(pathname)); + } catch (_e: unknown) { + if (!(_e instanceof URIError)) throw _e; + return await ctx.next(); + } + // Fast path bail out const startTime = performance.now() + performance.timeOrigin; const file = await buildCache.readFile(pathname); diff --git a/packages/fresh/src/middlewares/static_files_test.ts b/packages/fresh/src/middlewares/static_files_test.ts index 3ba5daac..3c0b311c 100644 --- a/packages/fresh/src/middlewares/static_files_test.ts +++ b/packages/fresh/src/middlewares/static_files_test.ts @@ -296,3 +296,29 @@ Deno.test("static files - fallthrough", async () => { expect(text).toEqual("it works"); expect(called).toEqual(["before", "after"]); }); + +Deno.test("static files - comma in pathname", async () => { + const key = systemPathToUrlEncoded("foo,bar.css"); + + const buildCache = new MockBuildCache({ + [key]: { + content: "body {}", + hash: null, + }, + }); + + const server = serveMiddleware( + staticFiles(), + { buildCache }, + ); + + // Browser/request path usually keeps the comma unencoded + let res = await server.get("/foo,bar.css"); + await res.body?.cancel(); + expect(res.status).toEqual(200); + + // Encoded form should also resolve + res = await server.get("/foo%2Cbar.css"); + await res.body?.cancel(); + expect(res.status).toEqual(200); +});