From 369a6c55e349e8785f1c5964f65db3258b210688 Mon Sep 17 00:00:00 2001 From: Michael Go Date: Fri, 5 Jan 2024 17:53:29 -0400 Subject: [PATCH] check template UTF8 validity before parsing --- lib/liquid/locales/en.yml | 1 + lib/liquid/template.rb | 5 +++++ test/integration/template_test.rb | 12 ++++++++++++ 3 files changed, 18 insertions(+) diff --git a/lib/liquid/locales/en.yml b/lib/liquid/locales/en.yml index 7e232de4..f33c61ce 100644 --- a/lib/liquid/locales/en.yml +++ b/lib/liquid/locales/en.yml @@ -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" diff --git a/lib/liquid/template.rb b/lib/liquid/template.rb index 30742dd6..de06a6fa 100644 --- a/lib/liquid/template.rb +++ b/lib/liquid/template.rb @@ -107,6 +107,11 @@ module Liquid # 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_template_encoding") + end + tokenizer = parse_context.new_tokenizer(source, start_line_number: @line_numbers && 1) @root = Document.parse(tokenizer, parse_context) self diff --git a/test/integration/template_test.rb b/test/integration/template_test.rb index d2225489..2ed1a6fa 100644 --- a/test/integration/template_test.rb +++ b/test/integration/template_test.rb @@ -337,4 +337,16 @@ class TemplateTest < Minitest::Test 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