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

Allow hosts redirects from hosts Rails configuration #45680

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Kevinrob
Copy link
Contributor

Summary

We use Rails with multiple domains (and subdomains). All these domains are configured with config.hosts.
Sometimes we have dynamic redirect from one domain to an other.
And ActionController::Redirecting::UnsafeRedirectError is raised.

It would be great to allow redirections to domains that the app accept in config.hosts.
This should not be considered as unsafe.

Other Information

@rails-bot rails-bot bot added the actionpack label Jul 28, 2022
@@ -11,6 +11,7 @@ class UnsafeRedirectError < StandardError; end

included do
mattr_accessor :raise_on_open_redirects, default: false
mattr_accessor :redirect_hosts_allowed, default: []
Copy link
Member

Choose a reason for hiding this comment

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

As this accessor is a collection of hosts, what do you think about renaming the accessor?

Suggested change
mattr_accessor :redirect_hosts_allowed, default: []
mattr_accessor :allowed_redirect_hosts, default: []

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it's better! I changed that 👍🏻

@Kevinrob Kevinrob force-pushed the allow_redirect_from_configuration_hosts branch from f8179f5 to edbaad2 Compare August 6, 2022 07:46
@Kevinrob Kevinrob requested a review from p8 August 6, 2022 08:10
@Kevinrob
Copy link
Contributor Author

Kevinrob commented Nov 4, 2022

@p8 Is there anything else I can do about this PR?

@fiestacasey
Copy link

I have a similar setup and in particular one domain is in charge of authentication and redirecting to the other domain after auth. This is further complicated because the redirect is within Devise and allow_other_host isn't controllable without monkey patching either Rails or Devise methods. I'm sure this is the case within a lot of different gems that call redirect_to internally so it would be great to be able to define allowed_redirect_hosts in this way.

@Kevinrob Kevinrob force-pushed the allow_redirect_from_configuration_hosts branch from 82c5bd3 to f2ed371 Compare February 7, 2023 14:43
@@ -1,3 +1,7 @@
* Allow hosts redirects from `hosts` Rails configuration
Copy link
Member

Choose a reason for hiding this comment

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

I think providing an example in the changelog would be nice

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What example would you like to view here?
This PR only fixes the raise of UnsafeRedirectError when redirecting to an allowed hosts from config.hosts.

@@ -198,6 +199,7 @@ def _url_host_allowed?(url)
host = URI(url.to_s).host

return true if host == request.host
return true if ActionDispatch::HostAuthorization::Permissions.new(allowed_redirect_hosts).allows?(host)
Copy link
Member

Choose a reason for hiding this comment

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

This looks like an anti-pattern to me, like the list of allowed hosts should be known before we get here or evaluated lazily. Might just be me though 🤔

@tom-lord
Copy link
Contributor

Any update on this feature?

We've run into the same issue, where we frequently want to redirect between two subdomains. Currently the only option is to disable this security feature and verify manually that the URL is safe.

Additionally, what's odd is that the url_from helper automatically considers other subdomains "safe" meanwhile UnsafeRedirectError will still be raised when redirecting to another subdomain.

@owst
Copy link
Contributor

owst commented Jan 19, 2024

Hi @Kevinrob, thanks for getting started with this PR, would you be able to get it across the line? There are a couple of sensible comments from @zzak and a build failure - hopefully they are both straightforward to address?

We'd like to see this added to Rails as we are also hitting an unsafe redirect during our authentication flow in the same way as apokalipto/devise_saml_authenticatable#237 - adding our identity provider's hostname to allowed_redirect_hosts would be a preferred fix. There is some discussion (but no conclusion) on a Devise issue (heartcombo/devise#5462) and it seems quite a few people are similarly affected, with a handful of alternative approaches/monkey patches being proposed - it would be great if there was an official Rails fix.

@Kevinrob Kevinrob force-pushed the allow_redirect_from_configuration_hosts branch from 5280793 to a3e1537 Compare February 23, 2024 10:01
@Kevinrob
Copy link
Contributor Author

Hi @Kevinrob, thanks for getting started with this PR, would you be able to get it across the line? There are a couple of sensible comments from @zzak and a build failure - hopefully they are both straightforward to address?

We'd like to see this added to Rails as we are also hitting an unsafe redirect during our authentication flow in the same way as apokalipto/devise_saml_authenticatable#237 - adding our identity provider's hostname to allowed_redirect_hosts would be a preferred fix. There is some discussion (but no conclusion) on a Devise issue (heartcombo/devise#5462) and it seems quite a few people are similarly affected, with a handful of alternative approaches/monkey patches being proposed - it would be great if there was an official Rails fix.

Hi @owst, I just rebased the PR against main and the tests is 🟢 (except one strange fail that I thing is not related).
I don't know how to bring more visibility to this PR.

@Kevinrob Kevinrob requested a review from zzak May 22, 2024 11:09
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.

None yet

6 participants