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 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
a80b153163
commit
de954d2ebf
@@ -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);
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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 = {
|
||||
|
||||
Reference in New Issue
Block a user