Skip to content

Commit b417159

Browse files
authored
Validate trace-id contains valid characters (#2747)
1 parent be35c3c commit b417159

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

gems/aws-sdk-core/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Unreleased Changes
22
------------------
33

4+
* Issue - Validate that `_X_AMZN_TRACE_ID` ENV value contains only valid, non-control characters.
5+
46
3.133.0 (2022-08-22)
57
------------------
68

gems/aws-sdk-core/lib/aws-sdk-core/plugins/recursion_detection.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,21 @@ def call(context)
1111

1212
unless context.http_request.headers.key?('x-amzn-trace-id')
1313
if ENV['AWS_LAMBDA_FUNCTION_NAME'] &&
14-
(trace_id = ENV['_X_AMZN_TRACE_ID'])
14+
(trace_id = validate_header(ENV['_X_AMZN_TRACE_ID']))
1515
context.http_request.headers['x-amzn-trace-id'] = trace_id
1616
end
1717
end
1818
@handler.call(context)
1919
end
20+
21+
private
22+
def validate_header(header_value)
23+
if (header_value.chars & (0..31).map(&:chr)).any?
24+
raise ArgumentError, 'Invalid _X_AMZN_TRACE_ID value: '\
25+
'contains ASCII control characters'
26+
end
27+
header_value
28+
end
2029
end
2130

2231
# should be at the end of build so that

gems/aws-sdk-core/spec/aws/plugins/recursion_detection_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,16 @@ module Plugins
8787
resp = client.operation_with_trace_id(trace_id: user_trace_id)
8888
expect(resp.context.http_request.headers['x-amzn-trace-id']).to eq(user_trace_id)
8989
end
90+
91+
context 'X_AMX_TRACE_ID with invalid characters' do
92+
let(:env_trace_id) { "invalid header" + 31.chr }
93+
94+
it 'validates the trace-id and raises an error' do
95+
expect do
96+
client.operation_with_trace_id
97+
end.to raise_error(ArgumentError)
98+
end
99+
end
90100
end
91101
end
92102
end

0 commit comments

Comments
 (0)