Skip to content

Commit 120d7cb

Browse files
Validate HTTP/1 request methods, targets, and header values. (#60)
1 parent 87a1ee5 commit 120d7cb

4 files changed

Lines changed: 229 additions & 17 deletions

File tree

‎lib/protocol/http1/connection.rb‎

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ module HTTP1
3333

3434
# HTTP/1.x request line parser:
3535
TOKEN = /[!#$%&'*+\-\.\^_`|~0-9a-zA-Z]+/.freeze
36+
VALID_METHOD = /\A#{TOKEN}\z/.freeze
3637
REQUEST_LINE = /\A(#{TOKEN}) ([^\s]+) (HTTP\/\d.\d)\z/.freeze
3738

3839
# HTTP/1.x header parser:
@@ -185,7 +186,8 @@ def write_connection_header(version)
185186

186187
# Write the appropriate header for connection upgrade.
187188
def write_upgrade_header(upgrade)
188-
write_stream("connection: upgrade\r\nupgrade: #{upgrade}\r\n")
189+
write_header(CONNECTION, "upgrade")
190+
write_header(UPGRADE, upgrade)
189191
end
190192

191193
# Indicates whether the connection has been hijacked meaning its IO has been handed over and is not usable anymore.
@@ -253,13 +255,32 @@ def open!
253255
#
254256
# Transitions to the open state.
255257
#
258+
# Requires the method to be an HTTP token and rejects CR, LF, and NUL bytes in the authority and target before writing any request bytes.
259+
#
256260
# @parameter authority [String] the authority of the request.
257261
# @parameter method [String] the HTTP method.
258262
# @parameter target [String] the request target.
259263
# @parameter version [String] the HTTP version.
260264
# @parameter headers [Hash] the HTTP headers.
261265
# @raises [RefusedError] if the request was not processed.
262266
def write_request(authority, method, target, version, headers)
267+
method = method.to_s
268+
unless method.b.match?(VALID_METHOD)
269+
raise InvalidRequest, "Invalid request method: #{method.inspect}"
270+
end
271+
272+
target = target.to_s
273+
unless target.b.match?(VALID_FIELD_VALUE)
274+
raise InvalidRequest, "Invalid request target: #{target.inspect}"
275+
end
276+
277+
if authority
278+
authority = authority.to_s
279+
unless authority.b.match?(VALID_FIELD_VALUE)
280+
raise InvalidRequest, "Invalid request authority: #{authority.inspect}"
281+
end
282+
end
283+
263284
open!
264285

265286
write_stream("#{method} #{target} #{version}\r\n")
@@ -311,27 +332,36 @@ def write_interim_response(version, status, headers, reason = nil)
311332
flush_stream
312333
end
313334

335+
# Write a header to the connection, rejecting CR, LF, and NUL in its value.
336+
#
337+
# @parameter name [String] the header name.
338+
# @parameter value [String] the header value.
339+
# @raises [BadHeader] if the header name or value is invalid.
340+
def write_header(name, value)
341+
# Convert it to a string:
342+
name = name.to_s
343+
value = value.to_s
344+
345+
# Validate it:
346+
unless name.match?(VALID_FIELD_NAME)
347+
raise BadHeader, "Invalid header name: #{name.inspect}"
348+
end
349+
350+
unless value.match?(VALID_FIELD_VALUE)
351+
raise BadHeader, "Invalid header value for #{name}: #{value.inspect}"
352+
end
353+
354+
# Write it:
355+
write_stream("#{name}: #{value}\r\n")
356+
end
357+
314358
# Write headers to the connection.
315359
#
316360
# @parameter headers [Hash] the headers to write.
317361
# @raises [BadHeader] if the header name or value is invalid.
318362
def write_headers(headers)
319363
headers.each do |name, value|
320-
# Convert it to a string:
321-
name = name.to_s
322-
value = value.to_s
323-
324-
# Validate it:
325-
unless name.match?(VALID_FIELD_NAME)
326-
raise BadHeader, "Invalid header name: #{name.inspect}"
327-
end
328-
329-
unless value.match?(VALID_FIELD_VALUE)
330-
raise BadHeader, "Invalid header value for #{name}: #{value.inspect}"
331-
end
332-
333-
# Write it:
334-
write_stream("#{name}: #{value}\r\n")
364+
write_header(name, value)
335365
end
336366
end
337367

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# frozen_string_literal: true
2+
3+
# Released under the MIT License.
4+
# Copyright, 2026, by Samuel Williams.
5+
6+
require "protocol/http1/connection"
7+
require "stringio"
8+
9+
describe Protocol::HTTP1::Connection do
10+
let(:stream) {StringIO.new("".b)}
11+
let(:connection) {subject.new(stream)}
12+
13+
["\r", "\n", "\0", "\r\n"].each do |character|
14+
with "a value containing #{character.inspect}", unique: character.inspect do
15+
let(:value) {"before#{character}after"}
16+
let(:headers) {Protocol::HTTP::Headers["location" => value]}
17+
18+
it "rejects ordinary header values before writing them" do
19+
expect do
20+
connection.write_headers(headers)
21+
end.to raise_exception(Protocol::HTTP1::BadHeader)
22+
23+
expect(stream.string).to be == ""
24+
end
25+
26+
it "rejects request header values" do
27+
expect do
28+
connection.write_request(nil, "GET", "/", "HTTP/1.1", headers)
29+
end.to raise_exception(Protocol::HTTP::RefusedError)
30+
end
31+
32+
it "rejects response header values" do
33+
connection.open!
34+
35+
expect do
36+
connection.write_response("HTTP/1.1", 302, headers)
37+
end.to raise_exception(Protocol::HTTP1::BadHeader)
38+
end
39+
40+
it "rejects interim response header values" do
41+
connection.open!
42+
43+
expect do
44+
connection.write_interim_response("HTTP/1.1", 103, headers)
45+
end.to raise_exception(Protocol::HTTP1::BadHeader)
46+
end
47+
48+
it "rejects the authority before writing any request bytes" do
49+
expect do
50+
connection.write_request(value, "GET", "/", "HTTP/1.1", {})
51+
end.to raise_exception(Protocol::HTTP::RefusedError)
52+
53+
expect(stream.string).to be == ""
54+
end
55+
56+
it "rejects the target before writing any request bytes" do
57+
expect do
58+
connection.write_request("example.com", "GET", "/#{value}", "HTTP/1.1", {})
59+
end.to raise_exception(Protocol::HTTP::RefusedError)
60+
61+
expect(stream.string).to be == ""
62+
end
63+
64+
it "rejects CONNECT targets before writing any request bytes" do
65+
expect do
66+
connection.write_request(nil, "CONNECT", "#{value}:443", "HTTP/1.1", {})
67+
end.to raise_exception(Protocol::HTTP::RefusedError)
68+
69+
expect(stream.string).to be == ""
70+
end
71+
72+
it "rejects generated Upgrade header values before writing them" do
73+
expect do
74+
connection.write_upgrade_header(value)
75+
end.to raise_exception(Protocol::HTTP1::BadHeader)
76+
77+
expect(stream.string).not.to be(:include?, "upgrade:")
78+
end
79+
end
80+
end
81+
82+
it "preserves an empty header value" do
83+
connection.write_headers("x-empty" => "")
84+
85+
expect(stream.string).to be == "x-empty: \r\n"
86+
end
87+
88+
it "preserves all other bytes in header values" do
89+
value = ((0..255).to_a - [0, 10, 13]).pack("C*")
90+
connection.write_headers("x-bytes" => value)
91+
92+
expect(stream.string).to be == "x-bytes: #{value}\r\n"
93+
end
94+
95+
[Encoding::ASCII_8BIT, Encoding::UTF_8, Encoding::US_ASCII].each do |encoding|
96+
with "non-ASCII bytes tagged as #{encoding}", unique: encoding.name do
97+
let(:value) {"a\xFFb".b.force_encoding(encoding)}
98+
99+
it "preserves the request target bytes" do
100+
connection.write_request("example.com", "GET", "/#{value}", "HTTP/1.1", {})
101+
102+
expect(stream.string.b).to be == "GET /a\xFFb HTTP/1.1\r\nhost: example.com\r\n".b
103+
expect(value.encoding).to be == encoding
104+
end
105+
106+
it "preserves the authority bytes" do
107+
connection.write_request(value, "GET", "/", "HTTP/1.1", {})
108+
109+
expect(stream.string.b).to be == "GET / HTTP/1.1\r\nhost: a\xFFb\r\n".b
110+
expect(value.encoding).to be == encoding
111+
end
112+
end
113+
end
114+
115+
it "writes a valid Host header" do
116+
connection.write_request("example.com", "GET", "/", "HTTP/1.1", {})
117+
118+
expect(stream.string).to be == "GET / HTTP/1.1\r\nhost: example.com\r\n"
119+
end
120+
121+
it "preserves percent-encoded request targets" do
122+
connection.write_request("example.com", "GET", "/%00%0D%0A", "HTTP/1.1", {})
123+
124+
expect(stream.string).to be == "GET /%00%0D%0A HTTP/1.1\r\nhost: example.com\r\n"
125+
end
126+
127+
it "writes a valid CONNECT target without a Host header" do
128+
connection.write_request(nil, "CONNECT", "example.com:443", "HTTP/1.1", {})
129+
130+
expect(stream.string).to be == "CONNECT example.com:443 HTTP/1.1\r\n"
131+
end
132+
133+
it "writes valid upgrade headers" do
134+
connection.write_upgrade_header("websocket")
135+
136+
expect(stream.string).to be == "connection: upgrade\r\nupgrade: websocket\r\n"
137+
end
138+
end

‎test/protocol/http1/connection/headers.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def validate_headers!(expected_headers = self.headers)
2020

2121
version, status, reason, headers, body = client.read_response("GET")
2222

23-
expect(headers).to be == headers
23+
expect(headers.to_a).to be == expected_headers.to_a
2424
end
2525

2626
with "a content-type header" do
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# frozen_string_literal: true
2+
3+
# Released under the MIT License.
4+
# Copyright, 2026, by Samuel Williams.
5+
6+
require "protocol/http1/connection"
7+
require "stringio"
8+
9+
describe Protocol::HTTP1::Connection do
10+
let(:stream) {StringIO.new("".b)}
11+
let(:connection) {subject.new(stream)}
12+
13+
["", "GE T", "GE\tT", "GET\r", "GET\n", "GE\0T", "GET:", "GÉT", "GE\xFFT".b.force_encoding(Encoding::UTF_8)].each do |method|
14+
with "an invalid method #{method.inspect}", unique: method.inspect do
15+
it "rejects the method before writing any request bytes" do
16+
expect do
17+
connection.write_request("example.com", method, "/", "HTTP/1.1", {})
18+
end.to raise_exception(Protocol::HTTP::RefusedError)
19+
20+
expect(stream.string).to be == ""
21+
expect(connection).to be(:idle?)
22+
end
23+
end
24+
end
25+
26+
it "rejects a method containing an injected request" do
27+
method = "GET /first HTTP/1.1\r\nhost: example.com\r\ncontent-length: 0\r\n\r\nGET"
28+
29+
expect do
30+
connection.write_request("example.com", method, "/", "HTTP/1.1", {})
31+
connection.write_body("HTTP/1.1", nil)
32+
end.to raise_exception(Protocol::HTTP::RefusedError)
33+
34+
expect(stream.string).to be == ""
35+
expect(connection).to be(:idle?)
36+
end
37+
38+
it "preserves extension methods using any HTTP token character" do
39+
method = "!\#$%&'*+-.^_`|~0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
40+
connection.write_request("example.com", method, "/", "HTTP/1.1", {})
41+
42+
expect(stream.string).to be == "#{method} / HTTP/1.1\r\nhost: example.com\r\n"
43+
end
44+
end

0 commit comments

Comments
 (0)