From a9c25a36b6628d7267db458f75209e2a9ea74336 Mon Sep 17 00:00:00 2001 From: benoit74 Date: Fri, 6 Nov 2026 19:25:19 +0000 Subject: [PATCH] Fix JS URL rewriter confusing %2B with raw + in query strings wombatSetup.js decoded the query string with decodeURIComponent() before replacing '+' with ' ', so a literal '+' that had been correctly percent-encoded as '%2B' (meaning "real plus sign") got turned back into '+' by the decode, then immediately smashed into a space by the replace - indistinguishable from a raw, unencoded '+' (which means "space", by convention). Apply the substitution on the still-encoded query string instead, before decoding, matching the order already used by the Python implementation (url_rewriting.py). Found while verifying the fix for openzim/warc2zim#327: MDN's "Try it" live-sample iframes use a base64-encoded `state` query parameter, which legitimately contains '+' characters; those got corrupted into spaces, so the ZIM path built from the rewritten URL no longer matched the WARC record and the iframe 404'd. (#389) --- CHANGELOG.md | 1 + javascript/src/wombatSetup.js | 8 ++++-- javascript/test/wombatUrlRewriting.js | 40 +++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a482956..7ccf588 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Keep escaped Unicode characters in CSS strings instead of turning them into literal characters when rewriting (#377) - Patch vendored `wombat.js` so element properties (`img.src`, `script.src`, `link.href`, `a.href` and its derived `.pathname`/`.search`/..., `Attr.value`) return and forget a server-side-rewriter-recorded original the same way `getAttribute` already does (#386, webrecorder/wombat#240) - Detect `export const`/`let`/`var`/`function`/`async function`/`*` as module code (`EXPORT_RX` only recognized `export {...}`, `export default` and `export class`), fixing JS rewriting wrongly injecting a non-module `let arguments;` wrapper into ES modules using these common export forms, which is a syntax error in strict/module code (#387) +- Fix JS URL rewriter confusing a percent-encoded `%2B` with a raw `+` in query strings (#389) - Patch vendored `wombat.js` so `retrieveWBOSRCAsURL` (the helper behind the above element-property fix) resolves a recorded original against the frame's true original URL (`wb_info.url`) instead of its replay-time `ownerDocument`, fixing it returning/using a URL under the wrong host for a page whose own URL was changed by a fuzzy rule (e.g. YouTube embeds), which broke playback (#398) ## [5.5.2] - 2026-11-06 diff --git a/javascript/src/wombatSetup.js b/javascript/src/wombatSetup.js index c3266d5..0aeac98 100644 --- a/javascript/src/wombatSetup.js +++ b/javascript/src/wombatSetup.js @@ -187,11 +187,15 @@ export function urlRewriteFunction( : decodeURIComponent(absolute_url_parts.path); // And the decoded query, only exception is that + sign must resolve to ' ' to avoid - // confusion (our convention, just like in Python) + // confusion (our convention, just like in Python). The `+` -> ' ' substitution must + // happen on the still-encoded query string, before decoding percent-escapes, + // otherwise a value containing a literal `+` correctly percent-encoded as `%2B` + // would get wrongly turned into a space once decodeURIComponent turns it back into + // a raw `+` (same order as the Python implementation). const decoded_query = !absolute_url_parts.query || absolute_url_parts.query.length === 0 ? '' - : '?' + decodeURIComponent(absolute_url_parts.query).replaceAll('+', ' '); + : '?' + decodeURIComponent(absolute_url_parts.query.replaceAll('+', ' ')); // combine all decoded parts to get the ZIM path // (consecutive slashes are kept, just like in Python) diff --git a/javascript/test/wombatUrlRewriting.js b/javascript/test/wombatUrlRewriting.js index f9db7a9..1973922 100644 --- a/javascript/test/wombatUrlRewriting.js +++ b/javascript/test/wombatUrlRewriting.js @@ -517,6 +517,46 @@ test('contentWithQueryStringDotDotSegment', (t) => { ); }); +test('contentWithQueryValueRawPlusSign', (t) => { + // a raw (unencoded) `+` in a query string means a space, by convention + t.is( + urlRewriteFunction( + t.context.currentUrl, + t.context.originalHost, + t.context.originalScheme, + t.context.originalUrl, + t.context.prefix, + 'https://www.example.com/javascript/content.txt?query=val+ue', + undefined, + undefined, + undefined, + ), + 'http://library.kiwix.org/content/myzim_yyyy-mm/www.example.com/javascript/content.txt%3Fquery=val%20ue', + ); +}); + +test('contentWithQueryValueEncodedPlusSign', (t) => { + // a percent-encoded `%2B` in a query string means a literal `+`, and must not be + // confused with a raw `+` (which means a space, see test above). The `+` -> ' ' + // substitution must happen on the still-encoded query string, before decoding + // percent-escapes, otherwise this would wrongly turn into a space once + // decodeURIComponent turns `%2B` back into a raw `+`. + t.is( + urlRewriteFunction( + t.context.currentUrl, + t.context.originalHost, + t.context.originalScheme, + t.context.originalUrl, + t.context.prefix, + 'https://www.example.com/javascript/content.txt?query=val%2Bue', + undefined, + undefined, + undefined, + ), + 'http://library.kiwix.org/content/myzim_yyyy-mm/www.example.com/javascript/content.txt%3Fquery=val%2Bue', + ); +}); + test('contentWithSimplePathWithEqualAndComma', (t) => { t.is( urlRewriteFunction(