Skip to content

Commit 3b99add

Browse files
author
Piotr Boniecki
committed
Ensure there is no new line character in header value
1 parent e716108 commit 3b99add

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

lib/http/headers.rb

+10
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def delete(name)
5050
# @return [void]
5151
def add(name, value)
5252
name = normalize_header name.to_s
53+
validate_value(value)
5354
Array(value).each { |v| @pile << [name, v.to_s] }
5455
end
5556

@@ -209,5 +210,14 @@ def normalize_header(name)
209210

210211
raise HeaderError, "Invalid HTTP header field name: #{name.inspect}"
211212
end
213+
214+
# Ensures there is no new line character in the header value
215+
#
216+
# @param [String] value
217+
# @raise [HeaderError] if value includes new line character
218+
def validate_value(value)
219+
return unless value && value.include?("\n")
220+
raise HeaderError, "Invalid HTTP header field value: #{value.inspect}"
221+
end
212222
end
213223
end

spec/lib/http/headers_spec.rb

+10
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@
4141
to raise_error HTTP::HeaderError
4242
end
4343
end
44+
45+
it "fails with invalid header value" do
46+
expect { headers.set "foo", "bar\nEvil-Header: evil-value" }.
47+
to raise_error HTTP::HeaderError
48+
end
4449
end
4550

4651
describe "#[]=" do
@@ -127,6 +132,11 @@
127132
to raise_error HTTP::HeaderError
128133
end
129134
end
135+
136+
it "fails with invalid header value" do
137+
expect { headers.add "foo", "bar\nEvil-Header: evil-value" }.
138+
to raise_error HTTP::HeaderError
139+
end
130140
end
131141

132142
describe "#get" do

0 commit comments

Comments
 (0)