From 3c5ad7db61ae323e842b34fa0de65482f108bfde Mon Sep 17 00:00:00 2001 From: Michael Go Date: Thu, 14 Dec 2023 13:55:28 -0400 Subject: [PATCH 1/2] don't allow invalid syntax inside comment tag --- lib/liquid/tags/comment.rb | 10 ++---- test/unit/tags/comment_tag_unit_test.rb | 44 ++++++++++++++++++------- 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/lib/liquid/tags/comment.rb b/lib/liquid/tags/comment.rb index 792c0860..7b83c3ee 100644 --- a/lib/liquid/tags/comment.rb +++ b/lib/liquid/tags/comment.rb @@ -49,15 +49,9 @@ module Liquid next if tag_name_match.nil? tag_name_match[1] - elsif token =~ BlockBody::FullToken && Regexp.last_match(2) == "comment" || Regexp.last_match(2) == "endcomment" - # aggressively match comment tag or comment tag delimiter - Regexp.last_match(2) else - tag_name_match = BlockBody::FullTokenPossiblyInvalid.match(token) - - next if tag_name_match.nil? - - tag_name_match[2] + token =~ BlockBody::FullToken + Regexp.last_match(2) end case tag_name diff --git a/test/unit/tags/comment_tag_unit_test.rb b/test/unit/tags/comment_tag_unit_test.rb index e4f8b149..d3ce4cb9 100644 --- a/test/unit/tags/comment_tag_unit_test.rb +++ b/test/unit/tags/comment_tag_unit_test.rb @@ -31,25 +31,45 @@ class CommentTagUnitTest < Minitest::Test LIQUID end - def test_allows_incomplete_tags_inside_a_comment - assert_template_result("", <<~LIQUID.chomp) + def test_allows_unclosed_tags + assert_template_result('', <<~LIQUID.chomp) {% comment %} - {% assign foo = "1" + {% if true %} + {% endcomment %} + LIQUID + end + + def test_block_body_must_be_valid + assert_template_result('', <<~LIQUID.chomp) + {% comment %} + {% assign a = 123 {% comment %} {% endcomment %} LIQUID - assert_template_result("", <<~LIQUID.chomp) - {% comment %} + assert_raises(Liquid::SyntaxError) do + assert_template_result("", <<~LIQUID.chomp) {% comment %} - {% invalid + {% assign foo = "1" {% endcomment %} - {% endcomment %} - LIQUID + LIQUID + end - assert_template_result("", <<~LIQUID.chomp) - {% comment %} - {% {{ {%- endcomment %} - LIQUID + assert_raises(Liquid::SyntaxError) do + assert_template_result("", <<~LIQUID.chomp) + {% comment %} + {% comment %} + {% invalid + {% endcomment %} + {% endcomment %} + LIQUID + end + + assert_raises(Liquid::SyntaxError) do + assert_template_result("", <<~LIQUID.chomp) + {% comment %} + {% {{ {%- endcomment %} + LIQUID + end end def test_child_comment_tags_need_to_be_closed From fc9c3386822fe37d81a509631a9717b03eb964ef Mon Sep 17 00:00:00 2001 From: Michael Go Date: Thu, 14 Dec 2023 17:08:26 -0400 Subject: [PATCH 2/2] refactor: rename comment tag unit test --- test/unit/tags/comment_tag_unit_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/tags/comment_tag_unit_test.rb b/test/unit/tags/comment_tag_unit_test.rb index d3ce4cb9..b5bc48fa 100644 --- a/test/unit/tags/comment_tag_unit_test.rb +++ b/test/unit/tags/comment_tag_unit_test.rb @@ -39,7 +39,7 @@ class CommentTagUnitTest < Minitest::Test LIQUID end - def test_block_body_must_be_valid + def test_open_tags_in_comment assert_template_result('', <<~LIQUID.chomp) {% comment %} {% assign a = 123 {% comment %}