Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't overwrite ip_address if already set on user #2350

Merged
merged 1 commit into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## Unreleased

### Bug Fixes

- Don't overwrite `ip_address` if already set on `user` [#2350](https://github.com/getsentry/sentry-ruby/pull/2350)
- Fixes [#2347](https://github.com/getsentry/sentry-ruby/issues/2347)

### Internal

- Use Concurrent.usable_processor_count when it is available ([#2339](https://github.com/getsentry/sentry-ruby/pull/2339))
Expand Down
4 changes: 1 addition & 3 deletions sentry-ruby/lib/sentry/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,7 @@ 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
user[:ip_address] ||= calculate_real_ip_from_rack(env) if @send_default_pii

if request_id = Utils::RequestId.read_from(env)
tags[:request_id] = request_id
Expand Down
6 changes: 6 additions & 0 deletions sentry-ruby/spec/sentry/event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@
expect(event.to_hash[:user][:ip_address]).to eq("2.2.2.2")
end

it "doesn't overwrite already set ip address" do
Sentry.set_user({ ip_address: "3.3.3.3" })
Sentry.get_current_scope.apply_to_event(event)
expect(event.to_hash[:user][:ip_address]).to eq("3.3.3.3")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sl0thentr0py note for the future - this should be event.to_h, because to_hash is a special coercion method that's used implicitly in some places in Ruby and may cause weird issues. For example ** uses it, see:

(ruby) {foo: "bar", **event}
{:foo=>"bar",
 :event_id=>"266e3389d9b64f57b48da6f3d0969a93",
 :level=>:error,
 :timestamp=>"2024-07-22T16:22:13Z",
 :environment=>"development",
 :server_name=>"44b9b235e7c3",
 :modules=>
  {"rake"=>"12.3.3",
 # ...

It should only be used when the object that implements it is actually a hash-like object, and events are not like that.

Copy link
Collaborator

@st0012 st0012 Jul 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! For context, I took it from the old SDK when implementing sentry-ruby. So we probably can make the switch rather freely. I'll put up a PR to see how it goes.

PR: #2351

end

context "with config.trusted_proxies = [\"2.2.2.2\"]" do
before do
Sentry.configuration.trusted_proxies = ["2.2.2.2"]
Expand Down
Loading