Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/http/accept/quoted_string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module HTTP
module Accept
# According to https://tools.ietf.org/html/rfc7231#appendix-C
TOKEN = /[!#$%&'*+\-.^_`|~0-9A-Z]+/i
QUOTED_STRING = /"(?:.(?!(?<!\\)"))*.?"/
QUOTED_STRING = /"(?:[^"\\\r\n]|\\.)*"/

module QuotedString
# Unquote a "quoted-string" value according to https://tools.ietf.org/html/rfc7230#section-3.2.6
Expand Down
25 changes: 25 additions & 0 deletions test/http/media_types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,31 @@
expect(media_types[0].parameters).to be == {'key' => "A,B,C"}
end

it "should not over-match a quoted value ending with an escaped backslash" do
# The QUOTED_STRING regex must stop at the real
# closing quote even when the value ends in an escaped backslash (\\).
# Otherwise it swallows the following parameters and media types, which
# raises a ParseError on this otherwise valid RFC 7230 header.
media_types = HTTP::Accept::MediaTypes.parse("text/html;a=\"\\\\\";q=0.5, text/plain;b=\"normal\"")

expect(media_types.size).to be == 2

expect(media_types[0].mime_type).to be == "text/plain"
expect(media_types[0].parameters).to be == {'b' => "normal"}

expect(media_types[1].mime_type).to be == "text/html"
expect(media_types[1].parameters).to be == {'a' => "\\", 'q' => "0.5"}
end

it "should reject line breaks inside quoted values" do
[
"foo/bar;x=\"line\rbreak\"",
"foo/bar;x=\"line\nbreak\"",
].each do |text|
expect{HTTP::Accept::MediaTypes.parse(text)}.to raise_exception(HTTP::Accept::ParseError)
end
end

it "should accept empty string" do
expect(HTTP::Accept::MediaTypes.parse("")).to be == []
end
Expand Down
Loading