-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsuggested-audience-bindings.ts
More file actions
424 lines (382 loc) · 17.4 KB
/
Copy pathsuggested-audience-bindings.ts
File metadata and controls
424 lines (382 loc) · 17.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
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
/**
* Suggested audience bindings (ADR-0090 D5/D9) — the queryable surface and
* confirm/dismiss flow for a package's `isDefault: true` install-time
* suggestion ("bind this set to the `everyone` position — accept?").
*
* The suggestion itself is declaration-only (spec `PermissionSetSchema
* .isDefault`); this module materializes it as `sys_audience_binding_suggestion`
* rows so an admin can see and act on it after install:
*
* - `syncAudienceBindingSuggestions` is CONVERGENT and idempotent: it reads
* every currently-declared `isDefault` set — boot-declared stack metadata
* AND installed package manifests (which the registry updates at
* `installPackage` time, so a runtime install is visible immediately,
* no reboot needed) — and reconciles the table: missing → pending row;
* binding already present → confirmed (observed); declaration gone
* (uninstall / flag dropped) → pending row pruned. It runs at boot, after
* a package-door `permission` publish, and on every list call.
* - `confirmAudienceBindingSuggestion` creates the anchor binding **with the
* caller's execution context**, so the ADR-0090 write gates do the real
* enforcement: the D5/D9 audience-anchor gate (no high-privilege set on
* `everyone`/`guest`) and the D12 delegated-admin gate (anchors are
* tenant-level only). Never auto-bound, never `isSystem`.
* - `dismissAudienceBindingSuggestion` records the admin's "no".
*
* All three service methods are additionally pre-gated on tenant-level
* admin (the ADR-0066 superuser wildcard) so the read surface and the
* no-write dismiss/idempotent-confirm paths are as strict as the bind write.
*/
import type { PermissionSet } from '@objectstack/spec/security';
import { describeAnchorForbiddenBits } from '@objectstack/spec/security';
import { EVERYONE_POSITION, AUDIENCE_ANCHOR_POSITIONS } from '@objectstack/spec';
import { PermissionDeniedError } from './errors.js';
import { readDeclared, upsertPackagePermissionSet } from './bootstrap-declared-permissions.js';
import { isTenantAdmin } from './delegated-admin-gate.js';
const SYSTEM_CTX = { isSystem: true };
const SUGGESTION_OBJECT = 'sys_audience_binding_suggestion';
/** Thrown when the referenced suggestion row does not exist → HTTP 404. */
export class SuggestionNotFoundError extends Error {
readonly code = 'SUGGESTION_NOT_FOUND';
readonly statusCode = 404;
constructor(id: string) {
super(`Audience-binding suggestion '${id}' not found`);
this.name = 'SuggestionNotFoundError';
}
}
/** Thrown when a confirm/dismiss hits a non-pending row or an unresolvable
* precondition (set not materialized, anchor missing) → HTTP 409. */
export class SuggestionStateError extends Error {
readonly code = 'SUGGESTION_STATE';
readonly statusCode = 409;
constructor(message: string) {
super(message);
this.name = 'SuggestionStateError';
}
}
export interface SuggestionDeps {
/** ObjectQL engine handle. */
ql: any;
/** Metadata service (fallback source for boot-declared sets). */
metadata?: any;
/** Shared permission-set resolution (same path as the CRUD middleware). */
resolveSets: (context: any) => Promise<PermissionSet[]>;
logger?: { info?: (m: string, meta?: any) => void; warn?: (m: string, meta?: any) => void };
}
export interface SuggestionSyncOutcome {
created: number;
confirmedObserved: number;
pruned: number;
}
export interface SuggestionListFilter {
status?: 'pending' | 'confirmed' | 'dismissed';
packageId?: string;
}
function genId(prefix: string): string {
const rand = Math.random().toString(36).slice(2, 10);
const ts = Date.now().toString(36);
return `${prefix}_${ts}${rand}`;
}
// Engine signatures: `find(object, query)` and `delete(object, options)` read
// `context` from their SECOND argument — a trailing `{ context }` arg is
// silently ignored and the operation runs principal-less.
async function tryFind(ql: any, object: string, where: any, limit = 500): Promise<any[]> {
try {
const rows = await ql.find(object, { where, limit, context: SYSTEM_CTX });
return Array.isArray(rows) ? rows : [];
} catch { return []; }
}
/** A declared `isDefault` permission set together with its owning package. */
interface DeclaredSuggestion {
packageId: string;
set: any;
anchor: string;
}
function suggestionKey(packageId: string, setName: string, anchor: string): string {
return `${packageId}\u0000${setName}\u0000${anchor}`;
}
/**
* Collect every currently-declared audience-binding suggestion from BOTH
* declaration sources:
*
* 1. boot-declared stack metadata (the app bundle's `permissions`, registered
* into the SchemaRegistry / metadata service at boot) — provenance via
* `_packageId` (ADR-0010) with spec `packageId` (ADR-0086 D3) as fallback;
* 2. installed package manifests (`registry.getAllPackages()`), which the
* registry updates synchronously at `installPackage` time — this is what
* makes a runtime `POST /api/v1/packages` install queryable immediately.
*
* Today the only declarable suggestion is `isDefault: true` → `everyone`
* (ADR-0090 D5); the shape is anchor-keyed so the D9 `guest` generalization
* slots in without a schema change.
*/
export function collectDeclaredSuggestions(ql: any, metadata?: any): DeclaredSuggestion[] {
const out = new Map<string, DeclaredSuggestion>();
const consider = (ps: any, packageId: string | undefined) => {
if (!ps || typeof ps !== 'object' || !ps.name) return;
if (ps.isDefault !== true) return;
if (!packageId) return; // unowned suggestion = unowned binding — refuse (ADR-0086 D3)
const anchor: string = EVERYONE_POSITION;
const key = suggestionKey(packageId, ps.name, anchor);
if (!out.has(key)) out.set(key, { packageId, set: ps, anchor });
};
// Source 1 — boot-declared stack metadata.
for (const ps of readDeclared(ql, 'permission')) {
consider(ps, (ps as any)._packageId ?? (ps as any).packageId ?? undefined);
}
try {
const listed = metadata?.list?.('permission');
if (Array.isArray(listed)) {
for (const ps of listed) consider(ps, (ps as any)._packageId ?? (ps as any).packageId ?? undefined);
}
} catch { /* metadata facade optional */ }
// Source 2 — installed package manifests (live at install time).
try {
const packages: any[] = ql?._registry?.getAllPackages?.() ?? [];
for (const pkg of packages) {
if (pkg?.enabled === false) continue;
const manifest = pkg?.manifest;
const declared = Array.isArray(manifest?.permissions) ? manifest.permissions : [];
for (const ps of declared) consider(ps, manifest?.id);
}
} catch { /* registry shape optional (test stubs) */ }
return [...out.values()];
}
/** Resolve the anchor position rows by name → `{ everyone: row?, guest: row? }`. */
async function findAnchorPositions(ql: any): Promise<Record<string, any>> {
const anchors: Record<string, any> = {};
for (const name of AUDIENCE_ANCHOR_POSITIONS) {
anchors[name] = (await tryFind(ql, 'sys_position', { name }, 1))[0] ?? null;
}
return anchors;
}
/** True when the set is already bound to the anchor (rows resolved by name). */
async function bindingExists(ql: any, anchorRow: any, setRow: any): Promise<boolean> {
if (!anchorRow?.id || !setRow?.id) return false;
const rows = await tryFind(ql, 'sys_position_permission_set', {
position_id: anchorRow.id,
permission_set_id: setRow.id,
}, 1);
return rows.length > 0;
}
/**
* Reconcile `sys_audience_binding_suggestion` against the current
* declarations. Idempotent; system-context; never touches confirmed or
* dismissed rows except to prune nothing (they are audit history).
*/
export async function syncAudienceBindingSuggestions(
ql: any,
metadata?: any,
logger?: SuggestionDeps['logger'],
): Promise<SuggestionSyncOutcome> {
const out: SuggestionSyncOutcome = { created: 0, confirmedObserved: 0, pruned: 0 };
if (!ql || typeof ql.find !== 'function' || typeof ql.insert !== 'function') return out;
const declared = collectDeclaredSuggestions(ql, metadata);
const declaredKeys = new Set(declared.map((d) => suggestionKey(d.packageId, d.set.name, d.anchor)));
const anchors = await findAnchorPositions(ql);
const existing = await tryFind(ql, SUGGESTION_OBJECT, {}, 1000);
const byKey = new Map<string, any>(
existing.map((row) => [suggestionKey(row.package_id, row.permission_set_name, row.anchor), row]),
);
for (const d of declared) {
const key = suggestionKey(d.packageId, d.set.name, d.anchor);
const row = byKey.get(key);
const setRow = (await tryFind(ql, 'sys_permission_set', { name: d.set.name }, 1))[0] ?? null;
const bound = await bindingExists(ql, anchors[d.anchor], setRow);
if (row) {
if (row.status === 'pending' && bound) {
// Bound outside the prompt (boot baseline, admin by hand) — record the
// observation so the surface never nags about a satisfied suggestion.
try {
await ql.update(SUGGESTION_OBJECT, {
id: row.id, status: 'confirmed', resolved_at: new Date().toISOString(),
}, { context: SYSTEM_CTX });
out.confirmedObserved += 1;
} catch { /* non-fatal */ }
}
continue;
}
if (bound) continue; // satisfied before ever being surfaced — nothing pending
try {
await ql.insert(SUGGESTION_OBJECT, {
id: genId('sug'),
package_id: d.packageId,
permission_set_name: d.set.name,
anchor: d.anchor,
status: 'pending',
}, { context: SYSTEM_CTX });
out.created += 1;
} catch { /* unique-index race with a concurrent sync — benign */ }
}
// Prune PENDING rows whose declaration is gone (package uninstalled or the
// flag dropped on upgrade). Confirmed/dismissed rows stay as audit history.
for (const row of existing) {
if (row.status !== 'pending') continue;
const key = suggestionKey(row.package_id, row.permission_set_name, row.anchor);
if (declaredKeys.has(key)) continue;
try {
await ql.delete(SUGGESTION_OBJECT, { where: { id: row.id }, context: SYSTEM_CTX });
out.pruned += 1;
} catch { /* non-fatal */ }
}
if (out.created + out.confirmedObserved + out.pruned > 0) {
logger?.info?.('[security] audience-binding suggestions reconciled (ADR-0090 D5/D9)', { ...out });
}
return out;
}
/**
* Tenant-admin pre-gate shared by all three service methods. The anchors are
* tenant-level only (ADR-0090 D12 — no delegated scope can touch them), so
* the whole suggestion surface requires the ADR-0066 superuser wildcard.
*/
async function assertTenantAdmin(deps: SuggestionDeps, callerCtx: any, action: string): Promise<void> {
if (callerCtx?.isSystem) return;
if (!callerCtx?.userId) {
throw new PermissionDeniedError(
`[Security] Access denied: ${action} requires an authenticated tenant administrator (ADR-0090 D5/D12).`,
);
}
let sets: PermissionSet[] = [];
try { sets = await deps.resolveSets(callerCtx); } catch { sets = []; }
if (!isTenantAdmin(sets)) {
throw new PermissionDeniedError(
`[Security] Access denied: ${action} requires a tenant-level administrator — ` +
`audience anchors stay tenant-level only (ADR-0090 D12).`,
{ userId: callerCtx.userId },
);
}
}
/** List suggestions (tenant-admin only), reconciling first so a package
* installed a moment ago is already visible. */
export async function listAudienceBindingSuggestions(
deps: SuggestionDeps,
callerCtx: any,
filter: SuggestionListFilter = {},
): Promise<{ suggestions: any[]; synced: SuggestionSyncOutcome }> {
await assertTenantAdmin(deps, callerCtx, 'listing audience-binding suggestions');
const synced = await syncAudienceBindingSuggestions(deps.ql, deps.metadata, deps.logger);
const where: Record<string, unknown> = {};
if (filter.status) where.status = filter.status;
if (filter.packageId) where.package_id = filter.packageId;
const suggestions = await tryFind(deps.ql, SUGGESTION_OBJECT, where, 1000);
return { suggestions, synced };
}
/**
* Confirm a pending suggestion: create the anchor binding **as the caller**
* (so the D5/D9 anchor gate and D12 delegated-admin gate run against the real
* principal — this is the "admin confirms" moment of ADR-0090 D5), then mark
* the row confirmed with the resolver's identity.
*/
export async function confirmAudienceBindingSuggestion(
deps: SuggestionDeps,
callerCtx: any,
id: string,
): Promise<{ suggestion: any; bindingCreated: boolean }> {
const { ql } = deps;
await assertTenantAdmin(deps, callerCtx, 'confirming an audience-binding suggestion');
const row = (await tryFind(ql, SUGGESTION_OBJECT, { id }, 1))[0];
if (!row) throw new SuggestionNotFoundError(id);
if (row.status !== 'pending') {
throw new SuggestionStateError(
`Audience-binding suggestion '${id}' is already ${row.status} — only pending suggestions can be confirmed.`,
);
}
const anchorRow = (await tryFind(ql, 'sys_position', { name: row.anchor }, 1))[0];
if (!anchorRow?.id) {
throw new SuggestionStateError(
`The '${row.anchor}' audience anchor position is not seeded yet — cannot bind.`,
);
}
// Resolve — or, for a package installed this session, materialize — the set
// row. Materialization goes through the SAME provenance-checked upsert as
// the boot seeder and the publish materializer (ADR-0086 D4), so a foreign-
// or env-owned name is refused, never clobbered.
let setRow = (await tryFind(ql, 'sys_permission_set', { name: row.permission_set_name }, 1))[0] ?? null;
if (!setRow) {
const declared = collectDeclaredSuggestions(ql, deps.metadata).find(
(d) => d.packageId === row.package_id && d.set.name === row.permission_set_name && d.anchor === row.anchor,
);
if (declared) {
const seedLogger = {
info: (m: string, meta?: Record<string, any>) => deps.logger?.info?.(m, meta),
warn: (m: string, meta?: Record<string, any>) => deps.logger?.warn?.(m, meta),
};
await upsertPackagePermissionSet(ql, declared.set, row.package_id, seedLogger);
setRow = (await tryFind(ql, 'sys_permission_set', { name: row.permission_set_name }, 1))[0] ?? null;
}
}
if (!setRow?.id) {
throw new SuggestionStateError(
`Permission set '${row.permission_set_name}' is not materialized in sys_permission_set — ` +
`is package '${row.package_id}' still installed?`,
);
}
if (setRow.package_id && setRow.package_id !== row.package_id) {
throw new SuggestionStateError(
`Permission set '${row.permission_set_name}' is owned by package '${setRow.package_id}', ` +
`not the suggesting package '${row.package_id}' — refusing to bind (ADR-0086 D4).`,
);
}
// Early, friendly rendition of the anchor gate so the caller gets the
// decision without a write attempt; the engine middleware re-enforces it
// unconditionally on the insert below.
const offending = describeAnchorForbiddenBits(setRow, row.anchor as 'everyone' | 'guest');
if (offending) {
throw new PermissionDeniedError(
`[Security] Access denied: permission set '${row.permission_set_name}' cannot be bound to the ` +
`'${row.anchor}' audience anchor — it carries ${offending} (ADR-0090 D5/D9).`,
{ suggestion: id, anchor: row.anchor },
);
}
let bindingCreated = false;
if (!(await bindingExists(ql, anchorRow, setRow))) {
// The caller's context — NOT isSystem — so the audience-anchor gate and
// the delegated-admin gate evaluate the real principal.
await ql.insert('sys_position_permission_set', {
id: genId('pps'),
position_id: anchorRow.id,
permission_set_id: setRow.id,
}, { context: callerCtx });
bindingCreated = true;
}
await ql.update(SUGGESTION_OBJECT, {
id: row.id,
status: 'confirmed',
resolved_by: callerCtx?.userId ?? null,
resolved_at: new Date().toISOString(),
}, { context: SYSTEM_CTX });
deps.logger?.info?.('[security] audience-binding suggestion confirmed (ADR-0090 D5)', {
id: row.id, package: row.package_id, set: row.permission_set_name, anchor: row.anchor,
by: callerCtx?.userId, bindingCreated,
});
const updated = (await tryFind(ql, SUGGESTION_OBJECT, { id: row.id }, 1))[0] ?? row;
return { suggestion: updated, bindingCreated };
}
/** Dismiss a pending suggestion (tenant-admin only). The set stays available
* for ordinary position binding — dismissal only retires the prompt. */
export async function dismissAudienceBindingSuggestion(
deps: SuggestionDeps,
callerCtx: any,
id: string,
): Promise<{ suggestion: any }> {
const { ql } = deps;
await assertTenantAdmin(deps, callerCtx, 'dismissing an audience-binding suggestion');
const row = (await tryFind(ql, SUGGESTION_OBJECT, { id }, 1))[0];
if (!row) throw new SuggestionNotFoundError(id);
if (row.status !== 'pending') {
throw new SuggestionStateError(
`Audience-binding suggestion '${id}' is already ${row.status} — only pending suggestions can be dismissed.`,
);
}
await ql.update(SUGGESTION_OBJECT, {
id: row.id,
status: 'dismissed',
resolved_by: callerCtx?.userId ?? null,
resolved_at: new Date().toISOString(),
}, { context: SYSTEM_CTX });
deps.logger?.info?.('[security] audience-binding suggestion dismissed (ADR-0090 D5)', {
id: row.id, package: row.package_id, set: row.permission_set_name, by: callerCtx?.userId,
});
const updated = (await tryFind(ql, SUGGESTION_OBJECT, { id: row.id }, 1))[0] ?? row;
return { suggestion: updated };
}