File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 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\r break\" " ,
57+ "foo/bar;x=\" line\n break\" " ,
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
You can’t perform that action at this time.
0 commit comments