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) ||