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:
co-authored by
Claude Opus 5.5
parent
4b0e44a92a
commit
33bf559bce
@@ -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", () => {
|
||||
|
||||
Reference in New Issue
Block a user