Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(w3c): fix traceparent validation #4791

Merged
merged 15 commits into from
Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions ddtrace/propagation/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,16 +552,18 @@ def _get_traceparent_values(tp):
"""

version, trace_id_hex, span_id_hex, trace_flags_hex = tp.strip().split("-")
# check version is a valid hexadecimal, if not it's invalid we will move on to the next prop method
int(version, 16)
# ensure traceparent values are valid hexadecimals, if not it's invalid we will move on
# to the next prop method
for val in [version, trace_id_hex, span_id_hex, trace_flags_hex]:
int(val, 16)
mabdinur marked this conversation as resolved.
Show resolved Hide resolved
# https://www.w3.org/TR/trace-context/#version
if version == "ff":
raise ValueError("'ff' is an invalid traceparent version")
# currently 00 is the only version format, but if future versions come up we may need to add changes
if version != "00":
mabdinur marked this conversation as resolved.
Show resolved Hide resolved
log.warning("unsupported traceparent version:%r, still attempting to parse", version)

if len(trace_id_hex) == 32 and len(span_id_hex) == 16 and len(trace_flags_hex) >= 2:
if len(version) == 2 and len(trace_id_hex) == 32 and len(span_id_hex) == 16 and len(trace_flags_hex) == 2:
mabdinur marked this conversation as resolved.
Show resolved Hide resolved
trace_id = _hex_id_to_dd_id(trace_id_hex)
span_id = _hex_id_to_dd_id(span_id_hex)

Expand Down Expand Up @@ -646,7 +648,7 @@ def _extract(headers):
return None
# uppercase char in tp makes it invalid:
# https://www.w3.org/TR/trace-context/#traceparent-header-field-values
if not tp.islower():
if any(x.isupper() for x in tp):
mabdinur marked this conversation as resolved.
Show resolved Hide resolved
raise ValueError("uppercase characters are not allowed in traceparent")
trace_id, span_id, sampling_priority = _TraceContext._get_traceparent_values(tp)
except (ValueError, AssertionError):
Expand Down
14 changes: 6 additions & 8 deletions tests/tracer/test_propagation.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,16 +449,13 @@ def test_tracecontext_get_sampling_priority(sampling_priority_tp, sampling_prior
# tp, trace_id, span_id, sampling_priority
(11803532876627986230, 67667974448284343, 1),
["unsupported traceparent version:'0', still attempting to parse"],
None,
ValueError,
),
(
"00-f92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01",
# tp, trace_id, span_id, sampling_priority
None,
[
"received invalid w3c traceparent: 00-f92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01.",
"W3C traceparent hex length incorrect: 00-f92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01",
],
[],
ValueError,
),
( # we still parse the trace flag and analyze the it as a bit field
Expand Down Expand Up @@ -489,9 +486,10 @@ def test_extract_traceparent(caplog, headers, expected_tuple, expected_logging,
else:
traceparent_values = _TraceContext._get_traceparent_values(headers)
assert traceparent_values == expected_tuple
if caplog.text or expected_logging:
for expected_log in expected_logging:
assert expected_log in caplog.text

if caplog.text or expected_logging:
for expected_log in expected_logging:
assert expected_log in caplog.text


@pytest.mark.parametrize(
Expand Down