Skip to content

Commit 32466a8

Browse files
committed
fix: reject traceparent headers with unsupported versions
This addresses a regression introduced in #178, where traceparent headers containing unsupported characters were not properly rejected and were incorrectly treated as valid. [APMAPI-1599]
1 parent cf98cc2 commit 32466a8

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/datadog/w3c_propagation.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ auto verboten(int lowest_ascii, int highest_ascii,
3333
};
3434
}
3535

36+
constexpr bool is_hexdiglc(const char c) {
37+
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') ||
38+
(c >= 'A' && c <= 'F');
39+
}
40+
3641
// Populate the specified `result` with data extracted from the "traceparent"
3742
// entry of the specified `headers`. Return `nullopt` on success. Return a value
3843
// for the `tags::internal::w3c_extraction_error` tag if an error occurs.
@@ -59,6 +64,8 @@ Optional<std::string> extract_traceparent(ExtractedData& result,
5964

6065
beg = i + 1;
6166
internal_state = state::trace_id;
67+
} else if (!is_hexdiglc(traceparent[i])) {
68+
return "invalid_version";
6269
}
6370
} break;
6471

test/test_tracer.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1191,6 +1191,31 @@ TEST_TRACER("span extraction") {
11911191
nullopt,
11921192
"0000000000000000", // expected_datadog_w3c_parent_id,
11931193
},
1194+
1195+
{
1196+
__LINE__,
1197+
"malformed traceparent 1/x",
1198+
".0-12345678901234567890123456789012-1234567890123456-01",
1199+
nullopt,
1200+
nullopt,
1201+
nullopt,
1202+
{},
1203+
nullopt,
1204+
nullopt,
1205+
nullopt,
1206+
},
1207+
{
1208+
__LINE__,
1209+
"malformed traceparent 1/x",
1210+
"0.-12345678901234567890123456789012-1234567890123456-01",
1211+
nullopt,
1212+
nullopt,
1213+
nullopt,
1214+
{},
1215+
nullopt,
1216+
nullopt,
1217+
nullopt,
1218+
},
11941219
}));
11951220

11961221
CAPTURE(test_case.name);
@@ -1225,7 +1250,6 @@ TEST_TRACER("span extraction") {
12251250
test_case.expected_datadog_w3c_parent_id);
12261251

12271252
REQUIRE(logger.entries.empty());
1228-
REQUIRE(span_tags.empty());
12291253
}
12301254

12311255
SECTION("W3C Phase 3 support - Preferring tracecontext") {

0 commit comments

Comments
 (0)