fix(storage): check the fixed object id grammar at every write, not only by sample (05-10)

Resolving a policy only rejected predicates that admitted one of seven sample
ids, so a policy also allowing ':', '@' or letters passed BEGIN_PUT and failed
at FINISH_PUT. Writes now require the grammar alongside the policy, and the
samples cover those widenings so they are refused when the policy is resolved.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
donghyeon-ka
2026-09-27 14:02:35 +09:00
co-authored by Claude Opus 5.5
parent 4b0e44a92a
commit 33bf559bce
3 changed files with 79 additions and 3 deletions
+15 -2
View File
@@ -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) &&
@@ -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) ||
+62
View File
@@ -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", () => {