Skip to content

Commit a3d87a6

Browse files
authored
Update sentry trace regexp (#2678)
1 parent 53f3b58 commit a3d87a6

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

sentry-ruby/lib/sentry/propagation_context.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@
88
module Sentry
99
class PropagationContext
1010
SENTRY_TRACE_REGEXP = Regexp.new(
11-
"^[ \t]*" + # whitespace
12-
"([0-9a-f]{32})?" + # trace_id
11+
"\\A([0-9a-f]{32})?" + # trace_id
1312
"-?([0-9a-f]{16})?" + # span_id
14-
"-?([01])?" + # sampled
15-
"[ \t]*$" # whitespace
13+
"-?([01])?\\z" # sampled
1614
)
1715

1816
# An uuid that can be used to identify a trace.
@@ -43,7 +41,10 @@ class PropagationContext
4341
# @param sentry_trace [String] the sentry-trace header value from the previous transaction.
4442
# @return [Array, nil]
4543
def self.extract_sentry_trace(sentry_trace)
46-
match = SENTRY_TRACE_REGEXP.match(sentry_trace)
44+
value = sentry_trace.to_s.strip
45+
return if value.empty?
46+
47+
match = SENTRY_TRACE_REGEXP.match(value)
4748
return if match.nil?
4849

4950
trace_id, parent_span_id, sampled_flag = match[1..3]

sentry-ruby/spec/sentry/propagation_context_spec.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,41 @@
132132
expect(subject.get_dynamic_sampling_context).to eq(subject.get_baggage.dynamic_sampling_context)
133133
end
134134
end
135+
136+
describe ".extract_sentry_trace" do
137+
it "extracts valid sentry-trace without whitespace" do
138+
sentry_trace = "771a43a4192642f0b136d5159a501700-7c51afd529da4a2a-1"
139+
result = described_class.extract_sentry_trace(sentry_trace)
140+
141+
expect(result).to eq(["771a43a4192642f0b136d5159a501700", "7c51afd529da4a2a", true])
142+
end
143+
144+
it "extracts valid sentry-trace with leading and trailing whitespace" do
145+
sentry_trace = " \t771a43a4192642f0b136d5159a501700-7c51afd529da4a2a-1\t "
146+
result = described_class.extract_sentry_trace(sentry_trace)
147+
148+
expect(result).to eq(["771a43a4192642f0b136d5159a501700", "7c51afd529da4a2a", true])
149+
end
150+
151+
it "extracts sentry-trace without sampled flag" do
152+
sentry_trace = "771a43a4192642f0b136d5159a501700-7c51afd529da4a2a"
153+
result = described_class.extract_sentry_trace(sentry_trace)
154+
155+
expect(result).to eq(["771a43a4192642f0b136d5159a501700", "7c51afd529da4a2a", nil])
156+
end
157+
158+
it "returns nil for invalid sentry-trace" do
159+
expect(described_class.extract_sentry_trace("invalid")).to be_nil
160+
expect(described_class.extract_sentry_trace("000-000-0")).to be_nil
161+
expect(described_class.extract_sentry_trace("")).to be_nil
162+
end
163+
164+
it "allows whitespace" do
165+
whitespace = " \t \t \t \t "
166+
sentry_trace = "#{whitespace}771a43a4192642f0b136d5159a501700-7c51afd529da4a2a-1#{whitespace}"
167+
result = described_class.extract_sentry_trace(sentry_trace)
168+
169+
expect(result).to eq(["771a43a4192642f0b136d5159a501700", "7c51afd529da4a2a", true])
170+
end
171+
end
135172
end

0 commit comments

Comments
 (0)