hopwire 0.4.0 → 0.5.1

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.
package/dist/signature.js CHANGED
@@ -73,12 +73,22 @@ const VERIFIERS = {
73
73
  return same(value.slice(7), hmac(secret, body, 'hex')) ? { timestamp: null } : MISMATCH;
74
74
  },
75
75
  },
76
- // X-Gitlab-Token: the secret token itself
76
+ // Two kinds of secret, both optional on a GitLab webhook: a signing token (whsec_<base64 key>, GitLab 19.0 and later)
77
+ // signs with the Standard Webhooks headers webhook-id, webhook-timestamp and webhook-signature, as Svix does; a
78
+ // secret token is sent as it is in X-Gitlab-Token. The secret named decides which one is checked.
77
79
  gitlab: {
78
- signs: (h) => h.has('x-gitlab-token'),
79
- check: (h, _body, secret) => same(h.get('x-gitlab-token') ?? '', secret)
80
- ? { timestamp: null }
81
- : 'the X-Gitlab-Token header is not the secret token of this webhook',
80
+ signs: (h) => h.has('x-gitlab-token') || (h.has('x-gitlab-event') && h.has('webhook-signature')),
81
+ check: (h, body, secret) => {
82
+ if (isSigningToken(secret) && h.has('webhook-signature')) {
83
+ return standardWebhook(h, body, secret, 'webhook');
84
+ }
85
+ if (h.has('x-gitlab-token')) {
86
+ return same(h.get('x-gitlab-token') ?? '', secret)
87
+ ? { timestamp: null }
88
+ : 'the X-Gitlab-Token header is not the secret token of this webhook';
89
+ }
90
+ return 'this webhook is signed with a signing token (whsec_...), and the secret named is not one';
91
+ },
82
92
  },
83
93
  // X-Shopify-Hmac-Sha256: base64 HMAC-SHA256 over the body
84
94
  shopify: {
@@ -99,24 +109,10 @@ const VERIFIERS = {
99
109
  },
100
110
  },
101
111
  // svix-id, svix-timestamp, svix-signature: "v1,<base64> ..."; the secret is whsec_<base64 key>
112
+ // (Standard Webhooks uses the same scheme with webhook-* headers; a GitLab webhook signed that way is GitLab's)
102
113
  svix: {
103
- signs: (h) => h.has('svix-signature') || h.has('webhook-signature'),
104
- check: (h, body, secret) => {
105
- const id = h.get('svix-id') ?? h.get('webhook-id') ?? '';
106
- const ts = h.get('svix-timestamp') ?? h.get('webhook-timestamp') ?? '';
107
- const value = h.get('svix-signature') ?? h.get('webhook-signature') ?? '';
108
- if (id === '' || !/^\d+$/.test(ts)) {
109
- return 'the id or timestamp header of the signature is missing';
110
- }
111
- const key = Buffer.from(secret.startsWith('whsec_') ? secret.slice(6) : secret, 'base64');
112
- const expected = createHmac('sha256', key)
113
- .update(Buffer.concat([Buffer.from(`${id}.${ts}.`), body]))
114
- .digest('base64');
115
- const signatures = value.split(' ').map((entry) => entry.split(',', 2));
116
- return signatures.some(([version, signature]) => version === 'v1' && same(signature ?? '', expected))
117
- ? { timestamp: Number(ts) * 1000 }
118
- : MISMATCH;
119
- },
114
+ signs: (h) => h.has('svix-signature') || (h.has('webhook-signature') && !h.has('x-gitlab-event')),
115
+ check: (h, body, secret) => standardWebhook(h, body, secret, h.has('svix-signature') ? 'svix' : 'webhook'),
120
116
  },
121
117
  // Paddle-Signature: ts=<seconds>;h1=<hex>; HMAC-SHA256 over "<ts>:<body>"
122
118
  paddle: {
@@ -138,6 +134,33 @@ const VERIFIERS = {
138
134
  check: (h, body, secret) => same(h.get('linear-signature') ?? '', hmac(secret, body, 'hex')) ? { timestamp: null } : MISMATCH,
139
135
  },
140
136
  };
137
+ /** A signing secret of the Standard Webhooks scheme: whsec_ and the base64 of the key. */
138
+ function isSigningToken(secret) {
139
+ return secret.startsWith('whsec_');
140
+ }
141
+ /** The key of a Standard Webhooks secret: the bytes the base64 after whsec_ stands for. */
142
+ function standardKey(secret) {
143
+ return Buffer.from(isSigningToken(secret) ? secret.slice(6) : secret, 'base64');
144
+ }
145
+ /** <prefix>-id, <prefix>-timestamp, <prefix>-signature: "v1,<base64> ..."; HMAC-SHA256 over "<id>.<timestamp>.<body>" */
146
+ function standardWebhook(h, body, secret, prefix) {
147
+ const id = h.get(`${prefix}-id`) ?? '';
148
+ const ts = h.get(`${prefix}-timestamp`) ?? '';
149
+ const value = h.get(`${prefix}-signature`) ?? '';
150
+ if (id === '' || !/^\d+$/.test(ts)) {
151
+ return 'the id or timestamp header of the signature is missing';
152
+ }
153
+ const expected = standardSignature(standardKey(secret), id, ts, body);
154
+ const signatures = value.split(' ').map((entry) => entry.split(',', 2));
155
+ return signatures.some(([version, signature]) => version === 'v1' && same(signature ?? '', expected))
156
+ ? { timestamp: Number(ts) * 1000 }
157
+ : MISMATCH;
158
+ }
159
+ function standardSignature(key, id, timestamp, body) {
160
+ return createHmac('sha256', key)
161
+ .update(Buffer.concat([Buffer.from(`${id}.${timestamp}.`), body]))
162
+ .digest('base64');
163
+ }
141
164
  function hmac(secret, data, encoding) {
142
165
  return createHmac('sha256', secret).update(data).digest(encoding);
143
166
  }
@@ -154,8 +177,8 @@ export function isVariableName(name) {
154
177
  /**
155
178
  * The headers of a request signed again for `provider` at `timestampSeconds` (hopwire verify and chaos): the recorded
156
179
  * signature headers are replaced, the rest kept. With `wrongKey` the signature is made with another key, for a handler
157
- * that must refuse it. Without a timestamp in the provider's scheme (GitHub, GitLab, Shopify, Linear) only the
158
- * signature changes.
180
+ * that must refuse it. Without a timestamp in the provider's scheme (GitHub, Shopify, Linear, a GitLab secret token)
181
+ * only the signature changes.
159
182
  */
160
183
  export function signHeaders(headers, body, secret, timestampSeconds, wrongKey = false) {
161
184
  const key = wrongKey ? `${secret.secret}-not-the-signing-secret` : secret.secret;
@@ -168,9 +191,17 @@ export function signHeaders(headers, body, secret, timestampSeconds, wrongKey =
168
191
  case 'github':
169
192
  set.set('x-hub-signature-256', `sha256=${hmac(key, body, 'hex')}`);
170
193
  break;
171
- case 'gitlab':
172
- set.set('x-gitlab-token', key);
194
+ case 'gitlab': {
195
+ // a signing token signs again with a fresh time; a secret token is the header itself
196
+ const lower = new Map(headers.map(([name, value]) => [name.toLowerCase(), value]));
197
+ if (isSigningToken(secret.secret) && lower.has('webhook-signature')) {
198
+ standardHeaders(set, 'webhook', lower.get('webhook-id') ?? 'msg_hopwire', t, body, secret.secret, wrongKey);
199
+ }
200
+ else {
201
+ set.set('x-gitlab-token', key);
202
+ }
173
203
  break;
204
+ }
174
205
  case 'shopify':
175
206
  set.set('x-shopify-hmac-sha256', hmac(key, body, 'base64'));
176
207
  break;
@@ -181,14 +212,7 @@ export function signHeaders(headers, body, secret, timestampSeconds, wrongKey =
181
212
  case 'svix': {
182
213
  const lower = new Map(headers.map(([name, value]) => [name.toLowerCase(), value]));
183
214
  const prefix = lower.has('webhook-id') && !lower.has('svix-id') ? 'webhook' : 'svix';
184
- const id = lower.get(`${prefix}-id`) ?? 'msg_hopwire';
185
- const raw = Buffer.from(key.startsWith('whsec_') ? key.slice(6) : key, 'base64');
186
- const signature = createHmac('sha256', wrongKey ? Buffer.concat([raw, Buffer.from('x')]) : raw)
187
- .update(Buffer.concat([Buffer.from(`${id}.${t}.`), body]))
188
- .digest('base64');
189
- set.set(`${prefix}-id`, id);
190
- set.set(`${prefix}-timestamp`, t);
191
- set.set(`${prefix}-signature`, `v1,${signature}`);
215
+ standardHeaders(set, prefix, lower.get(`${prefix}-id`) ?? 'msg_hopwire', t, body, secret.secret, wrongKey);
192
216
  break;
193
217
  }
194
218
  case 'paddle':
@@ -201,6 +225,31 @@ export function signHeaders(headers, body, secret, timestampSeconds, wrongKey =
201
225
  const kept = headers.filter(([name]) => !set.has(name.toLowerCase()));
202
226
  return [...kept, ...set.entries()];
203
227
  }
228
+ /** The Standard Webhooks headers of `body`, with another key when `wrongKey`. */
229
+ function standardHeaders(set, prefix, id, timestamp, body, secret, wrongKey) {
230
+ const raw = standardKey(secret);
231
+ const key = wrongKey ? Buffer.concat([raw, Buffer.from('x')]) : raw;
232
+ set.set(`${prefix}-id`, id);
233
+ set.set(`${prefix}-timestamp`, timestamp);
234
+ set.set(`${prefix}-signature`, `v1,${standardSignature(key, id, timestamp, body)}`);
235
+ }
236
+ /**
237
+ * Whether the signature of a request carries the time it was made, so a stale one can be made: always for Stripe,
238
+ * Slack, Svix and Paddle, for GitLab only when it is signed with a signing token and the secret named is one.
239
+ */
240
+ export function carriesTime(headers, secret) {
241
+ switch (secret.provider) {
242
+ case 'stripe':
243
+ case 'slack':
244
+ case 'svix':
245
+ case 'paddle':
246
+ return true;
247
+ case 'gitlab':
248
+ return isSigningToken(secret.secret) && headers.some(([name]) => name.toLowerCase() === 'webhook-signature');
249
+ default:
250
+ return false;
251
+ }
252
+ }
204
253
  /** Whether a request carries a signature of `provider`: only those are signed again. */
205
254
  export function signedBy(headers, provider) {
206
255
  return VERIFIERS[provider].signs(new Map(headers.map(([name, value]) => [name.toLowerCase(), value])));
package/dist/terminal.js CHANGED
@@ -35,13 +35,37 @@ export function terminalLook(env) {
35
35
  return {
36
36
  truecolor: /^(truecolor|24bit)$/i.test(env.COLORTERM ?? ''),
37
37
  palette: background === 7 || background === 15 ? LIGHT : DARK,
38
- // The Linux console prints the escape as text
39
- links: env.TERM !== 'linux',
38
+ links: supportsHyperlinks(env),
40
39
  };
41
40
  }
41
+ /**
42
+ * Whether the terminal turns OSC 8 into a link. Only terminals known to do it get the escape; the others show the URL
43
+ * as text, which they find by themselves (Cmd+click in Apple Terminal). FORCE_HYPERLINK=1 or 0 overrides, as other
44
+ * command line tools read it.
45
+ */
46
+ export function supportsHyperlinks(env) {
47
+ if (env.FORCE_HYPERLINK !== undefined && env.FORCE_HYPERLINK !== '') {
48
+ return env.FORCE_HYPERLINK !== '0' && env.FORCE_HYPERLINK.toLowerCase() !== 'false';
49
+ }
50
+ // Inside tmux or screen the escape only passes when they are set up for it
51
+ if (env.TMUX !== undefined || /^(screen|tmux)/.test(env.TERM ?? '')) {
52
+ return false;
53
+ }
54
+ if (['iTerm.app', 'WezTerm', 'vscode', 'ghostty', 'Hyper', 'WarpTerminal'].includes(env.TERM_PROGRAM ?? '')) {
55
+ return true;
56
+ }
57
+ if (env.KITTY_WINDOW_ID !== undefined || env.WT_SESSION !== undefined || env.DOMTERM !== undefined) {
58
+ return true;
59
+ }
60
+ if (/^(xterm-kitty|xterm-ghostty|alacritty|foot)/.test(env.TERM ?? '')) {
61
+ return true;
62
+ }
63
+ // GNOME Terminal and the other VTE terminals since 0.50
64
+ return Number(env.VTE_VERSION ?? '0') >= 5000;
65
+ }
42
66
  const RESET = '\u001b[0m';
43
67
  /** Text in a colour, optionally bold. */
44
- function paint(look, text, tone, bold = false) {
68
+ export function paint(look, text, tone, bold = false) {
45
69
  if (text === '') {
46
70
  return '';
47
71
  }
@@ -139,6 +163,10 @@ export class TerminalScreen {
139
163
  return ofService === undefined || ofService.length === 0 ? null : checkSignature(exchange, ofService);
140
164
  };
141
165
  }
166
+ /** The signature check of a webhook row; null without a secret for its service or for a request that is none. */
167
+ signatureOf(exchange) {
168
+ return recogniseWebhook(exchange) === null ? null : this.verify(exchange);
169
+ }
142
170
  get look() {
143
171
  return this.options.look;
144
172
  }
@@ -473,7 +501,7 @@ export function sparkline(counts) {
473
501
  '')
474
502
  .join('');
475
503
  }
476
- const SOURCE_WORDS = {
504
+ export const SOURCE_WORDS = {
477
505
  local: 'local',
478
506
  p2p_peer: 'from a teammate',
479
507
  public_edge: 'through the edge',
@@ -483,7 +511,7 @@ const SOURCE_WORDS = {
483
511
  const DETAIL_HEADERS = 14;
484
512
  const DETAIL_BODY_LINES = 24;
485
513
  /** Headers whose values are credentials: shown shortened in the details block. */
486
- const CREDENTIAL_HEADERS = new Set([
514
+ export const CREDENTIAL_HEADERS = new Set([
487
515
  'authorization',
488
516
  'proxy-authorization',
489
517
  'cookie',
@@ -492,7 +520,7 @@ const CREDENTIAL_HEADERS = new Set([
492
520
  'api-key',
493
521
  ]);
494
522
  /** The first characters of a secret, enough to tell two apart. */
495
- function hidden(value) {
523
+ export function hidden(value) {
496
524
  return value.length <= 8 ? '••••' : `${value.slice(0, 6)}… (${value.length} characters)`;
497
525
  }
498
526
  /**
@@ -516,7 +544,7 @@ export function bodyLines(body, truncated) {
516
544
  return text.split(/\r?\n/);
517
545
  }
518
546
  /** The mark of a signature check in a row and in the details. */
519
- function signatureMark(check, palette) {
547
+ export function signatureMark(check, palette) {
520
548
  switch (check.result) {
521
549
  case 'valid':
522
550
  return check.ageMs !== null && check.ageMs > TOLERANCE_MS
@@ -545,7 +573,7 @@ export function signatureSentence(check) {
545
573
  }
546
574
  }
547
575
  /** The time of day as HH:MM:SS on this machine's clock. */
548
- function clock(timestamp) {
576
+ export function clock(timestamp) {
549
577
  const date = new Date(timestamp);
550
578
  return [date.getHours(), date.getMinutes(), date.getSeconds()]
551
579
  .map((part) => String(part).padStart(2, '0'))
@@ -600,7 +628,7 @@ function sourceTag(exchange, look) {
600
628
  return `${paint(look, 'local', palette.muted)}${ws}`;
601
629
  }
602
630
  }
603
- function statusTone(palette, status) {
631
+ export function statusTone(palette, status) {
604
632
  if (status >= 500) {
605
633
  return palette.err;
606
634
  }