Turn any website into a lightweight desktop app with one command, on macOS, Windows and Linux.
Rust chatgptclaudeclidesktopdesktop-appelectron-alternativegeminilinuxmacosnativefierno-electronrusttauriweb-to-desktopwebsite-to-appwebviewwindowsyoutube
fix: decode percent-encoded file names when saving downloads
NgoQuocViet20012b075ac48b79
diff --git a/src-tauri/src/inject/event.js b/src-tauri/src/inject/event.jsindex 3a6e6b6e..87ad6742 100644--- a/src-tauri/src/inject/event.js+++ b/src-tauri/src/inject/event.js@@ -1628,10 +1628,24 @@ function setDefaultZoom() {}}+// Browsers decode the last URL path segment when naming a download. Keep the+// encoded form when decoding fails or yields a path separator, a character+// Windows rejects in file names, or a control/format character.+function decodeFilenameSegment(segment) {+ try {+ const decoded = decodeURIComponent(segment);+ return /[\\/:*?"<>|\p{Cc}\p{Cf}]/u.test(decoded) ? segment : decoded;+ } catch (e) {+ return segment;+ }+}+function getFilenameFromUrl(url) {try {const urlPath = new URL(url).pathname;- let filename = urlPath.substring(urlPath.lastIndexOf("/") + 1);+ let filename = decodeFilenameSegment(+ urlPath.substring(urlPath.lastIndexOf("/") + 1),+ );// If no filename or no extension, generate oneif (!filename || !filename.includes(".")) {diff --git a/tests/unit/event-link-guard.test.js b/tests/unit/event-link-guard.test.jsindex 270a9af6..67da3a7e 100644--- a/tests/unit/event-link-guard.test.js+++ b/tests/unit/event-link-guard.test.js@@ -926,3 +926,58 @@ describe("getFilenameFromUrl data URI extension", () => {expect(filename).not.toContain("data:image/");});});++describe("getFilenameFromUrl percent-encoded path", () => {+ it("decodes the file name the way browsers name downloads", () => {+ const context = loadEventHelpers();+ expect(+ context.getFilenameFromUrl(+ "https://example.com/files/Annual%20Report%202026.pdf",+ ),+ ).toBe("Annual Report 2026.pdf");+ expect(+ context.getFilenameFromUrl("https://example.com/%E6%8A%A5%E5%91%8A.pdf"),+ ).toBe("报告.pdf");+ });++ it.each(["a%2F..%2Fsecret.pdf", "bad%E0%A4.pdf", "%E2%80%AEfdp.pdf"])(+ "keeps the encoded name when decoding is unsafe or malformed: %s",+ (name) => {+ const context = loadEventHelpers();+ expect(+ context.getFilenameFromUrl(`https://example.com/files/${name}`),+ ).toBe(name);+ },+ );++ it("passes the decoded name to download_file", () => {+ const context = loadEventHelpers({ withTauri: true });+ runDomReady(context);+ const anchor = makeAnchor(+ "https://example.com/files/Annual%20Report.pdf",+ "_self",+ );+ getClickGuard(context)(makeClickEvent(anchor));+ expect(+ context.invokeCalls+ .filter(([command]) => command === "download_file")+ .map(([, payload]) => payload.params.filename),+ ).toEqual(["Annual Report.pdf"]);+ });++ it("names native Windows downloads with the decoded name", () => {+ const context = loadEventHelpers({+ withTauri: true,+ nativeDownloads: true,+ });+ runDomReady(context);+ const anchor = makeAnchor(+ "https://example.com/files/%E6%8A%A5%E5%91%8A%202026.pdf",+ "_self",+ );+ getClickGuard(context)(makeClickEvent(anchor));+ expect(context.nativeDownloadClicks).toEqual([+ [anchor.href, "报告 2026.pdf"],+ ]);+ });+});