Skip to content

Commit

Permalink
Add redirect_to
Browse files Browse the repository at this point in the history
  • Loading branch information
blittle committed Nov 21, 2022
1 parent 1df63c7 commit 3eac09e
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions packages/hydrogen-remix/src/routing/redirect.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {redirect} from '@remix-run/oxygen';
import type {HydrogenContext} from '@shopify/hydrogen';

export async function notFoundMaybeRedirect(
Expand All @@ -23,10 +24,31 @@ export async function notFoundMaybeRedirect(
},
});
} else {
const redirectPath = new URLSearchParams(search).get('redirect_to');

if (redirectPath && isLocalPath(redirectPath)) {
return redirect(redirectPath);
}

return new Response('Not found', {status: 404});
}
}

function isLocalPath(url: string) {
try {
// We don't want to redirect cross domain,
// doing so could create fishing vulnerability
// If `new URL()` succeeds, it's a fully qualified
// url which is cross domain. If it fails, it's just
// a path, which will be the current domain.
new URL(url);
} catch (e) {
return true;
}

return false;
}

const REDIRECT_QUERY = `#graphql
query redirects($url:String) {
urlRedirects(first:1, query:$url) {
Expand Down

0 comments on commit 3eac09e

Please sign in to comment.