Skip to content

Commit

Permalink
check template UTF8 validity before parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
ggmichaelgo committed Jan 10, 2024
1 parent a3c8376 commit b360602
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
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?
raise SyntaxError, parse_context.locale.t("errors.syntax.invalid_utf8_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 UTF-8 encoding', e.message)
end
end

0 comments on commit b360602

Please sign in to comment.