Skip to content

Commit c3979e4

Browse files
authored
Merge pull request #21 from securenative/dev
Align ip extraction
2 parents e14755a + 281e2fb commit c3979e4

6 files changed

Lines changed: 34 additions & 23 deletions

File tree

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
securenative (0.1.26)
4+
securenative (0.1.27)
55

66
GEM
77
remote: https://rubygems.org/

lib/securenative/utils/request_utils.rb

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,19 @@ def self.get_secure_header_from_request(headers)
1515
end
1616

1717
def self.get_client_ip_from_request(request, options = nil)
18-
begin
19-
return request.ip unless request.ip.nil?
20-
rescue NoMethodError
18+
unless options.nil?
19+
for header in options.proxy_headers do
20+
begin
21+
h = request.env[header]
22+
return h.scan(/\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b/)[0] unless h.nil?
23+
rescue NoMethodError
24+
begin
25+
h = request[header]
26+
return h.scan(/\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b/)[0] unless h.nil?
27+
rescue NoMethodError
28+
end
29+
end
30+
end
2131
end
2232

2333
begin
@@ -31,6 +41,17 @@ def self.get_client_ip_from_request(request, options = nil)
3141
end
3242
end
3343

44+
begin
45+
x_forwarded_for = request.env['HTTP_X_REAL_IP']
46+
return x_forwarded_for.scan(/\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b/)[0] unless x_forwarded_for.nil?
47+
rescue NoMethodError
48+
begin
49+
x_forwarded_for = request['HTTP_X_REAL_IP']
50+
return x_forwarded_for.scan(/\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b/)[0] unless x_forwarded_for.nil?
51+
rescue NoMethodError
52+
end
53+
end
54+
3455
begin
3556
x_forwarded_for = request.env['REMOTE_ADDR']
3657
return x_forwarded_for.scan(/\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b/)[0] unless x_forwarded_for.nil?
@@ -42,19 +63,9 @@ def self.get_client_ip_from_request(request, options = nil)
4263
end
4364
end
4465

45-
unless options.nil?
46-
for header in options.proxy_headers do
47-
begin
48-
h = request.env[header]
49-
return h.scan(/\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b/)[0] unless h.nil?
50-
rescue NoMethodError
51-
begin
52-
h = request[header]
53-
return h.scan(/\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b/)[0] unless h.nil?
54-
rescue NoMethodError
55-
end
56-
end
57-
end
66+
begin
67+
return request.ip unless request.ip.nil?
68+
rescue NoMethodError
5869
end
5970

6071
''

securenative.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require_relative 'lib/securenative/utils/version_utils'
66

77
Gem::Specification.new do |spec|
88
spec.name = 'securenative'
9-
spec.version = '0.1.26'
9+
spec.version = '0.1.27'
1010
spec.authors = ['SecureNative']
1111
spec.email = ['support@securenative.com']
1212

spec/spec_api_manager.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
6464
'Authorization' => 'YOUR_API_KEY',
6565
'Content-Type' => 'application/json',
66-
'Sn-Version' => '0.1.26',
66+
'Sn-Version' => '0.1.27',
6767
'User-Agent' => 'SecureNative-ruby'
6868
}
6969
).to_return(status: 200, body: '', headers: {})

spec/spec_event_manager.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def initialize
3232
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
3333
'Authorization' => 'YOUR_API_KEY',
3434
'Content-Type' => 'application/json',
35-
'Sn-Version' => '0.1.26',
35+
'Sn-Version' => '0.1.27',
3636
'User-Agent' => 'SecureNative-ruby'
3737
})
3838
.to_return(status: 200, body: '', headers: {})
@@ -56,7 +56,7 @@ def initialize
5656
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
5757
'Authorization' => 'YOUR_API_KEY',
5858
'Content-Type' => 'application/json',
59-
'Sn-Version' => '0.1.26',
59+
'Sn-Version' => '0.1.27',
6060
'User-Agent' => 'SecureNative-ruby'
6161
})
6262
.to_return(status: 401, body: '', headers: {})
@@ -77,7 +77,7 @@ def initialize
7777
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
7878
'Authorization' => 'YOUR_API_KEY',
7979
'Content-Type' => 'application/json',
80-
'Sn-Version' => '0.1.26',
80+
'Sn-Version' => '0.1.27',
8181
'User-Agent' => 'SecureNative-ruby'
8282
})
8383
.to_return(status: 500, body: '', headers: {})

spec/spec_securenative_http_client.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
1717
'Authorization' => 'YOUR_API_KEY',
1818
'Content-Type' => 'application/json',
19-
'Sn-Version' => '0.1.26',
19+
'Sn-Version' => '0.1.27',
2020
'User-Agent' => 'SecureNative-ruby'
2121
}).to_return(status: 200, body: '', headers: {})
2222

0 commit comments

Comments
 (0)