Repository navigation
Don't let binary object output corrupt or crash the render - #2157
Open
caiotheodoro wants to merge 1 commit into
Open
caiotheodoro wants to merge 1 commit into
caiotheodoro wants to merge 1 commit into
Conversation
Author
|
I have signed the CLA! |
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.
fix: don't let binary object output corrupt or crash the render
Closes #1453.
Problem
Strings rendered to the output buffer in an encoding different from the
buffer's — e.g. ASCII-8BIT results from
base64_decode/base64_url_safe_decode(whose binary return is intentional and tested),binary-encoded assigns, or
Drop#to_s/ custom filter / custom tag strings —were appended with
String#<<unchecked. Depending on buffer state that eitherbinary string, e.g.
"\x80x"),Liquid error: internal, orEncoding::CompatibilityErrorpastTemplate#renderunhandled —including through text-node appends, which have no rescue.
Fix
Utils.write_to_output(output, str)convertsstrto the output's encodingwhen they differ, then appends:
str.b)force_encoding(output.encoding); bytes valid there rendercorrectly (
"aHello 👋b"), invalid bytes raiseLiquid::ArgumentError("invalid byte sequence in UTF-8"), matching theurl_decodeprecedentString#encodetranscode;EncodingError→Liquid::ArgumentErrorApplied where object output reaches the buffer:
Variable#render_obj_to_output(covers
{{ }},{% echo %}, arrays, and partials, which share the buffer),Cycle#render_to_output_buffer,Tag#render_to_output_buffer(third-partytags),
Tag::Disableable.Source-derived strings (text nodes,
{% raw %}bodies,{% ifchanged %}fragments, error messages) keep their byte semantics via a lenient variant,
Utils.write_text_to_output— a binary buffer accepts their raw bytes —plus an inlined
output.encoding == Encoding::BINARYcheck on the hottext-node append in
BlockBody(avoids a method call per literal; seebenchmarks). Binary-source templates still render as before.
After the change the output buffer keeps its declared encoding end-to-end, so
no append can raise
Encoding::CompatibilityErrormid-render.UTF-16 assigns now transcode correctly instead of
Liquid error: internal, andrender(output: binary_buffer)no longer crashes on non-ASCII templates.Tests
Six new tests in
test/integration/output_test.rb: valid/invalid binaryoutput,
base64_decode/base64_url_safe_decodeend-to-end,{% echo %}/{% cycle %}/ custom-tag paths, transcoding, and rendering into a binaryoutput buffer.
bundle exec rake test: all green in lax, strict and strict2 modes(1077 + 808 runs, 0 failures). Rubocop clean.
Benchmarks
Append-dense micro-benchmark (200 text+variable nodes): within ~0-8% of
baseline after inlining the hot text-node check (an earlier all-helper version
was ~25% slower). Theme render benchmark (
performance/theme_runner.rb):within noise — 177 i/s vs 155 i/s baseline, ±20-40% run variance. The fast
path adds an
is_a?/encodingcomparison per appended node.