Merge branch 'trusted-proxies-ip-addr' into 'master'

Pass trusted_proxies to action_dispatch as IPAddrs instead of strings

Without this setting your own trusted_proxies does not work.

Fixes an issue introduce in: https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/3524

Fixes: https://gitlab.com/gitlab-org/gitlab-ce/issues/17004

See merge request !3970
This commit is contained in:
Rémy Coutable 2016-05-09 09:47:16 +00:00
commit 7d95d3cde7
2 changed files with 53 additions and 1 deletions

View file

@ -1,2 +1,3 @@
Rails.application.config.action_dispatch.trusted_proxies =
Rails.application.config.action_dispatch.trusted_proxies = (
[ '127.0.0.1', '::1' ] + Array(Gitlab.config.gitlab.trusted_proxies)
).map { |proxy| IPAddr.new(proxy) }

View file

@ -0,0 +1,51 @@
require 'spec_helper'
describe 'trusted_proxies', lib: true do
context 'with default config' do
before do
set_trusted_proxies([])
end
it 'preserves private IPs as remote_ip' do
request = stub_request('HTTP_X_FORWARDED_FOR' => '10.1.5.89')
expect(request.remote_ip).to eq('10.1.5.89')
end
it 'filters out localhost from remote_ip' do
request = stub_request('HTTP_X_FORWARDED_FOR' => '1.1.1.1, 10.1.5.89, 127.0.0.1')
expect(request.remote_ip).to eq('10.1.5.89')
end
end
context 'with private IP ranges added' do
before do
set_trusted_proxies([ "10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16" ])
end
it 'filters out private and local IPs from remote_ip' do
request = stub_request('HTTP_X_FORWARDED_FOR' => '1.2.3.6, 1.1.1.1, 10.1.5.89, 127.0.0.1')
expect(request.remote_ip).to eq('1.1.1.1')
end
end
context 'with proxy IP added' do
before do
set_trusted_proxies([ "60.98.25.47" ])
end
it 'filters out proxy IP from remote_ip' do
request = stub_request('HTTP_X_FORWARDED_FOR' => '1.2.3.6, 1.1.1.1, 60.98.25.47, 127.0.0.1')
expect(request.remote_ip).to eq('1.1.1.1')
end
end
def stub_request(headers = {})
ActionDispatch::RemoteIp.new(Proc.new { }, false, Rails.application.config.action_dispatch.trusted_proxies).call(headers)
ActionDispatch::Request.new(headers)
end
def set_trusted_proxies(proxies = [])
stub_config_setting('trusted_proxies' => proxies)
load File.join(__dir__, '../../config/initializers/trusted_proxies.rb')
end
end