Skip to content

Commit

Permalink
Merge pull request #21 from securenative/dev
Browse files Browse the repository at this point in the history
Align ip extraction
  • Loading branch information
inbaltako authored Sep 24, 2020
2 parents e14755a + 281e2fb commit c3979e4
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
securenative (0.1.26)
securenative (0.1.27)

GEM
remote: https://rubygems.org/
Expand Down
43 changes: 27 additions & 16 deletions lib/securenative/utils/request_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,19 @@ def self.get_secure_header_from_request(headers)
end

def self.get_client_ip_from_request(request, options = nil)
begin
return request.ip unless request.ip.nil?
rescue NoMethodError
unless options.nil?
for header in options.proxy_headers do
begin
h = request.env[header]
return h.scan(/\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b/)[0] unless h.nil?
rescue NoMethodError
begin
h = request[header]
return h.scan(/\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b/)[0] unless h.nil?
rescue NoMethodError
end
end
end
end

begin
Expand All @@ -31,6 +41,17 @@ def self.get_client_ip_from_request(request, options = nil)
end
end

begin
x_forwarded_for = request.env['HTTP_X_REAL_IP']
return x_forwarded_for.scan(/\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b/)[0] unless x_forwarded_for.nil?
rescue NoMethodError
begin
x_forwarded_for = request['HTTP_X_REAL_IP']
return x_forwarded_for.scan(/\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b/)[0] unless x_forwarded_for.nil?
rescue NoMethodError
end
end

begin
x_forwarded_for = request.env['REMOTE_ADDR']
return x_forwarded_for.scan(/\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b/)[0] unless x_forwarded_for.nil?
Expand All @@ -42,19 +63,9 @@ def self.get_client_ip_from_request(request, options = nil)
end
end

unless options.nil?
for header in options.proxy_headers do
begin
h = request.env[header]
return h.scan(/\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b/)[0] unless h.nil?
rescue NoMethodError
begin
h = request[header]
return h.scan(/\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b/)[0] unless h.nil?
rescue NoMethodError
end
end
end
begin
return request.ip unless request.ip.nil?
rescue NoMethodError
end

''
Expand Down
2 changes: 1 addition & 1 deletion securenative.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require_relative 'lib/securenative/utils/version_utils'

Gem::Specification.new do |spec|
spec.name = 'securenative'
spec.version = '0.1.26'
spec.version = '0.1.27'
spec.authors = ['SecureNative']
spec.email = ['support@securenative.com']

Expand Down
2 changes: 1 addition & 1 deletion spec/spec_api_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Authorization' => 'YOUR_API_KEY',
'Content-Type' => 'application/json',
'Sn-Version' => '0.1.26',
'Sn-Version' => '0.1.27',
'User-Agent' => 'SecureNative-ruby'
}
).to_return(status: 200, body: '', headers: {})
Expand Down
6 changes: 3 additions & 3 deletions spec/spec_event_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def initialize
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Authorization' => 'YOUR_API_KEY',
'Content-Type' => 'application/json',
'Sn-Version' => '0.1.26',
'Sn-Version' => '0.1.27',
'User-Agent' => 'SecureNative-ruby'
})
.to_return(status: 200, body: '', headers: {})
Expand All @@ -56,7 +56,7 @@ def initialize
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Authorization' => 'YOUR_API_KEY',
'Content-Type' => 'application/json',
'Sn-Version' => '0.1.26',
'Sn-Version' => '0.1.27',
'User-Agent' => 'SecureNative-ruby'
})
.to_return(status: 401, body: '', headers: {})
Expand All @@ -77,7 +77,7 @@ def initialize
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Authorization' => 'YOUR_API_KEY',
'Content-Type' => 'application/json',
'Sn-Version' => '0.1.26',
'Sn-Version' => '0.1.27',
'User-Agent' => 'SecureNative-ruby'
})
.to_return(status: 500, body: '', headers: {})
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_securenative_http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Authorization' => 'YOUR_API_KEY',
'Content-Type' => 'application/json',
'Sn-Version' => '0.1.26',
'Sn-Version' => '0.1.27',
'User-Agent' => 'SecureNative-ruby'
}).to_return(status: 200, body: '', headers: {})

Expand Down

0 comments on commit c3979e4

Please sign in to comment.