From aeca15eeb9f298e003fa6514fbc1e491dfbfe631 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Wed, 14 Feb 2024 15:25:16 +0100 Subject: [PATCH] fix: open redirect issue with trailing slash detection (#2313) Passing a path like `/foo/bar` as the location header is interpreted as a relative path. But when it contains two leading slashes like `//evil.com/` it is interpreted as a port relative url. This has the consequence that the origin of a URL can essentially be replaced: ```ts new URL("//evil.com", "https://example.com/"); // -> 'https://evil.com/' ``` Our trailing slash detection logic didn't guard against this case. By normalizing the pathname of the URL and stripping sibling forward slashes we sidestep this problem. --- src/server/context.ts | 5 +++++ tests/main_test.ts | 17 +++++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) 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);