diff --git a/tests/unit/browser-file-content.test.ts b/tests/unit/browser-file-content.test.ts index b44ef84a..fde5dcbf 100644 --- a/tests/unit/browser-file-content.test.ts +++ b/tests/unit/browser-file-content.test.ts @@ -981,6 +981,83 @@ describe("browser file content policy", () => { ).toThrowError(DOMException); lease.release(); expect(leases.aggregateLeaseByteLength).toBe(0); + + // 2026-11-01 02-04 (N29). A free slot does not lift the byte cap. + const byBytes = new ObjectUrlLeaseRegistry( + { + createObjectURL: () => `blob:bytes-${urlSequence++}`, + revokeObjectURL() {}, + }, + { + hardMaxActiveLeases: 2, + hardMaxSingleLeaseBytes: 4, + hardMaxAggregateLeaseBytes: 6, + }, + ); + const held = byBytes.create(new Blob([new Uint8Array(4)])); + expect(() => + byBytes.create(new Blob([new Uint8Array(3)])), + ).toThrowError(DOMException); + expect(byBytes.activeLeaseCount).toBe(1); + held.release(); + expect(() => + byBytes.create(new Blob([new Uint8Array(3)])), + ).not.toThrow(); + }); + + // 2026-11-01 02-04 (N27). Inspecting again replaces the receipt: the file + // may have been checked against a different policy, so the old one ends. + it("ends the previous receipt when the same file is inspected again", async () => { + const policies = pngPolicies(); + const vault = new BrowserFileVault({ policies }); + const captured = vault.captureFiles( + [ + new File( + [new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])], + "again.png", + { type: "image/png", lastModified: 1 }, + ), + ], + pngSelectionPolicy, + ); + if (!captured.ok || !captured.value[0]) throw new Error("capture"); + const ref = captured.value[0].ref; + const receiptOf = async () => { + const inspected = await vault.inspect({ + ref, + policy: pngPolicy, + signal: new AbortController().signal, + }); + if (!inspected.ok || !inspected.value.verificationReceipt) { + throw new Error("inspect"); + } + return inspected.value.verificationReceipt; + }; + const first = await receiptOf(); + const second = await receiptOf(); + expect(vault.activeVerificationCount).toBe(1); + const preview = new BrowserTransientPreview({ + files: vault, + policies, + leases: new ObjectUrlLeaseRegistry({ + createObjectURL: () => "blob:again", + revokeObjectURL() {}, + }), + hardMaxPreviewBytes: 10, + }); + const create = (verificationReceipt: FileVerificationReceipt) => + preview.create({ + ref, + verificationReceipt, + policy: pngPolicy, + signal: new AbortController().signal, + }); + + expect(await create(first)).toMatchObject({ + ok: false, + error: { code: "POLICY_REJECTED" }, + }); + expect(await create(second)).toMatchObject({ ok: true }); }); it("converts native stream exceptions to closed chunk failures", async () => { diff --git a/tests/unit/browser-file-download.test.ts b/tests/unit/browser-file-download.test.ts index 4521d7e7..40effd97 100644 --- a/tests/unit/browser-file-download.test.ts +++ b/tests/unit/browser-file-download.test.ts @@ -293,9 +293,11 @@ describe("browser download delivery", () => { it("rejects cross-origin or query-bearing browser-managed targets", async () => { // 02-08. Each href breaks one rule, so neither check hides the other. + // 2026-11-01 02-04 (N30): a fragment is refused on its own too. for (const href of [ "https://evil.example/downloads/a", "/downloads/a?token=secret", + "/downloads/a#token=secret", ]) { const handoff = vi.fn(); const adapter = createDownloadDeliveryAdapter({ @@ -767,6 +769,39 @@ describe("browser download delivery", () => { expect(revoked).toEqual(["blob:download-1"]); }); + // [0929] 02-05. A handoff that throws hands nothing to the browser, so the + // lease made for it is revoked at once instead of waiting for dispose. + it("revokes the lease of a handoff that throws", async () => { + const revoked: string[] = []; + const leases = new ObjectUrlLeaseRegistry({ + createObjectURL: () => "blob:download-failed", + revokeObjectURL: (url) => revoked.push(url), + }); + const adapter = createDownloadDeliveryAdapter({ + ...HARD_LIMITS, + policies, + host: { + handoff() { + throw new DOMException("blocked", "NotAllowedError"); + }, + }, + browserManagedCapabilities: capabilityResolver(), + objectUrls: leases, + userActivation: { isActive: true }, + }); + + expect( + await adapter.deliver( + deliveryInput( + { kind: "GENERATED", bytes: byteSource([new Uint8Array([1, 2])]) }, + "BOUNDED_OBJECT_URL", + ), + ), + ).toMatchObject({ ok: false }); + expect(leases.activeLeaseCount).toBe(0); + expect(revoked).toEqual(["blob:download-failed"]); + }); + it("revokes only its own leases on dispose when the registry is shared (2026-11-01 02-01)", async () => { const revoked: string[] = []; let urlSequence = 0;