From a453c93e622e9178df320bb0a12acc5bd40e0101 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Efe=20G=C3=B6kdemir?= Date: Tue, 3 Nov 2026 04:19:03 +0300 Subject: [PATCH 1/2] fix: parse ranges with dotted lookup keys --- lib/liquid/expression.rb | 66 ++++++++++++++++++++++++--- test/integration/tags/for_tag_test.rb | 7 +++ 2 files changed, 66 insertions(+), 7 deletions(-) diff --git a/lib/liquid/expression.rb b/lib/liquid/expression.rb index 7e15f85b0..18292eedf 100644 --- a/lib/liquid/expression.rb +++ b/lib/liquid/expression.rb @@ -28,6 +28,7 @@ class Expression RANGES_REGEX = /\A\(\s*(?>(\S+)\s*\.\.)\s*(\S+)\s*\)\z/ INTEGER_REGEX = /\A(-?\d+)\z/ FLOAT_REGEX = /\A(-?\d+)\.\d+\z/ + RANGE_MARKUP_WITH_NESTED_LOOKUPS = /["'\[]/ class << self def safe_parse(parser, ss = StringScanner.new(""), cache = nil) @@ -57,13 +58,14 @@ def parse(markup, ss = StringScanner.new(""), cache = nil) end def inner_parse(markup, ss, cache) - if markup.start_with?("(") && markup.end_with?(")") && markup =~ RANGES_REGEX - return RangeLookup.parse( - Regexp.last_match(1), - Regexp.last_match(2), - ss, - cache, - ) + if markup.start_with?("(") && markup.end_with?(")") && (match = RANGES_REGEX.match(markup)) + start_markup = match[1] + if start_markup.match?(RANGE_MARKUP_WITH_NESTED_LOOKUPS) && !range_markup_start_balanced?(start_markup) + range_markups = parse_range_markup(markup) + return RangeLookup.parse(range_markups[0], range_markups[1], ss, cache) if range_markups + else + return RangeLookup.parse(start_markup, match[2], ss, cache) + end end if (num = parse_number(markup, ss)) @@ -125,6 +127,56 @@ def parse_number(markup, ss) markup.byteslice(0, first_dot_pos).to_f end end + + # A separator inside a quoted lookup leaves the regex's left capture unbalanced. + def range_markup_start_balanced?(markup) + markup.count("[") == markup.count("]") && markup.count('"').even? && markup.count("'").even? + end + private :range_markup_start_balanced? + + def parse_range_markup(markup) + quote = nil + bracket_depth = 0 + delimiter_index = nil + index = 1 + end_index = markup.length - 1 + + # Keep the regex's rightmost-separator behaviour for triple-dot ranges. + while index < end_index + byte = markup.getbyte(index) + + if quote + if byte == "\\".ord + index += 2 + next + elsif byte == quote + quote = nil + end + else + case byte + when 34, 39 + quote = byte + when "[".ord + bracket_depth += 1 + when "]".ord + bracket_depth -= 1 if bracket_depth.positive? + when DOT + delimiter_index = index if bracket_depth.zero? && markup.getbyte(index + 1) == DOT + end + end + + index += 1 + end + + return unless delimiter_index + + start_markup = markup[1...delimiter_index].strip + end_markup = markup[(delimiter_index + 2)...-1].strip + return if start_markup.empty? || end_markup.empty? + + [start_markup, end_markup] + end + private :parse_range_markup end end end diff --git a/test/integration/tags/for_tag_test.rb b/test/integration/tags/for_tag_test.rb index 36510eeab..60eaacebe 100644 --- a/test/integration/tags/for_tag_test.rb +++ b/test/integration/tags/for_tag_test.rb @@ -52,6 +52,13 @@ def test_for_with_variable_range assert_template_result(' 1 2 3 ', '{%for item in (1..foobar) %} {{item}} {%endfor%}', { "foobar" => 3 }) end + def test_for_with_range_endpoints_containing_double_dots + assigns = { 'paths' => { '..' => 1, '...' => 5 } } + + assert_template_result('12345', '{% for item in (1..paths["..."]) %}{{ item }}{% endfor %}', assigns) + assert_template_result('12345', '{% for item in (paths[".."]..paths["..."]) %}{{ item }}{% endfor %}', assigns) + end + def test_for_with_hash_value_range foobar = { "value" => 3 } assert_template_result(' 1 2 3 ', '{%for item in (1..foobar.value) %} {{item}} {%endfor%}', { "foobar" => foobar }) From 611f2c3433abcc7a728cc10c6369477632e1b3a4 Mon Sep 17 00:00:00 2001 From: Efe Date: Tue, 3 Nov 2026 04:54:40 +0300 Subject: [PATCH 2/2] Fix byte offsets when splitting multibyte range endpoints --- lib/liquid/expression.rb | 6 +++--- test/integration/tags/for_tag_test.rb | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/lib/liquid/expression.rb b/lib/liquid/expression.rb index 18292eedf..8210c9aa9 100644 --- a/lib/liquid/expression.rb +++ b/lib/liquid/expression.rb @@ -139,7 +139,7 @@ def parse_range_markup(markup) bracket_depth = 0 delimiter_index = nil index = 1 - end_index = markup.length - 1 + end_index = markup.bytesize - 1 # Keep the regex's rightmost-separator behaviour for triple-dot ranges. while index < end_index @@ -170,8 +170,8 @@ def parse_range_markup(markup) return unless delimiter_index - start_markup = markup[1...delimiter_index].strip - end_markup = markup[(delimiter_index + 2)...-1].strip + start_markup = markup.byteslice(1...delimiter_index).strip + end_markup = markup.byteslice((delimiter_index + 2)...-1).strip return if start_markup.empty? || end_markup.empty? [start_markup, end_markup] diff --git a/test/integration/tags/for_tag_test.rb b/test/integration/tags/for_tag_test.rb index 60eaacebe..b7a1b4b07 100644 --- a/test/integration/tags/for_tag_test.rb +++ b/test/integration/tags/for_tag_test.rb @@ -59,6 +59,29 @@ def test_for_with_range_endpoints_containing_double_dots assert_template_result('12345', '{% for item in (paths[".."]..paths["..."]) %}{{ item }}{% endfor %}', assigns) end + def test_for_with_multibyte_dotted_range_keys + ["é", "界", "🍎"].each do |prefix| + assigns = { 'paths' => { "#{prefix}.." => 1, "#{prefix}..." => 5 } } + ranges = [ + '(paths["' + prefix + '.."]..paths["' + prefix + '..."])', + '(paths["' + prefix + '.."]..5)', + '(1..paths["' + prefix + '..."])', + ] + + ranges.each do |range| + assert_template_result('12345', '{% for item in ' + range + ' %}{{ item }}{% endfor %}', assigns) + end + end + end + + def test_for_with_dotted_range_keys_in_non_utf8_encoding + lower = "é..".encode(Encoding::ISO_8859_1) + upper = "é...".encode(Encoding::ISO_8859_1) + markup = '{% for item in (paths["' + lower + '"]..paths["' + upper + '"]) %}{{ item }}{% endfor %}' + + assert_template_result('12345', markup, { 'paths' => { lower => 1, upper => 5 } }) + end + def test_for_with_hash_value_range foobar = { "value" => 3 } assert_template_result(' 1 2 3 ', '{%for item in (1..foobar.value) %} {{item}} {%endfor%}', { "foobar" => foobar })