From 126f492ea0d168f4fdba76cc214406df8581dbe2 Mon Sep 17 00:00:00 2001 From: Felix <65357346+felix-schindler@users.noreply.github.com> Date: Fri, 6 Sep 2024 10:49:07 +0200 Subject: [PATCH] fix: decode static file paths (#2642) Fixes #2613 --- src/middlewares/static_files.ts | 2 +- src/middlewares/static_files_test.ts | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/middlewares/static_files.ts b/src/middlewares/static_files.ts index 2ae81ba0..eb47aa11 100644 --- a/src/middlewares/static_files.ts +++ b/src/middlewares/static_files.ts @@ -17,7 +17,7 @@ export function staticFiles(): MiddlewareFn { const { req, url, config } = ctx; const buildCache = getBuildCache(ctx); - let pathname = url.pathname; + let pathname = decodeURIComponent(url.pathname); if (config.basePath) { pathname = pathname !== config.basePath ? pathname.slice(config.basePath.length) diff --git a/src/middlewares/static_files_test.ts b/src/middlewares/static_files_test.ts index 5dc04596..d5219e01 100644 --- a/src/middlewares/static_files_test.ts +++ b/src/middlewares/static_files_test.ts @@ -181,3 +181,27 @@ Deno.test("static files - enables caching in production", async () => { "public, max-age=31536000, immutable", ); }); + +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 }, + }); + const server = serveMiddleware( + staticFiles(), + { buildCache }, + ); + + for ( + const path of [ + "C%23.svg", + "%E8%A5%BF%E5%AE%89%E5%B8%82.png", + "%EC%9D%B8%EC%B2%9C.avif", + ] + ) { + const res = await server.get("/" + path); + await res.body?.cancel(); + expect(res.status).toEqual(200); + } +});