From 2b9450fe9104f9ffed7e4e7a02283240bd128e5b Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Sat, 11 Jul 2026 11:10:03 -0700 Subject: [PATCH] Handle empty meta tag attributes during encoding detection Additionally, handle malformed metatags by assuming no encoding inside of raising a RuntimeError. Fixes #311 --- lib/net/http/response.rb | 70 +++++++++++++++-------------- test/net/http/test_httpresponse.rb | 72 ++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+), 34 deletions(-) diff --git a/lib/net/http/response.rb b/lib/net/http/response.rb index 0b5b326..68e0376 100644 --- a/lib/net/http/response.rb +++ b/lib/net/http/response.rb @@ -472,40 +472,42 @@ def check_bom(str) def scanning_meta(str) require 'strscan' ss = StringScanner.new(str) - if ss.scan_until(/]*/) name.downcase! - raise if name.empty? + throw :invalid_meta_tag if name.empty? ss.skip(/[\t\n\f\r ]*/) if ss.getch != '=' value = '' @@ -528,18 +530,18 @@ def get_attribute(ss) case ss.peek(1) when '"' ss.getch - value = ss.scan(/[^"]+/) + value = ss.scan(/[^"]*/) value.downcase! ss.getch when "'" ss.getch - value = ss.scan(/[^']+/) + value = ss.scan(/[^']*/) value.downcase! ss.getch when '>' value = '' else - value = ss.scan(/[^\t\n\f\r >]+/) + throw :invalid_meta_tag unless value = ss.scan(/[^\t\n\f\r >]+/) value.downcase! end [name, value] diff --git a/test/net/http/test_httpresponse.rb b/test/net/http/test_httpresponse.rb index 7e7ae8a..b113fe0 100644 --- a/test/net/http/test_httpresponse.rb +++ b/test/net/http/test_httpresponse.rb @@ -313,6 +313,78 @@ def test_read_body_body_encoding_true_with_iso8859_1_meta_content_charset assert_equal Encoding::ISO_8859_1, body.encoding end + def test_read_body_body_encoding_with_empty_attribute + res_body = "hello\u1234" + io = dummy_io(<