-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdefault-permission-sets.ts
More file actions
605 lines (600 loc) · 23 KB
/
Copy pathdefault-permission-sets.ts
File metadata and controls
605 lines (600 loc) · 23 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
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { PermissionSetSchema, type PermissionSet } from '@objectstack/spec/security';
import {
MCP_AGENT_PERMISSION_SET_READ,
MCP_AGENT_PERMISSION_SET_WRITE,
MCP_AGENT_PERMISSION_SET_RESTRICTED,
} from '@objectstack/spec/ai';
/**
* Identity tables managed by the better-auth plugin (see
* `packages/platform-objects/src/identity/`). Mutations to these tables
* MUST flow through the better-auth API endpoints (sign-up, password
* reset, organization invite/remove-member, api-key/create, …) rather
* than the generic CRUD pipeline so that password hashing, token
* signing, email verification, invitation flows and scope hashing all
* fire correctly.
*
* The default member/viewer permission sets therefore explicitly DENY
* `allowCreate / allowEdit / allowDelete` on these objects while still
* permitting reads (subject to the rest of the RLS chain). Admin
* permission sets keep their `*` wildcard so they can rescue data
* directly when needed.
*
* This is the COMPILE-TIME BASELINE. At `kernel:ready` it is unioned with the
* live registry by `applyManagedWriteDenies` (see `managed-object-write-denies.ts`),
* which injects a deny entry for every registered `managedBy: 'better-auth'`
* object the baseline missed — so a newly-declared identity table is covered
* automatically without editing this list. The baseline still matters: it covers
* the pre-`kernel:ready` window and hook-less test-stub kernels where the
* registry may be empty. `objects/default-permission-sets.test.ts` pins this
* list bidirectionally against the `@objectstack/platform-objects` schemas so it
* cannot silently rot again (the drift that motivated ADR-0092's registry-driven
* rule).
*/
export const BETTER_AUTH_MANAGED_OBJECTS = [
'sys_user',
'sys_account',
'sys_session',
'sys_organization',
'sys_member',
'sys_invitation',
'sys_team',
'sys_team_member',
'sys_api_key',
'sys_two_factor',
'sys_verification',
'sys_jwks',
'sys_device_code',
'sys_scim_provider',
'sys_sso_provider',
'sys_oauth_application',
'sys_oauth_access_token',
'sys_oauth_refresh_token',
'sys_oauth_consent',
'sys_oauth_resource',
'sys_oauth_client_resource',
'sys_oauth_client_assertion',
] as const;
const denyWritesOnManagedObjects = (): Record<string, {
allowRead: boolean;
allowCreate: boolean;
allowEdit: boolean;
allowDelete: boolean;
}> => Object.fromEntries(
BETTER_AUTH_MANAGED_OBJECTS.map((name) => [
name,
{ allowRead: true, allowCreate: false, allowEdit: false, allowDelete: false },
]),
);
/**
* Default permission sets seeded by the platform.
*
* These are referenced by name (`admin_full_access`, `member_default`,
* `viewer_readonly`) from `sys_position_permission_set` rows or assigned
* directly to users via `sys_user_permission_set`.
*
* The runtime SecurityPlugin reads these via the metadata service when a
* permission set name appears in the request `ExecutionContext.permissions[]`.
*
* Each entry is run through `PermissionSetSchema.parse(...)` so Zod fills
* in the boolean/`priority`/`enabled` defaults — keeping the literal
* source readable while still satisfying the strict output type.
*
* `objects: { '*': … }` uses the wildcard sentinel honoured by
* `PermissionEvaluator` — admins do not need an explicit row per object.
* Per-object entries fully override the wildcard for that object (see
* `PermissionEvaluator.checkObjectPermission` — lookup, not merge).
*
* RLS policies use the canonical `current_user.*` placeholders compiled
* by `RLSCompiler`. The active organization is exposed under
* `current_user.organization_id` (sourced from
* `ExecutionContext.tenantId` at request time) — there is no rewrite
* step or `tenantField` indirection in SecurityPlugin. Schemas with a
* different physical tenant column should fork these defaults.
*/
export const defaultPermissionSets: PermissionSet[] = [
PermissionSetSchema.parse({
name: 'admin_full_access',
label: 'Administrator — Full Access',
objects: {
'*': {
allowRead: true,
allowCreate: true,
allowEdit: true,
allowDelete: true,
viewAllRecords: true,
modifyAllRecords: true,
},
},
systemPermissions: [
'manage_users',
'manage_metadata',
'manage_platform_settings',
'setup.access',
'setup.write',
'studio.access',
],
}),
// ── Organization Administrator ──────────────────────────────────────
//
// Third tier between platform admin (`admin_full_access`) and rank-and-file
// member. Lives at the *organization* scope: full CRUD on business
// objects within their org (governed by the Layer 0 tenant wall, ADR-0095 D1), plus
// `setup.access` so the Setup app shell is reachable.
//
// **Deliberately withheld** vs `admin_full_access`:
// - `studio.access` — schema-design surfaces are platform-level (a
// tenant cannot mutate the shared metadata) and Studio is hidden.
// - `manage_metadata` — same reasoning.
// - `manage_platform_settings` — global settings manifests
// (mail / storage / AI / knowledge) and platform-only Setup pages
// (sharing rules, audit logs, OAuth apps, JWKS, …) require this
// and are hidden / 403'd for org admins. Tenant-scoped manifests
// (`branding`, `feature_flags`) keep using `setup.access` so org
// admins CAN configure their own org's branding.
//
// **Anti-escalation**: writes to the global RBAC tables
// (`sys_position`, `sys_permission_set`, `sys_position_permission_set`,
// `sys_user_permission_set`, `sys_user_position`) are denied. Allowing
// them would let an org admin bind `admin_full_access` (which has no
// RLS) to themselves and break out of tenant isolation. Reads are
// permitted so the Roles / Permission Sets nav entries still render.
//
// Auto-granted to every `sys_member` whose role contains `owner` or
// `admin` by `plugin-security/src/auto-org-admin-grant.ts`.
PermissionSetSchema.parse({
name: 'organization_admin',
label: 'Organization Administrator',
objects: {
'*': {
allowRead: true,
allowCreate: true,
allowEdit: true,
allowDelete: true,
viewAllRecords: true,
modifyAllRecords: true,
},
// Identity tables — go through better-auth endpoints (invite,
// accept, remove-member, transfer, …) rather than raw CRUD.
...denyWritesOnManagedObjects(),
// RBAC tables — read-only to prevent privilege escalation.
sys_position: { allowRead: true, allowCreate: false, allowEdit: false, allowDelete: false },
sys_permission_set: { allowRead: true, allowCreate: false, allowEdit: false, allowDelete: false },
sys_position_permission_set: { allowRead: true, allowCreate: false, allowEdit: false, allowDelete: false },
sys_user_permission_set: { allowRead: true, allowCreate: false, allowEdit: false, allowDelete: false },
sys_user_position: { allowRead: true, allowCreate: false, allowEdit: false, allowDelete: false },
},
systemPermissions: ['manage_org_users', 'setup.access', 'setup.write'],
rowLevelSecurity: [
// [ADR-0095 D1] The wildcard `tenant_isolation` policy RETIRED here — the
// tenant wall is now Layer 0 (`tenant-layer.ts`), AND-composed ahead of and
// independently of business RLS. Keeping it as an OR-merged RLS policy is
// what let a permissive business policy widen tenant scope (W1). The
// per-object `_org` / `_self` carve-outs below are NOT tenant walls — they
// are identity-table scoping and stay.
// ── better-auth system tables that lack `organization_id` and would
// otherwise be denied by the wildcard policy. Same self-only
// carve-outs as `member_default` — an org admin does not get to
// inspect cross-tenant identity rows.
{
name: 'sys_organization_self',
object: 'sys_organization',
operation: 'all',
using: 'id == current_user.organization_id',
},
{
name: 'sys_user_self',
object: 'sys_user',
operation: 'select',
using: 'id == current_user.id',
},
{
name: 'sys_user_org_members',
object: 'sys_user',
operation: 'select',
using: 'id in current_user.org_user_ids',
},
{
name: 'sys_session_self',
object: 'sys_session',
operation: 'all',
using: 'user_id == current_user.id',
},
{
name: 'sys_account_self',
object: 'sys_account',
operation: 'select',
using: 'user_id == current_user.id',
},
{
name: 'sys_team_member_self',
object: 'sys_team_member',
operation: 'select',
using: 'user_id == current_user.id',
},
{
name: 'sys_two_factor_self',
object: 'sys_two_factor',
operation: 'all',
using: 'user_id == current_user.id',
},
{
name: 'sys_user_preference_self',
object: 'sys_user_preference',
operation: 'all',
using: 'user_id == current_user.id',
},
{
name: 'sys_api_key_self',
object: 'sys_api_key',
operation: 'all',
using: 'user_id == current_user.id',
},
{
name: 'sys_device_code_self',
object: 'sys_device_code',
operation: 'all',
using: 'user_id == current_user.id',
},
{
name: 'sys_oauth_access_token_self',
object: 'sys_oauth_access_token',
operation: 'select',
using: 'user_id == current_user.id',
},
{
name: 'sys_oauth_refresh_token_self',
object: 'sys_oauth_refresh_token',
operation: 'select',
using: 'user_id == current_user.id',
},
{
name: 'sys_oauth_consent_self',
object: 'sys_oauth_consent',
operation: 'all',
using: 'user_id == current_user.id',
},
// OAuth applications a user has registered themselves (self-service
// developer flow exposed in the Account app's Developer section).
// `sys_oauth_application` has no `organization_id`; this `_self` carve-out
// is its Layer 1 scoping (Layer 0 is inert on a non-tenant object).
{
name: 'sys_oauth_application_self',
object: 'sys_oauth_application',
operation: 'all',
using: 'user_id == current_user.id',
},
// Org-scoped visibility for organization-owned identity-adjacent
// tables. Org admins may inspect their own org's invitations and
// memberships (read; writes still flow through better-auth).
{
name: 'sys_member_org',
object: 'sys_member',
operation: 'select',
using: 'organization_id == current_user.organization_id',
},
{
name: 'sys_invitation_org',
object: 'sys_invitation',
operation: 'select',
using: 'organization_id == current_user.organization_id',
},
{
name: 'sys_team_org',
object: 'sys_team',
operation: 'select',
using: 'organization_id == current_user.organization_id',
},
],
}),
PermissionSetSchema.parse({
name: 'member_default',
label: 'Member — Standard Access',
objects: {
// [ADR-0090 D5, #2753] NO `allowDelete`: delete/purge/transfer are
// anchor-forbidden bits, and this set IS the `everyone` baseline — the
// bootstrap binds it to the anchor, so it must stay anchor-safe.
// Deleting records is not a baseline right; grant it per object via an
// ordinary (position-distributed) set where the domain calls for it.
// The owner-scoped delete RLS below is KEPT as a narrowing defense for
// members who receive a delete bit from such a set.
'*': {
allowRead: true,
allowCreate: true,
allowEdit: true,
},
// Identity tables are managed by better-auth — no direct writes.
...denyWritesOnManagedObjects(),
},
rowLevelSecurity: [
// [ADR-0095 D1] The wildcard `tenant_isolation` policy RETIRED here — the
// tenant wall is now Layer 0 (`tenant-layer.ts`). Its old OR-merge with the
// owner-scoped policies below is exactly what widened a member's by-id write
// back to org-wide (W1's write-side twin); Layer 0 now AND-composes the
// tenant scope, so `owner_only_writes/deletes` finally narrow as intended.
// Owner-scoped writes/deletes for rank-and-file members: you may modify
// and delete the records you created, not other users'. Keyed on
// `created_by` — the column the engine stamps on EVERY record — rather
// than `owner_id`, which author-defined objects almost never declare. The
// old `owner_id` key referenced a missing column on real objects, so
// `computeRlsFilter` dropped the policy and the scoping silently no-op'd
// (any member could edit/delete any record — #1985). These policies are
// ENFORCED on writes via the security middleware's pre-image check (a
// by-id update/delete never builds an RLS `where`, so the predicate is
// verified against the target row before the mutation). Objects that
// model transferable ownership with a dedicated owner field should
// override these with a per-object policy.
// [ADR-0090 P2] Applicability domain made EXPLICIT: with the baseline
// resolving additively for every authenticated principal (the
// `everyone` anchor — no more fallback cliff), these members-only
// write restrictions must say who they bind. `org_member` is the
// rank-and-file membership identity; org admins/owners and platform
// admins are outside the domain, matching the pre-anchor behavior
// where they simply never resolved this set.
{
name: 'owner_only_writes',
object: '*',
operation: 'update',
using: 'created_by == current_user.id',
positions: ['org_member'],
},
{
name: 'owner_only_deletes',
object: '*',
operation: 'delete',
using: 'created_by == current_user.id',
positions: ['org_member'],
},
// ── better-auth system tables that lack `organization_id` and would
// otherwise be left unprotected by the wildcard rule above. ────
//
// The security plugin's RLS injector treats wildcard policies that
// target a missing field as `RLS_DENY_FILTER` (zero rows) unless a
// per-object policy contributes an alternate match. Each `*_self`
// policy below restores per-user visibility on a better-auth table
// that has `user_id` but no `organization_id`. Tables without
// `user_id` (`sys_verification`, `sys_jwks`, empty `sys_passkey`)
// stay DENY for non-admins by design — only platform admins (via
// `admin_full_access`, which has no RLS) should inspect them.
{
name: 'sys_organization_self',
object: 'sys_organization',
operation: 'all',
using: 'id == current_user.organization_id',
},
{
name: 'sys_user_self',
object: 'sys_user',
operation: 'select',
using: 'id == current_user.id',
},
// Org collaborators: members can see other users in the same
// organization. Without this, owner/assignee lookups, @-mention
// suggestions, reviewer pickers and team-roster surfaces all
// collapse to just the current user. `org_user_ids` is
// pre-resolved by runtime/resolve-execution-context from
// `sys_member` for the active organization. Sensitive credential
// tables (`sys_account`, `sys_session`, `sys_api_key`, …) keep
// their stricter self-only carve-outs above.
{
name: 'sys_user_org_members',
object: 'sys_user',
operation: 'select',
using: 'id in current_user.org_user_ids',
},
{
name: 'sys_session_self',
object: 'sys_session',
operation: 'all',
using: 'user_id == current_user.id',
},
{
name: 'sys_account_self',
object: 'sys_account',
operation: 'select',
using: 'user_id == current_user.id',
},
{
name: 'sys_team_member_self',
object: 'sys_team_member',
operation: 'select',
using: 'user_id == current_user.id',
},
{
name: 'sys_two_factor_self',
object: 'sys_two_factor',
operation: 'all',
using: 'user_id == current_user.id',
},
{
name: 'sys_user_preference_self',
object: 'sys_user_preference',
operation: 'all',
using: 'user_id == current_user.id',
},
{
name: 'sys_api_key_self',
object: 'sys_api_key',
operation: 'all',
using: 'user_id == current_user.id',
},
{
name: 'sys_device_code_self',
object: 'sys_device_code',
operation: 'all',
using: 'user_id == current_user.id',
},
{
name: 'sys_oauth_access_token_self',
object: 'sys_oauth_access_token',
operation: 'select',
using: 'user_id == current_user.id',
},
{
name: 'sys_oauth_refresh_token_self',
object: 'sys_oauth_refresh_token',
operation: 'select',
using: 'user_id == current_user.id',
},
{
name: 'sys_oauth_consent_self',
object: 'sys_oauth_consent',
operation: 'all',
using: 'user_id == current_user.id',
},
// OAuth applications a user has registered themselves (Account →
// Developer → OAuth Applications). `sys_oauth_application` has no
// `organization_id`; this `_self` carve-out is its Layer 1 scoping
// (Layer 0 is inert on a non-tenant object).
{
name: 'sys_oauth_application_self',
object: 'sys_oauth_application',
operation: 'all',
using: 'user_id == current_user.id',
},
],
}),
PermissionSetSchema.parse({
name: 'viewer_readonly',
label: 'Viewer — Read-Only',
objects: {
'*': {
allowRead: true,
allowCreate: false,
allowEdit: false,
allowDelete: false,
},
// Belt-and-suspenders: explicit deny on managed objects even though
// the wildcard already denies — keeps the policy readable when
// future relaxations might widen the wildcard.
...denyWritesOnManagedObjects(),
},
rowLevelSecurity: [
// [ADR-0095 D1] The wildcard `tenant_isolation` policy RETIRED here — the
// tenant wall is now Layer 0 (`tenant-layer.ts`). The `_self` carve-outs
// below are identity-table scoping, not tenant walls, and stay.
{
name: 'sys_organization_self',
object: 'sys_organization',
operation: 'select',
using: 'id == current_user.organization_id',
},
{
name: 'sys_user_self',
object: 'sys_user',
operation: 'select',
using: 'id == current_user.id',
},
// Org collaborators (read-only): see `sys_user_org_members` in
// `member_default` for rationale.
{
name: 'sys_user_org_members',
object: 'sys_user',
operation: 'select',
using: 'id in current_user.org_user_ids',
},
{
name: 'sys_session_self',
object: 'sys_session',
operation: 'select',
using: 'user_id == current_user.id',
},
{
name: 'sys_account_self',
object: 'sys_account',
operation: 'select',
using: 'user_id == current_user.id',
},
{
name: 'sys_team_member_self',
object: 'sys_team_member',
operation: 'select',
using: 'user_id == current_user.id',
},
{
name: 'sys_two_factor_self',
object: 'sys_two_factor',
operation: 'select',
using: 'user_id == current_user.id',
},
{
name: 'sys_user_preference_self',
object: 'sys_user_preference',
operation: 'select',
using: 'user_id == current_user.id',
},
{
name: 'sys_api_key_self',
object: 'sys_api_key',
operation: 'select',
using: 'user_id == current_user.id',
},
{
name: 'sys_device_code_self',
object: 'sys_device_code',
operation: 'select',
using: 'user_id == current_user.id',
},
{
name: 'sys_oauth_access_token_self',
object: 'sys_oauth_access_token',
operation: 'select',
using: 'user_id == current_user.id',
},
{
name: 'sys_oauth_refresh_token_self',
object: 'sys_oauth_refresh_token',
operation: 'select',
using: 'user_id == current_user.id',
},
{
name: 'sys_oauth_consent_self',
object: 'sys_oauth_consent',
operation: 'select',
using: 'user_id == current_user.id',
},
],
}),
// ── [ADR-0090 D10] MCP agent ceiling sets ────────────────────────────────
// The capability ceiling an OAuth-authenticated MCP agent runs under,
// derived from the token's consented scopes (see
// `scopesToAgentPermissionSets`). These are ONE SIDE of the D10 intersection:
// the delegating user's own sets provide all row/owner/tenant narrowing, so
// these carry pure CRUD bits and NO row-level security. They are never bound
// to a position or an audience anchor — the producer
// (`resolve-execution-context`) injects them onto the agent principal's
// context directly — so the anchor high-privilege gate does not apply.
PermissionSetSchema.parse({
name: MCP_AGENT_PERMISSION_SET_READ,
label: 'MCP Agent — Read Only',
description:
'Read-only ceiling for an AI agent acting on behalf of a user (OAuth `data:read`). ' +
'Bounded by the delegating user via the ADR-0090 D10 intersection.',
objects: {
'*': { allowRead: true },
},
}),
PermissionSetSchema.parse({
name: MCP_AGENT_PERMISSION_SET_WRITE,
label: 'MCP Agent — Read & Write',
description:
'Read+write ceiling for an AI agent acting on behalf of a user (OAuth `data:write`). ' +
'Full CRUD, still bounded by the delegating user via the ADR-0090 D10 intersection. ' +
'Identity tables stay read-only (better-auth managed).',
objects: {
'*': { allowRead: true, allowCreate: true, allowEdit: true, allowDelete: true },
// Even a write-scoped agent must not mutate better-auth identity tables
// directly — a belt to the intersection's braces (the user's baseline
// already denies these, but an admin delegator would not).
...denyWritesOnManagedObjects(),
},
}),
PermissionSetSchema.parse({
name: MCP_AGENT_PERMISSION_SET_RESTRICTED,
label: 'MCP Agent — No Data Access',
description:
'No-object-access floor for an agent with no data scope (e.g. `actions:execute` only). ' +
'Keeps the resolved set list non-empty so enforcement fails CLOSED, never open.',
objects: {},
}),
];