Skip to content

fix(trace): reject tracestate keys with vendor part longer than 13 chars - #5599

Draft
sidsri14 wants to merge 2 commits into
open-telemetry:mainfrom
sidsri14:fix/tracestate-vendor-key-validation
Draft

fix(trace): reject tracestate keys with vendor part longer than 13 chars#5599
sidsri14 wants to merge 2 commits into
open-telemetry:mainfrom
sidsri14:fix/tracestate-vendor-key-validation

Conversation

@sidsri14

Copy link
Copy Markdown

Which problem is this PR solving?

Fixes #5136. TraceState.add() (and TraceState construction) did not fully
validate keys against the W3C tracestate spec: the vendor part of a key
(after @) was allowed to be up to 14 characters, but the spec requires it to
be at most 13. As a result, illegal keys such as 1@nrabcdefghijkl were
silently accepted.

Short description of the changes

  • In opentelemetry-api/src/opentelemetry/trace/span.py, tightened the vendor
    quantifier in _KEY_FORMAT from {0,13} to {0,12} so the vendor part is
    at most 13 characters (1 + 12).
  • Invalid pairs are still discarded (the existing behavior): add() returns
    the unchanged TraceState rather than adding the illegal key.
  • Added regression test test_tracestate_rejects_illegal_vendor_key covering a
    14-char vendor, a long-tenant + 14-char-vendor key, a valid 241-char-tenant
    key, and a non-vendor key starting with a digit.

How to verify that this has the expected result

pytest opentelemetry-api/tests/trace/test_tracestate.py

All 11 tests pass. The new test asserts over-long vendor keys are not added
(len == 0) while valid keys remain accepted.

Assisted-by: opencode
Signed-off-by: Siddharth Srivastava 128143077+sidsri14@users.noreply.github.com

TraceState validated keys with a regex whose tenant/vendor alternative allowed
up to 14 characters (`[a-z][_0-9a-z\-\*\/]{0,13}`). Per the W3C tracestate
spec the vendor part (after `@`) MUST be at most 13 characters, so over-long
vendor keys such as `1@nrabcdefghijkl` were incorrectly accepted by `add()`.

Tighten the vendor quantifier to `{0,12}` so 14-character vendors are rejected.
Invalid pairs are discarded (the existing behavior), so `add()` returns the
unchanged TraceState instead of adding the illegal key.

Fixes open-telemetry#5136

Assisted-by: opencode
Signed-off-by: Siddharth Srivastava <128143077+sidsri14@users.noreply.github.com>
Signed-off-by: Siddharth Srivastava <128143077+sidsri14@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TraceState does not check illegal inputs

1 participant