Skip to content

Commit 13732e8

Browse files
fix(tracing): disallow the propagation of negative trace IDs [backport 1.13] (#6135)
Backport 3380b10 from #6133 to 1.13. This pull request fixes the symptom reported in #6125 by ensuring that ddtrace does not set negative values in the trace ID propagation header. Whatever underlying issue is causing the trace ID to be negative still exists despite this change. Co-authored-by: Emmett Butler <[email protected]>
1 parent c381481 commit 13732e8

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

ddtrace/propagation/http.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def _extract(headers):
217217
except ValueError:
218218
trace_id = 0
219219

220-
if trace_id == 0 or trace_id > _MAX_UINT_64BITS:
220+
if trace_id <= 0 or trace_id > _MAX_UINT_64BITS:
221221
log.warning(
222222
"Invalid trace id: %r. `x-datadog-trace-id` must be greater than zero and less than 2**64", trace_id_str
223223
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
fixes:
3+
- |
4+
tracing: This fix resolves an issue where negative trace ID values were allowed to propagate via Datadog
5+
distributed tracing HTTP headers.

tests/tracer/test_propagation.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,17 @@ def test_extract_tracecontext(headers, expected_context):
10751075
"dd_origin": "synthetics",
10761076
},
10771077
),
1078+
(
1079+
"invalid_datadog_negative_trace_id",
1080+
[PROPAGATION_STYLE_DATADOG],
1081+
{
1082+
HTTP_HEADER_TRACE_ID: "-1",
1083+
HTTP_HEADER_PARENT_ID: "5678",
1084+
HTTP_HEADER_SAMPLING_PRIORITY: "1",
1085+
HTTP_HEADER_ORIGIN: "synthetics",
1086+
},
1087+
CONTEXT_EMPTY,
1088+
),
10781089
(
10791090
"valid_datadog_explicit_style_wsgi",
10801091
[PROPAGATION_STYLE_DATADOG],
@@ -1560,7 +1571,6 @@ def test_propagation_extract_env(name, styles, headers, expected_context, run_py
15601571
env["DD_TRACE_PROPAGATION_STYLE"] = ",".join(styles)
15611572
stdout, stderr, status, _ = run_python_code_in_subprocess(code=code, env=env)
15621573
assert status == 0, (stdout, stderr)
1563-
assert stderr == b"", (stdout, stderr)
15641574

15651575
result = json.loads(stdout.decode())
15661576
assert result == expected_context

0 commit comments

Comments
 (0)