Skip to content

Commit

Permalink
fix(w3c trace context): consider traceparent format version ff invalid
Browse files Browse the repository at this point in the history
This is a follow up to commit
f283a4b
which added some tests for parsing newer versions. One of the tests used
format version "ff", which the W3C trace context specification
explicitly defines as invalid. This commit fixes that test and also the
regex used for parsing the traceparent header, so that a header with
version ff will be rejected as invalid.

Signed-off-by: Bastian Krol <bastian.krol@ibm.com>
  • Loading branch information
Bastian Krol authored and Ferenc- committed May 20, 2023
1 parent a7a89db commit b8c5179
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion instana/w3c_trace_context/traceparent.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class Traceparent:
SPECIFICATION_VERSION = "00"
TRACEPARENT_REGEX = re.compile("^[0-9a-f]{2}-(?!0{32})([0-9a-f]{32})-(?!0{16})([0-9a-f]{16})-[0-9a-f]{2}")
TRACEPARENT_REGEX = re.compile("^[0-9a-f][0-9a-e]-(?!0{32})([0-9a-f]{32})-(?!0{16})([0-9a-f]{16})-[0-9a-f]{2}")

def validate(self, traceparent):
"""
Expand Down
8 changes: 6 additions & 2 deletions tests/w3c_trace_context/test_traceparent.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ def test_validate_valid(self):
def test_validate_newer_version(self):
# Although the incoming traceparent header sports a newer version number, we should still be able to parse the
# parts that we understand (and consider it valid).
traceparent = "ff-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01-12345-abcd"
traceparent = "fe-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01-12345-abcd"
self.assertEqual(traceparent, self.tp.validate(traceparent))

def test_validate_unknown_flags(self):
traceparent = "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-ee"
self.assertEqual(traceparent, self.tp.validate(traceparent))

def test_validate_invalid_traceparent_version(self):
traceparent = "ff-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01"
self.assertIsNone(self.tp.validate(traceparent))

def test_validate_invalid_traceparent(self):
traceparent = "00-4bxxxxx3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01"
self.assertIsNone(self.tp.validate(traceparent))
Expand All @@ -48,7 +52,7 @@ def test_get_traceparent_fields_unsampled(self):
def test_get_traceparent_fields_newer_version(self):
# Although the incoming traceparent header sports a newer version number, we should still be able to parse the
# parts that we understand (and consider it valid).
traceparent = "ff-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01-12345-abcd"
traceparent = "fe-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01-12345-abcd"
version, trace_id, parent_id, sampled_flag = self.tp.get_traceparent_fields(traceparent)
self.assertEqual(trace_id, "4bf92f3577b34da6a3ce929d0e0e4736")
self.assertEqual(parent_id, "00f067aa0ba902b7")
Expand Down

0 comments on commit b8c5179

Please sign in to comment.