diff --git a/src/server/context.ts b/src/server/context.ts index f76fc97a..8bdcee4c 100644 --- a/src/server/context.ts +++ b/src/server/context.ts @@ -187,6 +187,11 @@ export class ServerContext { connInfo: ServeHandlerInfo = DEFAULT_CONN_INFO, ) { const url = new URL(req.url); + // Syntactically having double slashes in the pathname is valid per + // spec, but there is no behavior defined for that. Practically all + // servers normalize the pathname of a URL to not include double + // forward slashes. + url.pathname = url.pathname.replaceAll(/\/+/g, "/"); const aliveUrl = basePath + ALIVE_URL; diff --git a/tests/main_test.ts b/tests/main_test.ts index 252ac46b..259b8669 100644 --- a/tests/main_test.ts +++ b/tests/main_test.ts @@ -232,7 +232,7 @@ Deno.test("redirect /pages/fresh/ to /pages/fresh", async () => { ); }); -Deno.test("redirect /pages/////fresh///// to /pages/////fresh", async () => { +Deno.test("redirect /pages/////fresh///// to /pages/fresh", async () => { const resp = await handler( new Request("https://fresh.deno.dev/pages/////fresh/////"), ); @@ -240,11 +240,11 @@ Deno.test("redirect /pages/////fresh///// to /pages/////fresh", async () => { assertEquals(resp.status, STATUS_CODE.TemporaryRedirect); assertEquals( resp.headers.get("location"), - "/pages/////fresh", + "/pages/fresh", ); }); -Deno.test("redirect /pages/////fresh/ to /pages/////fresh", async () => { +Deno.test("redirect /pages/////fresh/ to /pages/fresh", async () => { const resp = await handler( new Request("https://fresh.deno.dev/pages/////fresh/"), ); @@ -252,7 +252,7 @@ Deno.test("redirect /pages/////fresh/ to /pages/////fresh", async () => { assertEquals(resp.status, STATUS_CODE.TemporaryRedirect); assertEquals( resp.headers.get("location"), - "/pages/////fresh", + "/pages/fresh", ); }); @@ -264,6 +264,15 @@ Deno.test("no redirect for /pages/////fresh", async () => { assertEquals(resp.status, STATUS_CODE.NotFound); }); +Deno.test("no open redirect when passing double slashes", async () => { + const resp = await handler( + new Request("https://fresh.deno.dev//evil.com/"), + ); + assert(resp); + assertEquals(resp.status, STATUS_CODE.TemporaryRedirect); + assertEquals(resp.headers.get("location"), "/evil.com"); +}); + Deno.test("/failure", async () => { const resp = await handler(new Request("https://fresh.deno.dev/failure")); assert(resp);