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

check template UTF8 validity before parsing #1774

Merged
merged 1 commit into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/liquid/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
include: "Error in tag 'include' - Valid syntax: include '[template]' (with|for) [object|collection]"
inline_comment_invalid: "Syntax error in tag '#' - Each line of comments must be prefixed by the '#' character"
invalid_delimiter: "'%{tag}' is not a valid delimiter for %{block_name} tags. use %{block_delimiter}"
invalid_template_encoding: "Invalid template encoding"
render: "Syntax error in tag 'render' - Template name must be a quoted string"
table_row: "Syntax Error in 'table_row loop' - Valid syntax: table_row [item] in [collection] cols=3"
tag_never_closed: "'%{block_name}' tag was never closed"
Expand Down
5 changes: 5 additions & 0 deletions lib/liquid/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ def initialize
# Returns self for easy chaining
def parse(source, options = {})
parse_context = configure_options(options)

unless source.valid_encoding?
Copy link
Contributor

Choose a reason for hiding this comment

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

Will the regex always throw the UTF-8 error? If so, then this duplicates the validation work. What if we caught the exception and re-threw instead?

Copy link
Contributor Author

@ggmichaelgo ggmichaelgo Jan 8, 2024

Choose a reason for hiding this comment

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

This change is also for Liquid-C parser. Since Liquid-C doesn't use regex, it allowed Liquid templates like this:

{% assign foo = "Broken String\x00" %}
{% comment \xC0 %}
  \xC0
{% endcomment \xC0 %}

With this change, we won't have to implement UTF-8 validation check on Liquid-C.

Also, the UTF-8 error would have to be handled by checking error message, and I think it is more cleaner to check the validity by using String#valid_encoding?

rescue ArgumentError => e
  if e.message == "invalid byte sequence in UTF-8"

raise SyntaxError, parse_context.locale.t("errors.syntax.invalid_template_encoding")
end

tokenizer = parse_context.new_tokenizer(source, start_line_number: @line_numbers && 1)
@root = Document.parse(tokenizer, parse_context)
self
Expand Down
12 changes: 12 additions & 0 deletions test/integration/template_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -337,4 +337,16 @@ def to_s
assert_equal("x=2", output)
assert_instance_of(String, output)
end

def test_raises_error_with_invalid_utf8
e = assert_raises(SyntaxError) do
Template.parse(<<~LIQUID)
{% comment %}
\xC0
{% endcomment %}
LIQUID
end

assert_equal('Liquid syntax error: Invalid template encoding', e.message)
end
end