Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't allow invalid syntax inside comment tag #1770

Merged
merged 2 commits into from
Jan 2, 2024

Conversation

ggmichaelgo
Copy link
Contributor

@ggmichaelgo ggmichaelgo commented Dec 14, 2023

What are you trying to solve?

#1755 introduced a new feature that allows invalid code to be inside a comment tag.

For Liquid version 5.4.0, this is a valid template, and not with our current main branch:

{% comment %}
  {% assign a = 1
  {% endcomment %}
{% endcomment %}

The assign tag is not closed immediately, {% assign a = 1 {% endcomment %} is parsed as the assign tag.

Since the #1755 changed the comment tag parser to use the BlockBody::FullTokenPossiblyInvalid regex pattern, it parses {% assign a = 1 {% endcomment %} to be the comment tag delimiter, and it raises a syntax error by the extra {% endcomment %} token.

How are you solving this issue?

In order to remove this breaking change, this PR updates the comment to parse tokens like BlockBody and not like raw tag.
However, the comment tag will still allow unclosed tags such as this example:

{% comment %}
  {% if true %}
  {% if  %}
{% endcomment %}

@ggmichaelgo ggmichaelgo marked this pull request as ready for review December 14, 2023 20:46
@ggmichaelgo ggmichaelgo force-pushed the revert-invalid-comment-body branch from 54996ab to 8b4bb21 Compare December 14, 2023 20:56
test/unit/tags/comment_tag_unit_test.rb Outdated Show resolved Hide resolved
test/test_helper.rb Outdated Show resolved Hide resolved
Gemfile Outdated Show resolved Hide resolved
@ggmichaelgo ggmichaelgo force-pushed the revert-invalid-comment-body branch from 8b4bb21 to 3c5ad7d Compare December 14, 2023 21:01
Comment on lines +49 to +72
assert_raises(Liquid::SyntaxError) do
assert_template_result("", <<~LIQUID.chomp)
{% comment %}
{% assign foo = "1"
{% endcomment %}
LIQUID
end

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these should be valid templates right? Incomplete tags should be allowed inside of a comment block.

Copy link
Contributor Author

@ggmichaelgo ggmichaelgo Dec 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, however, we can't introduce a change that will break existing Liquid templates.

For an example,

{% comment %}
  {% assign a = 123
  {% endcomment %}
{% endcomment %}

This Liquid template is a valid with version 5.4.0 because the assign tag is captured as {% assign a = 123 {% endcomment %}. (Liquid allows extra string inside a tag)

So, if we parse comment tag like the raw tag, we would capture, we would ignore the {% assign a = 123 and capture {% endcomment %} from line 3 as the comment tag's delimiter. Since the template has an extra {% endcomment %} tag delimiter, the template is no longer valid.

@ggmichaelgo ggmichaelgo merged commit 96a0363 into main Jan 2, 2024
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants