Skip to content

Commit

Permalink
Add check for Host Authorization middleware
Browse files Browse the repository at this point in the history
The Host Authorization middleware protects against DNS rebinding.
This middleware is primarily targeted at the development environment:

> It is included in the development environment by default ... In other
environments Rails.application.config.hosts is empty and no Host header
checks will be done.
rails/rails#33145

If someone decides to call `config.hosts.clear` because it's "only
development", we should warn them they are vulnerable to DNS rebinding.
  • Loading branch information
p8 committed Aug 23, 2021
1 parent 69101ca commit b9d6803
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
27 changes: 27 additions & 0 deletions lib/brakeman/checks/check_hosts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Brakeman::CheckHosts < Brakeman::BaseCheck
Brakeman::Checks.add_optional self

@description = "Check that hosts setting is not empty in development"

def run_check
return if tracker.config.rails.empty? or tracker.config.rails_version.nil?
return if tracker.config.rails_version < "6.0.0"

hosts = tracker.config.rails[:hosts]

if hosts.nil? || hosts.empty?
line = if sexp? hosts
hosts.line
else
1
end

warn :warning_type => "DNS rebinding",
:warning_code => :hosts_empty,
:message => msg("The application does not guard against DNS rebinding: ", msg_code("config.hosts"), " is empty"),
:confidence => :high,
:file => "config/environments/development.rb",
:line => line
end
end
end
1 change: 1 addition & 0 deletions lib/brakeman/warning_codes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ module Brakeman::WarningCodes
:erb_template_injection => 117,
:http_verb_confusion => 118,
:unsafe_method_reflection => 119,
:hosts_empty => 120,

:custom_check => 9090,
}
Expand Down
15 changes: 14 additions & 1 deletion test/tests/rails6.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def expected
:controller => 0,
:model => 0,
:template => 4,
:generic => 34
:generic => 35
}
end

Expand Down Expand Up @@ -332,6 +332,19 @@ def test_cross_site_scripting_4
:user_input => nil
end

def test_dns_rebinding_config
assert_warning :type => :warning,
:warning_code => 120,
:fingerprint => "6a26086cd2400fbbfb831b2f8d7291e320bcc2b36984d2abc359e41b3b63212b",
:warning_type => "DNS rebinding",
:line => 50,
:message => /^The\ application\ does\ not\ guard\ against/,
:confidence => 0,
:relative_path => "config/environments/development.rb",
:code => nil,
:user_input => nil
end

def test_cross_site_scripting_json_escape_config
assert_warning :type => :warning,
:warning_code => 113,
Expand Down

0 comments on commit b9d6803

Please sign in to comment.