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,131 +1,131 @@
1
- // Env-file masking for callers without env.reveal (see docs/env-files.md and
2
- // .claude/plans/permissions-v2-implementation.md section 6).
3
- //
4
- // The row model deliberately mirrors frontend/src/lib/envParse.ts: LF/CRLF, an optional
5
- // trailing newline, and a backslash-continued value kept as ONE raw row. Only `KEY=value`
6
- // lines are "pairs"; everything else is a raw row.
7
- //
8
- // Masking replaces every secret-bearing thing with the token `__pulse_masked__:<rowIndex>`:
9
- // * a pair's VALUE: KEY=__pulse_masked__:3
10
- // * a raw row that is neither blank nor a single-line `#` comment (`export K=v`,
11
- // indented assignments, continuation runs, ...) — the WHOLE row: __pulse_masked__:7
12
- // Blank lines and single-line comments pass through (do not keep secrets in comments).
13
- // The row index refers to the ORIGINAL file, so the server can resolve an untouched token
14
- // back to the real text on save without the client ever having seen it.
15
-
16
- const MASK_PREFIX = '__pulse_masked__:';
17
- const TOKEN_RE = /^__pulse_masked__:(\d+)$/;
18
- const PAIR_RE = /^([A-Za-z_][A-Za-z0-9_.-]*)=(.*)$/;
19
-
20
- // -> { rows: [{kind:'pair',key,value} | {kind:'raw',text}], eol, trailingNewline }
21
- function parseEnv(content) {
22
- const eol = content.includes('\r\n') ? '\r\n' : '\n';
23
- const trailingNewline = content === '' ? true : content.endsWith('\n');
24
- const body = trailingNewline ? content.slice(0, content.length - eol.length) : content;
25
- const lines = content === '' ? [] : body.split(/\r?\n/);
26
- const rows = [];
27
- for (let i = 0; i < lines.length; i++) {
28
- const line = lines[i];
29
- if (line.endsWith('\\')) {
30
- // A continued value spans several physical lines — keep the whole run untouched.
31
- let text = line;
32
- while (text.endsWith('\\') && i + 1 < lines.length) {
33
- i++;
34
- text += eol + lines[i];
35
- }
36
- rows.push({ kind: 'raw', text });
37
- continue;
38
- }
39
- const m = PAIR_RE.exec(line);
40
- rows.push(m ? { kind: 'pair', key: m[1], value: m[2] } : { kind: 'raw', text: line });
41
- }
42
- return { rows, eol, trailingNewline };
43
- }
44
-
45
- function serializeEnv({ rows, eol, trailingNewline }) {
46
- if (rows.length === 0) return '';
47
- const text = rows.map((r) => (r.kind === 'pair' ? `${r.key}=${r.value}` : r.text)).join(eol);
48
- return trailingNewline ? text + eol : text;
49
- }
50
-
51
- // Whether a raw row is masked (as opposed to passed through verbatim): anything that is not
52
- // blank and not a SINGLE-line comment. A multi-line run is always masked, even one that
53
- // starts with '#': a comment ending in a backslash swallows the following physical line(s),
54
- // which could hold a real assignment.
55
- function isMaskedRaw(row) {
56
- if (row.kind !== 'raw') return false;
57
- const trimmed = row.text.trim();
58
- if (trimmed === '') return false;
59
- if (trimmed.startsWith('#') && !/[\r\n]/.test(row.text)) return false;
60
- return true;
61
- }
62
-
63
- const tokenFor = (index) => `${MASK_PREFIX}${index}`;
64
-
65
- // -> { content, masked: true }
66
- function maskContent(content) {
67
- const parsed = parseEnv(content);
68
- const rows = parsed.rows.map((row, i) => {
69
- if (row.kind === 'pair') return { kind: 'pair', key: row.key, value: tokenFor(i) };
70
- if (isMaskedRaw(row)) return { kind: 'raw', text: tokenFor(i) };
71
- return row;
72
- });
73
- return { content: serializeEnv({ ...parsed, rows }), masked: true };
74
- }
75
-
76
- class MaskResolveError extends Error {
77
- constructor(message) {
78
- super(message);
79
- this.code = 'BAD_MASK_TOKEN';
80
- }
81
- }
82
-
83
- // Resolves the tokens in `submitted` (what a masked editor sent back) against the file's
84
- // CURRENT real content. A pair value or raw row that is EXACTLY a token becomes the original
85
- // value / row at that index; anything else is the user's own new text. Throws
86
- // MaskResolveError for an unknown index, a token pointing at the wrong kind of row, or a
87
- // token embedded inside longer text (which could otherwise be used to splice secrets next to
88
- // attacker-chosen text in a way the user never saw).
89
- function resolveMaskedTokens(submitted, originalContent) {
90
- const orig = parseEnv(originalContent);
91
- const sub = parseEnv(submitted);
92
-
93
- const original = (index, what) => {
94
- const row = orig.rows[index];
95
- if (!row) throw new MaskResolveError(`Unknown masked row ${index}.`);
96
- if (what === 'pair' ? row.kind !== 'pair' : !isMaskedRaw(row)) {
97
- throw new MaskResolveError(`Masked row ${index} does not match where it was placed.`);
98
- }
99
- return row;
100
- };
101
-
102
- const rows = sub.rows.map((row) => {
103
- if (row.kind === 'pair') {
104
- const m = TOKEN_RE.exec(row.value);
105
- if (m) return { kind: 'pair', key: row.key, value: original(Number(m[1]), 'pair').value };
106
- if (row.value.includes(MASK_PREFIX)) {
107
- throw new MaskResolveError('A masked placeholder cannot be embedded in a longer value.');
108
- }
109
- return row;
110
- }
111
- const m = TOKEN_RE.exec(row.text);
112
- if (m) return original(Number(m[1]), 'raw');
113
- if (row.text.includes(MASK_PREFIX)) {
114
- throw new MaskResolveError('A masked placeholder cannot be embedded in a longer line.');
115
- }
116
- // New raw text the user typed: normalise any line endings to the file's own.
117
- return { kind: 'raw', text: row.text.replace(/\r?\n/g, orig.eol) };
118
- });
119
-
120
- return serializeEnv({ rows, eol: orig.eol, trailingNewline: sub.trailingNewline });
121
- }
122
-
123
- module.exports = {
124
- MASK_PREFIX,
125
- PAIR_RE,
126
- MaskResolveError,
127
- parseEnv,
128
- serializeEnv,
129
- maskContent,
130
- resolveMaskedTokens,
131
- };
1
+ // Env-file masking for callers without env.reveal (see docs/env-files.md and
2
+ // .claude/plans/permissions-v2-implementation.md section 6).
3
+ //
4
+ // The row model deliberately mirrors frontend/src/lib/envParse.ts: LF/CRLF, an optional
5
+ // trailing newline, and a backslash-continued value kept as ONE raw row. Only `KEY=value`
6
+ // lines are "pairs"; everything else is a raw row.
7
+ //
8
+ // Masking replaces every secret-bearing thing with the token `__pulse_masked__:<rowIndex>`:
9
+ // * a pair's VALUE: KEY=__pulse_masked__:3
10
+ // * a raw row that is neither blank nor a single-line `#` comment (`export K=v`,
11
+ // indented assignments, continuation runs, ...) — the WHOLE row: __pulse_masked__:7
12
+ // Blank lines and single-line comments pass through (do not keep secrets in comments).
13
+ // The row index refers to the ORIGINAL file, so the server can resolve an untouched token
14
+ // back to the real text on save without the client ever having seen it.
15
+
16
+ const MASK_PREFIX = '__pulse_masked__:';
17
+ const TOKEN_RE = /^__pulse_masked__:(\d+)$/;
18
+ const PAIR_RE = /^([A-Za-z_][A-Za-z0-9_.-]*)=(.*)$/;
19
+
20
+ // -> { rows: [{kind:'pair',key,value} | {kind:'raw',text}], eol, trailingNewline }
21
+ function parseEnv(content) {
22
+ const eol = content.includes('\r\n') ? '\r\n' : '\n';
23
+ const trailingNewline = content === '' ? true : content.endsWith('\n');
24
+ const body = trailingNewline ? content.slice(0, content.length - eol.length) : content;
25
+ const lines = content === '' ? [] : body.split(/\r?\n/);
26
+ const rows = [];
27
+ for (let i = 0; i < lines.length; i++) {
28
+ const line = lines[i];
29
+ if (line.endsWith('\\')) {
30
+ // A continued value spans several physical lines — keep the whole run untouched.
31
+ let text = line;
32
+ while (text.endsWith('\\') && i + 1 < lines.length) {
33
+ i++;
34
+ text += eol + lines[i];
35
+ }
36
+ rows.push({ kind: 'raw', text });
37
+ continue;
38
+ }
39
+ const m = PAIR_RE.exec(line);
40
+ rows.push(m ? { kind: 'pair', key: m[1], value: m[2] } : { kind: 'raw', text: line });
41
+ }
42
+ return { rows, eol, trailingNewline };
43
+ }
44
+
45
+ function serializeEnv({ rows, eol, trailingNewline }) {
46
+ if (rows.length === 0) return '';
47
+ const text = rows.map((r) => (r.kind === 'pair' ? `${r.key}=${r.value}` : r.text)).join(eol);
48
+ return trailingNewline ? text + eol : text;
49
+ }
50
+
51
+ // Whether a raw row is masked (as opposed to passed through verbatim): anything that is not
52
+ // blank and not a SINGLE-line comment. A multi-line run is always masked, even one that
53
+ // starts with '#': a comment ending in a backslash swallows the following physical line(s),
54
+ // which could hold a real assignment.
55
+ function isMaskedRaw(row) {
56
+ if (row.kind !== 'raw') return false;
57
+ const trimmed = row.text.trim();
58
+ if (trimmed === '') return false;
59
+ if (trimmed.startsWith('#') && !/[\r\n]/.test(row.text)) return false;
60
+ return true;
61
+ }
62
+
63
+ const tokenFor = (index) => `${MASK_PREFIX}${index}`;
64
+
65
+ // -> { content, masked: true }
66
+ function maskContent(content) {
67
+ const parsed = parseEnv(content);
68
+ const rows = parsed.rows.map((row, i) => {
69
+ if (row.kind === 'pair') return { kind: 'pair', key: row.key, value: tokenFor(i) };
70
+ if (isMaskedRaw(row)) return { kind: 'raw', text: tokenFor(i) };
71
+ return row;
72
+ });
73
+ return { content: serializeEnv({ ...parsed, rows }), masked: true };
74
+ }
75
+
76
+ class MaskResolveError extends Error {
77
+ constructor(message) {
78
+ super(message);
79
+ this.code = 'BAD_MASK_TOKEN';
80
+ }
81
+ }
82
+
83
+ // Resolves the tokens in `submitted` (what a masked editor sent back) against the file's
84
+ // CURRENT real content. A pair value or raw row that is EXACTLY a token becomes the original
85
+ // value / row at that index; anything else is the user's own new text. Throws
86
+ // MaskResolveError for an unknown index, a token pointing at the wrong kind of row, or a
87
+ // token embedded inside longer text (which could otherwise be used to splice secrets next to
88
+ // attacker-chosen text in a way the user never saw).
89
+ function resolveMaskedTokens(submitted, originalContent) {
90
+ const orig = parseEnv(originalContent);
91
+ const sub = parseEnv(submitted);
92
+
93
+ const original = (index, what) => {
94
+ const row = orig.rows[index];
95
+ if (!row) throw new MaskResolveError(`Unknown masked row ${index}.`);
96
+ if (what === 'pair' ? row.kind !== 'pair' : !isMaskedRaw(row)) {
97
+ throw new MaskResolveError(`Masked row ${index} does not match where it was placed.`);
98
+ }
99
+ return row;
100
+ };
101
+
102
+ const rows = sub.rows.map((row) => {
103
+ if (row.kind === 'pair') {
104
+ const m = TOKEN_RE.exec(row.value);
105
+ if (m) return { kind: 'pair', key: row.key, value: original(Number(m[1]), 'pair').value };
106
+ if (row.value.includes(MASK_PREFIX)) {
107
+ throw new MaskResolveError('A masked placeholder cannot be embedded in a longer value.');
108
+ }
109
+ return row;
110
+ }
111
+ const m = TOKEN_RE.exec(row.text);
112
+ if (m) return original(Number(m[1]), 'raw');
113
+ if (row.text.includes(MASK_PREFIX)) {
114
+ throw new MaskResolveError('A masked placeholder cannot be embedded in a longer line.');
115
+ }
116
+ // New raw text the user typed: normalise any line endings to the file's own.
117
+ return { kind: 'raw', text: row.text.replace(/\r?\n/g, orig.eol) };
118
+ });
119
+
120
+ return serializeEnv({ rows, eol: orig.eol, trailingNewline: sub.trailingNewline });
121
+ }
122
+
123
+ module.exports = {
124
+ MASK_PREFIX,
125
+ PAIR_RE,
126
+ MaskResolveError,
127
+ parseEnv,
128
+ serializeEnv,
129
+ maskContent,
130
+ resolveMaskedTokens,
131
+ };
@@ -1,83 +1,83 @@
1
- // Invite flow helpers shared by routes/invites.js (create/resend), routes/invites-public.js
2
- // (info/accept/resend-request), and routes/auth.js (the Google OAuth callback parses an
3
- // invite token carried through the oauth_state cookie).
4
-
5
- const invites = require('./invites');
6
- const gmailSend = require('./gmail-send');
7
- const db = require('./db');
8
- const config = require('../config');
9
-
10
- // Split out so the resend route can check this BEFORE revoking the old invite — a
11
- // misconfigured hub shouldn't cost the operator their only pending invite for that email.
12
- function checkInviteSendingConfigured() {
13
- if (!gmailSend.isEnabled()) {
14
- return {
15
- error:
16
- 'Invite email sending is not configured on this hub. Set PULSE_GMAIL_ADDRESS and PULSE_GMAIL_APP_PASSWORD in backend/.env.',
17
- };
18
- }
19
- if (!config.PULSE_PUBLIC_URL) {
20
- return {
21
- error: 'PULSE_PUBLIC_URL is not configured on this hub, so an invite link cannot be built.',
22
- };
23
- }
24
- return {};
25
- }
26
-
27
- async function createAndSendInvite(email, invitedByUserId, expiresInHours, teamIds) {
28
- const configError = checkInviteSendingConfigured();
29
- if (configError.error) return configError;
30
- const { id, token } = invites.createInvite(email, invitedByUserId, expiresInHours, teamIds);
31
- const inviteLink = `${config.PULSE_PUBLIC_URL}/invite?token=${id}.${token}`;
32
- const resendLink = `${config.PULSE_PUBLIC_URL}/invite-resend?token=${id}.${token}`;
33
- const invitedByUser = db.getUserById(invitedByUserId);
34
- try {
35
- await gmailSend.sendInviteEmail({
36
- toEmail: email,
37
- inviteLink,
38
- resendLink,
39
- expiresInHours,
40
- invitedByLabel:
41
- (invitedByUser && (invitedByUser.email || invitedByUser.username)) || 'a Pulse user',
42
- });
43
- } catch (e) {
44
- invites.revokeInvite(id); // don't leave a pending invite behind if the email never actually went out
45
- return { error: e.message };
46
- }
47
- return { ok: true };
48
- }
49
- createAndSendInvite.checkConfigured = checkInviteSendingConfigured;
50
-
51
- // Invite links carry one combined "<inviteId>.<rawToken>" string (see lib/invites.js) —
52
- // a single query-string value.
53
- function parseCombinedInviteToken(combined) {
54
- if (!combined) return null;
55
- const dot = combined.indexOf('.');
56
- if (dot === -1) return null;
57
- return { inviteId: combined.slice(0, dot), token: combined.slice(dot + 1) };
58
- }
59
-
60
- // Renders the plain confirmation page a browser lands on after clicking the "trouble
61
- // with this link?" button in an invite email (see lib/gmail-send.js) — a real page, not
62
- // JSON, since a human is looking at it directly, not the invite-accept SPA (which needs
63
- // a working invite link to even load).
64
- function renderInviteResendPage(title, message) {
65
- return `<!doctype html>
66
- <html>
67
- <head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>${title} — Pulse</title></head>
68
- <body style="margin:0;min-height:100vh;display:flex;align-items:center;justify-content:center;background:#0b0b0c;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Helvetica,Arial,sans-serif;padding:24px;">
69
- <div style="max-width:420px;text-align:center;">
70
- <div style="font-size:13px;font-weight:700;letter-spacing:0.04em;color:#8b8b93;text-transform:uppercase;margin-bottom:16px;">Pulse</div>
71
- <h1 style="font-size:18px;color:#f5f5f6;margin:0 0 8px 0;">${title}</h1>
72
- <p style="font-size:14px;line-height:1.6;color:#a8a8b0;margin:0;">${message}</p>
73
- </div>
74
- </body>
75
- </html>`;
76
- }
77
-
78
- module.exports = {
79
- checkInviteSendingConfigured,
80
- createAndSendInvite,
81
- parseCombinedInviteToken,
82
- renderInviteResendPage,
83
- };
1
+ // Invite flow helpers shared by routes/invites.js (create/resend), routes/invites-public.js
2
+ // (info/accept/resend-request), and routes/auth.js (the Google OAuth callback parses an
3
+ // invite token carried through the oauth_state cookie).
4
+
5
+ const invites = require('./invites');
6
+ const gmailSend = require('./gmail-send');
7
+ const db = require('./db');
8
+ const config = require('../config');
9
+
10
+ // Split out so the resend route can check this BEFORE revoking the old invite — a
11
+ // misconfigured hub shouldn't cost the operator their only pending invite for that email.
12
+ function checkInviteSendingConfigured() {
13
+ if (!gmailSend.isEnabled()) {
14
+ return {
15
+ error:
16
+ 'Invite email sending is not configured on this hub. Set PULSE_GMAIL_ADDRESS and PULSE_GMAIL_APP_PASSWORD in backend/.env.',
17
+ };
18
+ }
19
+ if (!config.PULSE_PUBLIC_URL) {
20
+ return {
21
+ error: 'PULSE_PUBLIC_URL is not configured on this hub, so an invite link cannot be built.',
22
+ };
23
+ }
24
+ return {};
25
+ }
26
+
27
+ async function createAndSendInvite(email, invitedByUserId, expiresInHours, teamIds) {
28
+ const configError = checkInviteSendingConfigured();
29
+ if (configError.error) return configError;
30
+ const { id, token } = invites.createInvite(email, invitedByUserId, expiresInHours, teamIds);
31
+ const inviteLink = `${config.PULSE_PUBLIC_URL}/invite?token=${id}.${token}`;
32
+ const resendLink = `${config.PULSE_PUBLIC_URL}/invite-resend?token=${id}.${token}`;
33
+ const invitedByUser = db.getUserById(invitedByUserId);
34
+ try {
35
+ await gmailSend.sendInviteEmail({
36
+ toEmail: email,
37
+ inviteLink,
38
+ resendLink,
39
+ expiresInHours,
40
+ invitedByLabel:
41
+ (invitedByUser && (invitedByUser.email || invitedByUser.username)) || 'a Pulse user',
42
+ });
43
+ } catch (e) {
44
+ invites.revokeInvite(id); // don't leave a pending invite behind if the email never actually went out
45
+ return { error: e.message };
46
+ }
47
+ return { ok: true };
48
+ }
49
+ createAndSendInvite.checkConfigured = checkInviteSendingConfigured;
50
+
51
+ // Invite links carry one combined "<inviteId>.<rawToken>" string (see lib/invites.js) —
52
+ // a single query-string value.
53
+ function parseCombinedInviteToken(combined) {
54
+ if (!combined) return null;
55
+ const dot = combined.indexOf('.');
56
+ if (dot === -1) return null;
57
+ return { inviteId: combined.slice(0, dot), token: combined.slice(dot + 1) };
58
+ }
59
+
60
+ // Renders the plain confirmation page a browser lands on after clicking the "trouble
61
+ // with this link?" button in an invite email (see lib/gmail-send.js) — a real page, not
62
+ // JSON, since a human is looking at it directly, not the invite-accept SPA (which needs
63
+ // a working invite link to even load).
64
+ function renderInviteResendPage(title, message) {
65
+ return `<!doctype html>
66
+ <html>
67
+ <head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>${title} — Pulse</title></head>
68
+ <body style="margin:0;min-height:100vh;display:flex;align-items:center;justify-content:center;background:#0b0b0c;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Helvetica,Arial,sans-serif;padding:24px;">
69
+ <div style="max-width:420px;text-align:center;">
70
+ <div style="font-size:13px;font-weight:700;letter-spacing:0.04em;color:#8b8b93;text-transform:uppercase;margin-bottom:16px;">Pulse</div>
71
+ <h1 style="font-size:18px;color:#f5f5f6;margin:0 0 8px 0;">${title}</h1>
72
+ <p style="font-size:14px;line-height:1.6;color:#a8a8b0;margin:0;">${message}</p>
73
+ </div>
74
+ </body>
75
+ </html>`;
76
+ }
77
+
78
+ module.exports = {
79
+ checkInviteSendingConfigured,
80
+ createAndSendInvite,
81
+ parseCombinedInviteToken,
82
+ renderInviteResendPage,
83
+ };