fix: handle commas in static file paths (#3770)
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 <biwanczuk@gmail.com>
This commit is contained in:
co-authored by
Bartek Iwańczuk
parent
ecc42850b9
commit
49c4be1ac6
@@ -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<T>(): Middleware<T> {
|
||||
: "/";
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user