getsentry/sentry-ruby

User ip_address Overwritten Despite Being Set via Sentry.set_user

Closed this issue · 1 comments

Issue Description

When the @send_default_pii flag is set to true, the ip_address set via Sentry.set_user is being overwritten by the IP address calculated from the Rack environment (calculate_real_ip_from_rack). This behavior is not intended and leads to incorrect IP address information being sent to Sentry.

After attempting to disable the send_default_pii flag, the ip_address set via Sentry.set_user was correctly reflected in Sentry. This confirms that the issue is related to the send_default_pii flag causing the IP address to be overwritten by calculate_real_ip_from_rack.

def rack_env=(env)
unless request || env.empty?
add_request_interface(env)
if @send_default_pii
user[:ip_address] = calculate_real_ip_from_rack(env)
end
if request_id = Utils::RequestId.read_from(env)
tags[:request_id] = request_id
end
end
end

Reproduction Steps

  Sentry.set_user(
    ip_address: '123.123.123.123'
  )

Expected Behavior

The ip_address set via Sentry.set_user using request.remote_ip should be correctly recorded and not overwritten by calculate_real_ip_from_rack.

Actual Behavior

When @send_default_pii is true, the ip_address set via Sentry.set_user is overwritten by the IP address calculated from calculate_real_ip_from_rack. This results in Cloudflare’s IP address being recorded in Sentry instead of the actual client’s IP address. This discrepancy may arises because the cloudflare-rails gem modifies the IP addresses to Cloudflare’s proxies without change trusted proxies.

Ruby Version

3.2.2

SDK Version

5.18.1

Integration and Its Version

No response

Sentry Config

Sentry.init do |config|
  config.dsn = ''
  config.breadcrumbs_logger = %i[active_support_logger http_logger]
  config.send_default_pii = true
end

@ryush00 thx for the report, will fix very soon