Skip to content

Commit

Permalink
Fix absolute redirect detection (#9689)
Browse files Browse the repository at this point in the history
* Fix absolute redirect detection

* Use createClientSideUrl

* inline origin

* Add changeset
  • Loading branch information
brophdawg11 authored Dec 6, 2022
1 parent 51e65db commit bd58142
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/swift-lemons-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/router": patch
---

Fix URL creation in Remix integration tests
32 changes: 14 additions & 18 deletions packages/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1606,17 +1606,17 @@ export function createRouter(init: RouterInit): Router {
"Expected a location on the redirect navigation"
);

if (
redirect.external &&
typeof window !== "undefined" &&
typeof window.location !== "undefined"
) {
if (replace) {
window.location.replace(redirect.location);
} else {
window.location.assign(redirect.location);
// Check if this an external redirect that goes to a new origin
if (typeof window?.location !== "undefined") {
let newOrigin = createClientSideURL(redirect.location).origin;
if (window.location.origin !== newOrigin) {
if (replace) {
window.location.replace(redirect.location);
} else {
window.location.assign(redirect.location);
}
return;
}
return;
}

// There's no need to abort on redirects, since we don't detect the
Expand Down Expand Up @@ -2627,22 +2627,19 @@ async function callLoaderOrAction(
"Redirects returned/thrown from loaders/actions must have a Location header"
);

// Check if this an external redirect that goes to a new origin
let currentUrl = new URL(request.url);
let currentOrigin = currentUrl.origin;
let newOrigin = new URL(location, currentOrigin).origin;
let external = newOrigin !== currentOrigin;
let isAbsolute =
/^[a-z+]+:\/\//i.test(location) || location.startsWith("//");

// Support relative routing in internal redirects
if (!external) {
if (!isAbsolute) {
let activeMatches = matches.slice(0, matches.indexOf(match) + 1);
let routePathnames = getPathContributingMatches(activeMatches).map(
(match) => match.pathnameBase
);
let resolvedLocation = resolveTo(
location,
routePathnames,
currentUrl.pathname
new URL(request.url).pathname
);
invariant(
createPath(resolvedLocation),
Expand Down Expand Up @@ -2673,7 +2670,6 @@ async function callLoaderOrAction(
status,
location,
revalidate: result.headers.get("X-Remix-Revalidate") !== null,
external,
};
}

Expand Down
1 change: 0 additions & 1 deletion packages/router/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export interface RedirectResult {
status: number;
location: string;
revalidate: boolean;
external: boolean;
}

/**
Expand Down

0 comments on commit bd58142

Please sign in to comment.