From 3767ed7ccb0acbd87fb5a3f04e8790ceba3799ef Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Fri, 25 Sep 2026 22:48:03 +0900 Subject: [PATCH] fix(storage): refuse an OPFS policy that widens the object id grammar and keep one stableJson (05-10) isObjectIdAllowed could be widened while stored objects, journal rows and paths keep the fixed opaque id grammar, so such a policy failed puts late. Resolving the policy now rejects a predicate that admits ids just outside the grammar, and the prepared-object check uses the policy's own grammar. The worker and the journal share one stableJson that throws on values JSON cannot carry instead of writing NaN as null. Co-Authored-By: Claude Opus 5.5 --- .../storage/opfs/indexeddb-opfs-journal.ts | 24 +----------- src/adapters/storage/opfs/opfs-policy.ts | 35 ++++++++++++++++- .../storage/opfs/opfs-prepared-object.ts | 38 +++++++++++++++++-- .../storage/opfs/opfs-worker-runtime.ts | 27 +------------ tests/unit/opfs-worker-runtime.test.ts | 11 ++++++ 5 files changed, 82 insertions(+), 53 deletions(-) diff --git a/src/adapters/storage/opfs/indexeddb-opfs-journal.ts b/src/adapters/storage/opfs/indexeddb-opfs-journal.ts index cc58f704..e1dcb5d2 100644 --- a/src/adapters/storage/opfs/indexeddb-opfs-journal.ts +++ b/src/adapters/storage/opfs/indexeddb-opfs-journal.ts @@ -33,7 +33,7 @@ import { } from "../../platform/indexeddb-transaction.ts"; import { mapIndexedDbException } from "../indexeddb/indexeddb-failure.ts"; import { isValidOpfsStorageScope } from "./opfs-policy.ts"; -import { isPreparedObject } from "./opfs-prepared-object.ts"; +import { isPreparedObject, stableJson } from "./opfs-prepared-object.ts"; export type IndexedDbOpfsJournalDependencies = Readonly<{ authorityToken: string; @@ -1608,28 +1608,6 @@ function isChunkReference( ); } -function stableJson(value: unknown): string { - if ( - value === null || - typeof value === "string" || - typeof value === "boolean" || - typeof value === "number" - ) { - return JSON.stringify(value); - } - if (Array.isArray(value)) { - return `[${value.map(stableJson).join(",")}]`; - } - if (typeof value === "object") { - const record = value as Record; - return `{${Object.keys(record) - .sort() - .map((key) => `${JSON.stringify(key)}:${stableJson(record[key])}`) - .join(",")}}`; - } - throw new TypeError("Journal value is not JSON-safe."); -} - /** * `browserDataFailure` and `mapIndexedDbException` build a `Result`, while the * kernel's `translate` and `context.fail` want the failure on its own. Both diff --git a/src/adapters/storage/opfs/opfs-policy.ts b/src/adapters/storage/opfs/opfs-policy.ts index d5dd8a95..f62f4f42 100644 --- a/src/adapters/storage/opfs/opfs-policy.ts +++ b/src/adapters/storage/opfs/opfs-policy.ts @@ -120,12 +120,45 @@ export function assertOpfsRuntimePolicy(policy: OpfsRuntimePolicy): void { policy.maxCancellationTombstones < 16 || policy.maxCancellationTombstones > 10_000 || typeof policy.isObjectIdAllowed !== "function" || - typeof policy.isMediaTypeAllowed !== "function" + typeof policy.isMediaTypeAllowed !== "function" || + widensObjectIdGrammar(policy.isObjectIdAllowed) ) { throw new TypeError("OPFS runtime policy is invalid."); } } +/** + * 05-10. Stored prepared objects, journal rows and physical paths all use the + * opaque id grammar, so a policy may narrow it but never widen it: a wider + * policy let BEGIN through and failed every put at its last step. Ids just + * outside the grammar catch the usual widenings when the policy is resolved. + */ +const OBJECT_IDS_OUTSIDE_GRAMMAR = Object.freeze([ + "", + "short_7", + "a".repeat(129), + "object 12345", + "object.12345", + "object/12345", + "object~12345", +]); + +function widensObjectIdGrammar( + isObjectIdAllowed: (objectId: string) => boolean, +): boolean { + try { + return OBJECT_IDS_OUTSIDE_GRAMMAR.some((objectId) => + isObjectIdAllowed(objectId), + ); + } catch { + return true; + } +} + +export function isOpaqueObjectId(value: string): boolean { + return OPAQUE_OBJECT_ID.test(value); +} + export function validateObjectWriteInput( input: Readonly<{ scope: OpfsStorageScope; diff --git a/src/adapters/storage/opfs/opfs-prepared-object.ts b/src/adapters/storage/opfs/opfs-prepared-object.ts index ab44c1f8..5ee2af2b 100644 --- a/src/adapters/storage/opfs/opfs-prepared-object.ts +++ b/src/adapters/storage/opfs/opfs-prepared-object.ts @@ -3,9 +3,8 @@ import type { OpfsStorageScope, } from "../../../application/ports/browser-file-storage/opfs-ports.ts"; import { assertValidStoragePolicy } from "../../../application/ports/browser-file-storage/shared.ts"; -import { isValidOpfsStorageScope } from "./opfs-policy.ts"; +import { isOpaqueObjectId, isValidOpfsStorageScope } from "./opfs-policy.ts"; -const OPAQUE_OBJECT_ID = /^[A-Za-z0-9_-]{8,128}$/u; const SHA256_HEX = /^[a-f0-9]{64}$/u; /** @@ -53,7 +52,7 @@ export function isPreparedObject( : null; if ( typeof descriptor.objectId !== "string" || - !OPAQUE_OBJECT_ID.test(descriptor.objectId) || + !isOpaqueObjectId(descriptor.objectId) || !descriptor.scope || typeof descriptor.scope !== "object" || !isValidOpfsStorageScope(descriptor.scope as OpfsStorageScope) || @@ -129,3 +128,36 @@ export function isPreparedObject( Math.ceil(descriptor.byteLength / integrity.chunkSizeBytes) ); } + +/** + * 05-10. The one canonical JSON for comparing OPFS records, used by the + * worker and the journal alike. A value JSON cannot carry exactly (a + * non-finite number, `undefined`, a function) throws instead of being + * written as something else, so two different values never compare equal. + */ +export function stableJson(value: unknown): string { + if ( + value === null || + typeof value === "string" || + typeof value === "boolean" + ) { + return JSON.stringify(value); + } + if (typeof value === "number") { + if (!Number.isFinite(value)) { + throw new TypeError("OPFS value is not JSON-safe."); + } + return JSON.stringify(value); + } + if (Array.isArray(value)) { + return `[${value.map(stableJson).join(",")}]`; + } + if (typeof value === "object") { + const record = value as Record; + return `{${Object.keys(record) + .sort() + .map((key) => `${JSON.stringify(key)}:${stableJson(record[key])}`) + .join(",")}}`; + } + throw new TypeError("OPFS value is not JSON-safe."); +} diff --git a/src/adapters/storage/opfs/opfs-worker-runtime.ts b/src/adapters/storage/opfs/opfs-worker-runtime.ts index 07b23ba4..71e4991b 100644 --- a/src/adapters/storage/opfs/opfs-worker-runtime.ts +++ b/src/adapters/storage/opfs/opfs-worker-runtime.ts @@ -16,7 +16,7 @@ import { isValidOpfsStorageScope, type OpfsRuntimePolicy, } from "./opfs-policy.ts"; -import { isPreparedObject } from "./opfs-prepared-object.ts"; +import { isPreparedObject, stableJson } from "./opfs-prepared-object.ts"; import { OPFS_WORKER_PROTOCOL_VERSION, } from "./opfs-worker-protocol.ts"; @@ -1739,31 +1739,6 @@ function bytesToHex(bytes: Uint8Array): string { return hex; } -function stableJson(value: unknown): string { - if ( - value === null || - typeof value === "string" || - typeof value === "boolean" - ) { - return JSON.stringify(value); - } - if (typeof value === "number") { - if (!Number.isFinite(value)) throw new OpfsRuntimeFailure("CORRUPT_DATA"); - return JSON.stringify(value); - } - if (Array.isArray(value)) { - return `[${value.map(stableJson).join(",")}]`; - } - if (typeof value === "object") { - const record = value as Record; - return `{${Object.keys(record) - .sort() - .map((key) => `${JSON.stringify(key)}:${stableJson(record[key])}`) - .join(",")}}`; - } - throw new OpfsRuntimeFailure("CORRUPT_DATA"); -} - function success( requestId: string, kind: OpfsWorkerRequest["kind"], diff --git a/tests/unit/opfs-worker-runtime.test.ts b/tests/unit/opfs-worker-runtime.test.ts index dbac0184..d1b1bae3 100644 --- a/tests/unit/opfs-worker-runtime.test.ts +++ b/tests/unit/opfs-worker-runtime.test.ts @@ -2443,3 +2443,14 @@ describe("05-07 a put whose source stops lets go of the origin lock", () => { stalled.release(); }); }); + +describe("05-10 the object id grammar is the prepared-object grammar", () => { + it("rejects a policy that widens the object id grammar when it is resolved", () => { + expect(() => + resolveOpfsRuntimePolicy({ + ...runtimePolicy, + isObjectIdAllowed: (objectId) => /^[A-Za-z0-9_.-]{1,200}$/u.test(objectId), + }), + ).toThrow(TypeError); + }); +});