Skip to content

Commit

Permalink
check CF header for ip
Browse files Browse the repository at this point in the history
  • Loading branch information
bartes committed Jan 22, 2018
1 parent 7781f18 commit 39c4eae
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

**Enhancements:**

- [#100](github.com/castle/castle-ruby/pull/100) use request.remote_ip in favour of request.ip if present
- [#100](github.com/castle/castle-ruby/pull/100) use request.remote_ip and CF connecting IP in favour of request.ip if present

## 3.3.0 (2018-01-12)

Expand Down
1 change: 1 addition & 0 deletions lib/castle/extractors/ip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def initialize(request)
end

def call
return @request.env['HTTP_CF_CONNECTING_IP'] if @request.env['HTTP_CF_CONNECTING_IP']
return @request.remote_ip if @request.respond_to?(:remote_ip)
@request.ip
end
Expand Down
14 changes: 12 additions & 2 deletions spec/lib/castle/extractors/ip_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,18 @@
let(:request) { Rack::Request.new(env) }

describe 'ip' do
let(:env) { Rack::MockRequest.env_for( '/', 'HTTP_X_FORWARDED_FOR' => '1.2.3.4') }
context 'when regular ip' do
let(:env) { Rack::MockRequest.env_for('/', 'HTTP_X_FORWARDED_FOR' => '1.2.3.5') }

it { expect(extractor.call).to eql('1.2.3.4') }
it { expect(extractor.call).to eql('1.2.3.5') }
end

context 'when cf remote_ip' do
let(:env) { Rack::MockRequest.env_for('/',
'HTTP_CF_CONNECTING_IP' => '1.2.3.4',
'HTTP_X_FORWARDED_FOR' => '1.2.3.5') }

it { expect(extractor.call).to eql('1.2.3.4') }
end
end
end

0 comments on commit 39c4eae

Please sign in to comment.