Skip to content

Commit 04148e7

Browse files
Ginjalzap
authored andcommitted
Fixes #2259 - trusted hosts work with passenger
1 parent 2f75941 commit 04148e7

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

lib/smart_proxy.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class SmartProxy < Sinatra::Base
3535
require "dhcp_api" if SETTINGS.dhcp
3636
require "bmc_api" if SETTINGS.bmc
3737
require "chefproxy_api" if SETTINGS.chefproxy
38+
require "resolv" if SETTINGS.trusted_hosts
3839

3940
begin
4041
require "facter"
@@ -67,10 +68,17 @@ class SmartProxy < Sinatra::Base
6768

6869
before do
6970
# If we are using certificates and we reach here then the peer is verified and cannot be spoofed. ALWAYS use certificates OR ELSE!!!
70-
# If we are not using certificates then the hostname can be spoofed but this will still keep out most casual mischief.
71-
if (SETTINGS.trusted_hosts and !SETTINGS.trusted_hosts.empty?) and
72-
!SETTINGS.trusted_hosts.include?(request.env["REMOTE_HOST"].downcase)
73-
log_halt 403, "Untrusted client #{request.env["REMOTE_HOST"].downcase} attempted to access #{request.path_info}. Check :trusted_hosts: in settings.yml"
71+
# If we are not using certificates, and we've specified :trusted_hosts:, we'll check the reverse DNS entry of the remote IP, and ensure it's in our :trusted_hosts: array.
72+
if (SETTINGS.trusted_hosts and !SETTINGS.trusted_hosts.empty?)
73+
begin
74+
remote_fqdn = Resolv.new.getname(request.env["REMOTE_ADDR"])
75+
rescue Resolv::ResolvError => e
76+
log_halt 403, "Unable to resolve hostname for connecting client - #{request.env["REMOTE_ADDR"]}. If it's to be a trusted host, ensure it has a reverse DNS entry." +
77+
"\n\n" + "#{e.message}"
78+
end
79+
if !SETTINGS.trusted_hosts.include?(remote_fqdn.downcase)
80+
log_halt 403, "Untrusted client #{remote_fqdn.downcase} attempted to access #{request.path_info}. Check :trusted_hosts: in settings.yml"
81+
end
7482
end
7583
end
7684
end

0 commit comments

Comments
 (0)