Repository navigation
Description
url_decode throws URI malformed when its input has a % that isn't a valid escape, and that fails the whole render. The usual input for this filter is a query parameter, so a URL like ?q=100% is enough to break the page for any visitor.
Version: 10.29.0 (all builds)
Repro:
const { Liquid } = require('liquidjs')
const engine = new Liquid()
await engine.parseAndRender('{{ q | url_decode }}', { q: '100%' })
// RenderError: URI malformed| input | current | expected |
|---|---|---|
100% |
RenderError: URI malformed | 100% |
%zz |
RenderError: URI malformed | %zz |
%E2%9C (incomplete UTF-8) |
RenderError: URI malformed | %E2%9C |
%E2%9C%93 done 100% |
RenderError: URI malformed | ✓ done 100% |
caf%C3%A9 |
café |
café (unchanged) |
a+b%20c |
a b c |
a b c (unchanged) |
Cause: the filter calls decodeURIComponent without catching its error:
export const url_decode = (x) => decodeURIComponent(stringify(x).replace(/\+/g, ' '))Expected: I believe Shopify's url_decode goes through Ruby's CGI.unescape, which leaves a malformed escape as written instead of raising. May be worth double checking if the goal is to have Shopify parity, but the fix would be useful either way.
Suggested fix: keep the current path for valid input. If it fails, decode each run of well-formed escapes separately and leave everything else as written:
const PERCENT_ESCAPES = /(?:%[0-9A-Fa-f]{2})+/g
export const url_decode = (x) => {
const str = stringify(x).replace(/\+/g, ' ')
try {
return decodeURIComponent(str)
} catch {
return str.replace(PERCENT_ESCAPES, run => {
try { return decodeURIComponent(run) } catch { return run }
})
}
}Valid input goes through the same path as today, so it renders exactly as before. That includes #939's handling of %2B: a%2Bb still gives a+b, and a%2Bb 100% now gives a+b 100% instead of throwing.
Happy to open a PR if that's useful.
Edit: updated the cause and fix for #939, which now replaces + before decoding.
Activity
fix(url_decode): leave malformed escapes as written