From 6b40ac6b4c25a860bfbc3572fa55298bb2d6a7dc Mon Sep 17 00:00:00 2001 From: Aditya <152765789+adityawrk@users.noreply.github.com> Date: Sun, 29 Nov 2026 08:17:05 +0530 Subject: [PATCH 1/2] Test that CGI.escapeHTML always returns a String A String subclass came back as itself when nothing needed escaping and as a plain String when a character was escaped. --- test/cgi/test_cgi_escape.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/test/cgi/test_cgi_escape.rb b/test/cgi/test_cgi_escape.rb index 8c66dfa..d969267 100644 --- a/test/cgi/test_cgi_escape.rb +++ b/test/cgi/test_cgi_escape.rb @@ -148,6 +148,16 @@ def test_cgi_unescapeURIComponent_nil assert_raise(TypeError) { CGI.unescapeURIComponent(nil) } end + def test_cgi_escape_html_returns_a_string + subclass = Class.new(String) + plain = CGI.escapeHTML(subclass.new("a")) + escaped = CGI.escapeHTML(subclass.new("&")) + assert_instance_of(String, plain) + assert_instance_of(String, escaped) + assert_equal("a", plain) + assert_equal("&", escaped) + end + def test_cgi_escapeHTML assert_equal("'&"><", CGI.escapeHTML("'&\"><")) end @@ -407,6 +417,16 @@ def teardown include CGIEscapeTest::UnescapeHTMLTests + def test_cgi_escape_html_returns_a_string + subclass = Class.new(String) + plain = CGI.escapeHTML(subclass.new("a")) + escaped = CGI.escapeHTML(subclass.new("&")) + assert_instance_of(String, plain) + assert_instance_of(String, escaped) + assert_equal("a", plain) + assert_equal("&", escaped) + end + def test_cgi_escapeHTML_with_invalid_byte_sequence assert_equal("<\xA4??>", CGI.escapeHTML(%[<\xA4??>])) end From cf87b4be13216b1d9e5167c9051b2227ad42903e Mon Sep 17 00:00:00 2001 From: Aditya <152765789+adityawrk@users.noreply.github.com> Date: Sun, 29 Nov 2026 08:17:05 +0530 Subject: [PATCH 2/2] Return a String from CGI.escapeHTML The fast path duplicated the receiver when no character needed escaping, so a subclass survived only in that case. Both results are now a new String. Fixes #19 --- ext/cgi/escape/escape.c | 11 ++++------- ext/java/org/jruby/ext/cgi/escape/CGIEscape.java | 14 ++++++++------ 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/ext/cgi/escape/escape.c b/ext/cgi/escape/escape.c index 5d787d0..faa365d 100644 --- a/ext/cgi/escape/escape.c +++ b/ext/cgi/escape/escape.c @@ -77,13 +77,10 @@ optimized_escape_html(VALUE str) } } - if (RSTRING_LEN(str) < (dest - buf)) { - escaped = rb_str_new(buf, dest - buf); - preserve_original_state(str, escaped); - } - else { - escaped = rb_str_dup(str); - } + /* Always a String. rb_str_dup kept a subclass when nothing was + * escaped, so the class depended on the input (cgi#19). */ + escaped = rb_str_new(buf, dest - buf); + preserve_original_state(str, escaped); ALLOCV_END(vbuf); return escaped; } diff --git a/ext/java/org/jruby/ext/cgi/escape/CGIEscape.java b/ext/java/org/jruby/ext/cgi/escape/CGIEscape.java index 63c982b..69d888d 100644 --- a/ext/java/org/jruby/ext/cgi/escape/CGIEscape.java +++ b/ext/java/org/jruby/ext/cgi/escape/CGIEscape.java @@ -116,13 +116,15 @@ static void preserve_original_state(RubyString orig, RubyString dest) { } } - if (dest != null) { - dest.cat(cstrBytes, cstr + beg, len - beg); - preserve_original_state(str, dest); - return dest; - } else { - return str.strDup(runtime); + if (dest == null) { + // Always a String. strDup kept a subclass when nothing was + // escaped, so the class depended on the input (cgi#19). + dest = RubyString.newStringLight(runtime, len); + beg = 0; } + dest.cat(cstrBytes, cstr + beg, len - beg); + preserve_original_state(str, dest); + return dest; } // Set of i has to happen outside this; see MATCH macro in MRI ext/cgi/escape/escape.c