From 016aaa665f25c170d067e2f8acf7d719b258f495 Mon Sep 17 00:00:00 2001 From: Dorseuil Nicolas Date: Thu, 1 Feb 2024 21:54:54 +0100 Subject: [PATCH 1/2] fix trailing slash redirect to external domain --- .../open-next/src/adapters/routing/matcher.ts | 20 ++++++++++++++----- .../tests/appRouter/trailing.test.ts | 5 +++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/packages/open-next/src/adapters/routing/matcher.ts b/packages/open-next/src/adapters/routing/matcher.ts index d77e47b5b..5982214b3 100644 --- a/packages/open-next/src/adapters/routing/matcher.ts +++ b/packages/open-next/src/adapters/routing/matcher.ts @@ -169,11 +169,13 @@ export function handleRewrites( }; } -export function handleRedirects( - event: InternalEvent, - redirects: RedirectDefinition[], -): InternalResult | undefined { +function handleTrailingSlashRedirect(event: InternalEvent) { if (!NextConfig.skipTrailingSlashRedirect) { + const url = new URL(event.url, "http://localhost"); + // Someone is trying to redirect to a different origin, let's not do that + if (url.host !== "localhost") { + return false; + } if ( NextConfig.trailingSlash && !event.headers["x-nextjs-data"] && @@ -211,7 +213,15 @@ export function handleRedirects( isBase64Encoded: false, }; } - } + } else return false; +} + +export function handleRedirects( + event: InternalEvent, + redirects: RedirectDefinition[], +): InternalResult | undefined { + const trailingSlashRedirect = handleTrailingSlashRedirect(event); + if (trailingSlashRedirect) return trailingSlashRedirect; const { internalEvent, __rewrite } = handleRewrites( event, redirects.filter((r) => !r.internal), diff --git a/packages/tests-e2e/tests/appRouter/trailing.test.ts b/packages/tests-e2e/tests/appRouter/trailing.test.ts index aeffc92ed..a64a22610 100644 --- a/packages/tests-e2e/tests/appRouter/trailing.test.ts +++ b/packages/tests-e2e/tests/appRouter/trailing.test.ts @@ -15,3 +15,8 @@ test("trailingSlash redirect with search parameters", async ({ page }) => { ); expect(response?.request().url()).toMatch(/\/ssr\?happy=true$/); }); + +test("trailingSlash redirect to external domain", async ({ page, baseURL }) => { + const response = await page.goto(`${baseURL}//sst.dev/`); + expect(response?.status()).toBe(404); +}); From 72766a81b157fafbdff29fe4d616aab09aeefee8 Mon Sep 17 00:00:00 2001 From: conico974 Date: Thu, 1 Feb 2024 23:58:27 +0100 Subject: [PATCH 2/2] changeset --- .changeset/tame-hotels-tell.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/tame-hotels-tell.md diff --git a/.changeset/tame-hotels-tell.md b/.changeset/tame-hotels-tell.md new file mode 100644 index 000000000..b966b6f51 --- /dev/null +++ b/.changeset/tame-hotels-tell.md @@ -0,0 +1,6 @@ +--- +"open-next": patch +"tests-e2e": patch +--- + +Fix trailing slash redirect to external domain