From de954d2ebf63ba763e820435973d0b8cf413cd50 Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Thu, 24 Sep 2026 12:06:01 +0900 Subject: [PATCH] fix(browser-files): stop binding file pickers to the options object (R-2.4) EnhancedFilePicker and the download adapter captured showOpenFilePicker and showSaveFilePicker with `.bind(options)`. A Window operation throws TypeError("Illegal invocation") when `this` is not the Window, so passing `window.showOpenFilePicker` / `window.showSaveFilePicker` as-is failed on every call. Both are now captured with `.bind(undefined)`. They are still captured at construction, and Web IDL maps an undefined receiver to the global object, so the native functions can be injected directly. Verified: "calls an injected window picker without rebinding its receiver" and "calls an injected window save picker without rebinding its receiver" use a fake picker that throws "Illegal invocation" unless `this` is undefined or globalThis. Both failed before the fix (ok:false) and pass now. check:architecture, check:types:app, check:types:test, lint, test:component pass. test:unit passes except removal-fixture, which needs generated artifacts/release files that a fresh worktree lacks. Co-Authored-By: Claude Opus 5.5 --- .../browser-files/browser-file-picker.ts | 5 ++- .../download-delivery-adapter.ts | 5 ++- tests/unit/browser-file-download.test.ts | 32 +++++++++++++++++++ tests/unit/browser-file-picker.test.ts | 32 +++++++++++++++++++ 4 files changed, 72 insertions(+), 2 deletions(-) diff --git a/src/adapters/browser-files/browser-file-picker.ts b/src/adapters/browser-files/browser-file-picker.ts index c99270c4..cfd74975 100644 --- a/src/adapters/browser-files/browser-file-picker.ts +++ b/src/adapters/browser-files/browser-file-picker.ts @@ -431,8 +431,11 @@ export class EnhancedFilePicker implements DisposableFilePicker { if (typeof options.showOpenFilePicker !== "function") { throw new TypeError("Enhanced file picker API is invalid."); } + // Bound to undefined, not to options: `window.showOpenFilePicker` throws + // "Illegal invocation" for any other receiver, and Web IDL maps an + // undefined receiver to the global object. this.#showOpenFilePicker = - options.showOpenFilePicker.bind(options); + options.showOpenFilePicker.bind(undefined); this.#captureHandles = options.vault.captureHandles.bind(options.vault); this.#releaseFile = options.vault.release.bind(options.vault); diff --git a/src/adapters/browser-files/download-delivery-adapter.ts b/src/adapters/browser-files/download-delivery-adapter.ts index 91ecfa72..6c3b1139 100644 --- a/src/adapters/browser-files/download-delivery-adapter.ts +++ b/src/adapters/browser-files/download-delivery-adapter.ts @@ -224,8 +224,11 @@ export function createDownloadDeliveryAdapter( : {}), ...(originalShowSaveFilePicker ? { + // Bound to undefined, not to options: `window.showSaveFilePicker` + // throws "Illegal invocation" for any other receiver, and Web IDL + // maps an undefined receiver to the global object. showSaveFilePicker: - originalShowSaveFilePicker.bind(options), + originalShowSaveFilePicker.bind(undefined), } : {}), ...(originalIntegrityVerifier diff --git a/tests/unit/browser-file-download.test.ts b/tests/unit/browser-file-download.test.ts index 9c0890c4..0b40316f 100644 --- a/tests/unit/browser-file-download.test.ts +++ b/tests/unit/browser-file-download.test.ts @@ -440,6 +440,38 @@ describe("browser download delivery", () => { ); }); + it("calls an injected window save picker without rebinding its receiver", async () => { + const events: string[] = []; + const adapter = createDownloadDeliveryAdapter({ + ...HARD_LIMITS, + policies, + host: { handoff: vi.fn() }, + browserManagedCapabilities: capabilityResolver(), + // A Window operation throws "Illegal invocation" unless `this` is the + // Window. Web IDL maps an undefined `this` to the global object. + showSaveFilePicker: function (this: unknown) { + if (this !== undefined && this !== globalThis) { + throw new TypeError("Illegal invocation"); + } + return Promise.resolve(writableHandle(events)); + }, + userActivation: { isActive: true }, + }); + + expect( + await adapter.deliver( + deliveryInput( + { + kind: "GENERATED", + bytes: byteSource([new Uint8Array([1, 2])]), + }, + "PROMPT_AND_STREAM", + ), + ), + ).toMatchObject({ ok: true, value: { kind: "SAVED" } }); + expect(events).toEqual(["write:2", "close"]); + }); + it("snapshots the generated source before awaiting the save picker", async () => { const events: string[] = []; let resolvePicker: diff --git a/tests/unit/browser-file-picker.test.ts b/tests/unit/browser-file-picker.test.ts index 8f203992..b43fd79e 100644 --- a/tests/unit/browser-file-picker.test.ts +++ b/tests/unit/browser-file-picker.test.ts @@ -416,6 +416,38 @@ describe("browser file pickers", () => { }); }); + it("calls an injected window picker without rebinding its receiver", async () => { + const harness = createHarness(); + const picker = new EnhancedFilePicker({ + // A Window operation throws "Illegal invocation" unless `this` is the + // Window. Web IDL maps an undefined `this` to the global object. + showOpenFilePicker: function (this: unknown) { + if (this !== undefined && this !== globalThis) { + throw new TypeError("Illegal invocation"); + } + return Promise.resolve([ + { + kind: "file" as const, + name: "safe.txt", + async getFile() { + return new File(["safe"], "safe.txt", { + type: "text/plain", + lastModified: 1, + }); + }, + }, + ]); + }, + ...harness, + userActivation: { isActive: true }, + }); + + expect(await picker.select({ policy })).toMatchObject({ + ok: true, + value: { kind: "SELECTED" }, + }); + }); + it("rejects excessive enhanced selections before reading handles", async () => { const getFile = vi.fn(async () => new File(["a"], "a.txt")); const multiplePolicy = {