2019-10-20 17:06:17 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-05-04 18:49:39 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 05:08:32 -04:00
|
|
|
RSpec.describe 'trusted_proxies' do
|
2016-05-04 18:49:39 -04:00
|
|
|
context 'with default config' do
|
|
|
|
before do
|
|
|
|
set_trusted_proxies([])
|
|
|
|
end
|
|
|
|
|
2016-06-27 15:43:28 -04:00
|
|
|
it 'preserves private IPs' do
|
2016-05-04 18:49:39 -04:00
|
|
|
request = stub_request('HTTP_X_FORWARDED_FOR' => '10.1.5.89')
|
|
|
|
expect(request.remote_ip).to eq('10.1.5.89')
|
2016-06-27 15:43:28 -04:00
|
|
|
expect(request.ip).to eq('10.1.5.89')
|
2016-05-04 18:49:39 -04:00
|
|
|
end
|
|
|
|
|
2016-06-27 15:43:28 -04:00
|
|
|
it 'filters out localhost' do
|
2016-05-04 18:49:39 -04:00
|
|
|
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')
|
2016-06-27 15:43:28 -04:00
|
|
|
expect(request.ip).to eq('10.1.5.89')
|
2016-05-04 18:49:39 -04:00
|
|
|
end
|
2016-07-24 00:01:23 -04:00
|
|
|
|
|
|
|
it 'filters out bad values' do
|
|
|
|
request = stub_request('HTTP_X_FORWARDED_FOR' => '(null), 10.1.5.89')
|
|
|
|
expect(request.remote_ip).to eq('10.1.5.89')
|
|
|
|
expect(request.ip).to eq('10.1.5.89')
|
|
|
|
end
|
2016-05-04 18:49:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with private IP ranges added' do
|
|
|
|
before do
|
2017-02-21 18:33:53 -05:00
|
|
|
set_trusted_proxies(["10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"])
|
2016-05-04 18:49:39 -04:00
|
|
|
end
|
|
|
|
|
2016-06-27 15:43:28 -04:00
|
|
|
it 'filters out private and local IPs' do
|
2016-05-04 18:49:39 -04:00
|
|
|
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')
|
2016-06-27 15:43:28 -04:00
|
|
|
expect(request.ip).to eq('1.1.1.1')
|
2016-05-04 18:49:39 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with proxy IP added' do
|
|
|
|
before do
|
2017-02-21 18:33:53 -05:00
|
|
|
set_trusted_proxies(["60.98.25.47"])
|
2016-05-04 18:49:39 -04:00
|
|
|
end
|
|
|
|
|
2016-06-27 15:43:28 -04:00
|
|
|
it 'filters out proxy IP' do
|
2016-05-04 18:49:39 -04:00
|
|
|
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')
|
2016-06-27 15:43:28 -04:00
|
|
|
expect(request.ip).to eq('1.1.1.1')
|
2016-05-04 18:49:39 -04:00
|
|
|
end
|
2016-07-31 15:36:11 -04:00
|
|
|
|
|
|
|
it 'handles invalid ip addresses' do
|
|
|
|
request = stub_request('HTTP_X_FORWARDED_FOR' => '(null), 1.1.1.1:12345, 1.1.1.1')
|
|
|
|
expect(request.remote_ip).to eq('1.1.1.1')
|
|
|
|
expect(request.ip).to eq('1.1.1.1')
|
|
|
|
end
|
2016-05-04 18:49:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def stub_request(headers = {})
|
2022-08-26 14:12:09 -04:00
|
|
|
ActionDispatch::RemoteIp.new(proc {}, false, Rails.application.config.action_dispatch.trusted_proxies).call(headers)
|
2016-05-04 18:49:39 -04:00
|
|
|
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
|