Skip to content

Commit 5fe0f96

Browse files
hsbtclaude
andcommitted
Validate Content-Type value set by set_content_type
set_content_type assigned to @Header directly, bypassing the CR/LF validation in set_field, so a crafted media type or parameter could inject header lines. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 4f13ea5 commit 5fe0f96

2 files changed

Lines changed: 6 additions & 1 deletion

File tree

lib/net/http/header.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,7 @@ def type_params
782782
#
783783
# Net::HTTPHeader#content_type= is an alias for Net::HTTPHeader#set_content_type.
784784
def set_content_type(type, params = {})
785-
@header['content-type'] = [type + params.map{|k,v|"; #{k}=#{v}"}.join('')]
785+
set_field('Content-Type', type + params.map{|k,v|"; #{k}=#{v}"}.join(''))
786786
end
787787

788788
alias content_type= set_content_type

test/net/http/test_httpheader.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,11 @@ def test_type_params
451451
end
452452

453453
def test_set_content_type
454+
@c.set_content_type 'text/html', {'charset' => 'utf-8'}
455+
assert_equal 'text/html; charset=utf-8', @c['content-type']
456+
assert_raise(ArgumentError){ @c.set_content_type "text/html\r\nFoo: bar" }
457+
assert_raise(ArgumentError){ @c.set_content_type 'text/html', {'charset' => "x\r\nFoo: bar"} }
458+
assert_raise(ArgumentError){ @c.set_content_type 'text/html', {"x\nFoo: bar" => 'utf-8'} }
454459
end
455460

456461
def test_form_data=

0 commit comments

Comments
 (0)