Skip to content

Commit

Permalink
fix(propagation): filter out unsupported dd_origin values
Browse files Browse the repository at this point in the history
  • Loading branch information
nirrattner committed Sep 7, 2023
1 parent 6fa9b0e commit d73529c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
4 changes: 3 additions & 1 deletion ddtrace/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
]


_DD_ORIGIN_INVALID_CHARS_REGEX = re.compile(r"[^\x20-\x7E]+")

log = get_logger(__name__)


Expand Down Expand Up @@ -64,7 +66,7 @@ def __init__(
self.trace_id = trace_id # type: Optional[int]
self.span_id = span_id # type: Optional[int]

if dd_origin is not None:
if dd_origin is not None and _DD_ORIGIN_INVALID_CHARS_REGEX.search(dd_origin) is None:
self._meta[ORIGIN_KEY] = dd_origin
if sampling_priority is not None:
self._metrics[SAMPLING_PRIORITY_KEY] = sampling_priority
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
fixes:
- |
propagation: Prevent propagating unsupported non-ascii ``origin`` header values.
17 changes: 17 additions & 0 deletions tests/tracer/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,3 +326,20 @@ def test_traceparent(context, expected_traceparent):
def test_tracestate(context, expected_tracestate):
# type: (Context,str) -> None
assert context._tracestate == expected_tracestate


@pytest.mark.parametrize(
"ctx,expected_dd_origin",
[
(Context(), None),
(Context(dd_origin="abcABC"), "abcABC"),
(Context(dd_origin="abcABC123"), "abcABC123"),
(Context(dd_origin="!@#$%^&*()>?"), "!@#$%^&*()>?"),
(Context(dd_origin="\x00\x10"), None),
(Context(dd_origin="\x80"), None),
(Context(dd_origin="§¢À"), None),
],
)
def test_dd_origin_character_set(ctx, expected_dd_origin):
# type: (Context,Optional[str]) -> None
assert ctx.dd_origin == expected_dd_origin

0 comments on commit d73529c

Please sign in to comment.