Skip to content

Commit f4055fb

Browse files
authored
Merge pull request #25 from securenative/dev
Fix request header parsing
2 parents ca54755 + 810db9c commit f4055fb

File tree

9 files changed

+30
-14
lines changed

9 files changed

+30
-14
lines changed

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.30)
4+
securenative (0.1.31)
55

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

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ require 'securenative'
114114

115115
def track(request)
116116
securenative = SecureNative::Client.instance
117-
context = SecureNative::Context.from_http_request(request)
117+
context = securenative.from_http_request(request)
118118

119119
event_options = SecureNative::EventOptions.new(event: SecureNative::EventTypes::LOG_IN, user_id: '1234', context: context,
120120
user_traits: SecureNative::UserTraits.new(name: 'Your Name', email: '[email protected]', phone: '+1234567890'),
@@ -136,7 +136,7 @@ require 'securenative'
136136

137137
def verify(request)
138138
securenative = SecureNative::Client.instance
139-
context = SecureNative::Context.from_http_request(request)
139+
context = securenative.from_http_request(request)
140140

141141
event_options = SecureNative::EventOptions.new(event: SecureNative::EventTypes::LOG_IN, user_id: '1234', context: context,
142142
user_traits: SecureNative::UserTraits.new(name: 'Your Name', email: '[email protected]', phone: '+1234567890'),

lib/securenative/client.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ def self.init_with_api_key(api_key)
4242
end
4343
end
4444

45+
def from_http_request(request)
46+
SecureNative::Context.from_http_request(request, @options)
47+
end
48+
4549
def self.init
4650
options = SecureNative::Config::ConfigurationManager.load_config
4751
init_with_options(options)

lib/securenative/context.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def self.default_context_builder
2121
SecureNative::Context.new
2222
end
2323

24-
def self.from_http_request(request)
24+
def self.from_http_request(request, options)
2525
client_token = SecureNative::Frameworks::Rails.get_client_token(request)
2626
client_token = SecureNative::Frameworks::Sinatra.get_client_token(request) if client_token.nil?
2727
client_token = SecureNative::Frameworks::Hanami.get_client_token(request) if client_token.nil?
@@ -57,9 +57,9 @@ def self.from_http_request(request)
5757
client_token = SecureNative::Utils::RequestUtils.get_secure_header_from_request(headers)
5858
end
5959

60-
SecureNative::Context.new(client_token: client_token, ip: SecureNative::Utils::RequestUtils.get_client_ip_from_request(request),
61-
remote_ip: SecureNative::Utils::RequestUtils.get_remote_ip_from_request(request),
62-
headers: headers, url: url, http_method: method || '', body: body)
60+
SecureNative::Context.new(client_token: client_token, ip: SecureNative::Utils::RequestUtils.get_client_ip_from_request(request, options),
61+
remote_ip: SecureNative::Utils::RequestUtils.get_remote_ip_from_request(request),
62+
headers: headers, url: url, http_method: method || '', body: body)
6363
end
6464
end
6565
end

lib/securenative/utils/request_utils.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module Utils
55
class RequestUtils
66
SECURENATIVE_COOKIE = '_sn'
77
SECURENATIVE_HEADER = 'x-securenative'
8+
PREFIX = 'HTTP_'
89

910
def self.get_secure_header_from_request(headers)
1011
begin
@@ -15,15 +16,21 @@ def self.get_secure_header_from_request(headers)
1516
[]
1617
end
1718

18-
def self.get_client_ip_from_request(request, options = nil)
19+
def self.get_client_ip_from_request(request, options)
1920
unless options.nil?
2021
for header in options.proxy_headers do
2122
begin
2223
h = request.env[header]
24+
unless !h.nil?
25+
h = request.env[self.parse_ip(header)]
26+
end
2327
return h.scan(/\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b/)[0] unless h.nil?
2428
rescue NoMethodError
2529
begin
2630
h = request[header]
31+
unless !h.nil?
32+
h = request.env[self.parse_ip(header)]
33+
end
2734
return h.scan(/\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b/)[0] unless h.nil?
2835
rescue NoMethodError
2936
end
@@ -79,6 +86,11 @@ def self.get_remote_ip_from_request(request)
7986
''
8087
end
8188
end
89+
90+
def self.parse_ip(headers)
91+
h = headers.gsub('-', '_')
92+
return PREFIX + h.upcase
93+
end
8294
end
8395
end
8496
end

lib/securenative/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module SecureNative
4-
VERSION = '0.1.30'
4+
VERSION = '0.1.31'
55
end

spec/securenative/spec_api_manager.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
5656
'Authorization' => 'YOUR_API_KEY',
5757
'Content-Type' => 'application/json',
58-
'Sn-Version' => '0.1.29',
58+
'Sn-Version' => '0.1.31',
5959
'User-Agent' => 'SecureNative-ruby'
6060
}
6161
).to_return(status: 200, body: '', headers: {})

spec/securenative/spec_event_manager.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def initialize
2929
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
3030
'Authorization' => 'YOUR_API_KEY',
3131
'Content-Type' => 'application/json',
32-
'Sn-Version' => '0.1.29',
32+
'Sn-Version' => '0.1.31',
3333
'User-Agent' => 'SecureNative-ruby'
3434
})
3535
.to_return(status: 200, body: '', headers: {})
@@ -53,7 +53,7 @@ def initialize
5353
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
5454
'Authorization' => 'YOUR_API_KEY',
5555
'Content-Type' => 'application/json',
56-
'Sn-Version' => '0.1.29',
56+
'Sn-Version' => '0.1.31',
5757
'User-Agent' => 'SecureNative-ruby'
5858
})
5959
.to_return(status: 401, body: '', headers: {})
@@ -74,7 +74,7 @@ def initialize
7474
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
7575
'Authorization' => 'YOUR_API_KEY',
7676
'Content-Type' => 'application/json',
77-
'Sn-Version' => '0.1.29',
77+
'Sn-Version' => '0.1.31',
7878
'User-Agent' => 'SecureNative-ruby'
7979
})
8080
.to_return(status: 500, body: '', headers: {})

spec/securenative/spec_http_client.rb

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

0 commit comments

Comments
 (0)