fix(browser-files): return ABORTED instead of a leaked preview lease after a same-turn abort (02-01)

create() checked the signal only before resolving the file, so an abort in
the same turn still produced a lease the caller never released. Each leak
used one of the 16 object URL slots shared with downloads.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
DongHyeonka
2026-09-25 23:57:04 +09:00
co-authored by Claude Opus 5.5
parent 4d17259037
commit c9be8d65e4
2 changed files with 68 additions and 0 deletions
@@ -260,6 +260,10 @@ export class BrowserTransientPreview implements TransientPreviewPort {
browserDataFailure("UNAVAILABLE", "PREVIEW"),
);
}
// 02-01. The caller may abort while the file resolves; a lease created
// after that would never be released because the caller drops the result.
const abortedWhileResolving = abortedResult(input.signal, "PREVIEW");
if (abortedWhileResolving) return this.#observe(abortedWhileResolving);
const mediaType = resolved.value.mediaType.trim().toLowerCase();
if (
!EXACT_MEDIA_TYPE.test(mediaType) ||
+64
View File
@@ -83,6 +83,47 @@ function pngPolicies(
});
}
async function verifiedPngPreview(leases: ObjectUrlLeaseRegistry) {
const policies = pngPolicies();
const vault = new BrowserFileVault({ policies });
const captured = vault.captureFiles(
[
new File(
[new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])],
"preview.png",
{ type: "image/png", lastModified: 1 },
),
],
pngSelectionPolicy,
);
if (!captured.ok || !captured.value[0]) throw new Error("capture");
const ref = captured.value[0].ref;
const inspected = await vault.inspect({
ref,
policy: pngPolicy,
signal: new AbortController().signal,
});
if (!inspected.ok || !inspected.value.verificationReceipt) {
throw new Error("inspect");
}
const verificationReceipt = inspected.value.verificationReceipt;
const preview = new BrowserTransientPreview({
files: vault,
policies,
leases,
hardMaxPreviewBytes: 10,
});
return {
preview,
request: (signal: AbortSignal) => ({
ref,
verificationReceipt,
policy: pngPolicy,
signal,
}),
};
}
describe("browser file content policy", () => {
it("neutralizes path, bidi, device-name and executable filename tricks", () => {
expect(
@@ -570,6 +611,29 @@ describe("browser file content policy", () => {
});
});
it("returns ABORTED without a lease when the caller aborts right after create() (02-01)", async () => {
const revoked: string[] = [];
let urlSequence = 0;
const leases = new ObjectUrlLeaseRegistry({
createObjectURL: () => `blob:aborted-${urlSequence++}`,
revokeObjectURL: (url) => revoked.push(url),
});
const { preview, request } = await verifiedPngPreview(leases);
const controller = new AbortController();
// Same-turn abort: the effect cleanup of a React StrictMode double effect.
const pending = preview.create(request(controller.signal));
controller.abort();
expect(await pending).toMatchObject({
ok: false,
error: { code: "ABORTED", operation: "PREVIEW" },
});
expect(leases.activeLeaseCount).toBe(0);
expect(urlSequence).toBe(0);
expect(revoked).toEqual([]);
});
it("cannot weaken the built-in active-content preview denylist", async () => {
const htmlPolicy = browserFilePolicyReference(
"active-content",