Skip to content

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

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

piyush182004 wants to merge 1 commit 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: An earlier version of this PR also guarded _encode_baggage_pairs
in the W3C baggage propagator. That change 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__). The branch has been rebased onto the latest main
and squashed to a single commit.

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, and pylint (rated 10.00/10) — 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-10-17 10:43 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
Comment thread opentelemetry-api/src/opentelemetry/baggage/propagation/__init__.py
@piyush182004
piyush182004 force-pushed the fix/unguarded-str-crashes-on-invalid-attributes branch 2 times, most recently from f616f46 to 17cdf10 Compare October 16, 2026 17:41
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 17cdf10 to 539115f Compare October 16, 2026 17:53
@piyush182004

Copy link
Copy Markdown
Contributor Author

@herin049 can you please review this once?

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: No status

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