mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-12 23:40:45 -07:00
Merge pull request #1905 from Shopify/catlee/invalid_utf8
Raise SyntaxError on invalid UTF8 strings in lexer/tokenizer
This commit is contained in:
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
## 5.7.3 (unreleased)
|
## 5.7.3 (unreleased)
|
||||||
|
|
||||||
|
* Raise Liquid::SyntaxError when parsing invalidly encoded strings
|
||||||
|
|
||||||
## 5.7.2 2025-01-31
|
## 5.7.2 2025-01-31
|
||||||
|
|
||||||
* Fix array filters to not support nested properties
|
* Fix array filters to not support nested properties
|
||||||
|
|||||||
@@ -161,6 +161,12 @@ module Liquid
|
|||||||
end
|
end
|
||||||
# rubocop:enable Metrics/BlockNesting
|
# rubocop:enable Metrics/BlockNesting
|
||||||
output << EOS
|
output << EOS
|
||||||
|
rescue ::ArgumentError => e
|
||||||
|
if e.message == "invalid byte sequence in #{ss.string.encoding}"
|
||||||
|
raise SyntaxError, "Invalid byte sequence in #{ss.string.encoding}"
|
||||||
|
else
|
||||||
|
raise
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def raise_syntax_error(start_pos, ss)
|
def raise_syntax_error(start_pos, ss)
|
||||||
|
|||||||
@@ -103,6 +103,12 @@ module Liquid
|
|||||||
|
|
||||||
pos = @ss.pos -= 2
|
pos = @ss.pos -= 2
|
||||||
@source.byteslice(start, pos - start)
|
@source.byteslice(start, pos - start)
|
||||||
|
rescue ::ArgumentError => e
|
||||||
|
if e.message == "invalid byte sequence in #{@ss.string.encoding}"
|
||||||
|
raise SyntaxError, "Invalid byte sequence in #{@ss.string.encoding}"
|
||||||
|
else
|
||||||
|
raise
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def next_variable_token
|
def next_variable_token
|
||||||
|
|||||||
@@ -131,6 +131,16 @@ class LexerUnitTest < Minitest::Test
|
|||||||
assert_equal([[:id, "false"], [:number, "1"], [:end_of_string]], tokenize("false 1"))
|
assert_equal([[:id, "false"], [:number, "1"], [:end_of_string]], tokenize("false 1"))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_error_with_invalid_utf8
|
||||||
|
error = assert_raises(SyntaxError) do
|
||||||
|
tokenize("\x00\xff")
|
||||||
|
end
|
||||||
|
assert_equal(
|
||||||
|
'Liquid syntax error: Invalid byte sequence in UTF-8',
|
||||||
|
error.message,
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def tokenize(input)
|
def tokenize(input)
|
||||||
|
|||||||
@@ -35,4 +35,15 @@ class TemplateUnitTest < Minitest::Test
|
|||||||
def test_template_inheritance
|
def test_template_inheritance
|
||||||
assert_equal("foo", TemplateSubclass.parse("foo").render)
|
assert_equal("foo", TemplateSubclass.parse("foo").render)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_invalid_utf8
|
||||||
|
input = "\xff\x00"
|
||||||
|
error = assert_raises(SyntaxError) do
|
||||||
|
Liquid::Tokenizer.new(source: input, string_scanner: StringScanner.new(input))
|
||||||
|
end
|
||||||
|
assert_equal(
|
||||||
|
'Liquid syntax error: Invalid byte sequence in UTF-8',
|
||||||
|
error.message,
|
||||||
|
)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user