Skip to content

Commit 47fe789

Browse files
committed
fix(core): the managed write clamp lowers modifyAllRecords and clears transfer where the guard refuses the update
clampManagedObjectWrites cleared allowEdit/allowCreate/allowDelete on a guarded managed object that does not opt the verb in, but left modifyAllRecords (which grants edit, delete and transfer on its own) and allowTransfer (an owner change is an update). It now lowers modifyAllRecords to viewAllRecords plus the plain bit of each verb the object still opts in, through one helper shared with the external write clamp, and clears allowTransfer unless edit is opted in. Claude-Session: https://claude.ai/code/session_01Utp6CZUqByp4kJMhCQDWZ2 Co-authored-by: Claude <noreply@anthropic.com>
1 parent c580081 commit 47fe789

3 files changed

Lines changed: 119 additions & 7 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
'@objectstack/core': patch
3+
---
4+
5+
fix(core): effective object permissions on a guarded managed object grant no edit, delete or transfer the write guard refuses, `modifyAllRecords` included
6+
7+
Clause-②: yes (widening)
8+
Metadata-protocol: none
9+
10+
The managed write clamp behind `/auth/me/permissions` and `ISecurityService.getEffectiveObjectPermissions` cleared `allowEdit`, `allowCreate` and `allowDelete` on an object in a guarded `managedBy` bucket (`better-auth`, `engine-owned`, `append-only`) that does not opt the verb in through `userActions`, but left two grants the write guard refuses:
11+
12+
- **`modifyAllRecords`**, which grants edit, delete and transfer on its own. A permission set naming such an object with `{ modifyAllRecords: true }` made `current_user.can(object, 'edit' | 'delete' | 'transfer')` answer `true` while every one of those writes was refused. It is now lowered to `viewAllRecords` (the read it implies, so read is unchanged), and each verb the object still opts in is kept as its plain bit: an object opting `edit` in keeps edit and transfer and loses delete. An object opting both `edit` and `delete` in keeps `modifyAllRecords`.
13+
- **`allowTransfer`**, which now follows `edit`: an owner change is an update, refused unless `edit` is opted in. This also corrects a super-user wildcard admin, who was answered `can(object, 'transfer')` `true` on every guarded object.
14+
15+
No write changes: the guard refused each of these before and still does. Only the served capability answer, and the console buttons that read it, now match it.

‎packages/core/src/security/effective-object-permissions.test.ts‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,69 @@ describe('[#30370] an object whose external write gate is closed grants no write
555555
});
556556
});
557557

558+
describe('[#30440] a guarded managed object grants no verb its write guard refuses, modifyAllRecords included', () => {
559+
const SCHEMAS: Record<string, any> = {
560+
// engine-owned, nothing opted in: the guard refuses every user-context write.
561+
sys_automation_run: { name: 'sys_automation_run', managedBy: 'engine-owned' },
562+
// better-auth with only `edit` opted in (the sys_user shape).
563+
sys_user: { name: 'sys_user', managedBy: 'better-auth', userActions: { edit: true } },
564+
// append-only with both row verbs opted in (object form for delete).
565+
sys_feed_note: { name: 'sys_feed_note', managedBy: 'append-only', userActions: { edit: true, delete: { enabled: true } } },
566+
// no guard covers it.
567+
crm_lead: { name: 'crm_lead' },
568+
};
569+
const source = { allSchemas: () => Object.values(SCHEMAS), schemaOf: (name: string) => SCHEMAS[name] };
570+
const ROW_VERBS = ['allowEdit', 'allowDelete', 'allowTransfer'] as const;
571+
const can = (entry: any) => Object.fromEntries(
572+
(['allowRead', 'allowCreate', ...ROW_VERBS] as const).map((verb) => [verb, objectPermissionGrants(entry, verb)]),
573+
);
574+
575+
it('an explicit { modifyAllRecords: true } on an object opting nothing in answers no edit, delete or transfer, and the same read', () => {
576+
const map: any = buildEffectiveObjectPermissions([{ objects: { sys_automation_run: { modifyAllRecords: true } } }], source);
577+
expect(can(map.sys_automation_run)).toEqual({ allowRead: true, allowCreate: false, allowEdit: false, allowDelete: false, allowTransfer: false });
578+
expect(map.sys_automation_run).toMatchObject({ modifyAllRecords: false, viewAllRecords: true });
579+
});
580+
581+
it('an object opting edit in keeps edit and transfer through it, and loses delete', () => {
582+
const map: any = buildEffectiveObjectPermissions([{ objects: { sys_user: { modifyAllRecords: true } } }], source);
583+
expect(can(map.sys_user)).toEqual({ allowRead: true, allowCreate: false, allowEdit: true, allowDelete: false, allowTransfer: true });
584+
expect(map.sys_user).toMatchObject({ modifyAllRecords: false, viewAllRecords: true, allowEdit: true, allowTransfer: true });
585+
});
586+
587+
it('an object opting edit and delete in keeps modifyAllRecords as granted', () => {
588+
const map: any = buildEffectiveObjectPermissions([{ objects: { sys_feed_note: { modifyAllRecords: true } } }], source);
589+
expect(map.sys_feed_note.modifyAllRecords).toBe(true);
590+
expect(map.sys_feed_note).not.toHaveProperty('viewAllRecords');
591+
expect(can(map.sys_feed_note)).toEqual({ allowRead: true, allowCreate: false, allowEdit: true, allowDelete: true, allowTransfer: true });
592+
});
593+
594+
it('an explicit allowTransfer on an object not opting edit in answers no transfer', () => {
595+
const map: any = buildEffectiveObjectPermissions([{ objects: { sys_automation_run: { allowRead: true, allowTransfer: true } } }], source);
596+
expect(objectPermissionGrants(map.sys_automation_run, 'allowTransfer')).toBe(false);
597+
expect(objectPermissionGrants(map.sys_automation_run, 'allowRead')).toBe(true);
598+
});
599+
600+
it('CONTROL: the same entry on an unguarded object keeps every verb modifyAllRecords grants', () => {
601+
const map: any = buildEffectiveObjectPermissions([{ objects: { crm_lead: { modifyAllRecords: true } } }], source);
602+
expect(map.crm_lead.modifyAllRecords).toBe(true);
603+
expect(can(map.crm_lead)).toEqual({ allowRead: true, allowCreate: false, allowEdit: true, allowDelete: true, allowTransfer: true });
604+
});
605+
606+
it('CONTROL: the super-user wildcard fold answers as before, transfer on the object opting nothing in aside', () => {
607+
const map: any = buildEffectiveObjectPermissions(
608+
[{ objects: { '*': { allowRead: true, viewAllRecords: true, modifyAllRecords: true } } }],
609+
source,
610+
);
611+
expect(can(map.crm_lead)).toEqual({ allowRead: true, allowCreate: false, allowEdit: true, allowDelete: true, allowTransfer: true });
612+
expect(can(map.sys_user)).toEqual({ allowRead: true, allowCreate: false, allowEdit: true, allowDelete: false, allowTransfer: true });
613+
expect(can(map.sys_feed_note)).toEqual({ allowRead: true, allowCreate: false, allowEdit: true, allowDelete: true, allowTransfer: true });
614+
// The fold copies the bits the wildcard grants, never modifyAllRecords itself.
615+
for (const name of Object.keys(SCHEMAS)) expect(map[name], name).not.toHaveProperty('modifyAllRecords');
616+
// The one answer that moves: the guard refuses the update a transfer is.
617+
expect(can(map.sys_automation_run)).toEqual({ allowRead: true, allowCreate: false, allowEdit: false, allowDelete: false, allowTransfer: false });
618+
});
619+
});
620+
558621
// ── [#25866 / ruling of record 6408558941] A field permission entry's row
559622
// condition (`when`): the ONE vote per verdict, and the row-blind merge behind
560623
// `/auth/me/permissions`' `fields` slot.

‎packages/core/src/security/effective-object-permissions.ts‎

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,16 @@ const GUARDED_WRITE_BUCKETS: ReadonlySet<string> = new Set(['better-auth', 'engi
239239
* objects are NOT clamped — no guard covers them, so their permission-set result
240240
* stands (an admin CAN write them via the data API, and the hint must not
241241
* under-report that).
242+
*
243+
* [#30440] Every verb the guard refuses is refused whatever grants it:
244+
* - `allowTransfer` follows `edit` — an owner change is an update, and the
245+
* guard refuses the update unless `edit` is opted in;
246+
* - `modifyAllRecords` grants edit, delete and transfer on its own (the
247+
* spec's `objectPermissionGrants`), so where the guard refuses one of them
248+
* it is lowered by {@link lowerModifyAllRecords}: to `viewAllRecords`, the
249+
* read it implies, plus the plain bit of each verb the object still opts in.
250+
* Where `edit` and `delete` are both opted in it grants nothing the guard
251+
* refuses, and stays.
242252
*/
243253
export function clampManagedObjectWrites(
244254
objects: Record<string, any>,
@@ -249,15 +259,42 @@ export function clampManagedObjectWrites(
249259
const schema = schemaOf(obj);
250260
if (!schema?.managedBy || !GUARDED_WRITE_BUCKETS.has(schema.managedBy)) continue;
251261
const ua = schema.userActions ?? {};
252-
if (!isWriteOptedIn(ua.edit)) acc.allowEdit = false;
262+
const edit = isWriteOptedIn(ua.edit);
263+
const del = isWriteOptedIn(ua.delete);
264+
if (!edit) acc.allowEdit = false;
253265
// `create` reads through the same opt-in helper as edit/delete since
254266
// #7692 widened it to the object form — a bare `ua.create !== true`
255267
// would clamp away a legitimate `{ enabled: true, visibleWhen: … }`.
256268
if (!isWriteOptedIn(ua.create)) acc.allowCreate = false;
257-
if (!isWriteOptedIn(ua.delete)) acc.allowDelete = false;
269+
if (!del) acc.allowDelete = false;
270+
if (!edit) acc.allowTransfer = false;
271+
lowerModifyAllRecords(acc, { allowEdit: edit, allowDelete: del, allowTransfer: edit });
258272
}
259273
}
260274

275+
/**
276+
* Lower an entry's `modifyAllRecords` where a server-side layer refuses a verb
277+
* it grants. The spec's `objectPermissionGrants` answers edit, delete and
278+
* transfer `true` on `modifyAllRecords` alone, so no `allow*: false` beside it
279+
* takes any of them away: the bit itself has to go. It becomes
280+
* `viewAllRecords` — the read it implies, so the entry keeps exactly the read
281+
* it had — and each verb `kept` names is carried over as its plain bit, so a
282+
* verb the layer still admits is not lost with it. An entry the layer admits
283+
* every one of those verbs on is left as it is.
284+
*/
285+
function lowerModifyAllRecords(
286+
acc: Record<string, any>,
287+
kept: { allowEdit: boolean; allowDelete: boolean; allowTransfer: boolean },
288+
): void {
289+
if (acc.modifyAllRecords !== true) return;
290+
if (kept.allowEdit && kept.allowDelete && kept.allowTransfer) return;
291+
acc.modifyAllRecords = false;
292+
acc.viewAllRecords = true;
293+
if (kept.allowEdit) acc.allowEdit = true;
294+
if (kept.allowDelete) acc.allowDelete = true;
295+
if (kept.allowTransfer) acc.allowTransfer = true;
296+
}
297+
261298
/** The API-exposure-relevant slice of a registered object schema. */
262299
export interface ApiExposureSchemaLike {
263300
name?: string;
@@ -551,7 +588,7 @@ const EXTERNAL_WRITE_GATED_OPERATIONS: ReadonlySet<ApiOperation> = new Set<ApiOp
551588
* answers edit, delete and transfer `true` on it alone, so an entry keeping it
552589
* would still answer `current_user.can(object, 'edit')` `true`. It is lowered
553590
* to `viewAllRecords`, the read half it implies, so the entry keeps exactly
554-
* the read it had.
591+
* the read it had ({@link lowerModifyAllRecords}, keeping no verb).
555592
*
556593
* An object the map does not mention already grants nothing, and is left out.
557594
*/
@@ -563,10 +600,7 @@ function clampExternalReadOnlyWrites(objects: Record<string, any>, forbidden: Re
563600
acc.allowEdit = false;
564601
acc.allowDelete = false;
565602
acc.allowTransfer = false;
566-
if (acc.modifyAllRecords === true) {
567-
acc.modifyAllRecords = false;
568-
acc.viewAllRecords = true;
569-
}
603+
lowerModifyAllRecords(acc, { allowEdit: false, allowDelete: false, allowTransfer: false });
570604
}
571605
}
572606

0 commit comments

Comments
 (0)