Skip to content

misterdownloadmanager: The ultimate open-source, clean-room download manager written from scratch in pure Rust for Windows, Mac, and Linux

Firefox: a raw UTF-8 file name in a header reaches MDM readable (#447) (#466)

Ahmed Nefzaouib77ce8c04b1e
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0ce71bd6..f60114d0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -11,6 +11,7 @@ Each release's notes on GitHub come from its section here (`## [X.Y.Z]`, see
### Fixed
+- A file name sent by a site in plain UTF-8 now reaches MDM readable from Firefox and browsers built on it, such as Floorp, Waterfox and Zen: an Arabic name was saved as unreadable symbols ([#447](https://github.com/anefzaoui/misterdownloadmanager/issues/447)). A name written with percent signs in the same header is decoded as well. The add-on carries the fix
- A download saved under a numbered name because another file already had its name (`master (1).mp4`) now shows that name in the list and opens its own file, instead of keeping the old name and opening the other file ([#442](https://github.com/anefzaoui/misterdownloadmanager/issues/442)). Two downloads waiting under one name no longer collide when they start: the later one takes the next free name, instead of failing with "already being downloaded" or continuing the other one's data.
- A link sent from the browser no longer fails with 403 on sites whose bot check refuses a browser's User-Agent on a connection that isn't a browser's: MDM asks once more with its own, as Add URL does ([#372](https://github.com/anefzaoui/misterdownloadmanager/issues/372)).
- The video download button moves to a free corner instead of covering a control of the page, such as a thumbnail's watch-later button ([#421](https://github.com/anefzaoui/misterdownloadmanager/issues/421)). Dragging it still puts it where you want it.
diff --git a/ROADMAP.md b/ROADMAP.md
index fc171b3a..9daa52e3 100644
--- a/ROADMAP.md
+++ b/ROADMAP.md
@@ -508,6 +508,8 @@ These ship in a later version, when they're ready.
### Bugs being looked into
+- 🔄 Arabic and other non-Latin file names saved unreadable, from Firefox and browsers built on it
+ ([#447](https://github.com/anefzaoui/misterdownloadmanager/issues/447)): fix in review
- 🔄 A file renamed to avoid a clash still showing and opening under its old name
([#442](https://github.com/anefzaoui/misterdownloadmanager/issues/442)): fix in review
- 🔄 RSS feeds: the rule boxes losing the keyboard after each key
diff --git a/extension/background.js b/extension/background.js
index 434d43e6..b20e6e6a 100644
--- a/extension/background.js
+++ b/extension/background.js
@@ -1570,7 +1570,32 @@ function dispositionName(cd) {
}
const plain = /(?:^|;)\s*filename\s*=\s*("((?:[^"\\]|\\.)*)"|[^;]+)/i.exec(cd);
if (!plain) return "";
- return (plain[2] !== undefined ? plain[2].replace(/\\(.)/g, "$1") : plain[1]).trim();
+ return decodedPlain((plain[2] !== undefined ? plain[2].replace(/\\(.)/g, "$1") : plain[1]).trim());
+}
+
+// A plain `filename` as sent, or its percent-encoded UTF-8 decoded when that is what it holds
+// (non-ASCII once decoded: `100%25.txt` stays as it is), as the app reads it (naming.rs).
+function decodedPlain(name) {
+ if (!name.includes("%")) return name;
+ try {
+ const decoded = decodeURIComponent(name);
+ return /[^\x00-\x7f]/.test(decoded) ? decoded : name;
+ } catch {
+ return name; // not percent-encoded UTF-8
+ }
+}
+
+// A response header's value as text. Firefox gives its bytes one character each (ISO-8859-1), so
+// a name sent as raw UTF-8 (`filename="تقرير.pdf"`) arrived as `تم` (#447). It is read as UTF-8
+// when those bytes are valid UTF-8, as Firefox reads the name for its own downloads and the app
+// for its own requests; bytes that aren't (ISO-8859-1's `café`) stay as they came.
+function headerText(value) {
+ if (!/[\x80-\xff]/.test(value) || /[^\x00-\xff]/.test(value)) return value;
+ try {
+ return new TextDecoder("utf-8", { fatal: true }).decode(Uint8Array.from(value, (c) => c.charCodeAt(0)));
+ } catch {
+ return value;
+ }
}
// {name, size, mime, opened} when this answer to a page load becomes a download (or is one of
@@ -1588,7 +1613,7 @@ function answerFile(d) {
// No type: the browser guesses from the bytes (a page, mostly); a download it becomes is
// still seen by onCreated.
if (!attachment && !opened && (!mime || SHOWN.test(mime))) return null;
- let name = dispositionName(cd);
+ let name = dispositionName(headerText(cd));
if (!name) {
try {
name = decodeURIComponent(pathOf(d.url).split("/").pop() || "");
diff --git a/tools/extension/e2e.mjs b/tools/extension/e2e.mjs
index b5e6e831..b383d945 100644
--- a/tools/extension/e2e.mjs
+++ b/tools/extension/e2e.mjs
@@ -1428,9 +1428,14 @@ const CASES = {
await clickLink(t, `/gen/1M/get?cdstar=${encodeURIComponent(star)}`);
await arrived(star, 1 << 20);
await keptNothing(t.dl);
+ // #447: raw UTF-8 in filename=, no filename* (Firefox gives extensions its bytes).
+ const raw = `تقرير-e2e-${t.name}.zip`;
+ await clickLink(t, `/gen/1M/raw.php?cd=${encodeURIComponent(raw)}`);
+ await arrived(raw, 1 << 20);
+ await keptNothing(t.dl);
const wrong = fs.readdirSync(path.join(downloads, "Compressed")).filter((f) => /^(download\.php|get|fallback-name\.zip)/.test(f));
if (wrong.length) throw new Error(`saved under the address's or the fallback name too: ${wrong.join(", ")}`);
- return `${plain}, ${star}`;
+ return `${plain}, ${star}, ${raw}`;
},
// A redirect: MDM keeps the address clicked (it asks it again to renew the link).
diff --git a/tools/extension/test.mjs b/tools/extension/test.mjs
index 1a1e486e..68bf4d6e 100644
--- a/tools/extension/test.mjs
+++ b/tools/extension/test.mjs
@@ -1651,6 +1651,32 @@ const ask = (listeners) => (m, sender) => new Promise((r) => listeners.message(m
assert.deepEqual(steps(calls), []);
ok("#208 Firefox: a download caught from its answer, before the browser's dialog");
}
+// #447: Firefox hands a header's value over byte for byte, each byte one character. A name sent as
+// raw UTF-8 is read as UTF-8; bytes that aren't valid UTF-8 (ISO-8859-1) and a real percent sign stay.
+{
+ const asBytes = (text) => String.fromCharCode(...new TextEncoder().encode(text));
+ for (const [value, want] of [
+ [`attachment; filename="${asBytes("تقرير.pdf")}"`, "تقرير.pdf"],
+ [`attachment; filename="${asBytes("été à Paris.pdf")}"`, "été à Paris.pdf"],
+ [`attachment; filename="${asBytes("日本語.pdf")}"`, "日本語.pdf"],
+ ["attachment; filename=\"plain report.pdf\"", "plain report.pdf"],
+ // ISO-8859-1 bytes (é alone is not valid UTF-8).
+ ["attachment; filename=\"caf\u00e9.pdf\"", "café.pdf"],
+ // Percent-encoded UTF-8 in the plain name, as Firefox and the app read it.
+ ["attachment; filename=\"%D8%AA%D9%82%D8%B1%D9%8A%D8%B1.pdf\"", "تقرير.pdf"],
+ ["attachment; filename=\"100%25.pdf\"", "100%25.pdf"],
+ ["attachment; filename=\"50%.pdf\"", "50%.pdf"],
+ // RFC 8187 keeps winning, ASCII as it comes.
+ ["attachment; filename=\"fallback.pdf\"; filename*=UTF-8''%D8%AA%D9%82%D8%B1%D9%8A%D8%B1.pdf", "تقرير.pdf"],
+ ]) {
+ const { api, calls, listeners } = fakeApi({ firefox: true });
+ load(api, true);
+ await settle();
+ await held(listeners, answer("https://dl.test/get?id=2", { "Content-Type": "application/octet-stream", "Content-Disposition": value }));
+ assert.equal(natives(calls)[0].filename, want, value);
+ }
+ ok("#447 Firefox: a raw UTF-8 file name in a header reaches the app readable, other names as they were");
+}
// A download taken from its answer (#208) is cancelled, so its response never starts: the proxy a
// VPN add-on chose for it (proxy.onRequest, in the answer's proxyInfo) still goes with it.
{