Skip to content

Commit

Permalink
fix trailing slash redirect to external domain
Browse files Browse the repository at this point in the history
  • Loading branch information
conico974 committed Feb 1, 2024
1 parent afd9605 commit 016aaa6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
20 changes: 15 additions & 5 deletions packages/open-next/src/adapters/routing/matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,13 @@ export function handleRewrites<T extends RewriteDefinition>(
};
}

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"] &&
Expand Down Expand Up @@ -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),
Expand Down
5 changes: 5 additions & 0 deletions packages/tests-e2e/tests/appRouter/trailing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

0 comments on commit 016aaa6

Please sign in to comment.