diff --git a/std/http/file_server.ts b/std/http/file_server.ts index acf272764d5c48..d71b9ad533b9b7 100755 --- a/std/http/file_server.ts +++ b/std/http/file_server.ts @@ -301,9 +301,15 @@ function html(strings: TemplateStringsArray, ...values: unknown[]): string { listenAndServe( addr, async (req): Promise => { - const normalizedUrl = posix.normalize(req.url); - const decodedUrl = decodeURIComponent(normalizedUrl); - const fsPath = posix.join(target, decodedUrl); + let normalizedUrl = posix.normalize(req.url); + try { + normalizedUrl = decodeURIComponent(normalizedUrl); + } catch (e) { + if (!(e instanceof URIError)) { + throw e; + } + } + const fsPath = posix.join(target, normalizedUrl); let response: Response; try { diff --git a/std/http/file_server_test.ts b/std/http/file_server_test.ts index ba625b7c817e00..0329168e785aa0 100644 --- a/std/http/file_server_test.ts +++ b/std/http/file_server_test.ts @@ -83,12 +83,15 @@ test(async function serveFallback(): Promise { } }); -test(async function serveFallback(): Promise { +test(async function serveWithUnorthodoxFilename(): Promise { await startFileServer(); try { - const res = await fetch( - "http://localhost:4500/http/testdata/test%20file.txt" - ); + let res = await fetch("http://localhost:4500/http/testdata/%"); + assert(res.headers.has("access-control-allow-origin")); + assert(res.headers.has("access-control-allow-headers")); + assertEquals(res.status, 200); + + res = await fetch("http://localhost:4500/http/testdata/test%20file.txt"); assert(res.headers.has("access-control-allow-origin")); assert(res.headers.has("access-control-allow-headers")); assertEquals(res.status, 200); diff --git a/std/http/testdata/% b/std/http/testdata/% new file mode 100644 index 00000000000000..e69de29bb2d1d6