Skip to content

fix(url_decode): leave a stray % as written - #970

Merged
harttle merged 3 commits into
harttle:masterfrom
josefguenther:fix/url-decode-malformed-escape
Oct 29, 2026
Merged

harttle merged 3 commits into
harttle:masterfrom
josefguenther:fix/url-decode-malformed-escape

Conversation

@josefguenther

@josefguenther josefguenther commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

url_decode (src/filters/url.ts:3) passes its whole input to decodeURIComponent, so a single % that isn't followed by two hex digits throws and fails the render. {{ v | url_decode }}:

v master Shopify this PR
100% RenderError: URI malformed 100% 100%
%zz RenderError: URI malformed %zz %zz
%E2%9C%93 done 100% RenderError: URI malformed ✓ done 100% ✓ done 100%
a%2Bb 100% RenderError: URI malformed a+b 100% a+b 100%
%E2%9C RenderError: URI malformed error: invalid byte sequence in UTF-8 RenderError: URI malformed
%ff RenderError: URI malformed error: invalid byte sequence in UTF-8 RenderError: URI malformed
caf%C3%A9%ff RenderError: URI malformed error: invalid byte sequence in UTF-8 RenderError: URI malformed
caf%C3%A9 café café café
a+b%20c a b c a b c a b c

This decodes each run of %XX escapes with decodeURIComponent, as Shopify's CGI.unescape decodes each run, so a stray % stays as written and a run that isn't valid UTF-8 still throws. For input that decodes today the output is unchanged, and + still becomes a space before decoding, so #939's %2B handling stays. The docs page for the filter now says how malformed input behaves.

Two new tests in test/integration/filters/url.spec.ts: a stray % beside valid escapes, which fails on master, and invalid UTF-8, which still throws. npm run build, npm test (1674 tests) and npm run lint pass on Node 24.11.1.

Fixes #966

Written with AI assistance (Claude Code); I have reviewed the change.

url_decode passed its whole input to decodeURIComponent, so a single
malformed escape (a "%" not followed by two hex digits, or an
incomplete UTF-8 sequence) threw a RenderError and failed the render,
discarding any valid escapes around it. It now decodes each run of
well-formed escapes separately and leaves the rest as written, which
is how Shopify's CGI.unescape treats a stray "%". "+" is still turned
into a space before decoding, so %2B stays a literal plus.

Fixes harttle#966

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@harttle harttle added the bug label Oct 1, 2026
@harttle

harttle commented Oct 1, 2026 •

Copy link
Copy Markdown
Owner

I find one discrepency:

input                           ruby              liquidjs          PR #970
"100%"                          "100%"            error             "100%"
"%zz"                           "%zz"             error             "%zz"
"%E2%9C"                        error             error             "%E2%9C"
"%E2%9C%93 done 100%"           "✓ done 100%"     error             "✓ done 100%"
"a%2Bb 100%"                    "a+b 100%"        error             "a+b 100%"
"caf%C3%A9"                     "café"            "café"            "café"
"a+b c"                         "a b c"           "a b c"           "a b c"
"1%2B1"                         "1+1"             "1+1"             "1+1"
"foo+bar"                       "foo bar"         "foo bar"         "foo bar"
"foo%20bar"                     "foo bar"         "foo bar"         "foo bar"
"foo%[email protected]"           "[email protected]"  "[email protected]"  "[email protected]"
"%E2%9C%93+100%"                "✓ 100%"          error             "✓ 100%"
"%ff"                           error             error             "%ff"

Not sure for other cases, but you can check with Shopify/liquid.

Each run of escapes was decoded with decodeURIComponent and kept as
written if that threw, so one byte that isn't valid UTF-8 left its
whole run undecoded: "caf%C3%A9%ff" rendered as written rather than
"café�". Each run is now decoded as UTF-8 bytes, and a byte sequence
that forms no character becomes U+FFFD, as the URL Standard's
percent-decoding does. A leading byte order mark is kept, as
decodeURIComponent keeps it.

The docs page now states how malformed input renders.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@josefguenther josefguenther changed the title fix(url_decode): leave malformed escapes as written fix(url_decode): stop malformed escapes failing the render Oct 1, 2026
@josefguenther

Copy link
Copy Markdown
Contributor Author

Thanks for checking against Ruby. You're right about those two rows, and they led me to a worse case the first commit got wrong: one bad byte left its whole run of escapes undecoded, so caf%C3%A9%ff came out as caf%C3%A9%ff rather than café�. The new commit decodes each run as UTF-8 bytes and replaces only the bytes that don't form a character with U+FFFD, the way the URL Standard and URLSearchParams do. A stray % still stays as written, like Shopify.

The invalid-UTF-8 rows are the same question as base64 in #971; I've written up there why I think the render should continue.

Comment thread src/filters/url.ts Resolved
Each run of escapes is now decoded with decodeURIComponent rather than
TextDecoder, so a run that isn't valid UTF-8 throws, as Shopify/Liquid
does, instead of rendering U+FFFD. A "%" that isn't followed by two
hex digits is still left as written.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@josefguenther josefguenther changed the title fix(url_decode): stop malformed escapes failing the render fix(url_decode): leave a stray % as written Oct 7, 2026
@josefguenther

Copy link
Copy Markdown
Contributor Author

Dropped TextDecoder and Uint8Array: each run of %XX escapes now goes through decodeURIComponent. A stray % stays as written and invalid UTF-8 throws, both as in Shopify (I checked every row of your table against the gem). Nothing differs between the Node and browser bundles anymore, so there's no bundle-specific test. Updated the title and description.

@harttle
harttle merged commit 74428ae into harttle:master Oct 29, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

url_decode throws on a malformed % escape

2 participants