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,87 +1,87 @@
1
- // Shared plumbing for access-changing routes: delegation guard, impact confirmation, and an
2
- // atomic "mutation + audit + revision bump" transaction that emits the revision event only
3
- // after commit (so open streams revalidate against committed data).
4
- const { db } = require('../db');
5
- const store = require('../authz-store');
6
- const authz = require('./index');
7
- const { canDelegate } = require('./delegation');
8
- const previewLib = require('./preview');
9
-
10
- class StaleRevisionError extends Error {}
11
-
12
- // Runs fn() in one transaction, bumps the authorization revision inside it, then emits the
13
- // revision event after commit. If `expectedRevision` is given and the stored revision has
14
- // moved, aborts (nothing written) with StaleRevisionError.
15
- function mutate(fn, { expectedRevision = null } = {}) {
16
- let rev;
17
- const out = db.transaction(() => {
18
- if (expectedRevision !== null && store.getAuthzRevision() !== expectedRevision) {
19
- throw new StaleRevisionError('stale');
20
- }
21
- const r = fn();
22
- rev = store.bumpAuthzRevision();
23
- return r;
24
- })();
25
- authz.authzEvents.emit('revision', rev);
26
- return out;
27
- }
28
-
29
- function sendJson(res, status, body) {
30
- res.writeHead(status, { 'Content-Type': 'application/json' });
31
- res.end(JSON.stringify(body));
32
- }
33
-
34
- // Delegation guard (privilege-escalation check). Returns true to proceed; otherwise writes a
35
- // 403 listing what the actor cannot grant.
36
- function requireDelegable(req, res, change) {
37
- const state = store.loadState();
38
- const rules = previewLib.rulesToDelegate(state, change);
39
- if (rules.length === 0) return true;
40
- const r = canDelegate(req.authz.subject, rules);
41
- if (r.ok) return true;
42
- sendJson(res, 403, {
43
- error: 'You cannot grant access you do not hold yourself.',
44
- denied: r.denied.map((d) => d.rule || d),
45
- });
46
- return false;
47
- }
48
-
49
- // Impact confirmation. If the change would alter anyone's effective access, the request
50
- // must carry confirmRevision equal to the current revision, else 409 with a fresh preview.
51
- // Returns {proceed:true, expectedRevision} (expectedRevision null when nothing was affected).
52
- function requireConfirmation(req, res, change, confirmRevision) {
53
- const p = previewLib.preview(change, req.authz.subject);
54
- if (p.counts.affected === 0) return { proceed: true, expectedRevision: null };
55
- const given =
56
- confirmRevision === undefined || confirmRevision === null ? null : Number(confirmRevision);
57
- if (given === null) {
58
- sendJson(res, 409, { code: 'CONFIRMATION_REQUIRED', preview: p });
59
- return { proceed: false };
60
- }
61
- if (given !== p.revision) {
62
- sendJson(res, 409, { code: 'STALE_PREVIEW', preview: p });
63
- return { proceed: false };
64
- }
65
- return { proceed: true, expectedRevision: given };
66
- }
67
-
68
- // Maps a StaleRevisionError thrown by mutate() into the same 409 the pre-check produces.
69
- function handleStale(res, e, change, req) {
70
- if (e instanceof StaleRevisionError) {
71
- sendJson(res, 409, {
72
- code: 'STALE_PREVIEW',
73
- preview: previewLib.preview(change, req.authz.subject),
74
- });
75
- return true;
76
- }
77
- return false;
78
- }
79
-
80
- module.exports = {
81
- mutate,
82
- sendJson,
83
- requireDelegable,
84
- requireConfirmation,
85
- handleStale,
86
- StaleRevisionError,
87
- };
1
+ // Shared plumbing for access-changing routes: delegation guard, impact confirmation, and an
2
+ // atomic "mutation + audit + revision bump" transaction that emits the revision event only
3
+ // after commit (so open streams revalidate against committed data).
4
+ const { db } = require('../db');
5
+ const store = require('../authz-store');
6
+ const authz = require('./index');
7
+ const { canDelegate } = require('./delegation');
8
+ const previewLib = require('./preview');
9
+
10
+ class StaleRevisionError extends Error {}
11
+
12
+ // Runs fn() in one transaction, bumps the authorization revision inside it, then emits the
13
+ // revision event after commit. If `expectedRevision` is given and the stored revision has
14
+ // moved, aborts (nothing written) with StaleRevisionError.
15
+ function mutate(fn, { expectedRevision = null } = {}) {
16
+ let rev;
17
+ const out = db.transaction(() => {
18
+ if (expectedRevision !== null && store.getAuthzRevision() !== expectedRevision) {
19
+ throw new StaleRevisionError('stale');
20
+ }
21
+ const r = fn();
22
+ rev = store.bumpAuthzRevision();
23
+ return r;
24
+ })();
25
+ authz.authzEvents.emit('revision', rev);
26
+ return out;
27
+ }
28
+
29
+ function sendJson(res, status, body) {
30
+ res.writeHead(status, { 'Content-Type': 'application/json' });
31
+ res.end(JSON.stringify(body));
32
+ }
33
+
34
+ // Delegation guard (privilege-escalation check). Returns true to proceed; otherwise writes a
35
+ // 403 listing what the actor cannot grant.
36
+ function requireDelegable(req, res, change) {
37
+ const state = store.loadState();
38
+ const rules = previewLib.rulesToDelegate(state, change);
39
+ if (rules.length === 0) return true;
40
+ const r = canDelegate(req.authz.subject, rules);
41
+ if (r.ok) return true;
42
+ sendJson(res, 403, {
43
+ error: 'You cannot grant access you do not hold yourself.',
44
+ denied: r.denied.map((d) => d.rule || d),
45
+ });
46
+ return false;
47
+ }
48
+
49
+ // Impact confirmation. If the change would alter anyone's effective access, the request
50
+ // must carry confirmRevision equal to the current revision, else 409 with a fresh preview.
51
+ // Returns {proceed:true, expectedRevision} (expectedRevision null when nothing was affected).
52
+ function requireConfirmation(req, res, change, confirmRevision) {
53
+ const p = previewLib.preview(change, req.authz.subject);
54
+ if (p.counts.affected === 0) return { proceed: true, expectedRevision: null };
55
+ const given =
56
+ confirmRevision === undefined || confirmRevision === null ? null : Number(confirmRevision);
57
+ if (given === null) {
58
+ sendJson(res, 409, { code: 'CONFIRMATION_REQUIRED', preview: p });
59
+ return { proceed: false };
60
+ }
61
+ if (given !== p.revision) {
62
+ sendJson(res, 409, { code: 'STALE_PREVIEW', preview: p });
63
+ return { proceed: false };
64
+ }
65
+ return { proceed: true, expectedRevision: given };
66
+ }
67
+
68
+ // Maps a StaleRevisionError thrown by mutate() into the same 409 the pre-check produces.
69
+ function handleStale(res, e, change, req) {
70
+ if (e instanceof StaleRevisionError) {
71
+ sendJson(res, 409, {
72
+ code: 'STALE_PREVIEW',
73
+ preview: previewLib.preview(change, req.authz.subject),
74
+ });
75
+ return true;
76
+ }
77
+ return false;
78
+ }
79
+
80
+ module.exports = {
81
+ mutate,
82
+ sendJson,
83
+ requireDelegable,
84
+ requireConfirmation,
85
+ handleStale,
86
+ StaleRevisionError,
87
+ };