From 92019498f6361c31ad24decfc14e81660959f6cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=A8=E6=9D=89?= Date: Wed, 12 Feb 2020 04:53:09 +0800 Subject: [PATCH] fix(file_server): don't crash on "%" pathname (#3953) --- std/http/file_server.ts | 12 +++++++++--- std/http/file_server_test.ts | 11 +++++++---- std/http/testdata/% | 0 3 files changed, 16 insertions(+), 7 deletions(-) create mode 100644 std/http/testdata/% 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