Skip to content

Commit 9897f4b

Browse files
committed
Merge branch 'dkw-oss-minor-patch'
2 parents 3fa2564 + 89268b9 commit 9897f4b

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

‎lib/http/accept/quoted_string.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module HTTP
77
module Accept
88
# According to https://tools.ietf.org/html/rfc7231#appendix-C
99
TOKEN = /[!#$%&'*+\-.^_`|~0-9A-Z]+/i
10-
QUOTED_STRING = /"(?:.(?!(?<!\\)"))*.?"/
10+
QUOTED_STRING = /"(?:[^"\\\r\n]|\\.)*"/
1111

1212
module QuotedString
1313
# Unquote a "quoted-string" value according to https://tools.ietf.org/html/rfc7230#section-3.2.6

‎test/http/media_types.rb‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,31 @@
3535
expect(media_types[0].parameters).to be == {"key" => "A,B,C"}
3636
end
3737

38+
it "should not over-match a quoted value ending with an escaped backslash" do
39+
# The QUOTED_STRING regex must stop at the real
40+
# closing quote even when the value ends in an escaped backslash (\\).
41+
# Otherwise it swallows the following parameters and media types, which
42+
# raises a ParseError on this otherwise valid RFC 7230 header.
43+
media_types = HTTP::Accept::MediaTypes.parse("text/html;a=\"\\\\\";q=0.5, text/plain;b=\"normal\"")
44+
45+
expect(media_types.size).to be == 2
46+
47+
expect(media_types[0].mime_type).to be == "text/plain"
48+
expect(media_types[0].parameters).to be == {'b' => "normal"}
49+
50+
expect(media_types[1].mime_type).to be == "text/html"
51+
expect(media_types[1].parameters).to be == {'a' => "\\", 'q' => "0.5"}
52+
end
53+
54+
it "should reject line breaks inside quoted values" do
55+
[
56+
"foo/bar;x=\"line\rbreak\"",
57+
"foo/bar;x=\"line\nbreak\"",
58+
].each do |text|
59+
expect{HTTP::Accept::MediaTypes.parse(text)}.to raise_exception(HTTP::Accept::ParseError)
60+
end
61+
end
62+
3863
it "should accept empty string" do
3964
expect(HTTP::Accept::MediaTypes.parse("")).to be == []
4065
end

0 commit comments

Comments
 (0)