Skip to content

Commit 95f66bb

Browse files
committed
Add check for Host Authorization middleware
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.
1 parent 69101ca commit 95f66bb

4 files changed

Lines changed: 43 additions & 2 deletions

File tree

lib/brakeman/checks/check_hosts.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Brakeman::CheckHosts < Brakeman::BaseCheck
2+
Brakeman::Checks.add_optional self
3+
4+
@description = "Check that hosts setting is not empty in development"
5+
6+
def run_check
7+
return if tracker.config.rails.empty? or tracker.config.rails_version.nil?
8+
return if tracker.config.rails_version < "6"
9+
10+
hosts = tracker.config.rails[:hosts]
11+
12+
if hosts.nil? || hosts.empty?
13+
line = if sexp? hosts
14+
hosts.line
15+
else
16+
1
17+
end
18+
19+
warn :warning_type => "DNS rebinding",
20+
:warning_code => :hosts_empty,
21+
:message => msg("The application does not guard against DNS rebinding: ", msg_code("config.hosts"), " is empty"),
22+
:confidence => :high,
23+
:file => "config/environments/development.rb",
24+
:line => line
25+
end
26+
end
27+
end

lib/brakeman/warning_codes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ module Brakeman::WarningCodes
121121
:erb_template_injection => 117,
122122
:http_verb_confusion => 118,
123123
:unsafe_method_reflection => 119,
124+
:hosts_empty => 120,
124125

125126
:custom_check => 9090,
126127
}

test/tests/rails5.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def expected
1212
@@expected ||= {
1313
:controller => 0,
1414
:model => 0,
15-
:template => 19,
15+
:template => 20,
1616
:generic => 24
1717
}
1818
end

test/tests/rails6.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def expected
1313
:controller => 0,
1414
:model => 0,
1515
:template => 4,
16-
:generic => 34
16+
:generic => 35
1717
}
1818
end
1919

@@ -332,6 +332,19 @@ def test_cross_site_scripting_4
332332
:user_input => nil
333333
end
334334

335+
def test_dns_rebinding_config
336+
assert_warning :type => :warning,
337+
:warning_code => 120,
338+
:fingerprint => "34dfccb87369f7f659bb31070c4d33ccf06d5d1a45e0402a518ddfd25cfe7a9b",
339+
:warning_type => "DNS rebinding",
340+
:line => 1,
341+
:message => /^The\ application\ does\ not\ guard\ against/,
342+
:confidence => 0,
343+
:relative_path => "config/environments/development.rb",
344+
:code => nil,
345+
:user_input => nil
346+
end
347+
335348
def test_cross_site_scripting_json_escape_config
336349
assert_warning :type => :warning,
337350
:warning_code => 113,

0 commit comments

Comments
 (0)