diff --git a/src/adapters/storage/opfs/opfs-policy.ts b/src/adapters/storage/opfs/opfs-policy.ts index f62f4f42..c0776aa8 100644 --- a/src/adapters/storage/opfs/opfs-policy.ts +++ b/src/adapters/storage/opfs/opfs-policy.ts @@ -131,7 +131,9 @@ export function assertOpfsRuntimePolicy(policy: OpfsRuntimePolicy): void { * 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. + * outside the grammar catch the usual widenings when the policy is resolved; + * writes also check the grammar itself (isWritableObjectId), so a widening no + * sample finds is refused at BEGIN instead. */ const OBJECT_IDS_OUTSIDE_GRAMMAR = Object.freeze([ "", @@ -141,6 +143,9 @@ const OBJECT_IDS_OUTSIDE_GRAMMAR = Object.freeze([ "object.12345", "object/12345", "object~12345", + "object:12345", + "object@12345", + "objekt_\u00e4\u00f6\u00fc12", ]); function widensObjectIdGrammar( @@ -159,6 +164,14 @@ export function isOpaqueObjectId(value: string): boolean { return OPAQUE_OBJECT_ID.test(value); } +/** 05-10. The policy may narrow the id grammar for writes, never widen it. */ +export function isWritableObjectId( + policy: OpfsRuntimePolicy, + objectId: string, +): boolean { + return OPAQUE_OBJECT_ID.test(objectId) && policy.isObjectIdAllowed(objectId); +} + export function validateObjectWriteInput( input: Readonly<{ scope: OpfsStorageScope; @@ -179,7 +192,7 @@ export function validateObjectWriteInput( return ( isValidOpfsStorageScope(input.scope) && input.scope.namespace === input.storagePolicy.namespace && - policy.isObjectIdAllowed(input.objectId) && + isWritableObjectId(policy, input.objectId) && policy.isMediaTypeAllowed(input.mediaType) && (input.expectedGeneration === null || (Number.isSafeInteger(input.expectedGeneration) && diff --git a/src/adapters/storage/opfs/opfs-worker-runtime.ts b/src/adapters/storage/opfs/opfs-worker-runtime.ts index 71e4991b..44231cd4 100644 --- a/src/adapters/storage/opfs/opfs-worker-runtime.ts +++ b/src/adapters/storage/opfs/opfs-worker-runtime.ts @@ -14,6 +14,7 @@ import { createAbortableOperation } from "../../platform/abortable-operation.ts" import { resolveOpfsRuntimePolicy, isValidOpfsStorageScope, + isWritableObjectId, type OpfsRuntimePolicy, } from "./opfs-policy.ts"; import { isPreparedObject, stableJson } from "./opfs-prepared-object.ts"; @@ -328,7 +329,7 @@ export function createOpfsWorkerRuntime( if ( !SAFE_TRANSACTION_ID.test(request.transactionId) || !isValidOpfsStorageScope(request.scope) || - !dependencies.policy.isObjectIdAllowed(request.objectId) || + !isWritableObjectId(dependencies.policy, request.objectId) || !Number.isSafeInteger(request.generation) || request.generation < 1 || !isPhysicalGenerationId(request.physicalGenerationId) || diff --git a/tests/unit/opfs-worker-runtime.test.ts b/tests/unit/opfs-worker-runtime.test.ts index b36b5115..3899f8e3 100644 --- a/tests/unit/opfs-worker-runtime.test.ts +++ b/tests/unit/opfs-worker-runtime.test.ts @@ -2453,6 +2453,68 @@ describe("05-10 the object id grammar is the prepared-object grammar", () => { }), ).toThrow(TypeError); }); + + it.each([ + ["a colon", /^[A-Za-z0-9_:-]{8,128}$/u], + ["an at sign", /^[A-Za-z0-9_@-]{8,128}$/u], + ["Unicode letters", /^[\p{L}0-9_-]{8,128}$/u], + ])("rejects a policy that also allows %s when it is resolved", (_, grammar) => { + expect(() => + resolveOpfsRuntimePolicy({ + ...runtimePolicy, + isObjectIdAllowed: (objectId) => grammar.test(objectId), + }), + ).toThrow(TypeError); + }); + + // A predicate can widen the grammar in ways no sample finds. The grammar + // still applies at BEGIN_PUT, so such an id never reaches FINISH_PUT. + const widenedId = "object:tenant@12345"; + const widenedPolicy = (): typeof runtimePolicy => ({ + ...runtimePolicy, + isObjectIdAllowed: (objectId) => + objectId === widenedId || runtimePolicy.isObjectIdAllowed(objectId), + }); + + it("refuses an id outside the grammar at BEGIN_PUT even if the policy allows it", async () => { + const runtime = createOpfsWorkerRuntime({ + root: new MemoryDirectory() as unknown as FileSystemDirectoryHandle, + crypto: globalThis.crypto, + policy: widenedPolicy(), + leaseManager: immediateLeases({ count: 0 }), + dedicatedWorker: true, + supportsSynchronousAccessHandles: false, + }); + + expectFailureCode( + await runtime.handleRequest({ + ...beginRequest("request_begin_1234", "transaction_12345678", scopeA), + objectId: widenedId, + } as OpfsWorkerRequest), + "INVALID_INPUT", + ); + }); + + it("fails such a put as invalid input before it writes, never as corrupt data", async () => { + const { tab, root } = sharedRootTabs(widenedPolicy()); + const adapter = tab("a"); + + expect( + await adapter.objects.put({ + objectId: widenedId, + expectedGeneration: null, + mediaType: "application/octet-stream", + source: oneByteSource(), + }), + ).toMatchObject({ ok: false, error: { code: "INVALID_INPUT" } }); + expect(root.children.size).toBe(0); + expect(await adapter.objects.put({ + objectId: "object_12345678", + expectedGeneration: null, + mediaType: "application/octet-stream", + source: oneByteSource(), + })).toMatchObject({ ok: true }); + }); }); describe("05-14 a read that outlives its generation", () => {