Skip to content

Commit eb08701

Browse files
Reject malformed header with trailing slash and add test case
1 parent e28c00c commit eb08701

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

sentry/src/main/java/io/sentry/SentryTraceHeader.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public final class SentryTraceHeader {
1717

1818
final Pattern SENTRY_TRACEPARENT_HEADER_REGEX =
1919
Pattern.compile(
20-
"^[ \\t]*(?<traceId>[0-9a-f]{32})-(?<spanId>[0-9a-f]{16})-?(?<sampled>[01])?[ \\t]*$",
20+
"^[ \\t]*(?<traceId>[0-9a-f]{32})-(?<spanId>[0-9a-f]{16})(?<sampled>-[01])?[ \\t]*$",
2121
Pattern.CASE_INSENSITIVE);
2222

2323
public SentryTraceHeader(
@@ -39,7 +39,8 @@ public SentryTraceHeader(final @NotNull String value) throws InvalidSentryTraceH
3939

4040
this.traceId = new SentryId(matcher.group("traceId"));
4141
this.spanId = new SpanId(matcher.group("spanId"));
42-
this.sampled = matcher.group("sampled") == null ? null : "1".equals(matcher.group("sampled"));
42+
this.sampled =
43+
matcher.group("sampled") == null ? null : "1".equals(matcher.group("sampled").substring(1));
4344
}
4445

4546
public @NotNull String getName() {

sentry/src/test/java/io/sentry/SentryTraceHeaderTest.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ class SentryTraceHeaderTest {
1616
assertEquals("sentry-trace header does not conform to expected format: $sentryId", ex.message)
1717
}
1818

19+
@Test
20+
fun `when there is a trailing dash without sampling decision throws exception`() {
21+
val sentryId = SentryId()
22+
val spanId = SpanId()
23+
val ex =
24+
assertFailsWith<InvalidSentryTraceHeaderException> { SentryTraceHeader("$sentryId-$spanId-") }
25+
assertEquals(
26+
"sentry-trace header does not conform to expected format: $sentryId-$spanId-",
27+
ex.message,
28+
)
29+
}
30+
1931
@Test
2032
fun `when trace-id has less than 32 characters throws exception`() {
2133
val sentryId = SentryId().toString().substring(0, 8)

0 commit comments

Comments
 (0)