Skip to content

Fix crashes from unguarded str()/repr() on invalid attribute keys/values - #5598

Open
piyush182004 wants to merge 3 commits into
open-telemetry:mainfrom
piyush182004:fix/unguarded-str-crashes-on-invalid-attributes
Open

piyush182004 wants to merge 3 commits into
open-telemetry:mainfrom
piyush182004:fix/unguarded-str-crashes-on-invalid-attributes

Conversation

@piyush182004

@piyush182004 piyush182004 commented Aug 29, 2026 •

Copy link
Copy Markdown
Contributor

Description

Two call sites use "best-effort stringify" as a fallback for attribute
keys/values and log exception messages, but neither guards the str()
call itself, so an object whose __str__/__repr__ raises crashes the
caller's application:

  • opentelemetry-api/src/opentelemetry/attributes/__init__.py:
    _clean_attribute_value's docstring says an unstringifiable value "is
    replaced with None", but str(key)/str(value) were unguarded, so an
    object whose __str__/__repr__ itself raises crashed
    span.set_attribute(...) instead.

  • opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py:
    LoggingHandler._get_attributes called str(value.args[0]) unguarded
    when translating a stdlib LogRecord's exc_info, crashing the common
    logger.exception(...) pattern whenever the raised exception's first
    arg has a raising __str__.

Both now catch the stringification failure, log a warning, and degrade
gracefully (drop the offending key, or replace the value with None / a
placeholder message) instead of propagating the exception into the
caller's application code — which observability/telemetry code should
never do.

Note: Addressed review feedback from @herin049:

  • An earlier version of this PR also guarded _encode_baggage_pairs in the
    W3C baggage propagator. That change has been dropped (baggage keys and
    values are required to be strictly UTF-8 strings, so W3CBaggagePropagator
    is not the place to guard against a raising __str__/__repr__).
  • The redundant try/except around str() in _clean_attribute_value has
    been removed: the old _is_non_custom_str() check plus the per-call guard
    are now unified into a single _best_effort_str() helper that returns
    None both for objects with only the default object.__repr__ and for a
    custom __str__/__repr__ that raises.

The branch has been rebased onto the latest main.

Fixes #5597

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Added 4 regression tests across the two affected test files:

  • test_clean_attribute_value_raising_repr_does_not_crash,
    test_bounded_attributes_raising_repr_value_does_not_crash,
    test_bounded_attributes_raising_repr_key_does_not_crash
    (opentelemetry-api/tests/attributes/test_attributes.py)
  • test_log_record_exception_with_raising_arg_does_not_crash
    (opentelemetry-sdk/tests/logs/test_handler.py)

Verified each test is load-bearing: reverting the matching production fix
makes the test fail with the original crash traceback, and restoring the
fix makes it pass.

Ran the affected opentelemetry-api and opentelemetry-sdk suites, plus
ruff check, ruff format --check, pylint (rated 10.00/10), and pyright
— all clean.

  • Test A: test_clean_attribute_value_raising_repr_does_not_crash

Does This PR Require a Contrib Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@piyush182004
piyush182004 requested a review from a team as a code owner August 29, 2026 08:28
@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Aug 29, 2026 •

Copy link
Copy Markdown

Pull request dashboard status

Waiting on reviewers · refreshed 2026-11-22 20:26 UTC

Review the latest changes.

Status above doesn't look right?
  • Just replied or pushed? Anything around or after the refresh time above may not be picked up yet — give it a few minutes.
  • Anything look wrong? Report it with what you expected; it helps us improve the dashboard.

@github-actions

Copy link
Copy Markdown

This PR has been automatically marked as stale because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 days of this comment.
If you're still working on this, please add a comment or push new commits.

@github-actions github-actions Bot added the Stale label Sep 17, 2026
@piyush182004

Copy link
Copy Markdown
Contributor Author

Still active on this, just waiting on review. Happy to rebase or split it up if that helps.

@github-actions github-actions Bot removed the Stale label Oct 2, 2026
@piyush182004
piyush182004 force-pushed the fix/unguarded-str-crashes-on-invalid-attributes branch 3 times, most recently from 17cdf10 to 539115f Compare October 16, 2026 17:53
@piyush182004

Copy link
Copy Markdown
Contributor Author

@herin049 can you please review this once?

@github-project-automation github-project-automation Bot moved this to Reviewed PRs that need fixes in Python PR digest Nov 5, 2026
@opentelemetry-pr-dashboard

This comment has been minimized.

Two call sites use "best-effort stringify" as a fallback for attribute
keys/values and log exception messages, but neither guards the str()
call itself, so an object whose __str__/__repr__ raises crashes the
caller's application:

- opentelemetry-api/src/opentelemetry/attributes/__init__.py:
  _clean_attribute_value's docstring says an unstringifiable value "is
  replaced with None", but str(key)/str(value) were unguarded, so an
  object whose __str__/__repr__ itself raises crashed
  span.set_attribute(...) instead.

- opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py:
  LoggingHandler._get_attributes called str(value.args[0]) unguarded
  when translating a stdlib LogRecord's exc_info, crashing the common
  logger.exception(...) pattern whenever the raised exception's first
  arg has a raising __str__.

Both now catch the stringification failure, log a warning, and degrade
gracefully (drop the offending key or replace the value) instead of
propagating the exception into the caller's application code -- which
observability/telemetry code should never do.

The baggage propagation change from the original version of this PR has
been dropped per review: baggage keys and values are required to be
strictly UTF-8 strings, so W3CBaggagePropagator is not the place to
guard against a raising __str__/__repr__.

Fixes open-telemetry#5597
@piyush182004
piyush182004 force-pushed the fix/unguarded-str-crashes-on-invalid-attributes branch from 539115f to 0562e43 Compare November 13, 2026 19:00
herin049 noted the extra try/except around str() in _clean_attribute_value
was redundant given the existing _is_non_custom_str() check. Replace both
with a single _best_effort_str() helper that returns None when the type
only has the default object.__repr__ (so such objects are dropped, as
before) and also when a custom __str__/__repr__ raises (so the failure no
longer propagates into the caller). This removes the duplicated guard at
both call sites and simplifies _clean_attribute_value below pylint's
branch limits, so the earlier too-many-branches/too-many-return-statements
suppression is no longer needed.

No behavior change: objects with only the default repr are still dropped,
raising stringifiers still degrade to None, and the existing regression
tests continue to pass unmodified.
@piyush182004
piyush182004 force-pushed the fix/unguarded-str-crashes-on-invalid-attributes branch from 0562e43 to 6cea595 Compare November 13, 2026 19:07
@piyush182004

Copy link
Copy Markdown
Contributor Author

@herin049 can you please review it again did the fixes !

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Reviewed PRs that need fixes

Development

Successfully merging this pull request may close these issues.

Unguarded str()/repr() calls crash the app when an attribute key/value's __str__/__repr__ raises

3 participants