Skip to content

Commit

Permalink
Merge pull request #26 from securenative/dev
Browse files Browse the repository at this point in the history
Fix proxy headers and code cleanups
  • Loading branch information
inbaltako authored Oct 4, 2020
2 parents f4055fb + e36c1fb commit a9d168c
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 12 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.31)
securenative (0.1.32)

GEM
remote: https://rubygems.org/
Expand Down
15 changes: 10 additions & 5 deletions lib/securenative/utils/request_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,26 @@ def self.get_secure_header_from_request(headers)
end

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

begin
Expand All @@ -46,6 +47,7 @@ def self.get_client_ip_from_request(request, options)
x_forwarded_for = request['HTTP_X_FORWARDED_FOR']
return x_forwarded_for.scan(/\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b/)[0] unless x_forwarded_for.nil?
rescue NoMethodError
# Ignored
end
end

Expand All @@ -57,6 +59,7 @@ def self.get_client_ip_from_request(request, options)
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
# Ignored
end
end

Expand All @@ -68,12 +71,14 @@ def self.get_client_ip_from_request(request, options)
x_forwarded_for = request['REMOTE_ADDR']
return x_forwarded_for.scan(/\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b/)[0] unless x_forwarded_for.nil?
rescue NoMethodError
# Ignored
end
end

begin
return request.ip unless request.ip.nil?
rescue NoMethodError
# Ignored
end

''
Expand Down
2 changes: 1 addition & 1 deletion lib/securenative/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module SecureNative
VERSION = '0.1.31'
VERSION = '0.1.32'
end
2 changes: 1 addition & 1 deletion spec/securenative/spec_api_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,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.31',
'Sn-Version' => '0.1.32',
'User-Agent' => 'SecureNative-ruby'
}
).to_return(status: 200, body: '', headers: {})
Expand Down
6 changes: 3 additions & 3 deletions spec/securenative/spec_event_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,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.31',
'Sn-Version' => '0.1.32',
'User-Agent' => 'SecureNative-ruby'
})
.to_return(status: 200, body: '', headers: {})
Expand All @@ -53,7 +53,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.31',
'Sn-Version' => '0.1.32',
'User-Agent' => 'SecureNative-ruby'
})
.to_return(status: 401, body: '', headers: {})
Expand All @@ -74,7 +74,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.31',
'Sn-Version' => '0.1.32',
'User-Agent' => 'SecureNative-ruby'
})
.to_return(status: 500, body: '', headers: {})
Expand Down
2 changes: 1 addition & 1 deletion spec/securenative/spec_http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,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.31',
'Sn-Version' => '0.1.32',
'User-Agent' => 'SecureNative-ruby'
}).to_return(status: 200, body: '', headers: {})

Expand Down

0 comments on commit a9d168c

Please sign in to comment.