Repository navigation
Guard against DNS rebinding attacks by permitting hosts - #33145
Conversation
|
r? @eileencodes (@rails-bot has picked a reviewer for you, use r? to override) |
|
I'm happy to be convinced otherwise, but I doubt it needs to be so far up the middleware stack. X-Forwarded-Host. I do think it'd be nicer to have this far enough down-stack that it can raise an exception... but failing that, I'm inclined to YAGNI Watch the regexp anchoring: either the examples should be full of I see no reason we wouldn't allow all IP addresses by default; they are by definition not subject to DNS rebinding. (We could ask the OS for the machine hostname(s) too... those are technically subject to rebinding, but seem fine to trust by default. Further to that, is there a way, when resolving a name, to know whether it came from Should we do this in all environments by default, or only ones that grant special privileges? (For that matter, must an unknown host = blocked request, or could it just mean that I still keep expecting the browsers to find a fix for this, as it's an attack on their same-origin mechanism, but I do agree it's been a while and isn't looking great so far. cc @rails/security |
I also believe it is the browser issue, but just as it happened with CSRF they turned a blind eye to this moving the responsibility down the stack to web developers. There are no talks about fixing DNS rebinding in the browsers and it is unlikely to be ever fixed in standards. So yes, all websites must read Host header on their own. |
821b7fe to
7533be8
Compare
Agree, will try to move it down the stack and see if the exploit is still happening. My concern was that
Personally, I'd prefer if we don't have exceptions per environments. Hitting this error in testing may remind you to whitelist the hosts for the other environments.
We can easily do this for IPv4 and IPv6 with a wide
I'm not sure about this one, but will research it. |
|
Is there any reason why we are introducing a new middleware by default in rails given this problem only happens when webconsole or similar projects are enabled? Why not implement on the library? In production for example I don't think we need this given most of production environments will run rails with a proxy in front. |
Webconsole is just the one that leads to RCE straight. But even w/o that default rails app still leaks lots of ENV data and just all the info about local rails app. What if the hacker wants to steal your startup idea and leaks your entire :3000 app? Or, remember rails RCE via XML/YAML? It used to be exploitable with DNS rebinding and any machine could have been pwned (luckily very few thought of this), but with this safeguards that would not be the case. So fixing that higher in the stack would just cover more attack scenarios. |
|
@rafaelfranca I think we're best positioned to handle the config to list valid hostnames, so at that point we might was well enforce the rule too. (Per above, I think there are open questions around how much we need to enforce by default, vs just providing the tool... but I think the tool is indeed in our remit.) |
9bd5893 to
1fbb94e
Compare
|
@matthewd raising an error leaves the current vulnerability in place, because |
|
Does this protection makes sense in production? Because right now the host configuration lives only in the proxy level (nginx, apache, heroku dashboard). Now we have to configure in two places and we need to deploy the application in order to change/allow a new host where before we could just change the proxy config. |
|
That's the drawback. 😕 Web Console is the most common source of RCE source, but production can still fall under |
|
This is a huge drawback to me and I don't think Host header attack mitigation requires this drawback. I prefer to only enable this by default in development. Requiring deploy to be able to serve the application is a new host is too much overhead to me and will be a huge pain to support in most deploy platforms that exists. |
|
We can let all requests in if |
|
What would be a good default for production? |
|
@rafaelfranca We can have it's current default added at least to For production, we can put the hostname of the machine that runs |
|
It's best idea to fix it in development only for starters and maybe in the future add an option for production where the bug is significantly less likely. |
24e5a23 to
1d3a9b3
Compare
1d3a9b3 to
244a097
Compare
|
rafaelfranca gave me the heads up on this. i've tested this on my local machine and I believe i've found an issue. so the problem is an attacker would have full control over all headers including 'X-Forwarded-Host'. (Note: For some reason there is a comment up the stack that mentions 'X-Forwarded-Host' ?). Anyway, attacker can just set X-Forwarded-Host to a whitelist host [localhost] and then bypass the protection. I haven't actually tested a full attack so I could be wrong about this. But what I've done is:
This can be done doing:
and I receive:
which is good! However:
and I don't get the blocked page and instead get the web-console stuff This happens because the host is pulled in from rack and rack will look at the X_FORWARDED_HOST header. I've updated http://www.dnsrebinder.net/ to set x-forwarded-host and you should be able to trigger the vulnerability locally assuming your rails app is listening to localhost:3000. |
Yeah, I was -- perhaps just a little too tersely -- noting that same issue. |
8246501 to
bc8eaca
Compare
During the development of rails#33145, I have named a few concepts in the code as `whitelisted`. We decided to stay away from the term and I adjusted most of the code afterwards, but here are the cases I forgot to change. I also found a case in the API guide that we could have cleaned up as well. [ci skip]
Cleanup the whitelisting references after #33145
* master: (103 commits) Take in to account optional arguments when deprecating Improve performance of blank? and present? in an ActiveRecord::Base instance Add a link to where "inquiry" is defined in ActiveSupport docs Update template missing text in Guide Allow configuring the Azure Storage service with extra client options Cookie doesn't expire anymore unless a flag is set: Fix and update template_missing image in guide Cleanup the whitelisting references after #33145 Use consistent hash syntax for routes Pass source to template handler and deprecate old style handler Invalidate query cache for all connections in the current thread Keep cache for strings in notifications/fanout Make Notifications::Fanout#listeners_for faster Introduce a file type template, deprecate `Template#refresh` Add ability to change the names of the default handlers ActiveSupport typo fixes. Refactor options for middleware Railities typo fixes. Fix doc of `ActionDispatch::SystemTestCase` [ci skip] Enable `Lint/ErbNewArguments` cop to avoid the deprecated arguments warning ...
* Add Rails 6 to build. * Whitelist rspec-rails host for Rails 6 Thanks hashrocket <3 https://til.hashrocket.com/posts/vositxjceu-rails-6-blocked-hosts * Fix deprecation warnings in feature specs * Add host to test command mock request https://til.hashrocket.com/posts/vositxjceu-rails-6-blocked-hosts rails/rails#33145 * Add changelog entry * Add Circle config * Add Ruby 2.4 and JRuby to Circle build * Remove Rails 3 condition * Try setting bundler version for jruby * Fix indent
|
Can't believe the day it's fixed has come. 🎆 🍾 |
Guard against DNS rebinding attacks by permitting hosts rails/rails#33145
…uide [ci skip] This commit copies info from rails#33145 ([an excellent changelog](https://github.com/rails/rails/blob/6-0-stable/railties/CHANGELOG.md)) to our guides. Closes rails#36959 Not sure whether there is a need mentioning `config.hosts` in the "Upgrading to Rails 6.0" guide since it is configured to work in the development environment by default and we guide how to deal with "blocked host" issue, see: https://github.com/rails/rails/blob/6-0-stable/actionpack/lib/action_dispatch/middleware/templates/rescues/blocked_host.html.erb I would like to backport this to `6-0-stable` so users will be able to find info about `ActionDispatch::HostAuthorization` middleware and how to configure it. Give credit to @gsamokovarov since I copied the info from the changelog. <3 [bogdanvlviv, Genadi Samokovarov]
Introduce guard against DNS rebinding attacks
The
ActionDispatch::HostAuthorizationis a new middleware that preventagainst DNS rebinding and other
Hostheader attacks. It is included inthe development environment by default with the following configuration:
In other environments
Rails.application.config.hostsis empty and noHostheader checks will be done. If you want to guard against headerattacks on production, you have to manually whitelist the allowed hosts
with:
The host of a request is checked against the
hostsentries with the caseoperator (
#===), which letshostssupport entries of typeRegExp,ProcandIPAddrto name a few. Here is an example with a regexp.A special case is supported that allows you to whitelist all sub-domains:
EDIT:
This is an attack that was brought to me by @homakov a few years ago. You can learn more about it in this [lightning talk](https://www.youtube.com/watch?v=WnlgKWCt8wQ&t=2063) or this [tweet](https://twitter.com/homakov/status/1005098991946641409). The attack works on my computer to this day. It's also universal across languages and frameworks. As an example, here is how [Django](https://docs.djangoproject.com/en/2.0/ref/settings/#allowed-hosts) tackles it.The fix is pretty easy, whitelist the addresses that a request can go to. However,
as simple as the fix is, it will introduce a backwards incompatible change that will
require all apps in Rails 6 to whitelists their public hosts. Currently, I have come
up with this configuration:
The default value of
Rails.application.config.hostsis:The host of a request is checked against the
hostsentries with the caseoperator (
#===), which letshostssupport entries of typeRegExp,ProcandIPAddrto name a few. Here is an example with a regexp.If a request to the host is not allowed, the following response will be
generated:
This can be customized through the
Rails.application.config.hosts_response_appoption:
My current worries are:
How do we introduce this as painlessly?
Is the configuration interface okay?
How do we document it head on?
Do we wanna raise an error instead of returning custom responses? I return a
response, because of a technicality.
ActionDispatch::HostAuthorizationisthe very first middleware, to cut those errors as soon as possible. Raising an
error here wont be caught by
ActionDispatch::DebugExceptions.However, returning a response isn't so obvious and may slip through in
development. Maybe it can be caught in testing, staging environments, but an
error will be much more explicit.