hopwire 0.2.1 → 0.4.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.
- package/dist/bin.js +142 -6
- package/dist/cli.js +164 -14
- package/dist/commands/chaos.js +186 -0
- package/dist/commands/fix.js +177 -0
- package/dist/commands/http.js +5 -2
- package/dist/commands/snapshot.js +81 -0
- package/dist/commands/up.js +5 -2
- package/dist/commands/verify.js +64 -0
- package/dist/commands/watch.js +12 -4
- package/dist/config-template.js +1 -0
- package/dist/config.js +22 -1
- package/dist/curl.js +59 -0
- package/dist/errors.js +3 -0
- package/dist/inspector/assets/{index-CIOfyIbK.js → index-CdyIIKdA.js} +1 -1
- package/dist/inspector/index.html +1 -1
- package/dist/live.js +226 -0
- package/dist/main.js +17 -4
- package/dist/output.js +19 -0
- package/dist/p2p.js +59 -1
- package/dist/serve.js +27 -1
- package/dist/shapes.js +216 -0
- package/dist/shortcuts.js +242 -0
- package/dist/signature.js +207 -0
- package/dist/style.js +6 -0
- package/dist/terminal.js +671 -0
- package/dist/traffic.js +247 -0
- package/dist/version.js +12 -0
- package/dist/webhook.js +91 -0
- package/package.json +1 -1
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import { createHmac, timingSafeEqual } from 'node:crypto';
|
|
2
|
+
/**
|
|
3
|
+
* Checks the signature a webhook provider put on a request, with the signing secret the user named in an environment
|
|
4
|
+
* variable. It runs on this machine over what was captured; the secret never leaves the process, is never printed and
|
|
5
|
+
* never written to the traffic log. The result is shown next to the request; nothing is refused because of it.
|
|
6
|
+
*/
|
|
7
|
+
/** The providers whose signatures can be checked from the request alone. */
|
|
8
|
+
export const SIGNED_PROVIDERS = ['stripe', 'github', 'gitlab', 'shopify', 'slack', 'svix', 'paddle', 'linear'];
|
|
9
|
+
export function isSignedProvider(name) {
|
|
10
|
+
return SIGNED_PROVIDERS.includes(name);
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Stripe, Slack and Svix refuse a signature whose timestamp is older than five minutes by default; a replay keeps the
|
|
14
|
+
* original timestamp, so its signature is valid and still refused by those libraries. Said next to the result.
|
|
15
|
+
*/
|
|
16
|
+
export const TOLERANCE_MS = 5 * 60_000;
|
|
17
|
+
/** The check of one request against the secrets of its service; null when none of them is for this request. */
|
|
18
|
+
export function checkSignature(exchange, secrets, now = Date.now()) {
|
|
19
|
+
const headers = new Map(exchange.request.headers.map(([name, value]) => [name.toLowerCase(), value]));
|
|
20
|
+
for (const entry of secrets) {
|
|
21
|
+
const verify = VERIFIERS[entry.provider];
|
|
22
|
+
const signed = verify.signs(headers);
|
|
23
|
+
if (!signed) {
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
const { provider, variable } = entry;
|
|
27
|
+
if (exchange.request.bodyTruncated) {
|
|
28
|
+
return {
|
|
29
|
+
result: 'unchecked',
|
|
30
|
+
provider,
|
|
31
|
+
variable,
|
|
32
|
+
reason: 'the body was longer than the capture limit, so the signature cannot be checked',
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
const body = Buffer.from(exchange.request.bodyBase64, 'base64');
|
|
36
|
+
const outcome = verify.check(headers, body, entry.secret);
|
|
37
|
+
if (typeof outcome === 'string') {
|
|
38
|
+
return { result: 'invalid', provider, variable, reason: outcome };
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
result: 'valid',
|
|
42
|
+
provider,
|
|
43
|
+
variable,
|
|
44
|
+
ageMs: outcome.timestamp === null ? null : now - outcome.timestamp,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
const MISMATCH = 'does not match the secret; check that it is the signing secret of this endpoint and that the body reaches the handler unchanged';
|
|
50
|
+
const VERIFIERS = {
|
|
51
|
+
// Stripe-Signature: t=<seconds>,v1=<hex>[,v1=...]; HMAC-SHA256 over "<t>.<body>"
|
|
52
|
+
stripe: {
|
|
53
|
+
signs: (h) => h.has('stripe-signature'),
|
|
54
|
+
check: (h, body, secret) => {
|
|
55
|
+
const parts = (h.get('stripe-signature') ?? '').split(',').map((part) => part.split('=', 2));
|
|
56
|
+
const t = parts.find(([key]) => key === 't')?.[1];
|
|
57
|
+
const signatures = parts.filter(([key]) => key === 'v1').map(([, value]) => value ?? '');
|
|
58
|
+
if (t === undefined || !/^\d+$/.test(t) || signatures.length === 0) {
|
|
59
|
+
return 'the Stripe-Signature header has no t= and v1= parts';
|
|
60
|
+
}
|
|
61
|
+
const expected = hmac(secret, Buffer.concat([Buffer.from(`${t}.`), body]), 'hex');
|
|
62
|
+
return signatures.some((value) => same(value, expected)) ? { timestamp: Number(t) * 1000 } : MISMATCH;
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
// X-Hub-Signature-256: sha256=<hex>; HMAC-SHA256 over the body
|
|
66
|
+
github: {
|
|
67
|
+
signs: (h) => h.has('x-hub-signature-256'),
|
|
68
|
+
check: (h, body, secret) => {
|
|
69
|
+
const value = h.get('x-hub-signature-256') ?? '';
|
|
70
|
+
if (!value.startsWith('sha256=')) {
|
|
71
|
+
return 'the X-Hub-Signature-256 header does not start with sha256=';
|
|
72
|
+
}
|
|
73
|
+
return same(value.slice(7), hmac(secret, body, 'hex')) ? { timestamp: null } : MISMATCH;
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
// X-Gitlab-Token: the secret token itself
|
|
77
|
+
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',
|
|
82
|
+
},
|
|
83
|
+
// X-Shopify-Hmac-Sha256: base64 HMAC-SHA256 over the body
|
|
84
|
+
shopify: {
|
|
85
|
+
signs: (h) => h.has('x-shopify-hmac-sha256'),
|
|
86
|
+
check: (h, body, secret) => same(h.get('x-shopify-hmac-sha256') ?? '', hmac(secret, body, 'base64')) ? { timestamp: null } : MISMATCH,
|
|
87
|
+
},
|
|
88
|
+
// X-Slack-Signature: v0=<hex>; HMAC-SHA256 over "v0:<X-Slack-Request-Timestamp>:<body>"
|
|
89
|
+
slack: {
|
|
90
|
+
signs: (h) => h.has('x-slack-signature'),
|
|
91
|
+
check: (h, body, secret) => {
|
|
92
|
+
const ts = h.get('x-slack-request-timestamp') ?? '';
|
|
93
|
+
const value = h.get('x-slack-signature') ?? '';
|
|
94
|
+
if (!/^\d+$/.test(ts) || !value.startsWith('v0=')) {
|
|
95
|
+
return 'X-Slack-Request-Timestamp or the v0= of X-Slack-Signature is missing';
|
|
96
|
+
}
|
|
97
|
+
const expected = hmac(secret, Buffer.concat([Buffer.from(`v0:${ts}:`), body]), 'hex');
|
|
98
|
+
return same(value.slice(3), expected) ? { timestamp: Number(ts) * 1000 } : MISMATCH;
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
// svix-id, svix-timestamp, svix-signature: "v1,<base64> ..."; the secret is whsec_<base64 key>
|
|
102
|
+
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
|
+
},
|
|
120
|
+
},
|
|
121
|
+
// Paddle-Signature: ts=<seconds>;h1=<hex>; HMAC-SHA256 over "<ts>:<body>"
|
|
122
|
+
paddle: {
|
|
123
|
+
signs: (h) => h.has('paddle-signature'),
|
|
124
|
+
check: (h, body, secret) => {
|
|
125
|
+
const parts = new Map((h.get('paddle-signature') ?? '').split(';').map((part) => part.split('=', 2)));
|
|
126
|
+
const ts = parts.get('ts') ?? '';
|
|
127
|
+
const h1 = parts.get('h1') ?? '';
|
|
128
|
+
if (!/^\d+$/.test(ts) || h1 === '') {
|
|
129
|
+
return 'the Paddle-Signature header has no ts= and h1= parts';
|
|
130
|
+
}
|
|
131
|
+
const expected = hmac(secret, Buffer.concat([Buffer.from(`${ts}:`), body]), 'hex');
|
|
132
|
+
return same(h1, expected) ? { timestamp: Number(ts) * 1000 } : MISMATCH;
|
|
133
|
+
},
|
|
134
|
+
},
|
|
135
|
+
// Linear-Signature: hex HMAC-SHA256 over the body
|
|
136
|
+
linear: {
|
|
137
|
+
signs: (h) => h.has('linear-signature'),
|
|
138
|
+
check: (h, body, secret) => same(h.get('linear-signature') ?? '', hmac(secret, body, 'hex')) ? { timestamp: null } : MISMATCH,
|
|
139
|
+
},
|
|
140
|
+
};
|
|
141
|
+
function hmac(secret, data, encoding) {
|
|
142
|
+
return createHmac('sha256', secret).update(data).digest(encoding);
|
|
143
|
+
}
|
|
144
|
+
/** Compares in constant time; texts of different length are different. */
|
|
145
|
+
function same(a, b) {
|
|
146
|
+
const left = Buffer.from(a);
|
|
147
|
+
const right = Buffer.from(b);
|
|
148
|
+
return left.byteLength === right.byteLength && timingSafeEqual(left, right);
|
|
149
|
+
}
|
|
150
|
+
/** An environment variable name as hopwire.yaml and --webhook-secret take it. */
|
|
151
|
+
export function isVariableName(name) {
|
|
152
|
+
return /^[A-Za-z_][A-Za-z0-9_]{0,127}$/.test(name);
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* The headers of a request signed again for `provider` at `timestampSeconds` (hopwire verify and chaos): the recorded
|
|
156
|
+
* 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.
|
|
159
|
+
*/
|
|
160
|
+
export function signHeaders(headers, body, secret, timestampSeconds, wrongKey = false) {
|
|
161
|
+
const key = wrongKey ? `${secret.secret}-not-the-signing-secret` : secret.secret;
|
|
162
|
+
const t = String(timestampSeconds);
|
|
163
|
+
const set = new Map();
|
|
164
|
+
switch (secret.provider) {
|
|
165
|
+
case 'stripe':
|
|
166
|
+
set.set('stripe-signature', `t=${t},v1=${hmac(key, Buffer.concat([Buffer.from(`${t}.`), body]), 'hex')}`);
|
|
167
|
+
break;
|
|
168
|
+
case 'github':
|
|
169
|
+
set.set('x-hub-signature-256', `sha256=${hmac(key, body, 'hex')}`);
|
|
170
|
+
break;
|
|
171
|
+
case 'gitlab':
|
|
172
|
+
set.set('x-gitlab-token', key);
|
|
173
|
+
break;
|
|
174
|
+
case 'shopify':
|
|
175
|
+
set.set('x-shopify-hmac-sha256', hmac(key, body, 'base64'));
|
|
176
|
+
break;
|
|
177
|
+
case 'slack':
|
|
178
|
+
set.set('x-slack-request-timestamp', t);
|
|
179
|
+
set.set('x-slack-signature', `v0=${hmac(key, Buffer.concat([Buffer.from(`v0:${t}:`), body]), 'hex')}`);
|
|
180
|
+
break;
|
|
181
|
+
case 'svix': {
|
|
182
|
+
const lower = new Map(headers.map(([name, value]) => [name.toLowerCase(), value]));
|
|
183
|
+
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}`);
|
|
192
|
+
break;
|
|
193
|
+
}
|
|
194
|
+
case 'paddle':
|
|
195
|
+
set.set('paddle-signature', `ts=${t};h1=${hmac(key, Buffer.concat([Buffer.from(`${t}:`), body]), 'hex')}`);
|
|
196
|
+
break;
|
|
197
|
+
case 'linear':
|
|
198
|
+
set.set('linear-signature', hmac(key, body, 'hex'));
|
|
199
|
+
break;
|
|
200
|
+
}
|
|
201
|
+
const kept = headers.filter(([name]) => !set.has(name.toLowerCase()));
|
|
202
|
+
return [...kept, ...set.entries()];
|
|
203
|
+
}
|
|
204
|
+
/** Whether a request carries a signature of `provider`: only those are signed again. */
|
|
205
|
+
export function signedBy(headers, provider) {
|
|
206
|
+
return VERIFIERS[provider].signs(new Map(headers.map(([name, value]) => [name.toLowerCase(), value])));
|
|
207
|
+
}
|
package/dist/style.js
CHANGED
|
@@ -21,6 +21,12 @@ export function decorate(line) {
|
|
|
21
21
|
if (line === 'Next steps') {
|
|
22
22
|
return paint(line, BOLD);
|
|
23
23
|
}
|
|
24
|
+
// A check of verify or chaos: its verdict first
|
|
25
|
+
const verdict = /^(pass|FAIL|skip)( {2}.*)$/.exec(line);
|
|
26
|
+
if (verdict !== null) {
|
|
27
|
+
const [, word = '', rest = ''] = verdict;
|
|
28
|
+
return `${paint(word, word === 'pass' ? `${BOLD};${GREEN}` : word === 'FAIL' ? `${BOLD};${RED}` : DIM)}${rest}`;
|
|
29
|
+
}
|
|
24
30
|
if (line.startsWith('Requests appear below') || line === 'Press Ctrl+C to stop.') {
|
|
25
31
|
return paint(line, DIM);
|
|
26
32
|
}
|