Skip to content

Commit 98c7dce

Browse files
committed
fix: handle commas in static file paths
1 parent 36dd637 commit 98c7dce

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

packages/fresh/src/middlewares/static_files.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ASSET_CACHE_BUST_KEY } from "../constants.ts";
33
import { BUILD_ID } from "@fresh/build-id";
44
import { tracer } from "../otel.ts";
55
import { getBuildCache } from "../context.ts";
6+
import { systemPathToUrlEncoded } from "../dev/dev_build_cache.ts";
67

78
/**
89
* Fresh middleware to serve static files from the `static/` directory.
@@ -25,6 +26,12 @@ export function staticFiles<T>(): Middleware<T> {
2526
: "/";
2627
}
2728

29+
try {
30+
pathname = systemPathToUrlEncoded(decodeURIComponent(pathname));
31+
} catch {
32+
return await ctx.next();
33+
}
34+
2835
// Fast path bail out
2936
const startTime = performance.now() + performance.timeOrigin;
3037
const file = await buildCache.readFile(pathname);

packages/fresh/src/middlewares/static_files_test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,3 +296,29 @@ Deno.test("static files - fallthrough", async () => {
296296
expect(text).toEqual("it works");
297297
expect(called).toEqual(["before", "after"]);
298298
});
299+
300+
Deno.test("static files - comma in pathname", async () => {
301+
const key = systemPathToUrlEncoded("foo,bar.css");
302+
303+
const buildCache = new MockBuildCache({
304+
[key]: {
305+
content: "body {}",
306+
hash: null,
307+
},
308+
});
309+
310+
const server = serveMiddleware(
311+
staticFiles(),
312+
{ buildCache },
313+
);
314+
315+
// Browser/request path usually keeps the comma unencoded
316+
let res = await server.get("/foo,bar.css");
317+
await res.body?.cancel();
318+
expect(res.status).toEqual(200);
319+
320+
// Encoded form should also resolve
321+
res = await server.get("/foo%2Cbar.css");
322+
await res.body?.cancel();
323+
expect(res.status).toEqual(200);
324+
});

0 commit comments

Comments
 (0)