Created
April 12, 2018 11:55
-
-
Save mrhead/643dc047a7733b4db801da02953e59b3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lib/raven/processor/remove_ip_address.rb | |
class Raven::Processor::RemoveIpAddress < Raven::Processor | |
def process(data) | |
data = data.with_indifferent_access | |
if data.dig(:user, :ip_address) | |
data[:user][:ip_address] = nil | |
end | |
if data.dig(:request, :headers, "X-Forwarded-For") | |
data[:request][:headers]["X-Forwarded-For"] = STRING_MASK | |
end | |
if data.dig(:request, :env, "REMOTE_ADDR") | |
data[:request][:env]["REMOTE_ADDR"] = STRING_MASK | |
end | |
data | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# test/lib/raven/processor/remove_ip_address_test.rb | |
require "test_helper" | |
class Raven::Processor::RemoveIpAddressTest < ActiveSupport::TestCase | |
def setup | |
client = stub | |
@processor = Raven::Processor::RemoveIpAddress.new(client) | |
end | |
test "remove IP address from user context" do | |
test_data = { | |
user: { | |
id: 1, | |
ip_address: "1.1.1.1" | |
} | |
} | |
result = @processor.process(test_data) | |
assert_nil result[:user][:ip_address] | |
assert_equal 1, result[:user][:id] | |
end | |
test "remove IP address from request data" do | |
test_data = { | |
request: { | |
headers: { | |
"Host" => "host", | |
"X-Forwarded-For" => "1.1.1.1, 2.2.2.2" | |
}, | |
env: { | |
"SERVER_NAME" => "localhost", | |
"REMOTE_ADDR" => "192.168.1.1" | |
} | |
} | |
} | |
result = @processor.process(test_data) | |
assert_equal "********", result[:request][:headers]["X-Forwarded-For"] | |
assert_equal "********", result[:request][:env]["REMOTE_ADDR"] | |
assert_equal "host", result[:request][:headers]["Host"] | |
assert_equal "localhost", result[:request][:env]["SERVER_NAME"] | |
end | |
test "string keys" do | |
test_data = { | |
"request" => { | |
"headers" => { | |
"Host" => "host", | |
"X-Forwarded-For" => "1.1.1.1, 2.2.2.2" | |
}, | |
"env" => { | |
"SERVER_NAME" => "localhost", | |
"REMOTE_ADDR" => "192.168.1.1" | |
} | |
}, | |
"user" => { | |
"id" => 1, | |
"ip_address" => "1.1.1.1" | |
} | |
} | |
result = @processor.process(test_data) | |
assert_nil result[:user][:ip_address] | |
assert_equal "********", result[:request][:headers]["X-Forwarded-For"] | |
assert_equal "********", result[:request][:env]["REMOTE_ADDR"] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment