Skip to content

Commit 65670cf

Browse files
authored
Add validation for Trace ID (#1992)
1 parent 13f09db commit 65670cf

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
- `opentelemetry-distro` & `opentelemetry-sdk` Moved Auto Instrumentation Configurator code to SDK
99
to let distros use its default implementation
1010
([#1937](https://github.com/open-telemetry/opentelemetry-python/pull/1937))
11+
- Add Trace ID validation to meet [TraceID spec](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/overview.md#spancontext) ([#1992](https://github.com/open-telemetry/opentelemetry-python/pull/1992))
1112

1213
## [0.23.1](https://github.com/open-telemetry/opentelemetry-python/pull/1987) - 2021-07-26
1314

opentelemetry-api/src/opentelemetry/trace/span.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ def values(self) -> typing.ValuesView[str]:
389389

390390

391391
DEFAULT_TRACE_STATE = TraceState.get_default()
392+
_TRACE_ID_HEX_LENGTH = 2 ** 128 - 1
392393

393394

394395
class SpanContext(
@@ -420,7 +421,11 @@ def __new__(
420421
if trace_state is None:
421422
trace_state = DEFAULT_TRACE_STATE
422423

423-
is_valid = trace_id != INVALID_TRACE_ID and span_id != INVALID_SPAN_ID
424+
is_valid = (
425+
trace_id != INVALID_TRACE_ID
426+
and span_id != INVALID_SPAN_ID
427+
and trace_id < _TRACE_ID_HEX_LENGTH
428+
)
424429

425430
return tuple.__new__(
426431
cls,

opentelemetry-api/tests/trace/test_span_context.py

+9
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,12 @@ def test_span_context_pickle(self):
3434
pickle_sc = pickle.loads(pickle.dumps(sc))
3535
self.assertEqual(sc.trace_id, pickle_sc.trace_id)
3636
self.assertEqual(sc.span_id, pickle_sc.span_id)
37+
38+
invalid_sc = trace.SpanContext(
39+
9999999999999999999999999999999999999999999999999999999999999999999999999999,
40+
9,
41+
is_remote=False,
42+
trace_flags=trace.DEFAULT_TRACE_OPTIONS,
43+
trace_state=trace.DEFAULT_TRACE_STATE,
44+
)
45+
self.assertFalse(invalid_sc.is_valid)

0 commit comments

Comments
 (0)