Skip to content

Commit 2e54089

Browse files
Classify header parse errors as bad requests.
1 parent 5a09a3b commit 2e54089

16 files changed

Lines changed: 57 additions & 7 deletions

‎lib/protocol/http/header/accept.rb‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ class Accept < Split
2323
(?=,|\z) # Match until a comma or end of string
2424
/x
2525

26-
ParseError = Class.new(Error)
26+
ParseError = Class.new(Error) do
27+
include BadRequest
28+
end
2729

2830
MEDIA_RANGE = /\A(?<type>#{TOKEN})\/(?<subtype>#{TOKEN})(?<parameters>.*)\z/
2931

‎lib/protocol/http/header/accept_charset.rb‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ module HTTP
1212
module Header
1313
# The `accept-charset` header represents a list of character sets that the client can accept.
1414
class AcceptCharset < Split
15-
ParseError = Class.new(Error)
15+
ParseError = Class.new(Error) do
16+
include BadRequest
17+
end
1618

1719
# https://tools.ietf.org/html/rfc7231#section-5.3.3
1820
CHARSET = /\A(?<name>#{TOKEN})(;q=(?<q>#{QVALUE}))?\z/

‎lib/protocol/http/header/accept_encoding.rb‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ module HTTP
1212
module Header
1313
# The `accept-encoding` header represents a list of encodings that the client can accept.
1414
class AcceptEncoding < Split
15-
ParseError = Class.new(Error)
15+
ParseError = Class.new(Error) do
16+
include BadRequest
17+
end
1618

1719
# https://tools.ietf.org/html/rfc7231#section-5.3.1
1820
QVALUE = /0(\.[0-9]{0,3})?|1(\.[0]{0,3})?/

‎lib/protocol/http/header/accept_language.rb‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ module HTTP
1212
module Header
1313
# The `accept-language` header represents a list of languages that the client can accept.
1414
class AcceptLanguage < Split
15-
ParseError = Class.new(Error)
15+
ParseError = Class.new(Error) do
16+
include BadRequest
17+
end
1618

1719
# https://tools.ietf.org/html/rfc3066#section-2.1
1820
NAME = /\*|[A-Z]{1,8}(-[A-Z0-9]{1,8})*/i

‎lib/protocol/http/header/digest.rb‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ module Header
2323
# # => "sha-256=X48E9qOokqqrvdts8nOJRJN3OWDUoyWxBf7kbu9DBPE=, md5=9bb58f26192e4ba00f01e2e7b136bbd8"
2424
# ```
2525
class Digest < Split
26-
ParseError = Class.new(Error)
26+
ParseError = Class.new(Error) do
27+
include BadRequest
28+
end
2729

2830
# https://tools.ietf.org/html/rfc3230#section-4.3.2
2931
ENTRY = /\A(?<algorithm>[a-zA-Z0-9][a-zA-Z0-9\-]*)\s*=\s*(?<value>.*)\z/

‎lib/protocol/http/header/server_timing.rb‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ module Header
2323
# # => "db;dur=53.2, cache;dur=12.1;desc=\"Redis lookup\""
2424
# ```
2525
class ServerTiming < Split
26-
ParseError = Class.new(Error)
26+
ParseError = Class.new(Error) do
27+
include BadRequest
28+
end
2729

2830
# https://www.w3.org/TR/server-timing/
2931
METRIC = /\A(?<name>[a-zA-Z0-9][a-zA-Z0-9_\-]*)(;(?<parameters>.*))?\z/

‎lib/protocol/http/header/te.rb‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ module Header
1414
#
1515
# The `te` header allows a client to indicate which transfer encodings it can handle, and in what order of preference using quality factors.
1616
class TE < Split
17-
ParseError = Class.new(Error)
17+
ParseError = Class.new(Error) do
18+
include BadRequest
19+
end
1820

1921
# Transfer encoding token pattern
2022
TOKEN = /[!#$%&'*+\-.0-9A-Z^_`a-z|~]+/

‎readme.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ Please see the [project documentation](https://socketry.github.io/protocol-http/
3030

3131
Please see the [project releases](https://socketry.github.io/protocol-http/releases/index) for all releases.
3232

33+
### Unreleased
34+
35+
- Classify malformed header values as bad requests.
36+
3337
### v0.66.0
3438

3539
- Introduce `Protocol::HTTP::RemoteError` for remote endpoint failures where application processing may have occurred.

‎releases.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Releases
22

3+
## Unreleased
4+
5+
- Classify malformed header values as bad requests.
6+
37
## v0.66.0
48

59
- Introduce `Protocol::HTTP::RemoteError` for remote endpoint failures where application processing may have occurred.

‎test/protocol/http/header/accept.rb‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
end
2121

2222
describe Protocol::HTTP::Header::Accept do
23+
it "classifies parse errors as bad requests" do
24+
expect(subject::ParseError.new).to be_a(Protocol::HTTP::BadRequest)
25+
end
26+
2327
let(:header) {subject.parse(description)}
2428
let(:media_ranges) {header.media_ranges.sort}
2529

0 commit comments

Comments
 (0)