From 1f1e6e9b40bf339894e843dfd679c2fb1a5ddbf2 Mon Sep 17 00:00:00 2001 From: Watson Date: Tue, 16 Jul 2024 11:35:41 +0900 Subject: [PATCH] Fix ReDoS by using repeated space characters inside `]>` (#176) Fix performance by removing unnecessary spaces. This is occurred in Ruby 3.1 or earlier. --- lib/rexml/parsers/baseparser.rb | 2 +- test/parse/test_attlist.rb | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 test/parse/test_attlist.rb diff --git a/lib/rexml/parsers/baseparser.rb b/lib/rexml/parsers/baseparser.rb index 860be203..47380f0d 100644 --- a/lib/rexml/parsers/baseparser.rb +++ b/lib/rexml/parsers/baseparser.rb @@ -350,7 +350,7 @@ def pull_event contents = md[0] pairs = {} - values = md[0].scan( ATTDEF_RE ) + values = md[0].strip.scan( ATTDEF_RE ) values.each do |attdef| unless attdef[3] == "#IMPLIED" attdef.compact! diff --git a/test/parse/test_attlist.rb b/test/parse/test_attlist.rb new file mode 100644 index 00000000..eee9309c --- /dev/null +++ b/test/parse/test_attlist.rb @@ -0,0 +1,17 @@ +require "test/unit" +require "core_assertions" + +require "rexml/document" + +module REXMLTests + class TestParseAttlist < Test::Unit::TestCase + include Test::Unit::CoreAssertions + + def test_gt_linear_performance + seq = [10000, 50000, 100000, 150000, 200000] + assert_linear_performance(seq, rehearsal: 10) do |n| + REXML::Document.new(']>') + end + end + end +end