File tree Expand file tree Collapse file tree 5 files changed +79
-0
lines changed
Expand file tree Collapse file tree 5 files changed +79
-0
lines changed Original file line number Diff line number Diff line change @@ -44,6 +44,7 @@ class Configuration
4444 attr_accessor :scrub_user
4545 attr_accessor :scrub_password
4646 attr_accessor :collect_user_ip
47+ attr_accessor :anonymize_user_ip
4748 attr_accessor :user_ip_obfuscator_secret
4849 attr_accessor :randomize_scrub_length
4950 attr_accessor :uncaught_exception_level
@@ -122,6 +123,7 @@ def initialize
122123 @use_exception_level_filters_default = false
123124 @proxy = nil
124125 @collect_user_ip = true
126+ @anonymize_user_ip = false
125127 end
126128
127129 def initialize_copy ( orig )
Original file line number Diff line number Diff line change 55require 'rollbar/scrubbers/url'
66require 'rollbar/scrubbers/params'
77require 'rollbar/util/ip_obfuscator'
8+ require 'rollbar/util/ip_anonymizer'
89require 'rollbar/json'
910
1011module Rollbar
@@ -134,6 +135,8 @@ def rollbar_user_ip(env)
134135 return nil unless Rollbar . configuration . collect_user_ip
135136 user_ip_string = ( env [ 'action_dispatch.remote_ip' ] || env [ 'HTTP_X_REAL_IP' ] || x_forwarded_for_client ( env [ 'HTTP_X_FORWARDED_FOR' ] ) || env [ 'REMOTE_ADDR' ] ) . to_s
136137
138+ user_ip_string = Rollbar ::Util ::IPAnonymizer . anonymize_ip ( user_ip_string )
139+
137140 Rollbar ::Util ::IPObfuscator . obfuscate_ip ( user_ip_string )
138141 rescue
139142 nil
Original file line number Diff line number Diff line change 1+ module Rollbar
2+ module Util
3+ module IPAnonymizer
4+ require 'ipaddr'
5+
6+ def self . anonymize_ip ( ip_string )
7+ return ip_string unless Rollbar . configuration . anonymize_user_ip
8+ ip = IPAddr . new ( ip_string )
9+ return anonymize_ipv6 ip if ip . ipv6?
10+ return anonymize_ipv4 ip if ip . ipv4?
11+ rescue
12+ nil
13+ end
14+
15+ def self . anonymize_ipv4 ( ip )
16+ ip_parts = ip . to_s . split '.'
17+
18+ ip_parts [ ip_parts . count - 1 ] = '0'
19+
20+ IPAddr . new ( ip_parts . join ( '.' ) ) . to_s
21+ end
22+
23+ def self . anonymize_ipv6 ( ip )
24+ ip_parts = ip . to_s . split ':'
25+
26+ ip_string = ip_parts [ 0 ..2 ] . join ( ':' ) + ':0000:0000:0000:0000:0000'
27+
28+ IPAddr . new ( ip_string ) . to_s
29+ end
30+ end
31+ end
32+ end
Original file line number Diff line number Diff line change @@ -150,6 +150,18 @@ class ExtractorDummy
150150 expect ( result [ :user_ip ] ) . to be_nil
151151 end
152152 end
153+
154+ context 'with anonymize_user_ip configuration option enabled' do
155+ before do
156+ Rollbar . configuration . anonymize_user_ip = true
157+ end
158+
159+ it 'it anonymizes the IPv4 address' do
160+ result = subject . extract_request_data_from_rack ( env )
161+
162+ expect ( result [ :user_ip ] ) . to be_eql ( '2.2.2.0' )
163+ end
164+ end
153165 end
154166
155167 context 'with private first client IP' do
Original file line number Diff line number Diff line change 1+ require 'spec_helper'
2+ require 'rollbar/util/ip_anonymizer'
3+
4+ describe Rollbar ::Util ::IPAnonymizer do
5+
6+ before do
7+ Rollbar . configuration . anonymize_user_ip = true
8+ end
9+
10+ context 'with IPv4 address' do
11+ let ( :ip ) { '127.0.0.1' }
12+
13+ it 'anonymizes the IP by replacing the last octet with 0' do
14+ anonymized_ip = described_class . anonymize_ip ( ip )
15+
16+ expect ( anonymized_ip ) . to be_eql ( IPAddr . new ( '127.0.0.0' ) . to_s )
17+ end
18+ end
19+
20+ context 'with IPv6 address' do
21+ let ( :ip ) { '2001:0db8:85a3:0000:0000:8a2e:0370:7334' }
22+
23+ it 'anonymizes the IP by replacing the last 80 bits with 0' do
24+
25+ anonymized_ip = described_class . anonymize_ip ( ip )
26+
27+ expect ( anonymized_ip ) . to be_eql ( IPAddr . new ( '2001:db8:85a3::' ) . to_s )
28+ end
29+ end
30+ end
You can’t perform that action at this time.
0 commit comments