Skip to content
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

Only check for differing origin on absolute URL redirects #10033

Merged
merged 1 commit into from
Feb 2, 2023
Merged
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/thirty-frogs-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/router": patch
---

Only check for differing origin on absolute URL redirects
14 changes: 9 additions & 5 deletions packages/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,8 @@ export const IDLE_BLOCKER: BlockerUnblocked = {
location: undefined,
};

const ABSOLUTE_URL_REGEX = /^(?:[a-z][a-z0-9+.-]*:|\/\/)/i;

const isBrowser =
typeof window !== "undefined" &&
typeof window.document !== "undefined" &&
Expand Down Expand Up @@ -1915,8 +1917,12 @@ export function createRouter(init: RouterInit): Router {
"Expected a location on the redirect navigation"
);

// Check if this an external redirect that goes to a new origin
if (isBrowser && typeof window?.location !== "undefined") {
// Check if this an absolute external redirect that goes to a new origin
if (
ABSOLUTE_URL_REGEX.test(redirect.location) &&
isBrowser &&
typeof window?.location !== "undefined"
) {
Copy link
Contributor Author

@brophdawg11 brophdawg11 Feb 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a bit too loose - we only need to bother looking for a different origin if the redirect was an absolute URL.

let newOrigin = init.history.createURL(redirect.location).origin;
if (window.location.origin !== newOrigin) {
if (replace) {
Expand Down Expand Up @@ -3093,10 +3099,8 @@ async function callLoaderOrAction(
"Redirects returned/thrown from loaders/actions must have a Location header"
);

let isAbsolute = /^(?:[a-z][a-z0-9+.-]*:|\/\/)/i.test(location);

// Support relative routing in internal redirects
if (!isAbsolute) {
if (!ABSOLUTE_URL_REGEX.test(location)) {
let activeMatches = matches.slice(0, matches.indexOf(match) + 1);
let routePathnames = getPathContributingMatches(activeMatches).map(
(match) => match.pathnameBase
Expand Down