test(browser-files): isolate five rules another check used to hide (02-08)

Cross-origin handoff, retained byte cap, typed preview Blob, native picker
user activation and first-seen-source GAP each get an input that only that
rule rejects. Removing any of the five lines now fails exactly one test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
DongHyeonka
2026-09-26 00:29:34 +09:00
co-authored by Claude Opus 5.5
parent 2cbf500fcc
commit 98977df6f4
4 changed files with 118 additions and 29 deletions
+56 -3
View File
@@ -83,7 +83,10 @@ function pngPolicies(
});
}
async function verifiedPngPreview(leases: ObjectUrlLeaseRegistry) {
async function verifiedPngPreview(
leases: ObjectUrlLeaseRegistry,
reportedType = "image/png",
) {
const policies = pngPolicies();
const vault = new BrowserFileVault({ policies });
const captured = vault.captureFiles(
@@ -91,10 +94,10 @@ async function verifiedPngPreview(leases: ObjectUrlLeaseRegistry) {
new File(
[new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])],
"preview.png",
{ type: "image/png", lastModified: 1 },
{ type: reportedType, lastModified: 1 },
),
],
pngSelectionPolicy,
{ ...pngSelectionPolicy, accept: Object.freeze([]) },
);
if (!captured.ok || !captured.value[0]) throw new Error("capture");
const ref = captured.value[0].ref;
@@ -666,6 +669,25 @@ describe("browser file content policy", () => {
).toMatchObject({ ok: true });
});
it("types the preview Blob from the signature, not from File.type (02-08)", async () => {
const blobs: Blob[] = [];
const leases = new ObjectUrlLeaseRegistry({
createObjectURL: (blob) => {
blobs.push(blob);
return "blob:typed";
},
revokeObjectURL() {},
});
// An empty File.type still passes the signature check, so only the typed
// slice decides how the browser interprets the object URL.
const { preview, request } = await verifiedPngPreview(leases, "");
expect(
await preview.create(request(new AbortController().signal)),
).toMatchObject({ ok: true, value: { mediaType: "image/png" } });
expect(blobs.map((blob) => blob.type)).toEqual(["image/png"]);
});
it("cannot weaken the built-in active-content preview denylist", async () => {
const htmlPolicy = browserFilePolicyReference(
"active-content",
@@ -778,6 +800,37 @@ describe("browser file content policy", () => {
expect(vault.activeReferenceCount).toBe(0);
});
it("caps retained bytes while reference slots remain (02-08)", () => {
let referenceSequence = 0;
const vault = new BrowserFileVault({
policies: pngPolicies(),
createReference: () => `file:bytes-${referenceSequence++}`,
hardMaxActiveReferences: 8,
hardMaxRetainedBytes: 3,
});
const permissive = {
...pngSelectionPolicy,
accept: Object.freeze([]),
};
expect(
vault.captureFiles(
[new File([new Uint8Array(2)], "one.bin")],
permissive,
),
).toMatchObject({ ok: true });
expect(
vault.captureFiles(
[new File([new Uint8Array(2)], "two.bin")],
permissive,
),
).toMatchObject({
ok: false,
error: { code: "LIMIT_EXCEEDED" },
});
expect(vault.activeReferenceCount).toBe(1);
expect(vault.retainedByteLength).toBe(2);
});
it("caps retained files and aggregate object URL leases", () => {
let referenceSequence = 0;
const vault = new BrowserFileVault({
+30 -26
View File
@@ -292,34 +292,38 @@ describe("browser download delivery", () => {
});
it("rejects cross-origin or query-bearing browser-managed targets", async () => {
const handoff = vi.fn();
const adapter = createDownloadDeliveryAdapter({
...HARD_LIMITS,
policies,
host: { handoff },
browserManagedCapabilities: capabilityResolver(
() => "https://evil.example/a?token=secret",
),
baseOrigin: "https://app.example",
userActivation: { isActive: true },
});
// 02-08. Each href breaks one rule, so neither check hides the other.
for (const href of [
"https://evil.example/downloads/a",
"/downloads/a?token=secret",
]) {
const handoff = vi.fn();
const adapter = createDownloadDeliveryAdapter({
...HARD_LIMITS,
policies,
host: { handoff },
browserManagedCapabilities: capabilityResolver(() => href),
baseOrigin: "https://app.example",
userActivation: { isActive: true },
});
expect(
await adapter.deliver(
deliveryInput(
{
kind: "BROWSER_MANAGED_RESOURCE",
resourceId: "artifact-1",
capabilityReceipt,
},
"BROWSER_MANAGED",
expect(
await adapter.deliver(
deliveryInput(
{
kind: "BROWSER_MANAGED_RESOURCE",
resourceId: "artifact-1",
capabilityReceipt,
},
"BROWSER_MANAGED",
),
),
),
).toMatchObject({
ok: false,
error: { code: "POLICY_REJECTED" },
});
expect(handoff).not.toHaveBeenCalled();
).toMatchObject({
ok: false,
error: { code: "POLICY_REJECTED" },
});
expect(handoff).not.toHaveBeenCalled();
}
});
it("requires an exact, unexpired, server-bound synchronous capability receipt", async () => {
+22
View File
@@ -98,6 +98,28 @@ describe("browser file pickers", () => {
expect(baselineShowPicker).not.toHaveBeenCalled();
});
it("refuses to open the native input without user activation (02-08)", async () => {
const input = labelledFileInput();
const click = vi.fn(() => {
queueMicrotask(() => input.dispatchEvent(new Event("cancel")));
});
Object.defineProperty(input, "click", {
configurable: true,
value: click,
});
const picker = new NativeInputFilePicker({
input,
...createHarness(),
userActivation: { isActive: false },
});
expect(await picker.select({ policy })).toMatchObject({
ok: false,
error: { code: "PERMISSION_DENIED", operation: "FILE_SELECT" },
});
expect(click).not.toHaveBeenCalled();
});
it("resets the native input and supports same-file reselection", async () => {
const input = labelledFileInput();
const selected = new File(["hello"], "notes.txt", {
+10
View File
@@ -502,6 +502,15 @@ describe("browser cross-context invalidation transport", () => {
sequence: 1,
}),
);
// 02-08. A source first seen mid-stream has already sent events this tab
// missed, so its first event is a gap on its own.
network.emit(
wireEvent({
eventId: "joined-event-00000004",
sourceId: "joined-source-0000001",
sequence: 4,
}),
);
expect(
delivered.map(({ event, ordering }) => [
@@ -512,6 +521,7 @@ describe("browser cross-context invalidation transport", () => {
[1, "NEXT"],
[3, "GAP"],
[1, "NEXT"],
[4, "GAP"],
]);
expect(observations).toContainEqual({
operation: "RECEIVE",