Skip to content

Commit

Permalink
Only check for differing origin on absolute URL redirects (#10033)
Browse files Browse the repository at this point in the history
  • Loading branch information
brophdawg11 authored Feb 2, 2023
1 parent 80832fb commit 1a16a50
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
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"
) {
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

0 comments on commit 1a16a50

Please sign in to comment.