Skip to content

Commit 93884b8

Browse files
committed
Properly strip whitespace from the right side of header values
1 parent 25b9e48 commit 93884b8

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

lib/protocol/http1/connection.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ module HTTP1
3939
FIELD_NAME = TOKEN
4040
OWS = /[ \t]*/
4141
# A field value is any string of characters that does not contain a null character, CR, or LF. After reflecting on the RFCs and surveying real implementations, I came to the conclusion that the RFCs are too restrictive. Most servers only check for the presence of null bytes, and obviously CR/LF characters have semantic meaning in the parser. So, I decided to follow this defacto standard, even if I'm not entirely happy with it.
42-
FIELD_VALUE = /[^\0\r\n]+/.freeze
43-
HEADER = /\A(#{FIELD_NAME}):#{OWS}(?:(#{FIELD_VALUE})#{OWS})?\z/.freeze
42+
FIELD_VALUE = /[^\0\r\n]*?/.freeze
43+
HEADER = /\A(#{FIELD_NAME}):#{OWS}(#{FIELD_VALUE})#{OWS}\z/.freeze
4444

4545
VALID_FIELD_NAME = /\A#{FIELD_NAME}\z/.freeze
46-
VALID_FIELD_VALUE = /\A#{FIELD_VALUE}?\z/.freeze
46+
VALID_FIELD_VALUE = /\A#{FIELD_VALUE}\z/.freeze
4747

4848
DEFAULT_MAXIMUM_LINE_LENGTH = 8192
4949

test/protocol/http1/connection/headers.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,19 @@ def validate_headers!(expected_headers = self.headers)
106106
end
107107
end
108108

109+
with "a header that contains trailing whitespace" do
110+
let(:headers) {[
111+
"has-trailing-whitespace: here it is \t"
112+
]}
113+
114+
it "can parse the header" do
115+
authority, method, target, version, headers, body = server.read_request
116+
expect(headers).to have_keys(
117+
"has-trailing-whitespace" => be == ["here it is"]
118+
)
119+
end
120+
end
121+
109122
with "a header that contains obsolete folding whitespace" do
110123
let(:headers) {[
111124
"user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko)\n\tChrome/55.0.2883.95 Safari/537.36"

0 commit comments

Comments
 (0)