Repository navigation
fix(tokenizer): raise Liquid::SyntaxError for invalid UTF-8 whatever the first token is - #2148
Draft
EvilGenius13 wants to merge 1 commit into
Draft
EvilGenius13 wants to merge 1 commit into
EvilGenius13 wants to merge 1 commit into
Conversation
…the first token is
A template with invalid bytes raised a plain ArgumentError when its first
non-variable token was a tag, or when it was a {% liquid %} body. Callers that
rescue Liquid::Error, such as validators that build the tokenizer through
ParseContext#new_tokenizer, crashed instead of reporting a syntax error.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What and why
A template with an invalid byte raises a plain
ArgumentErrorinstead ofLiquid::SyntaxErrorwhen the first token that isn't a{{ }}variable is a tag. The firstskip_untilvalidates the whole string, so whichever scanner method runs first decides which error comes out. Onlynext_text_tokenturns it into aSyntaxError, which #1905 added. The tag scanners came from the StringScanner rewrite in #1844 and never did. Thesplitin{% liquid %}mode raises the plainArgumentErrortoo.Template.parsechecksvalid_encoding?before it tokenizes, so it never hits this. Callers that build the tokenizer throughParseContext#new_tokenizerdo.test_invalid_utf8already builds aTokenizerdirectly and expectsSyntaxError, so that is the tokenizer's contract. In Shopify core theArgumentErrorgets pastrescue Liquid::Errorin theme asset validation, so a bad byte fails the theme import or background job instead of reporting a syntax error.This checks
valid_encoding?once inTokenizer#initialize, raises the same message as before, and removes the rescue innext_text_tokenthat can no longer fire. Ruby caches the result of the encoding check on the string, so checking up front costs next to nothing.#2056 added the same check but never fully merged.