Skip to content

Instantly share code, notes, and snippets.

@mrhead
Created April 12, 2018 11:55
Show Gist options
  • Save mrhead/643dc047a7733b4db801da02953e59b3 to your computer and use it in GitHub Desktop.
Save mrhead/643dc047a7733b4db801da02953e59b3 to your computer and use it in GitHub Desktop.
# 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
# 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