Skip to content

Commit c80f1be

Browse files
authored
Fix trailing slash redirect to external domain (#358)
* fix trailing slash redirect to external domain * changeset
1 parent 186e28f commit c80f1be

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

Diff for: .changeset/tame-hotels-tell.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"open-next": patch
3+
"tests-e2e": patch
4+
---
5+
6+
Fix trailing slash redirect to external domain

Diff for: packages/open-next/src/adapters/routing/matcher.ts

+15-5
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,13 @@ export function handleRewrites<T extends RewriteDefinition>(
169169
};
170170
}
171171

172-
export function handleRedirects(
173-
event: InternalEvent,
174-
redirects: RedirectDefinition[],
175-
): InternalResult | undefined {
172+
function handleTrailingSlashRedirect(event: InternalEvent) {
176173
if (!NextConfig.skipTrailingSlashRedirect) {
174+
const url = new URL(event.url, "http://localhost");
175+
// Someone is trying to redirect to a different origin, let's not do that
176+
if (url.host !== "localhost") {
177+
return false;
178+
}
177179
if (
178180
NextConfig.trailingSlash &&
179181
!event.headers["x-nextjs-data"] &&
@@ -211,7 +213,15 @@ export function handleRedirects(
211213
isBase64Encoded: false,
212214
};
213215
}
214-
}
216+
} else return false;
217+
}
218+
219+
export function handleRedirects(
220+
event: InternalEvent,
221+
redirects: RedirectDefinition[],
222+
): InternalResult | undefined {
223+
const trailingSlashRedirect = handleTrailingSlashRedirect(event);
224+
if (trailingSlashRedirect) return trailingSlashRedirect;
215225
const { internalEvent, __rewrite } = handleRewrites(
216226
event,
217227
redirects.filter((r) => !r.internal),

Diff for: packages/tests-e2e/tests/appRouter/trailing.test.ts

+5
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,8 @@ test("trailingSlash redirect with search parameters", async ({ page }) => {
1515
);
1616
expect(response?.request().url()).toMatch(/\/ssr\?happy=true$/);
1717
});
18+
19+
test("trailingSlash redirect to external domain", async ({ page, baseURL }) => {
20+
const response = await page.goto(`${baseURL}//sst.dev/`);
21+
expect(response?.status()).toBe(404);
22+
});

0 commit comments

Comments
 (0)