Repository navigation
Expand file tree
/
Copy pathdefault-permission-sets.test.ts
More file actions
656 lines (603 loc) · 34 KB
/
Copy pathdefault-permission-sets.test.ts
File metadata and controls
656 lines (603 loc) · 34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
// #3325 — pin the better-auth managed-object deny-list against the real schemas
// so it can never silently drift again (ADR-0092: registry-driven, no rot).
import { describe, it, expect } from 'vitest';
import * as PlatformObjects from '@objectstack/platform-objects';
import { PermissionSetSchema } from '@objectstack/spec/security';
import { ADMIN_FULL_ACCESS_CAPABILITIES } from '@objectstack/spec';
import { defaultPermissionSets, BETTER_AUTH_MANAGED_OBJECTS } from './default-permission-sets.js';
import { applyManagedWriteDenies, MANAGED_DENY_TARGET_SETS } from '../managed-object-write-denies.js';
// Every object schema the platform-objects package exports whose bucket is
// `better-auth` — the ground truth the static baseline must mirror.
const betterAuthSchemaNames = Object.values(PlatformObjects as Record<string, any>)
.filter((v) => v && typeof v === 'object' && typeof v.name === 'string' && v.managedBy === 'better-auth')
.map((v) => v.name as string)
.sort();
// `string[]`, not the literal union `BETTER_AUTH_MANAGED_OBJECTS` carries: this
// pin compares the list against names read off the shipped schemas, which are
// plain strings, and the comparison runs in BOTH directions. Left as the union,
// `listNames.includes(<a schema name>)` is a type error rather than the
// membership question the pin asks.
const listNames: string[] = [...BETTER_AUTH_MANAGED_OBJECTS].sort();
const setByName = (name: string): any => defaultPermissionSets.find((s) => s.name === name);
describe('BETTER_AUTH_MANAGED_OBJECTS ↔ schemas (drift pin, #3325)', () => {
it('found the better-auth identity schemas to compare against', () => {
// Guard against a broken import silently passing the bidirectional check.
expect(betterAuthSchemaNames.length).toBeGreaterThanOrEqual(20);
});
it('every listed name is a real object declaring managedBy:"better-auth"', () => {
const notBetterAuth = listNames.filter((n) => !betterAuthSchemaNames.includes(n));
expect(notBetterAuth).toEqual([]);
});
it('every better-auth schema is in the list (this is the drift that #3325 fixes)', () => {
const missing = betterAuthSchemaNames.filter((n) => !listNames.includes(n));
expect(missing).toEqual([]);
});
it('the list has no duplicates', () => {
expect(listNames.length).toBe(new Set(listNames).size);
});
});
/**
* The deliberate exceptions to the blanket managed-object edit deny. Both are
* on `member_default`, both are self-service, and neither is bounded by the
* permission-set boolean it sits on:
*
* - [#8053] `sys_api_key` — a member may revoke their OWN personal key. The
* `sys_api_key_self` RLS carve-out decides which rows; ADR-0092 D2's column
* whitelist (`revoked` alone) decides which fields.
* - [#14959, maintainer ruling 2026-09-03] `sys_user` — a member may edit
* their OWN profile row. The `sys_user_self` RLS carve-out decides which
* rows; ADR-0092 D2's column whitelist (`SYS_USER_PROFILE_EDIT_FIELDS` —
* `name`, `image`, `locale`) decides which fields. The ruling amended
* ADR-0092 D5, whose original text said self-service stays on better-auth
* `/update-user` and that no RLS self-row EDIT carve-out would be built.
*
* Encoded as exact (set, object) pairs rather than by loosening the loop, so a
* third entry — or either of these on another set — still fails this pin. The
* create/delete/read axes are NOT excepted and are still asserted below.
*/
const EDIT_EXCEPTIONS = new Set(['member_default::sys_api_key', 'member_default::sys_user']);
describe('default permission sets carry the managed denies (static baseline)', () => {
it('each write-granting target set denies create/edit/delete on every managed object', () => {
for (const setName of MANAGED_DENY_TARGET_SETS) {
const set = setByName(setName);
expect(set, `set ${setName} exists`).toBeTruthy();
for (const obj of BETTER_AUTH_MANAGED_OBJECTS) {
const entry = set.objects[obj];
expect(entry, `${setName} has entry for ${obj}`).toBeTruthy();
expect(entry.allowCreate).toBe(false);
expect(entry.allowEdit, `${setName}.${obj} allowEdit`).toBe(
EDIT_EXCEPTIONS.has(`${setName}::${obj}`),
);
expect(entry.allowDelete).toBe(false);
expect(entry.allowRead).toBe(true);
}
}
});
it('the edit exceptions are exactly the two self-service pairs, and each rides its own row scope', () => {
// The exception list is itself pinned: a future widening has to edit THIS
// assertion, which is the moment someone is asked whether the new pair
// really rides an owner-scoping RLS policy and a column whitelist the way
// these two do. Without this, `EDIT_EXCEPTIONS` could grow silently.
expect([...EDIT_EXCEPTIONS]).toEqual([
'member_default::sys_api_key',
'member_default::sys_user',
]);
const member = setByName('member_default');
// The owner scoping each grant leans on must exist, or the edit bit is
// table-wide on an identity table. `operation` must reach the WRITE class:
// a `select`-only carve-out contributes nothing to the by-id write
// pre-image check, which is where the row scope is actually enforced.
const scopes: Array<[string, string, string]> = [
['sys_api_key', 'sys_api_key_self', 'user_id == current_user.id'],
['sys_user', 'sys_user_self', 'id == current_user.id'],
];
for (const [object, policyName, predicate] of scopes) {
expect(member.objects[object].allowEdit, `${object} allowEdit`).toBe(true);
const selfPolicy = (member.rowLevelSecurity ?? []).find(
(p: any) => p.object === object && p.name === policyName,
);
expect(selfPolicy, `member_default must keep the ${policyName} RLS carve-out`).toBeTruthy();
expect(selfPolicy.using).toBe(predicate);
expect(['all', 'update'], `${policyName} operation`).toContain(selfPolicy.operation);
}
// [#14959] The other `sys_user` policy in this set is the org-peer
// VISIBILITY scope, and it must stay read-only. Applicable policies
// OR-combine, so an `all` here would compose an update filter of
// `id == me OR id IN <every user in my org>` — i.e. the edit bit above
// would reach every colleague's profile row. Pinned next to the grant it
// bounds, because the two lines are 100+ apart in the source.
const orgPeers = (member.rowLevelSecurity ?? []).find(
(p: any) => p.object === 'sys_user' && p.name === 'sys_user_org_members',
);
expect(orgPeers, 'member_default must keep sys_user_org_members').toBeTruthy();
expect(orgPeers.operation, 'org-peer visibility must not become a write scope').toBe('select');
});
it('admin_full_access keeps its bare wildcard (zero per-object entries) — admin rescue path', () => {
const admin = setByName('admin_full_access');
expect(admin).toBeTruthy();
expect(Object.keys(admin.objects)).toEqual(['*']);
});
});
/**
* [#8095] The `sys_invitation` row scope — a DELETION TRIPWIRE, not the proof.
*
* The proof that the narrowing works lives over HTTP, in
* `packages/qa/dogfood/test/invitation-ledger-row-scope.dogfood.test.ts`: an
* assertion whose expectation and reality both come from this module cannot
* fail, so nothing here is evidence that a member is actually narrowed. What
* this block does buy is the one thing the HTTP fixture cannot — it names the
* predicate and the sets it must appear in, so removing a carve-out (or adding
* a managed object to `BETTER_AUTH_MANAGED_OBJECTS` and assuming the blanket
* read is safe for it) fails HERE, in the file being edited, instead of in a
* suite the author may not run.
*
* The predicate is written out rather than imported for the same reason.
*/
describe('sys_invitation is row-scoped to its addressee (#8095)', () => {
const SELF_PREDICATE = 'email == current_user.email';
const policiesFor = (setName: string, object: string): any[] =>
(setByName(setName)?.rowLevelSecurity ?? []).filter((p: any) => p.object === object);
it.each(['member_default', 'viewer_readonly'])(
'%s scopes sys_invitation to the addressee — the blanket managed read is NOT the whole story',
(setName) => {
// The object-level read bit stays open: it is what makes the invitee's OWN
// row reachable, and closing it would break the accept flow rather than
// narrow it. The narrowing is the row predicate.
expect(setByName(setName).objects.sys_invitation.allowRead).toBe(true);
// Named, not counted: `member_default` gained the #8240 issuer sibling
// and `viewer_readonly` did not, so an exact-array assertion over both
// would have to encode that asymmetry and would go red for the wrong
// reason. What each set must carry is the addressee scope; the issuer
// policy has its own block below.
const scoped = policiesFor(setName, 'sys_invitation');
const self = scoped.find((p) => p.name === 'sys_invitation_self');
expect(self, `${setName} must keep sys_invitation_self`).toBeTruthy();
expect(self.using).toBe(SELF_PREDICATE);
// Read the operation off THIS policy. The same-named carve-outs across
// these sets do not all agree (`sys_api_key_self` is spelled three times
// at two different operations), so a sibling's value is not evidence.
expect(self.operation).toBe('select');
expect(self.enabled).not.toBe(false);
// The addressee scope is UNDOMAINED, and that is load-bearing: it is the
// narrowing as well as the carve-out, so a `positions` domain on it would
// mean "no matching position ⇒ no policy ⇒ no row filter at all" — the
// wide read #8095 is about. Only the WIDENING policies carry a domain.
expect(self.positions ?? []).toEqual([]);
// No other set-level policy may quietly re-open the object here. Both
// sets are capped at the addressee scope plus (member_default only) the
// scope-bounded issuer sibling.
expect(scoped.map((p) => p.name).sort()).toEqual(
setName === 'member_default'
? ['sys_invitation_issuer', 'sys_invitation_self']
: ['sys_invitation_self'],
);
},
);
it('organization_admin keeps the ORG-wide ledger — the ruling narrowed members, not admins', () => {
// The other half of the ruling, and the half that is easy to get wrong:
// `member_default` resolves for admins too, so the member-side scope reaches
// them, and the two mechanisms that look like they already protect an admin
// are both absent on the DEFAULT `single` posture — the `viewAllRecords`
// short-circuit is withheld from `organization_admin_no_bypass` (ADR-0105
// D4), and `sys_invitation_org` is stripped as a platform tenant policy when
// org isolation is inactive (ADR-0105 D3). Only `sys_invitation_org_admin`
// survives both, which is why it must be present on BOTH variants.
for (const setName of ['organization_admin', 'organization_admin_no_bypass']) {
const scoped = policiesFor(setName, 'sys_invitation');
expect(scoped.map((p) => p.name).sort(), `${setName} sys_invitation policies`).toEqual([
'sys_invitation_org',
'sys_invitation_org_admin',
]);
const admission = scoped.find((p) => p.name === 'sys_invitation_org_admin');
// No tenant token — that is what keeps the strip from taking it.
expect(admission.using).not.toContain('current_user.organization_id');
expect(admission.using).toBe('id != null');
// Domained to the org-administration identities, so it can only WIDEN.
// A principal it does not match keeps the addressee scope and fails
// closed; putting the domain on the narrowing instead would make "no
// matching position" mean "no policy" — i.e. no row filter at all.
expect(admission.positions.sort()).toEqual(['org_admin', 'org_owner']);
expect(admission.operation).toBe('select');
}
});
/**
* [#8240] The issuer carve-out — again a TRIPWIRE, not the proof. The proof is
* the four-persona matrix in the dogfood file, which is the only place the
* ruled option and the rejected one look different over the wire.
*
* What this block adds that the HTTP matrix cannot: the matrix is blind to a
* dropped `positions` domain (measured — mutation M3 of this card's ablation
* left all four personas green, because only owner/admin/delegated_admin can
* ever be an `inviter_id` and the first two already read everything). The
* domain exists for the principal DEMOTED out of an administrative grade, a
* state no fixture in this repo constructs. So it is pinned here, structurally.
*/
it('member_default lets a delegated_admin read the invitations THEY issued — #8240, option C', () => {
const issuer = policiesFor('member_default', 'sys_invitation').find(
(p) => p.name === 'sys_invitation_issuer',
);
expect(issuer, 'member_default must carry the #8240 issuer carve-out').toBeTruthy();
// The predicate IS the ruling. Option B — `delegated_admin` reads the whole
// ledger — was REJECTED, and its shape (`id != null`, or anything else that
// does not bind a row to this caller) passes every "the delegate can see an
// invitation" assertion just as well. Written out rather than imported so
// that editing the module cannot edit the expectation with it.
expect(issuer.using).toBe('inviter_id == current_user.id');
expect(issuer.using).toContain('current_user.id');
expect(issuer.using).not.toBe('id != null');
// Scope-bounded issuance is about the ISSUING GRADE, not about everyone who
// ever held one. Without the domain a demoted ex-admin keeps a permanent
// window onto what they issued.
expect(issuer.positions).toEqual(['delegated_admin']);
// The domain must not have silently acquired the admin identities — that is
// option B wearing this policy's name, and it would widen the ledger's
// audience exactly as the ruling refused.
expect(issuer.positions).not.toContain('org_admin');
expect(issuer.positions).not.toContain('org_owner');
// Read-only, like every other policy on this object: the write classes are
// closed at the object layer, by the ADR-0092 D2 identity write guard, and
// by `apiMethods: ['get', 'list']`.
expect(issuer.operation).toBe('select');
expect(issuer.enabled).not.toBe(false);
// Owner/admin visibility is UNCHANGED by this card — the ruling said so in
// those words. Their admission still comes from `sys_invitation_org_admin`,
// which must not have been touched to make the delegate's case work.
for (const setName of ['organization_admin', 'organization_admin_no_bypass']) {
const names = policiesFor(setName, 'sys_invitation').map((p) => p.name).sort();
expect(names, `${setName} must be untouched by #8240`).toEqual([
'sys_invitation_org',
'sys_invitation_org_admin',
]);
}
});
});
/**
* [commit c25b2d52a] The `sys_comment` moderation carve-out — again a TRIPWIRE, not the
* proof. The proof is over HTTP, in
* `packages/qa/dogfood/test/comments-permission-matrix.dogfood.test.ts`, which
* boots org-bound and arms itself: an assertion whose expectation and reality
* both come from this module cannot fail, so nothing here shows that a
* moderator can actually delete anything.
*
* What this block buys is what the HTTP matrix cannot see. The matrix is blind
* to the `positions` DOMAIN — drop it and every case there stays green, because
* every principal it constructs holds `org_member` anyway. The domain exists for
* the principals the fixture does not build (an org-less session, an
* `everyone`-only anchor), where an undomained twin would carry a `using` into a
* delete class that is EMPTY today and thereby switch off #7665's
* derive-from-select rule — handing those principals a WIDER delete scope than
* they have now. So it is pinned here, structurally, next to the declaration.
*/
describe('sys_comment delete is moderation-shaped, not ownership-shaped (#8839)', () => {
const policiesFor = (setName: string, object: string): any[] =>
(setByName(setName)?.rowLevelSecurity ?? []).filter((p: any) => p.object === object);
it('member_default contributes the alternate match that stops the floor pre-empting the gate', () => {
const scoped = policiesFor('member_default', 'sys_comment');
expect(scoped.map((p) => p.name).sort()).toEqual(['sys_comment_moderation']);
const [policy] = scoped;
// The predicate IS the ruling, and its shape is deliberate: the
// parent-editor limb lives on ANOTHER record (the one `thread_id` names)
// and RLS has no join, so this policy contributes the alternate match and
// plugin-audit's gate keeps deciding. Written out rather than imported so
// that editing the module cannot edit the expectation with it.
expect(policy.using).toBe('id != null');
expect(policy.operation).toBe('delete');
expect(policy.enabled).not.toBe(false);
// The DOMAIN. Exactly the principals `owner_only_deletes` binds — no more.
expect(policy.positions).toEqual(['org_member']);
});
it('the wildcard delete floor itself is untouched — the widening is scoped to sys_comment', () => {
// ⛔ The ruling's explicit boundary. If this fails, the fix left the card:
// the floor must still be a wildcard `created_by` policy over the same
// domain, and `sys_comment` must be widened BESIDE it, never by loosening it.
const floor = (setByName('member_default').rowLevelSecurity ?? []).find(
(p: any) => p.name === 'owner_only_deletes',
);
expect(floor, 'member_default must keep the wildcard delete floor').toBeTruthy();
expect(floor.object).toBe('*');
expect(floor.operation).toBe('delete');
expect(floor.using).toBe('created_by == current_user.id');
expect(floor.positions).toEqual(['org_member']);
// The update limb is deliberately NOT widened: commit c25b2d52a's ruling is on delete, which
// is the limb it measured. A `sys_comment` update policy appearing here is a
// second access-widening riding in on this one's ruling.
expect(policiesFor('member_default', 'sys_comment').map((p) => p.operation)).toEqual(['delete']);
});
it('no OTHER set quietly re-opens sys_comment', () => {
// The carve-out belongs to the one set that ships the floor. `viewer_readonly`
// and the admin sets must not have grown a twin — an admin already bypasses
// Layer 1, and a read-only viewer holding a delete-class policy is nonsense
// that would only ever be a mistake.
for (const setName of ['viewer_readonly', 'admin_full_access', 'organization_admin', 'organization_admin_no_bypass']) {
if (!setByName(setName)) continue;
expect(policiesFor(setName, 'sys_comment'), `${setName} sys_comment policies`).toEqual([]);
}
});
});
/**
* [#22566] A comment reaction (`sys_comment_reaction`, plugin-audit) is its
* reactor's own record, so the wildcard own-record floor is the WHOLE of its
* delete rule — the opposite shape from `sys_comment` above, whose delete is
* moderation. A TRIPWIRE again: the behaviour is driven through the real
* middleware in `comment-reaction-own-record-floor.test.ts`.
*
* What it guards is the policy that would quietly undo the ruling ("a member
* cannot delete another member's reaction"): a `sys_comment_moderation`-style
* alternate match on reactions in ANY shipped set would OR a second match into
* the delete class and let every org member remove every reaction. A reaction
* policy appearing here is a decision, not a refactor.
*/
describe('sys_comment_reaction delete is ownership-shaped (#22566)', () => {
it('no shipped set carries a row-level policy on sys_comment_reaction', () => {
expect(defaultPermissionSets.length).toBeGreaterThanOrEqual(6);
for (const set of defaultPermissionSets) {
const scoped = (set.rowLevelSecurity ?? []).filter((p: any) => p.object === 'sys_comment_reaction');
expect(scoped, `${set.name} sys_comment_reaction policies`).toEqual([]);
}
});
});
/**
* [#11965 / #11663 Choice 6A] platform-admin re-anchor, L1 behaviour-neutrality
* pin.
*
* `admin_full_access`'s capability CONTENT moved to `@objectstack/spec`
* (`ADMIN_FULL_ACCESS_CAPABILITIES`) and is IMPORTED here — one list, one copy.
* L1 is ruled behaviour-neutral, so the parsed declaration must be deep-equal
* to what the previously-inline literal produced. The literal below is the
* exact pre-#11965 inline value (comments elided); if this pin fails, the spec
* export changed the declared capability set — that is a capability change
* riding on a refactor card, and it must not land silently.
*
* One capability change has landed on purpose since, on its own card, and is
* written into the literal below: `view_all_audit_log` (#21260, ruling B on
* #21175 — platform administrators hold the compliance ledger's audit
* capability by default). And one more, on #21237: the `fields` block keeping the
* identity object's `Admin` field group, which the `everyone` baseline withholds.
* And `manage_mailboxes` (ruling 6116489638, Q4 → A on the inbound-mail record —
* platform and organization administrators hold the mailbox administrator
* capability by default).
*
* The set's `description` is admin-facing copy, not capability, so it is left
* out of the comparison (its shape is pinned by
* `permission-set-descriptions.test.ts`).
*/
describe('admin_full_access imports the kernel capability declaration unchanged (#11965)', () => {
it('parsed declaration deep-equals the pre-#11965 inline literal', () => {
const preMove = PermissionSetSchema.parse({
name: 'admin_full_access',
label: 'Administrator — Full Access',
objects: {
'*': {
allowRead: true,
allowCreate: true,
allowEdit: true,
allowDelete: true,
viewAllRecords: true,
modifyAllRecords: true,
// [#8681] no `allowExport` — deliberate, see the spec declaration.
},
},
systemPermissions: [
'manage_users',
'manage_metadata',
'manage_platform_settings',
'manage_sharing',
'setup.access',
'setup.write',
'studio.access',
// [#21260] added on purpose — see the docblock above.
'view_all_audit_log',
// added on purpose — see the docblock above.
'manage_mailboxes',
],
// [#21237] added on purpose — see the docblock above. The group is read off
// the identity object's declaration, never listed here.
fields: Object.fromEntries(
Object.entries(PlatformObjects.SysUser.fields as Record<string, { group?: string }>)
.filter(([, field]) => field.group === 'Admin')
.map(([name]) => [`sys_user.${name}`, { readable: true, editable: true }]),
),
});
const { description: _copy, ...declared } = setByName('admin_full_access') as Record<string, unknown>;
expect(declared).toEqual(preMove);
});
it('[#21260] no other shipped set carries the ledger audit capability: every other position holds it only by explicit grant', () => {
const holders = (defaultPermissionSets as any[])
.filter((s) => (s.systemPermissions ?? []).includes('view_all_audit_log'))
.map((s) => s.name);
expect(holders).toEqual(['admin_full_access']);
});
it('the imported spec constant is the declaration content — no local fork', () => {
const admin = setByName('admin_full_access');
// Same values, sourced from the one spec-exported copy.
expect(admin.objects).toEqual(
PermissionSetSchema.parse({ name: 'admin_full_access', ...ADMIN_FULL_ACCESS_CAPABILITIES }).objects,
);
expect(admin.systemPermissions).toEqual(ADMIN_FULL_ACCESS_CAPABILITIES.systemPermissions);
});
});
/**
* [#14029] The managed-deny target list, pinned against an INDEPENDENT
* property instead of against itself.
*
* The old shape of this pin iterated `MANAGED_DENY_TARGET_SETS` to assert
* membership, so it was structurally unable to see a set that SHOULD have been
* a member — which is exactly how `organization_admin_no_bypass` (a shallow
* copy of `organization_admin` taken at module load, write-granting wildcard
* intact) sat outside the list while `applyManagedWriteDenies` walked it and
* skipped it at `kernel:ready`. The floor is therefore derived here from the
* REAL seeded sets — "holds a `'*'` wildcard granting any generic write
* class" — and diffed against the list; a non-empty difference is red.
*/
describe('managed-deny targets — independent-property floor + registry union reaches the derived variant (#14029)', () => {
// Derived from `defaultPermissionSets`, never from the list under test.
// "Grants a write" in the evaluator's own terms — the bits its grant routes
// read (`permission-evaluator.ts`):
// - FIRST route, a direct bit read off `OPERATION_TO_PERMISSION`: the three
// CRUD write flags, plus `allowTransfer` (`transfer: 'allowTransfer'` —
// reassigning `owner_id`; a real grant, ENFORCED today through the
// insert/update owner_id door, #3004), so
// `'*': { allowRead: true, allowTransfer: true }` is write-granting even
// with all three CRUD flags AND `modifyAllRecords` false (#14137);
// - SECOND route, the `modifyAllRecords` super-user bypass, which grants
// edit/delete and the destructive class (`MODIFY_ALL_WRITE_KEYS`), so
// `'*': { modifyAllRecords: true }` is write-granting even with all three
// CRUD flags false.
// (A census of grant routes beyond these two tables has NOT been done —
// #14137 "Not established" — so this clause list is exhaustive over the two
// known routes, not a claim about the whole evaluator.)
// Value tests (`=== true`), not key-existence: Zod materialises these bits
// with `.default(false)` (`permission.zod.ts`), so they are present-as-false
// (#14129 first review; pinned below).
const grantsWildcardWrite = (wc: any): boolean =>
wc.allowCreate === true ||
wc.allowEdit === true ||
wc.allowDelete === true ||
wc.modifyAllRecords === true ||
wc.allowTransfer === true;
// The REAL derivation under pin — also applied to synthetic sets below so
// the floor is testable against shapes no seeded set carries yet.
const deriveWriteGrantingWildcardSets = (sets: readonly any[]): string[] =>
sets
.filter((s: any) => {
const wc = s.objects?.['*'];
return !!wc && grantsWildcardWrite(wc);
})
.map((s) => s.name)
.sort();
const writeGrantingWildcardSets: string[] = deriveWriteGrantingWildcardSets(defaultPermissionSets);
/**
* The one documented exclusion: `admin_full_access` keeps its unqualified
* wildcard so an admin can rescue data directly (recorded in the
* `MANAGED_DENY_TARGET_SETS` docblock; runtime guards are its boundary).
* Pinned exactly, like `EDIT_EXCEPTIONS` above: widening it is an edit HERE,
* which is the moment a reviewer is asked why the new set may keep raw CRUD
* on identity tables.
*/
const WILDCARD_DENY_EXCLUSIONS = ['admin_full_access'];
it('the property derivation is live (found the known write-granting sets)', () => {
// If the filter silently matched nothing, the difference below would be
// vacuously empty — guard the probe itself.
expect(writeGrantingWildcardSets).toContain('organization_admin');
expect(writeGrantingWildcardSets).toContain('organization_admin_no_bypass');
expect(writeGrantingWildcardSets.length).toBeGreaterThanOrEqual(3);
});
it('every write-granting wildcard set is a managed-deny target or a documented exclusion', () => {
const missing = writeGrantingWildcardSets.filter(
(name) => !MANAGED_DENY_TARGET_SETS.includes(name) && !WILDCARD_DENY_EXCLUSIONS.includes(name),
);
expect(missing, 'write-granting sets missing from MANAGED_DENY_TARGET_SETS').toEqual([]);
});
it('the exclusion list is exactly admin_full_access, and it really is outside the target list', () => {
expect(WILDCARD_DENY_EXCLUSIONS).toEqual(['admin_full_access']);
expect([...MANAGED_DENY_TARGET_SETS]).not.toContain('admin_full_access');
});
// ── The floor judged against synthetic wildcards (#14137) ──
// `allowTransfer` is the evaluator's FIRST grant route (a direct bit read
// off `OPERATION_TO_PERMISSION`) and is ENFORCED today through the
// insert/update owner_id door (#3004): a transfer-only wildcard holds
// ownership reassignment on every managed identity table while all three
// CRUD write flags and `modifyAllRecords` are false — visible only in the
// evaluator's grant semantics, never in the flags the older clauses read.
// Every shape below goes through `PermissionSetSchema.parse` first so Zod
// materialises the `.default(false)` bits: the parsed wildcard carries the
// unauthored bits present-as-false, exactly what the seeded sets look like
// to the filter.
describe('the floor sees the evaluator first grant route — allowTransfer (#14137)', () => {
const parseProbeSet = (wildcard: Record<string, boolean>): any =>
PermissionSetSchema.parse({
name: 'synthetic_floor_probe',
label: 'Synthetic floor probe',
objects: { '*': wildcard },
});
it('a transfer-only wildcard is required to be a managed-deny target (the card)', () => {
const probe = parseProbeSet({ allowRead: true, allowTransfer: true });
const wc: any = probe.objects['*'];
// The shape really is the card's: all three CRUD write flags AND
// `modifyAllRecords` are (present-as-)false after parse.
expect(wc.allowCreate).toBe(false);
expect(wc.allowEdit).toBe(false);
expect(wc.allowDelete).toBe(false);
expect(wc.modifyAllRecords).toBe(false);
expect(wc.allowTransfer).toBe(true);
expect(deriveWriteGrantingWildcardSets([probe])).toEqual(['synthetic_floor_probe']);
});
it('reverse control: a read-only wildcard is still NOT required', () => {
const probe = parseProbeSet({ allowRead: true });
expect(deriveWriteGrantingWildcardSets([probe])).toEqual([]);
});
it('present-as-false: an explicit allowTransfer:false wildcard does not trip the floor (value test, not key-existence)', () => {
const probe = parseProbeSet({ allowRead: true, allowTransfer: false });
const wc: any = probe.objects['*'];
// The key IS present after parse — `.default(false)` materialises it —
// so a key-existence rewrite of the floor turns exactly this pin red
// (#14129 first review; not to be re-litigated).
expect('allowTransfer' in wc).toBe(true);
expect(wc.allowTransfer).toBe(false);
expect(deriveWriteGrantingWildcardSets([probe])).toEqual([]);
});
it('invariance: the allowTransfer clause changes no existing seeded set verdict (zero delta today)', () => {
// The pre-#14137 four-clause floor, restated ONLY to diff verdicts
// against: if a future seeded set legitimately relies on the
// `allowTransfer` clause, this pin goes red and the zero-delta claim is
// consciously retired — the same review moment the membership diff
// above forces.
let wildcardsSeen = 0;
for (const s of defaultPermissionSets as any[]) {
const wc = s.objects?.['*'];
if (!wc) continue;
wildcardsSeen += 1;
const preFloor =
wc.allowCreate === true ||
wc.allowEdit === true ||
wc.allowDelete === true ||
wc.modifyAllRecords === true;
expect(grantsWildcardWrite(wc), `verdict drifted for ${s.name}`).toBe(preFloor);
}
// Non-vacuousness: the loop really visited the seeded wildcards.
expect(wildcardsSeen).toBeGreaterThanOrEqual(3);
});
});
// ── The behaviour the membership buys, measured on the REAL derived set ──
// (clones so the module-level instances other tests read stay unmutated;
// the kernel path hands the same objects to the same function in place).
const FUTURE = 'sys_future_identity_table';
const DENY = { allowRead: true, allowCreate: false, allowEdit: false, allowDelete: false };
it('a managedBy:better-auth object OUTSIDE the static list now reaches the variant (the card)', () => {
const sets: any[] = structuredClone(defaultPermissionSets as any);
const variant = sets.find((s) => s.name === 'organization_admin_no_bypass');
const parent = sets.find((s) => s.name === 'organization_admin');
expect(variant.objects[FUTURE]).toBeUndefined(); // genuinely not in the compile-time baseline
applyManagedWriteDenies(sets, [{ name: FUTURE, managedBy: 'better-auth' }]);
expect(variant.objects[FUTURE]).toEqual(DENY);
// The fix ADDS a target; the parent keeps receiving its injection too.
expect(parent.objects[FUTURE]).toEqual(DENY);
});
it('reverse control: the variant pre-existing explicit entries survive the injection unchanged', () => {
const sets: any[] = structuredClone(defaultPermissionSets as any);
const variant = sets.find((s) => s.name === 'organization_admin_no_bypass');
const before = structuredClone(variant.objects);
const registry = [
...BETTER_AUTH_MANAGED_OBJECTS.map((n) => ({ name: n, managedBy: 'better-auth' })),
{ name: FUTURE, managedBy: 'better-auth' },
];
applyManagedWriteDenies(sets, registry);
for (const name of BETTER_AUTH_MANAGED_OBJECTS) {
expect(variant.objects[name], `variant entry ${name}`).toEqual(before[name]);
}
// Wildcard and the anti-escalation RBAC read-only block untouched as well.
expect(variant.objects['*']).toEqual(before['*']);
expect(variant.objects.sys_position).toEqual(before.sys_position);
// Only the future table was new on the variant.
expect(variant.objects[FUTURE]).toEqual(DENY);
expect(Object.keys(variant.objects).sort()).toEqual([...Object.keys(before), FUTURE].sort());
});
it('control: admin_full_access is untouched by the injection (admin rescue path)', () => {
const sets: any[] = structuredClone(defaultPermissionSets as any);
const admin = sets.find((s) => s.name === 'admin_full_access');
const before = structuredClone(admin);
applyManagedWriteDenies(sets, [{ name: FUTURE, managedBy: 'better-auth' }]);
expect(admin).toEqual(before);
});
});