pulse-hub 3.5.0 → 3.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/backend/.env.example +52 -52
  3. package/backend/agent.js +372 -372
  4. package/backend/cli.js +23 -1
  5. package/backend/emails/env-backup.html +52 -52
  6. package/backend/emails/invite.html +53 -53
  7. package/backend/lib/agent-rpc.js +72 -72
  8. package/backend/lib/async-route.js +17 -17
  9. package/backend/lib/authz/bootstrap.js +35 -35
  10. package/backend/lib/authz/catalog.js +291 -291
  11. package/backend/lib/authz/delegation.js +104 -104
  12. package/backend/lib/authz/effective.js +104 -104
  13. package/backend/lib/authz/engine.js +251 -251
  14. package/backend/lib/authz/index.js +134 -134
  15. package/backend/lib/authz/migrate.js +871 -871
  16. package/backend/lib/authz/mutate.js +87 -87
  17. package/backend/lib/authz/preview.js +327 -327
  18. package/backend/lib/authz/scope.js +282 -282
  19. package/backend/lib/env-mask.js +131 -131
  20. package/backend/lib/invite-helpers.js +83 -83
  21. package/backend/lib/invites.js +160 -160
  22. package/backend/lib/logs.js +244 -244
  23. package/backend/lib/metrics.js +249 -249
  24. package/backend/lib/relay-state.js +564 -564
  25. package/backend/lib/session.js +48 -48
  26. package/backend/middleware/authz.js +65 -65
  27. package/backend/public/assets/favicon.svg +6 -6
  28. package/backend/public/assets/index.js +1 -1
  29. package/backend/public/index.html +23 -24
  30. package/backend/routes/access.js +123 -123
  31. package/backend/routes/audit.js +41 -41
  32. package/backend/routes/auth.js +207 -207
  33. package/backend/routes/invites-public.js +91 -91
  34. package/backend/routes/invites.js +121 -121
  35. package/backend/routes/logs.js +68 -68
  36. package/backend/routes/metrics.js +76 -76
  37. package/backend/routes/policies.js +240 -240
  38. package/backend/routes/teams.js +221 -221
  39. package/backend/routes/users.js +100 -100
  40. package/backend/seed-admins.js +68 -68
  41. package/backend/server.js +106 -106
  42. package/backend/ws/agent-relay.js +159 -159
  43. package/package.json +1 -1
@@ -1,240 +1,240 @@
1
- // Policies: named sets of (action, scope) rules, attached to teams. Built-in templates are
2
- // read-only and can only be copied, never attached. Rule input is validated with the same
3
- // validateRule the engine applies on load; `glob` matchers are only accepted when they are
4
- // already present, unchanged, in the stored policy (migrated globs stay editable-by-keeping).
5
-
6
- const express = require('express');
7
- const db = require('../lib/db');
8
- const store = require('../lib/authz-store');
9
- const audit = require('../lib/audit');
10
- const { forbidden, readJsonBody } = require('../lib/http-helpers');
11
- const { validateRule } = require('../lib/authz/scope');
12
- const { canDelegate } = require('../lib/authz/delegation');
13
- const {
14
- mutate,
15
- sendJson,
16
- requireDelegable,
17
- requireConfirmation,
18
- handleStale,
19
- } = require('../lib/authz/mutate');
20
-
21
- const router = express.Router();
22
- const MAX_RULES = 200;
23
-
24
- function need(req, res, action) {
25
- if (req.authz.can(action)) return true;
26
- audit.record(
27
- { action, targetType: 'policy', outcome: 'denied', summary: `Denied ${action}` },
28
- req,
29
- );
30
- forbidden(res, 'You do not have permission to do that.');
31
- return false;
32
- }
33
-
34
- function parseRules(p) {
35
- return store.getPolicyRules(p.id).map((r) => ({
36
- id: r.id,
37
- action: r.action,
38
- scope: r.scope ? JSON.parse(r.scope) : null,
39
- }));
40
- }
41
-
42
- function shape(p) {
43
- const teamCount = db.db
44
- .prepare('SELECT COUNT(*) AS n FROM team_policies WHERE policy_id = ?')
45
- .get(p.id).n;
46
- return {
47
- id: p.id,
48
- name: p.name,
49
- description: p.description || '',
50
- builtin: !!p.builtin,
51
- builtinKey: p.builtin_key || null,
52
- rules: parseRules(p),
53
- teamCount,
54
- };
55
- }
56
-
57
- const keyOf = (r) => `${r.action}|${JSON.stringify(r.scope || null)}`;
58
-
59
- // Returns {rules} or {error}. `existing` = the policy's stored rules, for preserving globs.
60
- function validateRules(input, existing = []) {
61
- if (!Array.isArray(input)) return { error: 'rules must be an array.' };
62
- if (input.length > MAX_RULES) return { error: `A policy can have at most ${MAX_RULES} rules.` };
63
- const kept = new Set(existing.map(keyOf));
64
- const rules = [];
65
- for (const r of input) {
66
- if (!r || typeof r.action !== 'string') return { error: 'Each rule needs an action.' };
67
- const rule = { action: r.action, scope: r.scope === undefined ? null : r.scope };
68
- const v = validateRule(rule, { fromMigration: kept.has(keyOf(rule)) });
69
- if (!v.ok) return { error: v.error };
70
- rules.push(rule);
71
- }
72
- return { rules };
73
- }
74
-
75
- function cleanName(raw) {
76
- return typeof raw === 'string' ? raw.trim().slice(0, 80) : '';
77
- }
78
-
79
- const isUnique = (e) => /UNIQUE/.test(String(e.message));
80
-
81
- router.get('/api/policies', (req, res) => {
82
- if (!need(req, res, 'policies.view')) return;
83
- sendJson(res, 200, store.listPolicies().map(shape));
84
- });
85
-
86
- router.post('/api/policies', async (req, res) => {
87
- if (!need(req, res, 'policies.create')) return;
88
- const body = await readJsonBody(req);
89
- const name = cleanName(body.name);
90
- if (!name) return sendJson(res, 400, { error: 'A policy needs a name.' });
91
- const parsed = validateRules(body.rules || []);
92
- if (parsed.error) return sendJson(res, 400, { error: parsed.error });
93
- const del = canDelegate(req.authz.subject, parsed.rules);
94
- if (!del.ok)
95
- return sendJson(res, 403, {
96
- error: 'You cannot grant access you do not hold yourself.',
97
- denied: del.denied.map((d) => d.rule || d),
98
- });
99
- try {
100
- const policy = mutate(() => {
101
- const p = store.createPolicy({ name, description: body.description }, req.sessionUser.id);
102
- parsed.rules.forEach((r) => store.addPolicyRule(p.id, r));
103
- audit.record(
104
- {
105
- action: 'policy.create',
106
- targetType: 'policy',
107
- targetId: p.id,
108
- outcome: 'succeeded',
109
- summary: `Created policy ${name}`,
110
- },
111
- req,
112
- );
113
- return p;
114
- });
115
- sendJson(res, 200, shape(policy));
116
- } catch (e) {
117
- if (isUnique(e))
118
- return sendJson(res, 409, { error: 'A policy with that name already exists.' });
119
- throw e;
120
- }
121
- });
122
-
123
- router.post('/api/policies/:id/copy', async (req, res) => {
124
- if (!need(req, res, 'policies.create')) return;
125
- const src = store.getPolicy(req.params.id);
126
- if (!src) return sendJson(res, 404, { error: 'No such policy.' });
127
- const body = await readJsonBody(req);
128
- const name = cleanName(body.name) || `${src.name} (copy)`;
129
- const rules = parseRules(src).map((r) => ({ action: r.action, scope: r.scope }));
130
- const del = canDelegate(req.authz.subject, rules);
131
- if (!del.ok)
132
- return sendJson(res, 403, {
133
- error: 'You cannot grant access you do not hold yourself.',
134
- denied: del.denied.map((d) => d.rule || d),
135
- });
136
- try {
137
- const policy = mutate(() => {
138
- const p = store.createPolicy({ name, description: src.description }, req.sessionUser.id);
139
- rules.forEach((r) => store.addPolicyRule(p.id, r));
140
- audit.record(
141
- {
142
- action: 'policy.create',
143
- targetType: 'policy',
144
- targetId: p.id,
145
- outcome: 'succeeded',
146
- summary: `Copied policy ${src.name} as ${name}`,
147
- },
148
- req,
149
- );
150
- return p;
151
- });
152
- sendJson(res, 200, shape(policy));
153
- } catch (e) {
154
- if (isUnique(e))
155
- return sendJson(res, 409, { error: 'A policy with that name already exists.' });
156
- throw e;
157
- }
158
- });
159
-
160
- router.put('/api/policies/:id', async (req, res) => {
161
- if (!need(req, res, 'policies.update')) return;
162
- const policy = store.getPolicy(req.params.id);
163
- if (!policy) return sendJson(res, 404, { error: 'No such policy.' });
164
- if (policy.builtin)
165
- return sendJson(res, 400, { error: 'Built-in templates are read-only. Copy it to customise.' });
166
- const body = await readJsonBody(req);
167
- const name = body.name === undefined ? policy.name : cleanName(body.name);
168
- if (!name) return sendJson(res, 400, { error: 'A policy needs a name.' });
169
- const description = body.description === undefined ? policy.description : body.description;
170
- const parsed = validateRules(
171
- body.rules === undefined ? parseRules(policy) : body.rules,
172
- parseRules(policy),
173
- );
174
- if (parsed.error) return sendJson(res, 400, { error: parsed.error });
175
- const change = { kind: 'policy.update', policyId: policy.id, rules: parsed.rules };
176
- if (!requireDelegable(req, res, change)) return;
177
- const gate = requireConfirmation(req, res, change, body.confirmRevision);
178
- if (!gate.proceed) return;
179
- try {
180
- mutate(
181
- () => {
182
- store.updatePolicy(policy.id, { name, description }, req.sessionUser.id);
183
- store.deletePolicyRules(policy.id);
184
- parsed.rules.forEach((r) => store.addPolicyRule(policy.id, r));
185
- audit.record(
186
- {
187
- action: 'policy.update',
188
- targetType: 'policy',
189
- targetId: policy.id,
190
- outcome: 'succeeded',
191
- summary: `Updated policy ${name}`,
192
- metadata: { ruleCount: parsed.rules.length },
193
- },
194
- req,
195
- );
196
- },
197
- { expectedRevision: gate.expectedRevision },
198
- );
199
- } catch (e) {
200
- if (handleStale(res, e, change, req)) return;
201
- if (isUnique(e))
202
- return sendJson(res, 409, { error: 'A policy with that name already exists.' });
203
- throw e;
204
- }
205
- sendJson(res, 200, shape(store.getPolicy(policy.id)));
206
- });
207
-
208
- router.delete('/api/policies/:id', (req, res) => {
209
- if (!need(req, res, 'policies.delete')) return;
210
- const policy = store.getPolicy(req.params.id);
211
- if (!policy) return sendJson(res, 404, { error: 'No such policy.' });
212
- if (policy.builtin) return sendJson(res, 400, { error: 'Built-in templates cannot be deleted.' });
213
- const change = { kind: 'policy.delete', policyId: policy.id };
214
- const gate = requireConfirmation(req, res, change, req.query.confirmRevision);
215
- if (!gate.proceed) return;
216
- try {
217
- mutate(
218
- () => {
219
- store.deletePolicy(policy.id);
220
- audit.record(
221
- {
222
- action: 'policy.delete',
223
- targetType: 'policy',
224
- targetId: policy.id,
225
- outcome: 'succeeded',
226
- summary: `Deleted policy ${policy.name}`,
227
- },
228
- req,
229
- );
230
- },
231
- { expectedRevision: gate.expectedRevision },
232
- );
233
- } catch (e) {
234
- if (handleStale(res, e, change, req)) return;
235
- throw e;
236
- }
237
- sendJson(res, 200, { ok: true });
238
- });
239
-
240
- module.exports = router;
1
+ // Policies: named sets of (action, scope) rules, attached to teams. Built-in templates are
2
+ // read-only and can only be copied, never attached. Rule input is validated with the same
3
+ // validateRule the engine applies on load; `glob` matchers are only accepted when they are
4
+ // already present, unchanged, in the stored policy (migrated globs stay editable-by-keeping).
5
+
6
+ const express = require('express');
7
+ const db = require('../lib/db');
8
+ const store = require('../lib/authz-store');
9
+ const audit = require('../lib/audit');
10
+ const { forbidden, readJsonBody } = require('../lib/http-helpers');
11
+ const { validateRule } = require('../lib/authz/scope');
12
+ const { canDelegate } = require('../lib/authz/delegation');
13
+ const {
14
+ mutate,
15
+ sendJson,
16
+ requireDelegable,
17
+ requireConfirmation,
18
+ handleStale,
19
+ } = require('../lib/authz/mutate');
20
+
21
+ const router = express.Router();
22
+ const MAX_RULES = 200;
23
+
24
+ function need(req, res, action) {
25
+ if (req.authz.can(action)) return true;
26
+ audit.record(
27
+ { action, targetType: 'policy', outcome: 'denied', summary: `Denied ${action}` },
28
+ req,
29
+ );
30
+ forbidden(res, 'You do not have permission to do that.');
31
+ return false;
32
+ }
33
+
34
+ function parseRules(p) {
35
+ return store.getPolicyRules(p.id).map((r) => ({
36
+ id: r.id,
37
+ action: r.action,
38
+ scope: r.scope ? JSON.parse(r.scope) : null,
39
+ }));
40
+ }
41
+
42
+ function shape(p) {
43
+ const teamCount = db.db
44
+ .prepare('SELECT COUNT(*) AS n FROM team_policies WHERE policy_id = ?')
45
+ .get(p.id).n;
46
+ return {
47
+ id: p.id,
48
+ name: p.name,
49
+ description: p.description || '',
50
+ builtin: !!p.builtin,
51
+ builtinKey: p.builtin_key || null,
52
+ rules: parseRules(p),
53
+ teamCount,
54
+ };
55
+ }
56
+
57
+ const keyOf = (r) => `${r.action}|${JSON.stringify(r.scope || null)}`;
58
+
59
+ // Returns {rules} or {error}. `existing` = the policy's stored rules, for preserving globs.
60
+ function validateRules(input, existing = []) {
61
+ if (!Array.isArray(input)) return { error: 'rules must be an array.' };
62
+ if (input.length > MAX_RULES) return { error: `A policy can have at most ${MAX_RULES} rules.` };
63
+ const kept = new Set(existing.map(keyOf));
64
+ const rules = [];
65
+ for (const r of input) {
66
+ if (!r || typeof r.action !== 'string') return { error: 'Each rule needs an action.' };
67
+ const rule = { action: r.action, scope: r.scope === undefined ? null : r.scope };
68
+ const v = validateRule(rule, { fromMigration: kept.has(keyOf(rule)) });
69
+ if (!v.ok) return { error: v.error };
70
+ rules.push(rule);
71
+ }
72
+ return { rules };
73
+ }
74
+
75
+ function cleanName(raw) {
76
+ return typeof raw === 'string' ? raw.trim().slice(0, 80) : '';
77
+ }
78
+
79
+ const isUnique = (e) => /UNIQUE/.test(String(e.message));
80
+
81
+ router.get('/api/policies', (req, res) => {
82
+ if (!need(req, res, 'policies.view')) return;
83
+ sendJson(res, 200, store.listPolicies().map(shape));
84
+ });
85
+
86
+ router.post('/api/policies', async (req, res) => {
87
+ if (!need(req, res, 'policies.create')) return;
88
+ const body = await readJsonBody(req);
89
+ const name = cleanName(body.name);
90
+ if (!name) return sendJson(res, 400, { error: 'A policy needs a name.' });
91
+ const parsed = validateRules(body.rules || []);
92
+ if (parsed.error) return sendJson(res, 400, { error: parsed.error });
93
+ const del = canDelegate(req.authz.subject, parsed.rules);
94
+ if (!del.ok)
95
+ return sendJson(res, 403, {
96
+ error: 'You cannot grant access you do not hold yourself.',
97
+ denied: del.denied.map((d) => d.rule || d),
98
+ });
99
+ try {
100
+ const policy = mutate(() => {
101
+ const p = store.createPolicy({ name, description: body.description }, req.sessionUser.id);
102
+ parsed.rules.forEach((r) => store.addPolicyRule(p.id, r));
103
+ audit.record(
104
+ {
105
+ action: 'policy.create',
106
+ targetType: 'policy',
107
+ targetId: p.id,
108
+ outcome: 'succeeded',
109
+ summary: `Created policy ${name}`,
110
+ },
111
+ req,
112
+ );
113
+ return p;
114
+ });
115
+ sendJson(res, 200, shape(policy));
116
+ } catch (e) {
117
+ if (isUnique(e))
118
+ return sendJson(res, 409, { error: 'A policy with that name already exists.' });
119
+ throw e;
120
+ }
121
+ });
122
+
123
+ router.post('/api/policies/:id/copy', async (req, res) => {
124
+ if (!need(req, res, 'policies.create')) return;
125
+ const src = store.getPolicy(req.params.id);
126
+ if (!src) return sendJson(res, 404, { error: 'No such policy.' });
127
+ const body = await readJsonBody(req);
128
+ const name = cleanName(body.name) || `${src.name} (copy)`;
129
+ const rules = parseRules(src).map((r) => ({ action: r.action, scope: r.scope }));
130
+ const del = canDelegate(req.authz.subject, rules);
131
+ if (!del.ok)
132
+ return sendJson(res, 403, {
133
+ error: 'You cannot grant access you do not hold yourself.',
134
+ denied: del.denied.map((d) => d.rule || d),
135
+ });
136
+ try {
137
+ const policy = mutate(() => {
138
+ const p = store.createPolicy({ name, description: src.description }, req.sessionUser.id);
139
+ rules.forEach((r) => store.addPolicyRule(p.id, r));
140
+ audit.record(
141
+ {
142
+ action: 'policy.create',
143
+ targetType: 'policy',
144
+ targetId: p.id,
145
+ outcome: 'succeeded',
146
+ summary: `Copied policy ${src.name} as ${name}`,
147
+ },
148
+ req,
149
+ );
150
+ return p;
151
+ });
152
+ sendJson(res, 200, shape(policy));
153
+ } catch (e) {
154
+ if (isUnique(e))
155
+ return sendJson(res, 409, { error: 'A policy with that name already exists.' });
156
+ throw e;
157
+ }
158
+ });
159
+
160
+ router.put('/api/policies/:id', async (req, res) => {
161
+ if (!need(req, res, 'policies.update')) return;
162
+ const policy = store.getPolicy(req.params.id);
163
+ if (!policy) return sendJson(res, 404, { error: 'No such policy.' });
164
+ if (policy.builtin)
165
+ return sendJson(res, 400, { error: 'Built-in templates are read-only. Copy it to customise.' });
166
+ const body = await readJsonBody(req);
167
+ const name = body.name === undefined ? policy.name : cleanName(body.name);
168
+ if (!name) return sendJson(res, 400, { error: 'A policy needs a name.' });
169
+ const description = body.description === undefined ? policy.description : body.description;
170
+ const parsed = validateRules(
171
+ body.rules === undefined ? parseRules(policy) : body.rules,
172
+ parseRules(policy),
173
+ );
174
+ if (parsed.error) return sendJson(res, 400, { error: parsed.error });
175
+ const change = { kind: 'policy.update', policyId: policy.id, rules: parsed.rules };
176
+ if (!requireDelegable(req, res, change)) return;
177
+ const gate = requireConfirmation(req, res, change, body.confirmRevision);
178
+ if (!gate.proceed) return;
179
+ try {
180
+ mutate(
181
+ () => {
182
+ store.updatePolicy(policy.id, { name, description }, req.sessionUser.id);
183
+ store.deletePolicyRules(policy.id);
184
+ parsed.rules.forEach((r) => store.addPolicyRule(policy.id, r));
185
+ audit.record(
186
+ {
187
+ action: 'policy.update',
188
+ targetType: 'policy',
189
+ targetId: policy.id,
190
+ outcome: 'succeeded',
191
+ summary: `Updated policy ${name}`,
192
+ metadata: { ruleCount: parsed.rules.length },
193
+ },
194
+ req,
195
+ );
196
+ },
197
+ { expectedRevision: gate.expectedRevision },
198
+ );
199
+ } catch (e) {
200
+ if (handleStale(res, e, change, req)) return;
201
+ if (isUnique(e))
202
+ return sendJson(res, 409, { error: 'A policy with that name already exists.' });
203
+ throw e;
204
+ }
205
+ sendJson(res, 200, shape(store.getPolicy(policy.id)));
206
+ });
207
+
208
+ router.delete('/api/policies/:id', (req, res) => {
209
+ if (!need(req, res, 'policies.delete')) return;
210
+ const policy = store.getPolicy(req.params.id);
211
+ if (!policy) return sendJson(res, 404, { error: 'No such policy.' });
212
+ if (policy.builtin) return sendJson(res, 400, { error: 'Built-in templates cannot be deleted.' });
213
+ const change = { kind: 'policy.delete', policyId: policy.id };
214
+ const gate = requireConfirmation(req, res, change, req.query.confirmRevision);
215
+ if (!gate.proceed) return;
216
+ try {
217
+ mutate(
218
+ () => {
219
+ store.deletePolicy(policy.id);
220
+ audit.record(
221
+ {
222
+ action: 'policy.delete',
223
+ targetType: 'policy',
224
+ targetId: policy.id,
225
+ outcome: 'succeeded',
226
+ summary: `Deleted policy ${policy.name}`,
227
+ },
228
+ req,
229
+ );
230
+ },
231
+ { expectedRevision: gate.expectedRevision },
232
+ );
233
+ } catch (e) {
234
+ if (handleStale(res, e, change, req)) return;
235
+ throw e;
236
+ }
237
+ sendJson(res, 200, { ok: true });
238
+ });
239
+
240
+ module.exports = router;