문제: download 정책 registry는 safeExtension의 형식만 본다. 파일명 금지 목록(.exe, .html, .js 등)은 사용자가 준 이름에만 쓴다 (1101/02-02). 그래서 mediaType application/pdf + safeExtension .exe 정책이 조립되고, invoice.pdf가 invoice.exe로 browser handoff된다 (리뷰 E-7). 고른 방법: snapshotDownload가 금지 목록에 있거나 그것으로 끝나는 (.tar.js) safeExtension을 다른 정책 오류와 같이 TypeError로 거절한다. 목록은 file-policy.ts의 DEFAULT_FORBIDDEN_EXTENSIONS 하나를 쓰고, isForbiddenDownloadExtension으로 내보낸다. browser-managed capability는 정책과 같은 safeExtension이어야 하므로 그 경로도 함께 막힌다. 버린 대안: media type과 확장자 짝 표를 두는 것. 표를 누가 소유할지 정해야 하고 범위가 크다. 이번에는 하지 않았다. 위험·한계: 이 확장자를 쓰던 다운스트림 정책은 조립 때 실패한다(보안 강화 방향). media type과 확장자가 서로 맞는지는 여전히 보지 않는다 (설계 문서에 적었다). file-transfer 번들 56,068 → 56,100 / 57,800 gzip B. 검증: - RED: browser-file-policy-registry.test.ts 새 3건(.exe, .HTML, .tar.js)이 throw 없음으로 실패. - GREEN: browser-files 단위 5파일 + tests/recipes 98건 통과. check:types, 바꾼 파일 eslint, check:optional-recipes:source PASS, verify:documentation 통과. Refs: docs/reviews/2026-11-03-maturity-review/02-adapters-browser-files-cache.md 1101/02-02
310 lines
8.2 KiB
TypeScript
310 lines
8.2 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import {
|
|
BrowserFilePolicyRegistry,
|
|
browserFilePolicyReference,
|
|
} from "../../src/adapters/browser-files/browser-file-policy-registry.ts";
|
|
import { BrowserFileVault } from "../../src/adapters/browser-files/browser-file-vault.ts";
|
|
import {
|
|
BrowserTransientPreview,
|
|
ObjectUrlLeaseRegistry,
|
|
} from "../../src/adapters/browser-files/object-url-lease.ts";
|
|
|
|
const hardLimits = Object.freeze({
|
|
maxInspectionBytes: 64,
|
|
maxRetainedFileBytes: 1_024,
|
|
maxPreviewBytes: 1_024,
|
|
maxObjectUrlBytes: 1_024,
|
|
maxTransferBytes: 1_024,
|
|
});
|
|
|
|
describe("browser file composition policy registry", () => {
|
|
it("deep-snapshots every policy axis and permits only limit reductions", () => {
|
|
const reference = browserFilePolicyReference(
|
|
"documents",
|
|
"select-preview-download-pdf",
|
|
);
|
|
const selection = {
|
|
policyId: "documents-v1",
|
|
purpose: "document",
|
|
classification: "PERSONAL" as const,
|
|
multiple: false,
|
|
maxCount: 1,
|
|
maxFileBytes: 100,
|
|
maxTotalBytes: 100,
|
|
allowEmpty: false,
|
|
accept: [
|
|
{ mediaType: "application/pdf", extensions: [".pdf"] },
|
|
],
|
|
};
|
|
const inspection = {
|
|
policyId: "documents-v1",
|
|
maxInspectionBytes: 8,
|
|
acceptedSignatures: [
|
|
{
|
|
mediaType: "application/pdf",
|
|
extensions: [".pdf"],
|
|
patterns: [{ offset: 0, bytes: [0x25, 0x50] }],
|
|
},
|
|
],
|
|
};
|
|
const preview = {
|
|
allowedMediaTypes: ["application/pdf"],
|
|
maxPreviewBytes: 100,
|
|
};
|
|
const download = {
|
|
strategy: "PROMPT_AND_STREAM" as const,
|
|
mediaType: "application/pdf",
|
|
safeExtension: ".pdf",
|
|
maxTransferBytes: 100,
|
|
maxBufferedBytes: 50,
|
|
integrity: "REQUIRED" as const,
|
|
};
|
|
const registry = new BrowserFilePolicyRegistry({
|
|
profiles: [
|
|
{
|
|
reference,
|
|
selection,
|
|
inspection,
|
|
preview,
|
|
download,
|
|
},
|
|
],
|
|
hardLimits,
|
|
});
|
|
|
|
selection.maxFileBytes = 1;
|
|
selection.accept[0]!.extensions[0] = ".exe";
|
|
inspection.acceptedSignatures[0]!.patterns[0]!.bytes[0] = 0;
|
|
preview.allowedMediaTypes[0] = "text/html";
|
|
(
|
|
download as { strategy: string }
|
|
).strategy = "BROWSER_MANAGED";
|
|
download.safeExtension = ".exe";
|
|
|
|
expect(
|
|
registry.resolveSelection(reference, {
|
|
maxFileBytes: 80,
|
|
}),
|
|
).toMatchObject({
|
|
ok: true,
|
|
value: {
|
|
maxFileBytes: 80,
|
|
accept: [{ extensions: [".pdf"] }],
|
|
},
|
|
});
|
|
expect(
|
|
registry.resolveSelection(reference, {
|
|
maxFileBytes: 101,
|
|
}),
|
|
).toMatchObject({
|
|
ok: false,
|
|
error: { code: "LIMIT_EXCEEDED" },
|
|
});
|
|
expect(registry.resolveInspection(reference)).toMatchObject({
|
|
ok: true,
|
|
value: {
|
|
acceptedSignatures: [
|
|
{ patterns: [{ bytes: [0x25, 0x50] }] },
|
|
],
|
|
},
|
|
});
|
|
const resolvedPreview = registry.resolvePreview(reference);
|
|
expect(resolvedPreview.ok).toBe(true);
|
|
if (resolvedPreview.ok) {
|
|
expect(
|
|
resolvedPreview.value.allowedMediaTypes.has(
|
|
"application/pdf",
|
|
),
|
|
).toBe(true);
|
|
expect(
|
|
resolvedPreview.value.allowedMediaTypes.has("text/html"),
|
|
).toBe(false);
|
|
(
|
|
resolvedPreview.value.allowedMediaTypes as Set<string>
|
|
).clear();
|
|
expect(
|
|
registry.resolvePreview(reference),
|
|
).toMatchObject({
|
|
ok: true,
|
|
value: {
|
|
allowedMediaTypes: new Set(["application/pdf"]),
|
|
},
|
|
});
|
|
}
|
|
expect(
|
|
registry.resolveDownload(reference, {}),
|
|
).toMatchObject({
|
|
ok: true,
|
|
value: {
|
|
strategy: "PROMPT_AND_STREAM",
|
|
safeExtension: ".pdf",
|
|
},
|
|
});
|
|
expect(
|
|
registry.resolveDownload(
|
|
browserFilePolicyReference("unknown", "unknown"),
|
|
{},
|
|
),
|
|
).toMatchObject({
|
|
ok: false,
|
|
error: { code: "POLICY_REJECTED" },
|
|
});
|
|
expect(
|
|
registry.resolveSelection(
|
|
browserFilePolicyReference(
|
|
"documents",
|
|
"select-preview-download-pdf",
|
|
),
|
|
),
|
|
).toMatchObject({
|
|
ok: false,
|
|
error: { code: "POLICY_REJECTED" },
|
|
});
|
|
expect(
|
|
registry.resolveSelection(
|
|
{
|
|
policyKey: "documents",
|
|
intention: "select-preview-download-pdf",
|
|
} as typeof reference,
|
|
),
|
|
).toMatchObject({
|
|
ok: false,
|
|
error: { code: "POLICY_REJECTED" },
|
|
});
|
|
});
|
|
|
|
// 2026-11-01 02-02. The download extension was checked for shape only, so a
|
|
// policy could hand `invoice.pdf` to the browser as `invoice.exe`. The
|
|
// extensions a user-supplied file name is stripped of are refused here too.
|
|
it.each([".exe", ".HTML", ".tar.js"])(
|
|
"refuses a download policy whose safe extension is %s",
|
|
(safeExtension) => {
|
|
expect(
|
|
() =>
|
|
new BrowserFilePolicyRegistry({
|
|
profiles: [
|
|
{
|
|
reference: browserFilePolicyReference("downloads", "invoice"),
|
|
download: {
|
|
strategy: "BOUNDED_OBJECT_URL",
|
|
mediaType: "application/pdf",
|
|
safeExtension,
|
|
maxTransferBytes: 100,
|
|
maxBufferedBytes: 50,
|
|
integrity: "OPTIONAL",
|
|
},
|
|
},
|
|
],
|
|
hardLimits,
|
|
}),
|
|
).toThrow(TypeError);
|
|
},
|
|
);
|
|
|
|
it("binds verification receipts to the exact profile, not a reused policyId", async () => {
|
|
const profileA = browserFilePolicyReference(
|
|
"images-a",
|
|
"preview-avatar",
|
|
);
|
|
const profileB = browserFilePolicyReference(
|
|
"images-b",
|
|
"preview-banner",
|
|
);
|
|
const inspection = {
|
|
policyId: "shared-png-v1",
|
|
maxInspectionBytes: 8,
|
|
acceptedSignatures: [
|
|
{
|
|
mediaType: "image/png",
|
|
extensions: [".png"],
|
|
patterns: [{ offset: 0, bytes: [0x89, 0x50] }],
|
|
},
|
|
],
|
|
};
|
|
const policies = new BrowserFilePolicyRegistry({
|
|
profiles: [profileA, profileB].map((reference) => ({
|
|
reference,
|
|
inspection,
|
|
preview: {
|
|
allowedMediaTypes: ["image/png"],
|
|
maxPreviewBytes: 16,
|
|
},
|
|
})),
|
|
hardLimits,
|
|
});
|
|
const vault = new BrowserFileVault({
|
|
policies,
|
|
createReference: () => "file:profile-bound",
|
|
createVerificationReceipt: () =>
|
|
"verification:profile-bound",
|
|
});
|
|
const captured = vault.captureFiles(
|
|
[
|
|
new File(
|
|
[new Uint8Array([0x89, 0x50, 1])],
|
|
"avatar.png",
|
|
{ type: "image/png", lastModified: 1 },
|
|
),
|
|
],
|
|
{
|
|
policyId: "capture-v1",
|
|
purpose: "capture",
|
|
classification: "PERSONAL",
|
|
multiple: false,
|
|
maxCount: 1,
|
|
maxFileBytes: 16,
|
|
maxTotalBytes: 16,
|
|
allowEmpty: false,
|
|
accept: [
|
|
{ mediaType: "image/png", extensions: [".png"] },
|
|
],
|
|
},
|
|
);
|
|
expect(captured.ok).toBe(true);
|
|
if (!captured.ok || !captured.value[0]) return;
|
|
const inspected = await vault.inspect({
|
|
ref: captured.value[0].ref,
|
|
policy: profileA,
|
|
signal: new AbortController().signal,
|
|
});
|
|
expect(inspected.ok).toBe(true);
|
|
if (!inspected.ok || !inspected.value.verificationReceipt) {
|
|
return;
|
|
}
|
|
const createObjectURL = vi.fn(() => "blob:profile-bound");
|
|
const previews = new BrowserTransientPreview({
|
|
files: vault,
|
|
policies,
|
|
hardMaxPreviewBytes: 16,
|
|
leases: new ObjectUrlLeaseRegistry({
|
|
createObjectURL,
|
|
revokeObjectURL() {},
|
|
}),
|
|
});
|
|
|
|
expect(
|
|
await previews.create({
|
|
ref: captured.value[0].ref,
|
|
verificationReceipt:
|
|
inspected.value.verificationReceipt,
|
|
policy: profileB,
|
|
signal: new AbortController().signal,
|
|
}),
|
|
).toMatchObject({
|
|
ok: false,
|
|
error: { code: "POLICY_REJECTED" },
|
|
});
|
|
expect(createObjectURL).not.toHaveBeenCalled();
|
|
expect(
|
|
await previews.create({
|
|
ref: captured.value[0].ref,
|
|
verificationReceipt:
|
|
inspected.value.verificationReceipt,
|
|
policy: profileA,
|
|
signal: new AbortController().signal,
|
|
}),
|
|
).toMatchObject({ ok: true });
|
|
});
|
|
});
|