From 1d9f5c67426a8e05eb94a0bdce4ea7d7a837f595 Mon Sep 17 00:00:00 2001 From: Go MAEDA Date: Mon, 17 Oct 2022 13:35:20 +0000 Subject: [PATCH] Escape tags that start with pre (#37767). Patch by Holger Just. git-svn-id: https://svn.redmine.org/redmine/trunk@21902 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- lib/redmine/wiki_formatting/textile/redcloth3.rb | 12 +++++++----- .../wiki_formatting/textile_formatter_test.rb | 11 +++++++++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/lib/redmine/wiki_formatting/textile/redcloth3.rb b/lib/redmine/wiki_formatting/textile/redcloth3.rb index b935db6b9..c292926a5 100644 --- a/lib/redmine/wiki_formatting/textile/redcloth3.rb +++ b/lib/redmine/wiki_formatting/textile/redcloth3.rb @@ -1208,13 +1208,15 @@ class RedCloth3 < String end end - ALLOWED_TAGS = %w(redpre pre code kbd notextile) + ALLOWED_TAGS = %w(pre code kbd notextile) def escape_html_tags(text) - text.gsub!(%r{<(\/?([!\w]+)[^<>\n]*)(>?)}) do |m| - if ALLOWED_TAGS.include?($2) && $3.present? - "<#{$1}#{$3}" + text.gsub!(%r{<(\/?([!\w][^ >\t\f\r\n]*)[^<>\n]*)(>?)}) do |m| + all, tag, close = $1, $2, $3 + + if close.present? && (ALLOWED_TAGS.include?(tag) || (tag =~ /\Aredpre#\d+\z/)) + "<#{all}#{close}" else - "<#{$1}#{'>' unless $3.blank?}" + "<#{all}#{'>' unless close.blank?}" end end end diff --git a/test/unit/lib/redmine/wiki_formatting/textile_formatter_test.rb b/test/unit/lib/redmine/wiki_formatting/textile_formatter_test.rb index 23f6d7538..9d2a1815d 100644 --- a/test/unit/lib/redmine/wiki_formatting/textile_formatter_test.rb +++ b/test/unit/lib/redmine/wiki_formatting/textile_formatter_test.rb @@ -719,6 +719,17 @@ class Redmine::WikiFormatting::TextileFormatterTest < ActionView::TestCase assert_equal expected.gsub(%r{[\r\n\t]}, ''), to_html(text).gsub(%r{[\r\n\t]}, '') end + def test_should_escape_tags_that_start_with_pre + text = <<~STR + Text + STR + + expected = <<~EXPECTED +

<preä demo>Text

+ EXPECTED + assert_equal expected.gsub(%r{[\r\n\t]}, ''), to_html(text).gsub(%r{[\r\n\t]}, '') + end + def test_should_remove_html_comments text = <<~STR -- 2.39.5