-
-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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: properly handle external redirects #9590
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@remix-run/router": patch | ||
--- | ||
|
||
properly handle redirects to external domains |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1582,6 +1582,16 @@ export function createRouter(init: RouterInit): Router { | |
navigation.location, | ||
"Expected a location on the redirect navigation" | ||
); | ||
|
||
if ( | ||
redirect.external && | ||
typeof window !== "undefined" && | ||
typeof window.location !== "undefined" | ||
) { | ||
window.location.replace(redirect.location); | ||
return; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just navigate directly for external redirects instead of "starting" a new router navigation |
||
|
||
// There's no need to abort on redirects, since we don't detect the | ||
// redirect until the action/loaders have settled | ||
pendingNavigationController = null; | ||
|
@@ -2161,7 +2171,7 @@ export function unstable_createStaticHandler( | |
|
||
// Short circuit if we have no loaders to run (queryRoute()) | ||
if (isRouteRequest && !routeMatch?.route.loader) { | ||
throw getInternalRouterError(405, { | ||
throw getInternalRouterError(400, { | ||
method: request.method, | ||
pathname: createURL(request.url).pathname, | ||
routeId: routeMatch?.route.id, | ||
|
@@ -2554,26 +2564,31 @@ async function callLoaderOrAction( | |
"Redirects returned/thrown from loaders/actions must have a Location header" | ||
); | ||
|
||
// Support relative routing in redirects | ||
let activeMatches = matches.slice(0, matches.indexOf(match) + 1); | ||
let routePathnames = getPathContributingMatches(activeMatches).map( | ||
(match) => match.pathnameBase | ||
); | ||
let requestPath = createURL(request.url).pathname; | ||
let resolvedLocation = resolveTo(location, routePathnames, requestPath); | ||
invariant( | ||
createPath(resolvedLocation), | ||
`Unable to resolve redirect location: ${result.headers.get("Location")}` | ||
); | ||
// Check if this an external redirect that goes to a new origin | ||
let external = createURL(location).origin !== createURL("/").origin; | ||
|
||
// Prepend the basename to the redirect location if we have one | ||
if (basename) { | ||
let path = resolvedLocation.pathname; | ||
resolvedLocation.pathname = | ||
path === "/" ? basename : joinPaths([basename, path]); | ||
} | ||
// Support relative routing in internal redirects | ||
if (!external) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't do any post-processing of the redirect location if it's an external location There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm in the middle of migrating parts of a PHP website to remix, using nginx to route some urls to remix. In my very particular case, if I Would testing for a scheme pattern (regex, or a loose check on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, good call. Is that something you can do in Remix today? Or does it have this same issue? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we go with a manual check, I think we'd need to check for both protocol-less URLs ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remix has the same issue today. I'm not sure we want to support protocol-less urls, as far as I know, those are only meant to apply the current scheme in HTML. MDN says relative or absolute, and RFC 9110 seems to require a Also, if my There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok yeah I figured it should since I stole the approach from there ;) I haven't dug into the spec, but was more considering this from a browser-emulator perspective. I did some quick testing and protocol-less URLs work in both of the redirect mechanisms at play here - neither of which are truly "handled" by RR/Remix - we're just handing off a location provided by the user to a built-in browser mechanism. Document redirects - returning a 301 with So I don't think we need to implement the logic on how to handle the redirect, so much as decide when to hand off the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @machour Do you mind creating a separate proposal for "same domain hard redirects"? I'm going to merge this since we need this external redirect handling in the RRR work - but I don't want to lose sight of that potential enhancement There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in #9634 |
||
let activeMatches = matches.slice(0, matches.indexOf(match) + 1); | ||
let routePathnames = getPathContributingMatches(activeMatches).map( | ||
(match) => match.pathnameBase | ||
); | ||
let requestPath = createURL(request.url).pathname; | ||
let resolvedLocation = resolveTo(location, routePathnames, requestPath); | ||
invariant( | ||
createPath(resolvedLocation), | ||
`Unable to resolve redirect location: ${location}` | ||
); | ||
|
||
location = createPath(resolvedLocation); | ||
// Prepend the basename to the redirect location if we have one | ||
if (basename) { | ||
let path = resolvedLocation.pathname; | ||
resolvedLocation.pathname = | ||
path === "/" ? basename : joinPaths([basename, path]); | ||
} | ||
|
||
location = createPath(resolvedLocation); | ||
} | ||
|
||
// Don't process redirects in the router during static requests requests. | ||
// Instead, throw the Response and let the server handle it with an HTTP | ||
|
@@ -2589,6 +2604,7 @@ async function callLoaderOrAction( | |
status, | ||
location, | ||
revalidate: result.headers.get("X-Remix-Revalidate") !== null, | ||
external, | ||
}; | ||
} | ||
|
||
|
@@ -2897,7 +2913,14 @@ function getInternalRouterError( | |
|
||
if (status === 400) { | ||
statusText = "Bad Request"; | ||
errorMessage = "Cannot submit binary form data using GET"; | ||
if (method && pathname && routeId) { | ||
errorMessage = | ||
`You made a ${method} request to "${pathname}" but ` + | ||
`did not provide a \`loader\` for route "${routeId}", ` + | ||
`so there is no way to handle the request.`; | ||
} else { | ||
errorMessage = "Cannot submit binary form data using GET"; | ||
} | ||
} else if (status === 403) { | ||
statusText = "Forbidden"; | ||
errorMessage = `Route "${routeId}" does not match URL "${pathname}"`; | ||
|
@@ -2907,17 +2930,10 @@ function getInternalRouterError( | |
} else if (status === 405) { | ||
statusText = "Method Not Allowed"; | ||
if (method && pathname && routeId) { | ||
if (validActionMethods.has(method)) { | ||
errorMessage = | ||
`You made a ${method} request to "${pathname}" but ` + | ||
`did not provide an \`action\` for route "${routeId}", ` + | ||
`so there is no way to handle the request.`; | ||
} else { | ||
errorMessage = | ||
`You made a ${method} request to "${pathname}" but ` + | ||
`did not provide a \`loader\` for route "${routeId}", ` + | ||
`so there is no way to handle the request.`; | ||
} | ||
errorMessage = | ||
`You made a ${method} request to "${pathname}" but ` + | ||
`did not provide an \`action\` for route "${routeId}", ` + | ||
`so there is no way to handle the request.`; | ||
} else { | ||
errorMessage = `Invalid request method "${method}"`; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These didn't need to be fetches, that was just as copy/paste from a prior test