Skip to content
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
3 changes: 3 additions & 0 deletions sentry_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2168,6 +2168,9 @@
https://develop.sentry.dev/sdk/telemetry/attributes/
"""
if isinstance(val, (bool, int, float, str)):
if isinstance(val, float) and (math.isinf(val) or math.isnan(val)):
return str(val)

Check warning on line 2173 in sentry_sdk/utils.py

View check run for this annotation

@sentry/warden / warden: code-review

Non-finite float-array attributes still break envelope JSON serialization

Homogeneous float lists and tuples containing NaN or infinity pass through `format_attribute` unchanged. `serialize_attribute` then emits them as array values, but envelope payloads are encoded with `json_dumps(allow_nan=False)`, which raises `ValueError`. Normalize non-finite elements in float arrays before serialization, or otherwise ensure the resulting payload is valid JSON.

Check warning on line 2173 in sentry_sdk/utils.py

View check run for this annotation

@sentry/warden / warden: find-bugs

Non-finite floats in list/tuple attributes still break envelope JSON

Scalar NaN/Inf are stringified, but homogeneous float lists/tuples still keep non-finite values, so `json_dumps(..., allow_nan=False)` can fail and drop the envelope; sanitize array elements the same way (or reject/coerce the whole array).
Comment on lines +2171 to +2173

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Non-finite float-array attributes still break envelope JSON serialization

Homogeneous float lists and tuples containing NaN or infinity pass through format_attribute unchanged. serialize_attribute then emits them as array values, but envelope payloads are encoded with json_dumps(allow_nan=False), which raises ValueError. Normalize non-finite elements in float arrays before serialization, or otherwise ensure the resulting payload is valid JSON.

Evidence
  • format_attribute handles non-finite values only in its scalar-float branch; its homogeneous list and tuple branches return float elements unchanged.
  • serialize_attribute emits homogeneous lists as array values, so a value such as [float("nan")] remains in the payload.
  • Batchers place these serialized attributes in JSON payloads; PayloadRef.get_bytes() encodes them with json_dumps, which sets allow_nan=False and raises ValueError for NaN or infinity.
Also found at 1 additional location
  • tests/test_attributes.py:228-234

Identified by Warden · code-review · D6L-C4X

return val

if isinstance(val, (list, tuple)) and not val:
Expand Down
31 changes: 31 additions & 0 deletions tests/test_attributes.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

import sentry_sdk


Expand Down Expand Up @@ -221,3 +223,32 @@
assert span["attributes"]["user.name"] == "username"
assert span["attributes"]["user.email"] == "username@example.com"
assert span["attributes"]["user.ip_address"] == "127.0.0.1"


@pytest.mark.parametrize(
"bad_value, expected",
[
(float("inf"), "inf"),
(float("-inf"), "-inf"),
(float("nan"), "nan"),
],

Check warning on line 234 in tests/test_attributes.py

View check run for this annotation

@sentry/warden / warden: code-review

[D6L-C4X] Non-finite float-array attributes still break envelope JSON serialization (additional location)

Homogeneous float lists and tuples containing NaN or infinity pass through `format_attribute` unchanged. `serialize_attribute` then emits them as array values, but envelope payloads are encoded with `json_dumps(allow_nan=False)`, which raises `ValueError`. Normalize non-finite elements in float arrays before serialization, or otherwise ensure the resulting payload is valid JSON.

Check warning on line 234 in tests/test_attributes.py

View check run for this annotation

@sentry/warden / warden: find-bugs

[DHD-DR5] Non-finite floats in list/tuple attributes still break envelope JSON (additional location)

Scalar NaN/Inf are stringified, but homogeneous float lists/tuples still keep non-finite values, so `json_dumps(..., allow_nan=False)` can fail and drop the envelope; sanitize array elements the same way (or reject/coerce the whole array).
)
def test_unserializable_attributes(sentry_init, capture_items, bad_value, expected):
sentry_init(
traces_sample_rate=1.0,
trace_lifecycle="stream",
)

items = capture_items("span")

with sentry_sdk.traces.start_span(name="login", attributes={"bad_attr": bad_value}):
pass

sentry_sdk.flush()

assert items

(span,) = [item.payload for item in items]

assert "bad_attr" in span["attributes"]
assert span["attributes"]["bad_attr"] == expected
Loading