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 { tracer } from "../otel.ts";
|
||||||
import { getBuildCache } from "../context.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.
|
* Fresh middleware to serve static files from the `static/` directory.
|
||||||
* ```ts
|
* ```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
|
// Fast path bail out
|
||||||
const startTime = performance.now() + performance.timeOrigin;
|
const startTime = performance.now() + performance.timeOrigin;
|
||||||
const file = await buildCache.readFile(pathname);
|
const file = await buildCache.readFile(pathname);
|
||||||
|
|||||||
@@ -296,3 +296,29 @@ Deno.test("static files - fallthrough", async () => {
|
|||||||
expect(text).toEqual("it works");
|
expect(text).toEqual("it works");
|
||||||
expect(called).toEqual(["before", "after"]);
|
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);
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user