From 7754d5aef57134fd9fe6c36a40f9a5514ecf2874 Mon Sep 17 00:00:00 2001 From: Dylan Thacker-Smith Date: Wed, 28 Oct 2020 10:37:00 -0400 Subject: [PATCH] Attempt to strict parse variables before lax parsing in lax error mode (#1338) --- lib/liquid/parser_switching.rb | 12 ++++++++++++ lib/liquid/variable.rb | 2 +- test/integration/assign_test.rb | 5 +++++ test/integration/variable_test.rb | 5 +++++ 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/liquid/parser_switching.rb b/lib/liquid/parser_switching.rb index 1a8d4ff..78afd58 100644 --- a/lib/liquid/parser_switching.rb +++ b/lib/liquid/parser_switching.rb @@ -2,6 +2,18 @@ module Liquid module ParserSwitching + def strict_parse_with_error_mode_fallback(markup) + strict_parse_with_error_context(markup) + rescue SyntaxError => e + case parse_context.error_mode + when :strict + raise + when :warn + parse_context.warnings << e + end + lax_parse(markup) + end + def parse_with_selected_parser(markup) case parse_context.error_mode when :strict then strict_parse_with_error_context(markup) diff --git a/lib/liquid/variable.rb b/lib/liquid/variable.rb index 77d6b0c..891425c 100644 --- a/lib/liquid/variable.rb +++ b/lib/liquid/variable.rb @@ -30,7 +30,7 @@ module Liquid @parse_context = parse_context @line_number = parse_context.line_number - parse_with_selected_parser(markup) + strict_parse_with_error_mode_fallback(markup) end def raw diff --git a/test/integration/assign_test.rb b/test/integration/assign_test.rb index b956fd1..c5a8dd9 100644 --- a/test/integration/assign_test.rb +++ b/test/integration/assign_test.rb @@ -48,6 +48,11 @@ class AssignTest < Minitest::Test end end + def test_expression_with_whitespace_in_square_brackets + source = "{% assign r = a[ 'b' ] %}{{ r }}" + assert_template_result('result', source, 'a' => { 'b' => 'result' }) + end + def test_assign_score_exceeding_resource_limit t = Template.parse("{% assign foo = 42 %}{% assign bar = 23 %}") t.resource_limits.assign_score_limit = 1 diff --git a/test/integration/variable_test.rb b/test/integration/variable_test.rb index 168f43a..ad1b28e 100644 --- a/test/integration/variable_test.rb +++ b/test/integration/variable_test.rb @@ -21,6 +21,11 @@ class VariableTest < Minitest::Test assert_equal(' worked wonderfully ', template.render!('test' => 'worked wonderfully')) end + def test_expression_with_whitespace_in_square_brackets + assert_template_result('result', "{{ a[ 'b' ] }}", 'a' => { 'b' => 'result' }) + assert_template_result('result', "{{ a[ [ 'b' ] ] }}", 'b' => 'c', 'a' => { 'c' => 'result' }) + end + def test_ignore_unknown template = Template.parse(%({{ test }})) assert_equal('', template.render!)