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,104 +1,104 @@
1
- // Delegation and privilege escalation guard
2
- const { db } = require('../db');
3
- const { serverScopeSubset, serviceScopeSubset, envFileScopeSubset } = require('./scope');
4
- const { getAction } = require('./catalog');
5
-
6
- // Check if actor can delegate the given rules
7
- // Returns {ok: true} or {denied: [{rule, reason}]}
8
- function canDelegate(actorSubject, rules) {
9
- if (!actorSubject) return { denied: [{ reason: 'No subject' }] };
10
- if (actorSubject.isRoot) return { ok: true };
11
-
12
- const denied = [];
13
-
14
- for (const rule of rules) {
15
- if (!canDelegateRule(actorSubject, rule)) {
16
- denied.push({ rule, reason: 'Rule exceeds delegation scope' });
17
- }
18
- }
19
-
20
- return denied.length === 0 ? { ok: true } : { denied };
21
- }
22
-
23
- function canDelegateRule(actorSubject, ruleToDelegate) {
24
- if (actorSubject.isRoot) return true;
25
-
26
- // Actor must hold the action they're delegating
27
- // A held rule covers an action it implies too (env.edit covers env.view).
28
- const covers = (held, wanted) => {
29
- if (held === wanted) return true;
30
- const def = getAction(held);
31
- return !!def && def.implies.some((i) => covers(i, wanted));
32
- };
33
- const actorRules = actorSubject.rules.filter((r) => covers(r.action, ruleToDelegate.action));
34
- if (actorRules.length === 0) return false;
35
-
36
- // Check if any actor rule can cover this rule's scope
37
- return actorRules.some((actorRule) => ruleScopeSubset(ruleToDelegate, actorRule));
38
- }
39
-
40
- // Check if ruleA's scope is a subset of ruleB's scope
41
- function ruleScopeSubset(ruleA, ruleB) {
42
- // org actions have no scope
43
- if (!ruleA.scope && !ruleB.scope) return true;
44
- if (!ruleA.scope || !ruleB.scope) return false;
45
-
46
- // Check servers. ids ⊆ tags is only provable point-in-time against current access tags.
47
- const a = ruleA.scope.servers;
48
- const b = ruleB.scope.servers;
49
- if (a.type === 'ids' && b.type === 'tags') {
50
- if (!instanceIdsSatisfyTags(a.ids, b)) return false;
51
- } else if (!serverScopeSubset(a, b)) {
52
- return false;
53
- }
54
-
55
- // Check services
56
- if (!serviceScopeSubset(ruleA.scope.services, ruleB.scope.services)) {
57
- return false;
58
- }
59
-
60
- // Check env files
61
- if (!envFileScopeSubset(ruleA.scope.envFiles, ruleB.scope.envFiles)) {
62
- return false;
63
- }
64
-
65
- return true;
66
- }
67
-
68
- // For tag scopes: check if instanceIds currently satisfy the tag condition
69
- // Used at delegation time to determine if ids A are a subset of tags B
70
- function instanceIdsSatisfyTags(instanceIds, tagScope) {
71
- if (tagScope.type !== 'tags') return false;
72
-
73
- const match = tagScope.match || 'all';
74
-
75
- for (const instanceId of instanceIds) {
76
- const tags = db
77
- .prepare(
78
- `
79
- SELECT tag FROM instance_auth_tags WHERE instance_id = ?
80
- `,
81
- )
82
- .all(instanceId);
83
- const instanceTags = tags.map((t) => t.tag);
84
-
85
- if (match === 'all') {
86
- if (!tagScope.tags.every((t) => instanceTags.includes(t))) {
87
- return false;
88
- }
89
- } else {
90
- if (!tagScope.tags.some((t) => instanceTags.includes(t))) {
91
- return false;
92
- }
93
- }
94
- }
95
-
96
- return true;
97
- }
98
-
99
- module.exports = {
100
- canDelegate,
101
- canDelegateRule,
102
- ruleScopeSubset,
103
- instanceIdsSatisfyTags,
104
- };
1
+ // Delegation and privilege escalation guard
2
+ const { db } = require('../db');
3
+ const { serverScopeSubset, serviceScopeSubset, envFileScopeSubset } = require('./scope');
4
+ const { getAction } = require('./catalog');
5
+
6
+ // Check if actor can delegate the given rules
7
+ // Returns {ok: true} or {denied: [{rule, reason}]}
8
+ function canDelegate(actorSubject, rules) {
9
+ if (!actorSubject) return { denied: [{ reason: 'No subject' }] };
10
+ if (actorSubject.isRoot) return { ok: true };
11
+
12
+ const denied = [];
13
+
14
+ for (const rule of rules) {
15
+ if (!canDelegateRule(actorSubject, rule)) {
16
+ denied.push({ rule, reason: 'Rule exceeds delegation scope' });
17
+ }
18
+ }
19
+
20
+ return denied.length === 0 ? { ok: true } : { denied };
21
+ }
22
+
23
+ function canDelegateRule(actorSubject, ruleToDelegate) {
24
+ if (actorSubject.isRoot) return true;
25
+
26
+ // Actor must hold the action they're delegating
27
+ // A held rule covers an action it implies too (env.edit covers env.view).
28
+ const covers = (held, wanted) => {
29
+ if (held === wanted) return true;
30
+ const def = getAction(held);
31
+ return !!def && def.implies.some((i) => covers(i, wanted));
32
+ };
33
+ const actorRules = actorSubject.rules.filter((r) => covers(r.action, ruleToDelegate.action));
34
+ if (actorRules.length === 0) return false;
35
+
36
+ // Check if any actor rule can cover this rule's scope
37
+ return actorRules.some((actorRule) => ruleScopeSubset(ruleToDelegate, actorRule));
38
+ }
39
+
40
+ // Check if ruleA's scope is a subset of ruleB's scope
41
+ function ruleScopeSubset(ruleA, ruleB) {
42
+ // org actions have no scope
43
+ if (!ruleA.scope && !ruleB.scope) return true;
44
+ if (!ruleA.scope || !ruleB.scope) return false;
45
+
46
+ // Check servers. ids ⊆ tags is only provable point-in-time against current access tags.
47
+ const a = ruleA.scope.servers;
48
+ const b = ruleB.scope.servers;
49
+ if (a.type === 'ids' && b.type === 'tags') {
50
+ if (!instanceIdsSatisfyTags(a.ids, b)) return false;
51
+ } else if (!serverScopeSubset(a, b)) {
52
+ return false;
53
+ }
54
+
55
+ // Check services
56
+ if (!serviceScopeSubset(ruleA.scope.services, ruleB.scope.services)) {
57
+ return false;
58
+ }
59
+
60
+ // Check env files
61
+ if (!envFileScopeSubset(ruleA.scope.envFiles, ruleB.scope.envFiles)) {
62
+ return false;
63
+ }
64
+
65
+ return true;
66
+ }
67
+
68
+ // For tag scopes: check if instanceIds currently satisfy the tag condition
69
+ // Used at delegation time to determine if ids A are a subset of tags B
70
+ function instanceIdsSatisfyTags(instanceIds, tagScope) {
71
+ if (tagScope.type !== 'tags') return false;
72
+
73
+ const match = tagScope.match || 'all';
74
+
75
+ for (const instanceId of instanceIds) {
76
+ const tags = db
77
+ .prepare(
78
+ `
79
+ SELECT tag FROM instance_auth_tags WHERE instance_id = ?
80
+ `,
81
+ )
82
+ .all(instanceId);
83
+ const instanceTags = tags.map((t) => t.tag);
84
+
85
+ if (match === 'all') {
86
+ if (!tagScope.tags.every((t) => instanceTags.includes(t))) {
87
+ return false;
88
+ }
89
+ } else {
90
+ if (!tagScope.tags.some((t) => instanceTags.includes(t))) {
91
+ return false;
92
+ }
93
+ }
94
+ }
95
+
96
+ return true;
97
+ }
98
+
99
+ module.exports = {
100
+ canDelegate,
101
+ canDelegateRule,
102
+ ruleScopeSubset,
103
+ instanceIdsSatisfyTags,
104
+ };
@@ -1,104 +1,104 @@
1
- // "What can this person access?" — a per-person, per-server breakdown of the actions their
2
- // teams' policies grant, with provenance (which team + policy granted each). Powers
3
- // GET /api/users/:id/access and GET /api/me/access.
4
- const db = require('../db');
5
- const store = require('../authz-store');
6
- const engine = require('./engine');
7
- const { ACTIONS, getAction } = require('./catalog');
8
- const { serverMatches } = require('./scope');
9
-
10
- function grants(rule, action) {
11
- if (rule.action === action) return true;
12
- const def = getAction(rule.action);
13
- return !!def && def.implies.some((i) => grants({ action: i }, action));
14
- }
15
-
16
- function describeMatcher(m) {
17
- const v = `"${m.value}"`;
18
- switch (m.op) {
19
- case 'startsWith':
20
- return `starting with ${v}`;
21
- case 'endsWith':
22
- return `ending with ${v}`;
23
- case 'contains':
24
- return `containing ${v}`;
25
- case 'exact':
26
- return `named ${v}`;
27
- default:
28
- return `matching custom pattern ${v}`;
29
- }
30
- }
31
-
32
- function describeScope(scope, def) {
33
- if (!scope) return '';
34
- const parts = [];
35
- if (def.scopeKind !== 'org' && def.scopeKind !== 'server' && scope.services) {
36
- if (scope.services.matchers.length > 0)
37
- parts.push(`services ${scope.services.matchers.map(describeMatcher).join(' or ')}`);
38
- }
39
- if (def.scopeKind === 'envfile' && scope.envFiles)
40
- parts.push(`${scope.envFiles.ids.length} specific env file(s)`);
41
- return parts.length ? `Only ${parts.join(', ')}` : 'All services';
42
- }
43
-
44
- function provenance(rules) {
45
- const seen = new Set();
46
- const out = [];
47
- for (const r of rules) {
48
- const key = `${r.teamId}|${r.policyId}`;
49
- if (seen.has(key)) continue;
50
- seen.add(key);
51
- out.push({ teamName: r.teamName, policyName: r.policyName });
52
- }
53
- return out;
54
- }
55
-
56
- function effectiveAccess(user) {
57
- const teams = store.getUserTeams(user.id).map((t) => ({ id: t.id, name: t.name }));
58
- const subject = engine.buildSubject(user, store);
59
- if (!subject || subject.isRoot)
60
- return { isRoot: !!(subject && subject.isRoot), teams, org: [], servers: [] };
61
-
62
- const org = [];
63
- for (const def of ACTIONS.filter((a) => a.scopeKind === 'org')) {
64
- const rs = subject.rules.filter((r) => grants(r, def.id));
65
- if (rs.length) org.push({ action: def.id, label: def.label, grantedBy: provenance(rs) });
66
- }
67
-
68
- const tagsOf = new Map();
69
- for (const t of db.db.prepare('SELECT instance_id, tag FROM instance_auth_tags').all()) {
70
- if (!tagsOf.has(t.instance_id)) tagsOf.set(t.instance_id, []);
71
- tagsOf.get(t.instance_id).push(t.tag);
72
- }
73
- const authTagsOf = (id) => tagsOf.get(id) || [];
74
-
75
- const servers = [];
76
- for (const inst of db.listInstances()) {
77
- const byFeature = new Map();
78
- for (const def of ACTIONS.filter((a) => a.scopeKind !== 'org')) {
79
- const rs = subject.rules.filter(
80
- (r) =>
81
- r.scope &&
82
- grants(r, def.id) &&
83
- serverMatches(r.scope.servers, { id: inst.id }, authTagsOf),
84
- );
85
- if (rs.length === 0) continue;
86
- if (!byFeature.has(def.feature)) byFeature.set(def.feature, []);
87
- byFeature.get(def.feature).push({
88
- action: def.id,
89
- label: def.label,
90
- scopeDescription: [...new Set(rs.map((r) => describeScope(r.scope, def)))].join('; '),
91
- grantedBy: provenance(rs),
92
- });
93
- }
94
- if (byFeature.size === 0) continue;
95
- servers.push({
96
- instanceId: inst.id,
97
- label: inst.label,
98
- features: [...byFeature].map(([feature, actions]) => ({ feature, actions })),
99
- });
100
- }
101
- return { isRoot: false, teams, org, servers };
102
- }
103
-
104
- module.exports = { effectiveAccess, describeScope };
1
+ // "What can this person access?" — a per-person, per-server breakdown of the actions their
2
+ // teams' policies grant, with provenance (which team + policy granted each). Powers
3
+ // GET /api/users/:id/access and GET /api/me/access.
4
+ const db = require('../db');
5
+ const store = require('../authz-store');
6
+ const engine = require('./engine');
7
+ const { ACTIONS, getAction } = require('./catalog');
8
+ const { serverMatches } = require('./scope');
9
+
10
+ function grants(rule, action) {
11
+ if (rule.action === action) return true;
12
+ const def = getAction(rule.action);
13
+ return !!def && def.implies.some((i) => grants({ action: i }, action));
14
+ }
15
+
16
+ function describeMatcher(m) {
17
+ const v = `"${m.value}"`;
18
+ switch (m.op) {
19
+ case 'startsWith':
20
+ return `starting with ${v}`;
21
+ case 'endsWith':
22
+ return `ending with ${v}`;
23
+ case 'contains':
24
+ return `containing ${v}`;
25
+ case 'exact':
26
+ return `named ${v}`;
27
+ default:
28
+ return `matching custom pattern ${v}`;
29
+ }
30
+ }
31
+
32
+ function describeScope(scope, def) {
33
+ if (!scope) return '';
34
+ const parts = [];
35
+ if (def.scopeKind !== 'org' && def.scopeKind !== 'server' && scope.services) {
36
+ if (scope.services.matchers.length > 0)
37
+ parts.push(`services ${scope.services.matchers.map(describeMatcher).join(' or ')}`);
38
+ }
39
+ if (def.scopeKind === 'envfile' && scope.envFiles)
40
+ parts.push(`${scope.envFiles.ids.length} specific env file(s)`);
41
+ return parts.length ? `Only ${parts.join(', ')}` : 'All services';
42
+ }
43
+
44
+ function provenance(rules) {
45
+ const seen = new Set();
46
+ const out = [];
47
+ for (const r of rules) {
48
+ const key = `${r.teamId}|${r.policyId}`;
49
+ if (seen.has(key)) continue;
50
+ seen.add(key);
51
+ out.push({ teamName: r.teamName, policyName: r.policyName });
52
+ }
53
+ return out;
54
+ }
55
+
56
+ function effectiveAccess(user) {
57
+ const teams = store.getUserTeams(user.id).map((t) => ({ id: t.id, name: t.name }));
58
+ const subject = engine.buildSubject(user, store);
59
+ if (!subject || subject.isRoot)
60
+ return { isRoot: !!(subject && subject.isRoot), teams, org: [], servers: [] };
61
+
62
+ const org = [];
63
+ for (const def of ACTIONS.filter((a) => a.scopeKind === 'org')) {
64
+ const rs = subject.rules.filter((r) => grants(r, def.id));
65
+ if (rs.length) org.push({ action: def.id, label: def.label, grantedBy: provenance(rs) });
66
+ }
67
+
68
+ const tagsOf = new Map();
69
+ for (const t of db.db.prepare('SELECT instance_id, tag FROM instance_auth_tags').all()) {
70
+ if (!tagsOf.has(t.instance_id)) tagsOf.set(t.instance_id, []);
71
+ tagsOf.get(t.instance_id).push(t.tag);
72
+ }
73
+ const authTagsOf = (id) => tagsOf.get(id) || [];
74
+
75
+ const servers = [];
76
+ for (const inst of db.listInstances()) {
77
+ const byFeature = new Map();
78
+ for (const def of ACTIONS.filter((a) => a.scopeKind !== 'org')) {
79
+ const rs = subject.rules.filter(
80
+ (r) =>
81
+ r.scope &&
82
+ grants(r, def.id) &&
83
+ serverMatches(r.scope.servers, { id: inst.id }, authTagsOf),
84
+ );
85
+ if (rs.length === 0) continue;
86
+ if (!byFeature.has(def.feature)) byFeature.set(def.feature, []);
87
+ byFeature.get(def.feature).push({
88
+ action: def.id,
89
+ label: def.label,
90
+ scopeDescription: [...new Set(rs.map((r) => describeScope(r.scope, def)))].join('; '),
91
+ grantedBy: provenance(rs),
92
+ });
93
+ }
94
+ if (byFeature.size === 0) continue;
95
+ servers.push({
96
+ instanceId: inst.id,
97
+ label: inst.label,
98
+ features: [...byFeature].map(([feature, actions]) => ({ feature, actions })),
99
+ });
100
+ }
101
+ return { isRoot: false, teams, org, servers };
102
+ }
103
+
104
+ module.exports = { effectiveAccess, describeScope };