Skip to content

base64_decode throws in browser builds on input the node build accepts #965

Closed

Description

@josefguenther

base64_decode has two implementations, and they handle invalid input differently. The node build decodes leniently and always renders. The browser builds (liquid.browser.mjs, liquid.browser.umd.js) call atob, which throws, and that fails the whole render. So the same template renders or errors depending on which build loads it.

Version: 10.29.0

Repro:

const { Liquid } = require('liquidjs')   // or the browser build
const engine = new Liquid()

await engine.parseAndRender('{{ "!!!" | base64_decode }}')
await engine.parseAndRender('{{ n | base64_decode }}', { n: 5 })
await engine.parseAndRender('{{ o | base64_decode }}', { o: { a: 1 } })
input node build browser build
"!!!" "" RenderError: Invalid character
5 "" RenderError: The string to be decoded is not correctly encoded.
{ a: 1 } renders (garbage bytes) RenderError: Invalid character
"YWJj" "abc" "abc"

Cause: the two builds use different decoders.

// liquid.node.js
function base64Decode(str) {
    return Buffer.from(str, 'base64').toString('utf8');
}

// liquid.browser.mjs
function base64Decode(str) {
    return new TextDecoder().decode(Uint8Array.from(atob(str), c => c.charCodeAt(0)));
}

A template can't catch a filter error, and the input to this filter is often user data. That's why I'd suggest the node build's lenient behaviour for both. I know Shopify raises on invalid base64, so if throwing is the intended contract, the node build is the one that's out of line.

Suggested fix: have the browser build reproduce what Buffer.from(str, 'base64') does. That means stopping at the first =, accepting the URL-safe alphabet, dropping any other character, and dropping one trailing character that can't form a byte.

function base64Decode(str) {
    const clean = String(str).split('=')[0]
        .replace(/-/g, '+').replace(/_/g, '/')
        .replace(/[^A-Za-z0-9+/]/g, '');
    const usable = clean.length % 4 === 1 ? clean.slice(0, -1) : clean;
    return new TextDecoder().decode(Uint8Array.from(atob(usable), c => c.charCodeAt(0)));
}

I checked this against Buffer.from(s, 'base64').toString('utf8') on 200k random strings built from base64, URL-safe, =, whitespace and junk characters. There were no differences.

Happy to open a PR if that's useful.

Activity

  1. added a commit that references this issue on Oct 28, 2026
    83bbfe0
  2. github-actions commented on Oct 28, 2026

    @github-actions

    🎉 This issue has been resolved in version 10.30.1 🎉

    The release is available on:

    Your semantic-release bot 📦🚀

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions