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 <noreply@anthropic.com>
This commit is contained in:
DongHyeonka
2026-09-25 22:48:03 +09:00
co-authored by Claude Opus 5.5
parent 4d8ceade21
commit 3767ed7ccb
5 changed files with 82 additions and 53 deletions
@@ -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<string, unknown>;
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
+34 -1
View File
@@ -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;
@@ -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<string, unknown>;
return `{${Object.keys(record)
.sort()
.map((key) => `${JSON.stringify(key)}:${stableJson(record[key])}`)
.join(",")}}`;
}
throw new TypeError("OPFS value is not JSON-safe.");
}
@@ -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<string, unknown>;
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"],
+11
View File
@@ -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);
});
});