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

Fix absolute redirect detection #9689

Merged
merged 4 commits into from
Dec 6, 2022

Conversation

brophdawg11
Copy link
Contributor

@brophdawg11 brophdawg11 commented Dec 6, 2022

Ran into issues with our URL creation fix with Remix integration tests using a test://test.com base, so re-thought the approach a bit. We were sort of conflating 2 things - whether a redirect was external and whether it was absolute. In both cases we want to skip the relative redirect logic, but we do not need to check for external on the server since the browser will handle redirecting to the external domain. Instead, we can do a simpler isAbsolute check in shared code (no URL creation required) and limit our isExternal check to client side code where we can more reliably create URLs with window.location.origin

Follow up to #9682
Closes remix-run/remix#4740

@changeset-bot
Copy link

changeset-bot bot commented Dec 6, 2022

🦋 Changeset detected

Latest commit: 55776c4

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 5 packages
Name Type
@remix-run/router Patch
react-router Patch
react-router-dom Patch
react-router-dom-v5-compat Patch
react-router-native Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

packages/router/router.ts Outdated Show resolved Hide resolved
// 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;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was the problem - since the origin for a test://test.com URL used in Remix integration tests is null 😕 , and new URL(path, "null") throws an error.

Screenshot 2022-12-06 at 9 32 36 AM

let newOrigin = new URL(location, currentOrigin).origin;
let external = newOrigin !== currentOrigin;
let isAbsolute =
/^[a-z+]+:\/\//i.test(location) || location.startsWith("//");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Instead we'll just look for the presence of a leading protocol or a protocol-less URL to determine if we can skip relative routing logic

Comment on lines -1609 to -1619
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;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got rid of the redirect.external flag in favor of just doing the external detection here in client-side only code.

@brophdawg11 brophdawg11 merged commit bd58142 into release-next Dec 6, 2022
@brophdawg11 brophdawg11 deleted the brophdawg11/detect-absolute-redirects branch December 6, 2022 16:33
@brophdawg11
Copy link
Contributor Author

This is a follow up to the initial fix for remix-run/remix#4740

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants