-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdefault-permission-sets.ts
More file actions
536 lines (532 loc) · 18.4 KB
/
Copy pathdefault-permission-sets.ts
File metadata and controls
536 lines (532 loc) · 18.4 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
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { PermissionSetSchema, type PermissionSet } from '@objectstack/spec/security';
/**
* 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.
*
* Each entry mirrors the `managedBy: 'better-auth'` flag declared on
* the corresponding object schema in `packages/platform-objects/src/identity/`.
*/
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_oauth_application',
'sys_oauth_access_token',
'sys_oauth_refresh_token',
'sys_oauth_consent',
] 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_role_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',
isProfile: true,
objects: {
'*': {
allowRead: true,
allowCreate: true,
allowEdit: true,
allowDelete: true,
viewAllRecords: true,
modifyAllRecords: true,
},
},
systemPermissions: [
'manage_users',
'manage_metadata',
'manage_platform_settings',
'setup.access',
'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 `tenant_isolation` RLS), 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_role`, `sys_permission_set`, `sys_role_permission_set`,
// `sys_user_permission_set`, `sys_user_role`) 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',
isProfile: true,
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_role: { allowRead: true, allowCreate: false, allowEdit: false, allowDelete: false },
sys_permission_set: { allowRead: true, allowCreate: false, allowEdit: false, allowDelete: false },
sys_role_permission_set: { allowRead: true, allowCreate: false, allowEdit: false, allowDelete: false },
sys_user_permission_set: { allowRead: true, allowCreate: false, allowEdit: false, allowDelete: false },
sys_user_role: { allowRead: true, allowCreate: false, allowEdit: false, allowDelete: false },
},
systemPermissions: ['manage_org_users', 'setup.access'],
rowLevelSecurity: [
{
name: 'tenant_isolation',
object: '*',
operation: 'all',
using: 'organization_id = current_user.organization_id',
},
// ── 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` so the wildcard
// `tenant_isolation` policy would otherwise deny every row.
{
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',
isProfile: true,
objects: {
'*': {
allowRead: true,
allowCreate: true,
allowEdit: true,
allowDelete: true,
},
// Identity tables are managed by better-auth — no direct writes.
...denyWritesOnManagedObjects(),
},
rowLevelSecurity: [
{
name: 'tenant_isolation',
object: '*',
operation: 'all',
using: 'organization_id = current_user.organization_id',
},
// 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.
{
name: 'owner_only_writes',
object: '*',
operation: 'update',
using: 'created_by = current_user.id',
},
{
name: 'owner_only_deletes',
object: '*',
operation: 'delete',
using: 'created_by = current_user.id',
},
// ── 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`, so without this carve-out the wildcard
// `tenant_isolation` policy returns zero rows even for the owner.
{
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',
isProfile: true,
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: [
{
name: 'tenant_isolation',
object: '*',
operation: 'select',
using: 'organization_id = current_user.organization_id',
},
{
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',
},
],
}),
];