From f26ea65c4970f4a0853828ddc31f885c2d45285f Mon Sep 17 00:00:00 2001 From: Moksh Upadhyay Date: Tue, 1 Dec 2026 20:59:49 +0530 Subject: [PATCH 1/2] Fix XPath parsing of expressions and literals that contain line breaks The XPath parser matched its tokens with `^`, which in Ruby matches at the start of every line, not only at the start of the string. When the remaining expression did not start with the token being tried, the pattern could match at the start of a later line and everything before it was dropped. As a result an expression such as "//a[\ncount(b) > 1\n]" raised ParseException, and a string literal with a line break was cut, e.g. string("a\n1") returned "1". Use `\A` so that the patterns only match at the current position. --- lib/rexml/parsers/xpathparser.rb | 36 ++++++++++++------------- test/parser/test_xpath.rb | 46 ++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 18 deletions(-) diff --git a/lib/rexml/parsers/xpathparser.rb b/lib/rexml/parsers/xpathparser.rb index cb2f5c67..72d3bf1e 100644 --- a/lib/rexml/parsers/xpathparser.rb +++ b/lib/rexml/parsers/xpathparser.rb @@ -11,7 +11,7 @@ module Parsers # back while you still can! class XPathParser # :nodoc: include XMLTokens - LITERAL = /^'([^']*)'|^"([^"]*)"/u + LITERAL = /\A'([^']*)'|\A"([^"]*)"/u def namespaces=( namespaces ) @namespaces = namespaces @@ -276,7 +276,7 @@ def LocationPath path, parsed # | '.' | '..' AbbreviatedStep # | RelativeLocationPath '/' Step # | RelativeLocationPath '//' Step - AXIS = /^(ancestor|ancestor-or-self|attribute|child|descendant|descendant-or-self|following|following-sibling|namespace|parent|preceding|preceding-sibling|self)::/ + AXIS = /\A(ancestor|ancestor-or-self|attribute|child|descendant|descendant-or-self|following|following-sibling|namespace|parent|preceding|preceding-sibling|self)::/ def RelativeLocationPath path, parsed loop do original_path = path @@ -348,11 +348,11 @@ def RelativeLocationPath path, parsed # | NODE_TYPE '(' ')' NodeType # | PI '(' LITERAL ')' PI # | '[' expr ']' Predicate - PREFIX_WILDCARD = /^\*:(#{NCNAME_STR})/u - LOCAL_NAME_WILDCARD = /^(#{NCNAME_STR}):\*/u + PREFIX_WILDCARD = /\A\*:(#{NCNAME_STR})/u + LOCAL_NAME_WILDCARD = /\A(#{NCNAME_STR}):\*/u QNAME = Namespace::NAMESPLIT - NODE_TYPE = /^(comment|text|node)\(\s*\)/m - PI = /^processing-instruction\(/ + NODE_TYPE = /\A(comment|text|node)\(\s*\)/m + PI = /\Aprocessing-instruction\(/ def NodeTest path, parsed original_path = path path = path.lstrip @@ -364,7 +364,7 @@ def NodeTest path, parsed parsed << :qname parsed << prefix parsed << name - when /^\*/ + when /\A\*/ path = $' parsed << :any when NODE_TYPE @@ -433,7 +433,7 @@ def OrExpr path, parsed n = [] rest = AndExpr( path, n ) if rest != path - while rest =~ /^\s*( or )/ + while rest =~ /\A\s*( or )/ n = [ :or, n, [] ] rest = AndExpr( $', n[-1] ) end @@ -452,7 +452,7 @@ def AndExpr path, parsed n = [] rest = EqualityExpr( path, n ) if rest != path - while rest =~ /^\s*( and )/ + while rest =~ /\A\s*( and )/ n = [ :and, n, [] ] rest = EqualityExpr( $', n[-1] ) end @@ -471,7 +471,7 @@ def EqualityExpr path, parsed n = [] rest = RelationalExpr( path, n ) if rest != path - while rest =~ /^\s*(!?=)\s*/ + while rest =~ /\A\s*(!?=)\s*/ if $1[0] == ?! n = [ :neq, n, [] ] else @@ -494,7 +494,7 @@ def RelationalExpr path, parsed n = [] rest = AdditiveExpr( path, n ) if rest != path - while rest =~ /^\s*([<>]=?)\s*/ + while rest =~ /\A\s*([<>]=?)\s*/ if $1[0] == ?< sym = "lt" else @@ -519,7 +519,7 @@ def AdditiveExpr path, parsed n = [] rest = MultiplicativeExpr( path, n ) if rest != path - while rest =~ /^\s*(\+|-)\s*/ + while rest =~ /\A\s*(\+|-)\s*/ if $1[0] == ?+ n = [ :plus, n, [] ] else @@ -542,7 +542,7 @@ def MultiplicativeExpr path, parsed n = [] rest = UnaryExpr( path, n ) if rest != path - while rest =~ /^\s*(\*| div | mod )\s*/ + while rest =~ /\A\s*(\*| div | mod )\s*/ if $1[0] == ?* n = [ :mult, n, [] ] elsif $1.include?( "div" ) @@ -585,7 +585,7 @@ def UnionExpr path, parsed n = [] rest = PathExpr( path, n ) if rest != path - while rest =~ /^\s*(\|)\s*/ + while rest =~ /\A\s*(\|)\s*/ n = [ :union, n, [] ] rest = PathExpr( $', n[-1] ) end @@ -640,8 +640,8 @@ def FilterExpr path, parsed #| LITERAL #| NUMBER #| FunctionCall - VARIABLE_REFERENCE = /^\$(#{NAME_STR})/u - NUMBER = /^(\d*\.?\d+)/ + VARIABLE_REFERENCE = /\A\$(#{NAME_STR})/u + NUMBER = /\A(\d*\.?\d+)/ NT = /^comment|text|processing-instruction|node$/ def PrimaryExpr path, parsed case path @@ -651,7 +651,7 @@ def PrimaryExpr path, parsed parsed << :variable parsed << varname #arry << @variables[ varname ] - when /^(\w[-\w]*)(?:\()/ + when /\A(\w[-\w]*)(?:\()/ fname = $1 tmp = $' return path if fname =~ NT @@ -669,7 +669,7 @@ def PrimaryExpr path, parsed path = $' parsed << :literal parsed << varname - when /^\(/ #/ + when /\A\(/ #/ path, contents = get_group(path) contents = contents[1..-2] n = [] diff --git a/test/parser/test_xpath.rb b/test/parser/test_xpath.rb index bb20c1fd..f8b601ec 100644 --- a/test/parser/test_xpath.rb +++ b/test/parser/test_xpath.rb @@ -180,6 +180,52 @@ def test_spaces_between_tokens ) end + def test_line_breaks_between_tokens + parser = REXML::Parsers::XPathParser.new + assert_equal( + parser.parse('//a[1]'), + parser.parse("//a[\n1\n]"), + ) + assert_equal( + parser.parse('//a[count(b) > 1]'), + parser.parse("//a[\ncount(b) > 1\n]"), + ) + assert_equal( + parser.parse('//a[@b = "c"]'), + parser.parse("//a[@b =\n\"c\"]"), + ) + assert_equal( + parser.parse('//a[@b = $c]'), + parser.parse("//a[@b =\n$c]"), + ) + assert_equal( + parser.parse('//a[ancestor::b]/*'), + parser.parse("//a[\nancestor::b]/\n*"), + ) + assert_equal( + parser.parse('//a = (1 + 2)'), + parser.parse("//a =\n(1 + 2)"), + ) + end + + def test_line_breaks_in_literal + parser = REXML::Parsers::XPathParser.new + assert_equal([:literal, "a\n1"], + parser.parse("\"a\n1\"")) + assert_equal([:literal, "a\nb(c)"], + parser.parse("'a\nb(c)'")) + assert_equal([:function, "concat", [[:literal, "a"], [:literal, "b\n$c"]]], + parser.parse("concat('a', 'b\n$c')")) + assert_equal([:eq, + [:child, :qname, "", "a"], + [:literal, "b\n* c"]], + parser.parse("a = 'b\n* c'")) + assert_equal([:eq, + [:child, :qname, "", "a"], + [:literal, "b\n| c"]], + parser.parse("a = 'b\n| c'")) + end + def test_mult_asterisk_without_surrounding_spaces parser = REXML::Parsers::XPathParser.new assert_equal(parser.parse("1 * 2 * 3"), parser.parse("1*2*3")) From 08185d9419cfb54282069aab8e80a91d4076a37c Mon Sep 17 00:00:00 2001 From: Moksh Upadhyay Date: Wed, 2 Dec 2026 09:26:24 +0530 Subject: [PATCH 2/2] Anchor Namespace::NAMESPLIT at the start of the string QNAME in the XPath parser uses NAMESPLIT, and with ^ a name on a later line matched, so invalid expressions such as "//a/1\nb" were parsed with the first line silently dropped instead of raising ParseException. --- lib/rexml/namespace.rb | 2 +- test/parser/test_xpath.rb | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/rexml/namespace.rb b/lib/rexml/namespace.rb index 232b7ca4..b5681152 100644 --- a/lib/rexml/namespace.rb +++ b/lib/rexml/namespace.rb @@ -11,7 +11,7 @@ module Namespace attr_accessor :prefix include XMLTokens NAME_WITHOUT_NAMESPACE = /\A#{NCNAME_STR}\z/ - NAMESPLIT = /^(?:(#{NCNAME_STR}):)?(#{NCNAME_STR})/u + NAMESPLIT = /\A(?:(#{NCNAME_STR}):)?(#{NCNAME_STR})/u # Sets the name and the expanded name def name=( name ) diff --git a/test/parser/test_xpath.rb b/test/parser/test_xpath.rb index 13f099d6..9d8ee3fb 100644 --- a/test/parser/test_xpath.rb +++ b/test/parser/test_xpath.rb @@ -226,6 +226,17 @@ def test_line_breaks_in_literal parser.parse("a = 'b\n| c'")) end + def test_qname_must_start_at_the_beginning_of_the_expression + parser = REXML::Parsers::XPathParser.new + assert_raise(REXML::ParseException) do + parser.parse("//a/1\nb") + end + assert_raise(REXML::ParseException) do + parser.parse("child::1\nb") + end + assert_equal([:child, :qname, "a", "b"], parser.parse("a:b")) + end + def test_unary_minus_with_spaces parser = REXML::Parsers::XPathParser.new assert_equal(