Skip to content

Don't let binary object output corrupt or crash the render - #2157

Open
caiotheodoro wants to merge 1 commit into
Shopify:mainfrom
caiotheodoro:fix/output-encoding-mismatch
Open

caiotheodoro wants to merge 1 commit into
Shopify:mainfrom
caiotheodoro:fix/output-encoding-mismatch

Conversation

@caiotheodoro

Copy link
Copy Markdown

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 either

  • silently turned the UTF-8 output buffer ASCII-8BIT (the rendered result is a
    binary string, e.g. "\x80x"),
  • was swallowed as Liquid error: internal, or
  • raised Encoding::CompatibilityError past Template#render unhandled —
    including through text-node appends, which have no rescue.
Liquid::Template.parse('{{ "gA==" | base64_decode }}é').render
# before: Encoding::CompatibilityError escapes render
Liquid::Template.parse("a{{ s | base64_encode | base64_decode }}b").render("s" => "Hello 👋")
# before: binary-tagged "aHello \xF0\x9F\x91\x8Bb" (or `Liquid error: internal`)

Fix

Utils.write_to_output(output, str) converts str to the output's encoding
when they differ, then appends:

  • binary output buffer → appends the raw bytes (str.b)
  • binary input → force_encoding(output.encoding); bytes valid there render
    correctly ("aHello 👋b"), invalid bytes raise
    Liquid::ArgumentError ("invalid byte sequence in UTF-8"), matching the
    url_decode precedent
  • any other mismatch → String#encode transcode; EncodingError →
    Liquid::ArgumentError

Applied 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-party
tags), 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::BINARY check on the hot
text-node append in BlockBody (avoids a method call per literal; see
benchmarks). 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::CompatibilityError mid-render.

Liquid::Template.parse('{{ "gA==" | base64_decode }}é').render
# => "Liquid error: invalid byte sequence in UTF-8é" (+ Liquid::ArgumentError in template.errors)
Liquid::Template.parse("a{{ s | base64_encode | base64_decode }}b").render("s" => "Hello 👋")
# => "aHello 👋b" (UTF-8)

UTF-16 assigns now transcode correctly instead of Liquid error: internal, and
render(output: binary_buffer) no longer crashes on non-ASCII templates.

Tests

Six new tests in test/integration/output_test.rb: valid/invalid binary
output, base64_decode/base64_url_safe_decode end-to-end, {% echo %} /
{% cycle %} / custom-tag paths, transcoding, and rendering into a binary
output 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?/encoding comparison per appended node.

@caiotheodoro

Copy link
Copy Markdown
Author

I have signed the CLA!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Base64 Decode Non-ASCII Data

1 participant