From cd25129b7b8ff4d120b1fc942419ce2cb57e237a Mon Sep 17 00:00:00 2001 From: Dushan Karovich-Wynne Date: Sat, 5 Sep 2026 20:56:29 +1000 Subject: [PATCH] fix: QUOTED_STRING regex --- lib/http/accept/quoted_string.rb | 2 +- test/http/media_types.rb | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/http/accept/quoted_string.rb b/lib/http/accept/quoted_string.rb index 77ca921..24a66be 100644 --- a/lib/http/accept/quoted_string.rb +++ b/lib/http/accept/quoted_string.rb @@ -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 = /"(?:.(?!(? "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