Repository navigation
Expand file tree
/
Copy pathcapabilities.ts
More file actions
266 lines (257 loc) · 17.5 KB
/
Copy pathcapabilities.ts
File metadata and controls
266 lines (257 loc) · 17.5 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
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
import { z } from 'zod';
import { lazySchema } from '../shared/lazy-schema';
import { MetadataProtectionFields } from '../kernel/metadata-protection.zod';
import { strictObject } from '../shared/strict-object';
/**
* [ADR-0066 D1] Canonical platform capability registry.
*
* The built-in authorization capabilities the framework ships, as first-class
* definitions (name / label / description / scope). This module is the SINGLE
* SOURCE OF TRUTH consumed by:
* - `@objectstack/plugin-security` `registerBuiltinCapabilities`, which
* registers these as `capability` metadata items at boot (ADR-0131 D3), and
* - the authoring lint `validateCapabilityReferences` (ADR-0066 ⑨), which
* resolves capability references (`requiredPermissions`) against these names
* plus any a stack declares via a permission set's `systemPermissions`.
*
* Keeping the list here (in the contract package, which everything depends on)
* avoids a spec→plugin dependency inversion and keeps the registry and the lint
* from drifting apart.
*/
export interface PlatformCapability {
/** Stable capability name referenced by `systemPermissions` / `requiredPermissions`. */
name: string;
/** Human label shown in Setup. */
label: string;
/** What holding the capability permits. */
description: string;
/** `platform` = global; `org` = scoped to the caller's organization. */
scope: 'platform' | 'org';
}
/**
* The curated built-in capabilities. Back-compat: string references to any of
* these keep resolving because they are seeded as records with the same `name`.
*/
export const PLATFORM_CAPABILITIES: readonly PlatformCapability[] = [
{ name: 'manage_users', label: 'Manage Users', description: 'Create, edit, and deactivate users across the platform.', scope: 'platform' },
{ name: 'manage_org_users', label: 'Manage Organization Users', description: 'Manage members within the caller’s organization.', scope: 'org' },
{ name: 'manage_metadata', label: 'Manage Metadata', description: 'Author and publish object/view/flow and other metadata.', scope: 'platform' },
// [ADR-0131 D6] `manage_org_presentation` (#12702) retired with the
// per-organization overlay axis: no `/meta` door threads an organization into
// a metadata write, so the org-scoped authoring it admitted no longer exists
// (ADR-0087 entry `manage-org-presentation-retired`).
{ name: 'manage_platform_settings', label: 'Manage Platform Settings', description: 'Configure global platform settings (mail, storage, AI, licensing, …) and platform-only Setup pages.', scope: 'platform' },
{ name: 'setup.access', label: 'Setup Access', description: 'Enter the Setup app shell.', scope: 'platform' },
// [Finding-1] The write counterpart to `setup.access`: saving changes to
// tenant/Setup settings pages (branding, company, localization, feature
// flags). Previously referenced by settings manifests but never declared or
// granted — which was harmless only while settings writes went ungated.
{ name: 'setup.write', label: 'Write Settings', description: 'Save changes to tenant/Setup settings pages.', scope: 'org' },
{ name: 'studio.access', label: 'App Studio Access', description: 'Enter the App Studio metadata-design surfaces.', scope: 'platform' },
// [ADR-0111 D9] Sharing administration right-sized below full platform admin:
// gates the sharing-rule surface (define / delete / evaluate — an org-wide
// grant generator) and, in the DEPTH extension, the non-owner path to
// per-record share management. Salesforce ships the analogous standalone
// "Manage Sharing" permission. Seeded into `admin_full_access` so existing
// admin flows are unchanged; `manage_platform_settings` is honoured as a
// legacy equivalent by the enforcement seams that predate this capability.
{ name: 'manage_sharing', label: 'Manage Sharing', description: 'Administer record sharing: author and evaluate sharing rules, and manage per-record shares beyond one’s own records.', scope: 'org' },
// [#21260, ruling B on #21175] The compliance ledger's audit capability. A
// read of `sys_audit_log` takes the parent-record read gate (a row about a
// record is served only to a caller who can read that record), so without
// it no non-system caller is served the deletion and sign-out trail, and a
// broad read fails closed at the gate's pre-scan bound. Its holder is exempt
// from that gate — and from it alone: the ledger's object grant, its other
// read seams and field-level security still apply. The activity stream's
// gate does not honour it. Held by default by platform administrators
// (`ADMIN_FULL_ACCESS_CAPABILITIES`); every other position only by explicit
// grant. `org`, measured rather than assumed: the ledger carries no
// organization column (ADR-0131 D7); its writers stamp the organization a
// row is about in the attribution field `tenant_id`, and under a
// wall-enforcing tenancy posture the platform row policy on that field
// (`sys_audit_log_org`, plugin-security's default sets) still bounds a holder
// to its own organization's rows (only the parent-record gate is lifted). A
// platform administrator reaches every row, deployment-level rows included,
// through its superuser read bypass, not through this capability; the
// deployment-level rows (no `tenant_id`) are served, in every posture, only
// to a request that carries the `PLATFORM_ADMIN` posture rung (#26750,
// plugin-audit's `audit-log-read-visibility.ts`).
{ name: 'view_all_audit_log', label: 'View All Audit Log', description: 'Read every compliance-ledger (sys_audit_log) row the holder’s ledger grant and row scope reach, past the parent-record read gate: rows about deleted records, ended sessions and records the holder cannot open. Under an organization wall an organization reader’s row scope is the rows about its organization (tenant_id); a platform administrator reads every row, deployment-level rows included. Under the single tenancy posture too, where no wall is enforced, deployment-level rows (no tenant_id) are served only to the platform administrator. Field-level security still narrows each row’s before/after snapshots.', scope: 'org' },
// The mailbox administrator capability (ruling 6116489638, Q4 → A on the
// inbound-mail record). A read of a mirrored inbound message
// (`sys_mail_message`, `@objectstack/service-mailbox`) takes a read gate: a
// message is served to a caller who can read a record it is associated with,
// or who owns the mailbox it arrived in. A message nothing matched (the
// unassociated pool) has no record to read, so it is RESTRICTED by default
// to its mailbox's owner and to this capability's holder. Its holder is
// exempt from the message gate and the association gate, and is the one
// caller the mailbox directory (`sys_mailbox`) serves; the object grants and
// field-level security still apply. Held by default by platform
// administrators (`ADMIN_FULL_ACCESS_CAPABILITIES`) and by organization
// administrators; every other position only by explicit grant. `org`: every
// row it reaches carries the organization column, so the organization wall
// still bounds a holder to its own organization's mail.
{ name: 'manage_mailboxes', label: 'Manage Mailboxes', description: 'Administer inbound mailboxes: read the mailbox directory, and read every mirrored inbound message and its record associations in the holder’s organization past the mail read gates, the unassociated pool (messages no record matched) included. Without it a message is served only to its mailbox’s owner and to a caller who can read a record it is associated with.', scope: 'org' },
// [ADR-0143 D5] The connector connection capability. Connecting a vendor
// account to an `oauth2` `authorization_code` connector instance is an
// organization act (Q1 = A: one connection per instance per organization),
// so the door that reads, begins, completes and revokes that connection
// (`/automation/connectors/:name/connection` and the OAuth callback, in
// `@objectstack/runtime`) asks this capability and nothing else — never the
// platform-scoped `manage_metadata`. Held by default by platform
// administrators (`ADMIN_FULL_ACCESS_CAPABILITIES`) and by organization
// administrators (the shipped `organization_admin` and
// `organization_admin_no_bypass` sets, ruling B, record 6545223261); every
// other position only by explicit grant. `org`: the door acts on the
// caller's own organization's connection, and the holder never sees a
// token.
{ name: 'manage_connector_connections', label: 'Manage Connector Connections', description: 'Connect, reconnect and revoke the holder’s organization’s connection of an OAuth2 connector instance, and read its status. A connection authorizes the platform to call the vendor on the organization’s behalf; no token is ever shown to the holder.', scope: 'org' },
// [ADR-0161 K4, guard one; ruling Q2 = B on #37304] The knowledge source
// capability. Saving a `knowledge_source` sends the fields it lists, for
// every record it selects, to the embedding provider, and the first build is
// billed — an act of another weight than editing a layout. So every door
// that creates, changes or removes an item of that type (Studio and REST
// `/meta`, the dispatcher, the MCP draft door), and every write door of an
// existing package that holds one (`/packages/:id/…`), asks this capability
// BESIDE `manage_metadata`, never instead of it. The install door is not
// asked: a packaged source arrives by that door's own admission. Nor is the
// ensure-customization door, which writes nothing of the package it names
// and creates an empty pair. Held by default by platform
// administrators only (`ADMIN_FULL_ACCESS_CAPABILITIES`); every other
// position, organization administrators included, only by explicit grant.
// A grant on the indexed object's records does not imply it. `platform`:
// the item is environment metadata, as `manage_metadata`'s subjects are.
{ name: 'manage_knowledge_sources', label: 'Manage Knowledge Sources', description: 'Create, change and remove knowledge sources, together with Manage Metadata. A knowledge source sends the fields it lists, for every record it selects, to the embedding provider and starts an index build that may be billed.', scope: 'platform' },
];
/** Set of built-in capability names, for fast membership checks (lint, gating). */
export const PLATFORM_CAPABILITY_NAMES: ReadonlySet<string> = new Set(
PLATFORM_CAPABILITIES.map((c) => c.name),
);
/**
* [ADR-0066 D1] PACKAGE-LEVEL capability DECLARATION.
*
* The formal entry point for a package/app to DEFINE an authorization
* capability of its own — the counterpart, on the declaration side, of the
* curated {@link PLATFORM_CAPABILITIES}. Authored via {@link defineCapability}
* and collected on a stack's `capabilities` array, these declarations flow
* EXPLICITLY into the registry at boot under their owning package (ADR-0131
* D3); no `sys_capability` row is written for them.
*
* This replaces the old implicit "derive an untitled capability from whatever a
* permission set happens to reference in `systemPermissions[]`" back-door: a
* package now says what capabilities it owns, with a real label/description,
* and the registry can attribute + uninstall them. The implicit derivation
* still runs for back-compat (references with no declaration keep resolving),
* but an EXPLICIT declaration takes precedence and carries package provenance.
*
* NOTE — a capability is NOT a contract: resources REFERENCE a capability by
* name via `requiredPermissions` (the requirement side); packages DEFINE one
* here (the declaration side); permission sets GRANT one via `systemPermissions`
* (the assignment side). There is no `inputs` shape — see ADR-0066's three-way
* separation (capability / assignment / requirement).
*
* ## [#5961] This is also the REGISTERED schema for the `capability` metadata
* kind
*
* `capability` joined `MetadataTypeSchema` / `BUILTIN_METADATA_TYPE_SCHEMAS` /
* `DEFAULT_METADATA_TYPE_REGISTRY` under the template #5271 set for `api`, so
* this shape is what `PUT /api/v1/meta/capability/:name` is judged against and
* what `/meta/types` publishes as the kind's JSON Schema. Two consequences the
* declaration side did not have before:
*
* - **The ADR-0010 protection envelope is declared.** `MetadataPlugin`'s
* loader calls `applyProtection` on every registered type, so a stored
* capability carries `_packageId` / `_provenance` / `_lock*`. A registered
* schema that cannot represent them either 422s the loader's own output or
* drops it on round-trip (`metadata-type-schemas.test.ts` asserts both).
* - **The shape is `.strict()`.** Unlike `api` (which is on that suite's
* `STILL_STRIP` list because `ApiEndpointSchema` doubles as the parser for
* STORED rows carrying `packageId` / `state`), nothing re-parses a stored
* `sys_capability` row through this schema — no row is written for a
* declared capability (ADR-0131 D3). So closing it
* costs nothing and buys the declared = enforced posture an AUTHORIZATION
* surface should have had from the start.
*/
export const CapabilityDeclarationSchema = lazySchema(() => strictObject({
surface: 'this capability declaration',
// No `CAPABILITY_KEYS` array: `strictObject` derives the known-key list from
// the shape below, so there is no second copy to drift and no probe test
// needed to catch the drift (#5483, and the ratchet in
// `alias-integrity.test.ts` that keeps hand-transcribed tables shrinking).
aliases: { key: 'name', title: 'label' },
guidance: {
permissionSets:
'`permissionSets` is not a capability field — a capability is only the ' +
'DECLARATION (ADR-0066 D1). Permission sets GRANT it from their side via ' +
'`systemPermissions`, which is the assignment edge; a capability never ' +
'names its own holders.',
requiredPermissions:
'`requiredPermissions` is not a capability field — that is the REQUIREMENT ' +
'side, authored on the resource (action / view / api) that needs the ' +
'capability. Declaring it here would invert ADR-0066\'s three-way separation.',
inputs:
'`inputs` is not a capability field — a capability is a name, not a ' +
'contract (ADR-0066: capability / assignment / requirement are three ' +
'separate shapes, and only the requirement side carries arguments).',
},
history:
'Until this shape was closed, `capability` resolved no registered schema at all, so `PUT ' +
'/api/v1/meta/capability/:name` stored ANY JSON under an AUTHORIZATION type ' +
'and stray keys on a declaration were dropped in silence.',
}, {
/**
* Stable capability key referenced by `systemPermissions` / `requiredPermissions`.
* Lowercase, dot/underscore separable (e.g. `export_data`, `billing.refund`).
*/
name: z.string()
.min(1)
.regex(/^[a-z][a-z0-9_.]*$/, 'Capability name must be lowercase and may contain digits, "_" and "." (e.g. export_data, billing.refund)')
.describe('Stable capability key referenced by systemPermissions / requiredPermissions'),
/** Human label shown in Setup. Defaults to a humanized `name` when omitted. */
label: z.string().min(1).optional().describe('Human label shown in Setup'),
/** What holding the capability permits. */
description: z.string().optional().describe('What holding this capability permits'),
/** `platform` = global; `org` = scoped to the caller's organization. Defaults to `platform`. */
scope: z.enum(['platform', 'org']).default('platform')
.describe('platform = a platform-wide power; org = scoped to an organization'),
/**
* [ADR-0086 D3] Owning package id — the author-declared fallback provenance.
* The registry stamps `_packageId` at load; this is used only when that is
* absent. A capability with no resolvable owner is not materialized as a
* package row.
*/
packageId: z.string().optional()
.describe('[ADR-0086 D3] Owning package id (author-declared fallback; absent = registry-stamped)'),
// ADR-0010 — runtime protection envelope (internal — set by the loader).
//
// [#5961] Declared when `capability` became a registered metadata kind: the
// artifact loader's `applyProtection` stamps these on EVERY registered type,
// and this schema is now strict, so without the spread the loader's own
// output fails to parse (a hard 422 on the write path). Same gap #4001 closed
// for `permission` and `position` — the two sibling security kinds.
...MetadataProtectionFields,
}));
/** A validated package-level capability declaration (output of {@link defineCapability}). */
export type CapabilityDeclaration = z.infer<typeof CapabilityDeclarationSchema>;
/** Authoring input for {@link CapabilityDeclaration} — defaulted fields are optional. */
export type CapabilityDeclarationInput = z.input<typeof CapabilityDeclarationSchema>;
/**
* Type-safe factory for a package-level capability declaration (ADR-0066 D1).
* Validates at authoring time via `.parse()` and accepts input-shape config
* (optional `label`/`description`, defaulted `scope`) — preferred over a bare
* object literal.
*
* @example
* ```ts
* export const ExportDataCapability = defineCapability({
* name: 'export_data',
* label: 'Export Data',
* description: 'Bulk-export records to CSV/XLSX.',
* scope: 'org',
* });
* ```
*/
export function defineCapability(config: CapabilityDeclarationInput): CapabilityDeclaration {
return CapabilityDeclarationSchema.parse(config);
}