Skip to content

Commit

Permalink
Fix redirect bug (#1629)
Browse files Browse the repository at this point in the history
  • Loading branch information
wizardlyhel authored Jan 12, 2024
1 parent 357becf commit 810f48c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/blue-beds-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/hydrogen': patch
---

Fix redirect bug
10 changes: 10 additions & 0 deletions packages/hydrogen/src/routing/redirect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,16 @@ describe('storefrontRedirect', () => {
headers: {location: '/some-page?param=https://another.com'},
}),
);

await expect(
storefrontRedirect({
response,
storefront: storefrontMock,
request: new Request(
'http://localhost:3000/missing?return_to=%01http%3A%2F%2Fexample.com',
),
}),
).resolves.toEqual(response);
});
});
});
12 changes: 9 additions & 3 deletions packages/hydrogen/src/routing/redirect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export async function storefrontRedirect(
searchParams.get('return_to') || searchParams.get('redirect');

if (redirectTo) {
if (isLocalPath(redirectTo)) {
if (isLocalPath(request.url, redirectTo)) {
return redirect(redirectTo);
} else {
console.warn(
Expand All @@ -74,12 +74,18 @@ export async function storefrontRedirect(
return response;
}

function isLocalPath(url: string) {
function isLocalPath(requestUrl: string, redirectUrl: string) {
// We don't want to redirect cross domain,
// doing so could create phishing vulnerability
// Test for protocols, e.g. https://, http://, //
// and uris: mailto:, tel:, javascript:, etc.
return !/^(([a-z+-]+:)?\/\/|[a-z+-]+:)/i.test(url.trim());
try {
return (
new URL(requestUrl).origin === new URL(redirectUrl, requestUrl).origin
);
} catch (e) {
return false;
}
}

const REDIRECT_QUERY = `#graphql
Expand Down

0 comments on commit 810f48c

Please sign in to comment.