Skip to content

fix: Ensure Location header is properly encoded in redirects #871

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/proud-dogs-punch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/aws": patch
---

fix: Ensure Location header is properly encoded in redirects happening from next config
13 changes: 13 additions & 0 deletions examples/app-router/app/config-redirect/dest/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export default async function Page({
searchParams,
}: {
searchParams: Promise<{ [key: string]: string | string[] | undefined }>;
}) {
const q = (await searchParams).q;

return (
<>
<div data-testid="searchParams">q: {q}</div>
</>
);
}
12 changes: 12 additions & 0 deletions examples/app-router/app/config-redirect/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@ export default function RedirectDestination() {
<div>
<h1>I was redirected from next.config.js</h1>
<p>/next-config-redirect =&gt; /config-redirect</p>
<a
data-testid="redirect-link"
href="/next-config-redirect-encoding?q=äöå€"
>
/next-config-redirect-encoding?q=äöå€
</a>
<a
data-testid="redirect-link-already-encoded"
href="/next-config-redirect-encoding?q=%C3%A4%C3%B6%C3%A5%E2%82%AC"
>
/next-config-redirect-encoding?q=%C3%A4%C3%B6%C3%A5%E2%82%AC
</a>
</div>
);
}
12 changes: 5 additions & 7 deletions examples/app-router/next.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@ const nextConfig: NextConfig = {
transpilePackages: ["@example/shared"],
output: "standalone",
// outputFileTracingRoot: "../sst",
eslint: {
ignoreDuringBuilds: true,
},
//TODO: remove this when i'll figure out why it fails locally
typescript: {
ignoreBuildErrors: true,
},
images: {
remotePatterns: [
{
Expand Down Expand Up @@ -60,6 +53,11 @@ const nextConfig: NextConfig = {
basePath: false,
locale: false,
},
{
source: "/next-config-redirect-encoding",
destination: "/config-redirect/dest",
permanent: false,
},
];
},
async headers() {
Expand Down
5 changes: 5 additions & 0 deletions packages/open-next/src/core/routingHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ export default async function routingHandler(

const redirect = handleRedirects(internalEvent, RoutesManifest.redirects);
if (redirect) {
// We need to encode the value in the Location header to make sure it is valid according to RFC
// https://stackoverflow.com/a/7654605/16587222
redirect.headers.Location = new URL(
redirect.headers.Location as string,
).href;
debug("redirect", redirect);
return redirect;
}
Expand Down
42 changes: 42 additions & 0 deletions packages/tests-e2e/tests/appRouter/config.redirect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,46 @@ test.describe("Next Config Redirect", () => {
});
await expect(el).toBeVisible();
});
test("Should properly encode the Location header for redirects with query params", async ({
page,
baseURL,
}) => {
await page.goto("/config-redirect");
const responsePromise = page.waitForResponse((response) => {
return response.status() === 307;
});
page.getByTestId("redirect-link").click();
const res = await responsePromise;
await page.waitForURL("/config-redirect/dest?q=äöå€");

const locationHeader = res.headers().location;
expect(locationHeader).toBe(
`${baseURL}/config-redirect/dest?q=%C3%A4%C3%B6%C3%A5%E2%82%AC`,
);
expect(res.status()).toBe(307);

const searchParams = page.getByTestId("searchParams");
await expect(searchParams).toHaveText("q: äöå€");
});
test("Should respect already encoded query params", async ({
page,
baseURL,
}) => {
await page.goto("/config-redirect");
const responsePromise = page.waitForResponse((response) => {
return response.status() === 307;
});
page.getByTestId("redirect-link-already-encoded").click();
const res = await responsePromise;
await page.waitForURL("/config-redirect/dest?q=äöå€");

const locationHeader = res.headers().location;
expect(locationHeader).toBe(
`${baseURL}/config-redirect/dest?q=%C3%A4%C3%B6%C3%A5%E2%82%AC`,
);
expect(res.status()).toBe(307);

const searchParams = page.getByTestId("searchParams");
await expect(searchParams).toHaveText("q: äöå€");
});
});
Loading