kryptheon 0.1.48 → 0.1.50
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/README.md +435 -333
- package/bin/kryptheon.js +231 -16
- package/kryptheon-code.js +158 -2
- package/kryptheon-dashboard-page.js +78 -6
- package/kryptheon-dashboard.html +9 -0
- package/kryptheon-dashboard.js +241 -13
- package/kryptheon-edge.js +83 -14
- package/kryptheon-heal.js +57 -0
- package/kryptheon-payments-run.js +501 -0
- package/kryptheon-payments.js +626 -0
- package/kryptheon-probe-fixture.js +175 -8
- package/kryptheon-probe.js +368 -317
- package/kryptheon-reliability.js +430 -40
- package/kryptheon-replay.js +0 -0
- package/kryptheon-secrets.js +14 -3
- package/package.json +12 -5
|
@@ -0,0 +1,626 @@
|
|
|
1
|
+
// The payment check: does the same payment webhook, sent again, give the
|
|
2
|
+
// customer the plan, the credits or the order a second time?
|
|
3
|
+
//
|
|
4
|
+
// Payment providers send a webhook again whenever the server is slow or
|
|
5
|
+
// answers with an error, so this is not a rare case - it is what Razorpay and
|
|
6
|
+
// Stripe do on an ordinary bad day. Reading the code cannot answer it: the
|
|
7
|
+
// answer is in what the database looks like after the second copy.
|
|
8
|
+
//
|
|
9
|
+
// So it is asked of the real thing. A small proxy sits in front of the app on
|
|
10
|
+
// this computer; the person makes one payment in the provider's TEST mode; the
|
|
11
|
+
// provider's own signed webhook passes through the proxy to the app unchanged,
|
|
12
|
+
// and a copy is kept. Then:
|
|
13
|
+
//
|
|
14
|
+
// 1. the database is read once the first delivery has settled;
|
|
15
|
+
// 2. the identical webhook - same bytes, same signature - is sent again, and
|
|
16
|
+
// the database is read again. Anything a customer would notice that
|
|
17
|
+
// changed between the two (a credit, a plan date, a new order) is the
|
|
18
|
+
// finding, shown as it changed: profiles.credits 100 -> 200;
|
|
19
|
+
// 3. a copy with a wrong signature is sent. An app that answers it with
|
|
20
|
+
// success may be taking payments from anyone who can reach it.
|
|
21
|
+
//
|
|
22
|
+
// Three promises this file keeps:
|
|
23
|
+
//
|
|
24
|
+
// - Only an app on this computer. The proxy forwards to a local address and
|
|
25
|
+
// nothing else, and a webhook from the provider's live mode is refused
|
|
26
|
+
// before anything is sent again.
|
|
27
|
+
// - The database is read, never written, by this file. What changes is what
|
|
28
|
+
// the person's own app does with a webhook it was sent.
|
|
29
|
+
// - What could not be tested is said as not tested: a database too big to
|
|
30
|
+
// compare row by row, a copy the app refused for being stale, a database
|
|
31
|
+
// that never stopped changing.
|
|
32
|
+
|
|
33
|
+
const http = require('http');
|
|
34
|
+
const crypto = require('crypto');
|
|
35
|
+
|
|
36
|
+
const SIGNATURE_HEADER = { razorpay: 'x-razorpay-signature', stripe: 'stripe-signature' };
|
|
37
|
+
|
|
38
|
+
// The headers kept with a captured webhook: what the app needs to accept the
|
|
39
|
+
// copy, and nothing it sent about itself.
|
|
40
|
+
const KEPT_HEADERS = ['content-type', 'user-agent', 'x-razorpay-signature', 'x-razorpay-event-id', 'stripe-signature'];
|
|
41
|
+
|
|
42
|
+
// A table this big is counted, not compared row by row.
|
|
43
|
+
const ROW_LIMIT = 20000;
|
|
44
|
+
|
|
45
|
+
// The longest the first payment webhook is held back while the database is
|
|
46
|
+
// read before it - the provider is waiting for an answer.
|
|
47
|
+
const BEFORE_FORWARD_MS = 2500;
|
|
48
|
+
|
|
49
|
+
// Stripe refuses a webhook whose signed timestamp is more than five minutes
|
|
50
|
+
// old, so a copy sent later says nothing about the app.
|
|
51
|
+
const STRIPE_TOLERANCE_MS = 5 * 60 * 1000;
|
|
52
|
+
|
|
53
|
+
/** This computer, or a name that only ever means a development copy. */
|
|
54
|
+
function isLocalAddress(url) {
|
|
55
|
+
let host;
|
|
56
|
+
try {
|
|
57
|
+
host = new URL(url).hostname.toLowerCase();
|
|
58
|
+
} catch (err) {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
return host === 'localhost' || host === '127.0.0.1' || host === '0.0.0.0' || host === '::1' || host === '[::1]' ||
|
|
62
|
+
host.endsWith('.local') || host.endsWith('.localhost') || host.endsWith('.test');
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Which provider signed this request, from its headers, or null. */
|
|
66
|
+
function providerOf(headers) {
|
|
67
|
+
const h = headers || {};
|
|
68
|
+
if (h['x-razorpay-signature']) return 'razorpay';
|
|
69
|
+
if (h['stripe-signature']) return 'stripe';
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function parsed(body) {
|
|
74
|
+
try {
|
|
75
|
+
return JSON.parse(Buffer.isBuffer(body) ? body.toString('utf8') : String(body));
|
|
76
|
+
} catch (err) {
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Live or test mode. Stripe says so in every event. Razorpay does not, so its
|
|
83
|
+
* mode is read from the app's own key id: rzp_live_ or rzp_test_. `env` is the
|
|
84
|
+
* app's .env files, read by the caller. Unknown is null, never a guess.
|
|
85
|
+
*/
|
|
86
|
+
function modeOf(provider, body, env) {
|
|
87
|
+
if (provider === 'stripe') {
|
|
88
|
+
const json = parsed(body);
|
|
89
|
+
if (json && json.livemode === true) return { mode: 'live', why: 'the event says "livemode": true' };
|
|
90
|
+
if (json && json.livemode === false) return { mode: 'test', why: 'the event says "livemode": false' };
|
|
91
|
+
return { mode: null, why: 'the event does not say whether it is live' };
|
|
92
|
+
}
|
|
93
|
+
if (provider === 'razorpay') {
|
|
94
|
+
const keys = Object.values(env || {}).map(String);
|
|
95
|
+
if (keys.some((v) => /^rzp_live_/.test(v))) return { mode: 'live', why: 'your app has a live Razorpay key (rzp_live_)' };
|
|
96
|
+
if (keys.some((v) => /^rzp_test_/.test(v))) return { mode: 'test', why: 'your app has a test Razorpay key (rzp_test_)' };
|
|
97
|
+
return { mode: null, why: 'no Razorpay key id (rzp_test_ or rzp_live_) was found in your app\'s .env files' };
|
|
98
|
+
}
|
|
99
|
+
return { mode: null, why: 'not a payment webhook' };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** What the webhook is about, for saying it to a person. */
|
|
103
|
+
function eventOf(hook) {
|
|
104
|
+
const json = parsed(hook.body) || {};
|
|
105
|
+
if (hook.provider === 'razorpay') {
|
|
106
|
+
const payment = json.payload && json.payload.payment && json.payload.payment.entity;
|
|
107
|
+
return {
|
|
108
|
+
type: json.event || 'a Razorpay event',
|
|
109
|
+
id: hook.headers['x-razorpay-event-id'] || null,
|
|
110
|
+
about: payment && payment.id ? payment.id : null,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
if (hook.provider === 'stripe') {
|
|
114
|
+
const object = json.data && json.data.object;
|
|
115
|
+
return { type: json.type || 'a Stripe event', id: json.id || null, about: object && object.id ? object.id : null };
|
|
116
|
+
}
|
|
117
|
+
return { type: 'a request', id: null, about: null };
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** The headers a copy is sent with, taken from what arrived. */
|
|
121
|
+
function keptHeaders(headers) {
|
|
122
|
+
const out = {};
|
|
123
|
+
for (const name of KEPT_HEADERS) if (headers[name] !== undefined) out[name] = headers[name];
|
|
124
|
+
return out;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* The proxy. Every request to it goes on to `target` unchanged - same method,
|
|
129
|
+
* path, headers and bytes - and its answer comes back unchanged, so the app
|
|
130
|
+
* and the provider cannot tell it is there. A request carrying a provider's
|
|
131
|
+
* signature is also kept, with the answer the app gave it.
|
|
132
|
+
*/
|
|
133
|
+
function startCapture(options) {
|
|
134
|
+
const target = new URL(options.target);
|
|
135
|
+
if (!isLocalAddress(target.href)) {
|
|
136
|
+
return Promise.reject(new Error('the payment check only sends to an app on this computer, and ' + target.origin + ' is not'));
|
|
137
|
+
}
|
|
138
|
+
const captured = [];
|
|
139
|
+
const server = http.createServer((req, res) => {
|
|
140
|
+
const chunks = [];
|
|
141
|
+
req.on('data', (c) => chunks.push(c));
|
|
142
|
+
req.on('end', async () => {
|
|
143
|
+
const body = Buffer.concat(chunks);
|
|
144
|
+
const receivedAt = new Date().toISOString();
|
|
145
|
+
const signed = providerOf(req.headers) && req.method === 'POST';
|
|
146
|
+
// The caller may read the database once before the first payment
|
|
147
|
+
// webhook reaches the app, so that delivery's own writes can be shown.
|
|
148
|
+
// Capped: the provider is waiting, and Razorpay gives up after 5 s.
|
|
149
|
+
let beforeTook = null;
|
|
150
|
+
if (signed && options.beforeForward) {
|
|
151
|
+
const began = Date.now();
|
|
152
|
+
await Promise.race([
|
|
153
|
+
Promise.resolve().then(() => options.beforeForward()).catch(() => {}),
|
|
154
|
+
new Promise((r) => setTimeout(r, BEFORE_FORWARD_MS)),
|
|
155
|
+
]);
|
|
156
|
+
beforeTook = Date.now() - began;
|
|
157
|
+
}
|
|
158
|
+
const forwardedAt = Date.now();
|
|
159
|
+
const headers = Object.assign({}, req.headers, { host: target.host });
|
|
160
|
+
const out = http.request({
|
|
161
|
+
hostname: target.hostname === 'localhost' ? '127.0.0.1' : target.hostname,
|
|
162
|
+
port: target.port || 80,
|
|
163
|
+
method: req.method,
|
|
164
|
+
path: req.url,
|
|
165
|
+
headers: headers,
|
|
166
|
+
}, (answer) => {
|
|
167
|
+
const back = [];
|
|
168
|
+
answer.on('data', (c) => back.push(c));
|
|
169
|
+
answer.on('end', () => {
|
|
170
|
+
const said = Buffer.concat(back);
|
|
171
|
+
res.writeHead(answer.statusCode, answer.headers);
|
|
172
|
+
res.end(said);
|
|
173
|
+
if (signed) {
|
|
174
|
+
const hook = {
|
|
175
|
+
provider: providerOf(req.headers),
|
|
176
|
+
method: req.method,
|
|
177
|
+
path: req.url,
|
|
178
|
+
headers: keptHeaders(req.headers),
|
|
179
|
+
body: body,
|
|
180
|
+
status: answer.statusCode,
|
|
181
|
+
at: Date.now(),
|
|
182
|
+
receivedAt: receivedAt,
|
|
183
|
+
answeredAt: new Date().toISOString(),
|
|
184
|
+
ms: Date.now() - forwardedAt,
|
|
185
|
+
heldFor: beforeTook,
|
|
186
|
+
said: said.toString('utf8').slice(0, 300),
|
|
187
|
+
};
|
|
188
|
+
captured.push(hook);
|
|
189
|
+
if (options.onWebhook) options.onWebhook(hook);
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
out.on('error', (err) => {
|
|
194
|
+
res.writeHead(502, { 'content-type': 'text/plain' });
|
|
195
|
+
res.end('Kryptheon could not reach your app at ' + target.origin + ': ' + err.message);
|
|
196
|
+
});
|
|
197
|
+
out.end(body);
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
return new Promise((resolve, reject) => {
|
|
201
|
+
server.once('error', reject);
|
|
202
|
+
server.listen(options.port || 0, '127.0.0.1', () => {
|
|
203
|
+
const port = server.address().port;
|
|
204
|
+
resolve({
|
|
205
|
+
url: 'http://127.0.0.1:' + port,
|
|
206
|
+
port: port,
|
|
207
|
+
captured: captured,
|
|
208
|
+
close: () => new Promise((done) => server.close(() => done())),
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** A signature that is wrong but looks right: its last hex digit changed. */
|
|
215
|
+
function spoiled(provider, value) {
|
|
216
|
+
const flip = (hex) => hex.slice(0, -1) + (hex.slice(-1) === '0' ? '1' : '0');
|
|
217
|
+
if (provider === 'stripe') return String(value).replace(/v1=([0-9a-f]+)/g, (all, hex) => 'v1=' + flip(hex));
|
|
218
|
+
return flip(String(value));
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Sends a kept webhook to the app again, byte for byte. With `badSignature`,
|
|
223
|
+
* the same bytes under a signature that does not match them.
|
|
224
|
+
*/
|
|
225
|
+
function send(target, hook, options) {
|
|
226
|
+
const url = new URL(target);
|
|
227
|
+
if (!isLocalAddress(url.href)) return Promise.reject(new Error(url.origin + ' is not this computer'));
|
|
228
|
+
const headers = Object.assign({}, hook.headers, { 'content-length': Buffer.byteLength(hook.body), host: url.host });
|
|
229
|
+
if (options && options.badSignature) {
|
|
230
|
+
const name = SIGNATURE_HEADER[hook.provider];
|
|
231
|
+
headers[name] = spoiled(hook.provider, headers[name]);
|
|
232
|
+
}
|
|
233
|
+
const sentAt = new Date().toISOString();
|
|
234
|
+
const began = Date.now();
|
|
235
|
+
const timed = (status, said) => ({ status: status, said: said, sentAt: sentAt, answeredAt: new Date().toISOString(), ms: Date.now() - began });
|
|
236
|
+
return new Promise((resolve) => {
|
|
237
|
+
const req = http.request({
|
|
238
|
+
hostname: url.hostname === 'localhost' ? '127.0.0.1' : url.hostname,
|
|
239
|
+
port: url.port || 80,
|
|
240
|
+
method: hook.method || 'POST',
|
|
241
|
+
path: hook.path,
|
|
242
|
+
headers: headers,
|
|
243
|
+
}, (answer) => {
|
|
244
|
+
const back = [];
|
|
245
|
+
answer.on('data', (c) => back.push(c));
|
|
246
|
+
answer.on('end', () => resolve(timed(answer.statusCode, Buffer.concat(back).toString('utf8').slice(0, 300))));
|
|
247
|
+
});
|
|
248
|
+
req.setTimeout(30000, () => req.destroy(new Error('no answer in 30 seconds')));
|
|
249
|
+
req.on('error', (err) => resolve(timed(null, err.message)));
|
|
250
|
+
req.end(hook.body);
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
const q = (name) => '"' + String(name).replace(/"/g, '""') + '"';
|
|
255
|
+
|
|
256
|
+
/** The tables of a schema, each with its primary key, read from the catalogue. */
|
|
257
|
+
async function tablesOf(client, schema) {
|
|
258
|
+
const { rows } = await client.query(
|
|
259
|
+
// attname is a "name"; pg hands a name[] back as one string, so text[].
|
|
260
|
+
"SELECT c.relname AS name, coalesce((SELECT array_agg(a.attname::text ORDER BY k.ord) " +
|
|
261
|
+
" FROM pg_index i CROSS JOIN LATERAL unnest(i.indkey) WITH ORDINALITY AS k(attnum, ord) " +
|
|
262
|
+
" JOIN pg_attribute a ON a.attrelid = i.indrelid AND a.attnum = k.attnum " +
|
|
263
|
+
" WHERE i.indrelid = c.oid AND i.indisprimary), '{}'::text[]) AS key " +
|
|
264
|
+
"FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace " +
|
|
265
|
+
"WHERE n.nspname = $1 AND c.relkind IN ('r', 'p') ORDER BY c.relname",
|
|
266
|
+
[schema],
|
|
267
|
+
);
|
|
268
|
+
return rows.map((r) => ({ name: r.name, key: r.key || [] }));
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Every row of every table, read as JSON. A table over ROW_LIMIT rows is only
|
|
273
|
+
* counted. A table with no primary key is compared as a bag of identical rows.
|
|
274
|
+
*/
|
|
275
|
+
async function snapshot(client, schema, tables, limit) {
|
|
276
|
+
const most = limit || ROW_LIMIT;
|
|
277
|
+
const out = {};
|
|
278
|
+
for (const table of tables) {
|
|
279
|
+
const where = q(schema) + '.' + q(table.name);
|
|
280
|
+
const { rows: counted } = await client.query('SELECT count(*)::int AS n FROM ' + where);
|
|
281
|
+
const count = counted[0].n;
|
|
282
|
+
if (count > most) {
|
|
283
|
+
out[table.name] = { count: count, tooBig: true, rows: new Map() };
|
|
284
|
+
continue;
|
|
285
|
+
}
|
|
286
|
+
const { rows } = await client.query('SELECT row_to_json(t) AS r FROM ' + where + ' t');
|
|
287
|
+
const map = new Map();
|
|
288
|
+
for (const { r } of rows) {
|
|
289
|
+
let k = table.key.length ? JSON.stringify(table.key.map((c) => r[c])) : JSON.stringify(r);
|
|
290
|
+
if (!table.key.length) {
|
|
291
|
+
let n = 0;
|
|
292
|
+
while (map.has(k + '#' + n)) n += 1;
|
|
293
|
+
k = k + '#' + n;
|
|
294
|
+
}
|
|
295
|
+
map.set(k, r);
|
|
296
|
+
}
|
|
297
|
+
out[table.name] = { count: count, tooBig: false, rows: map };
|
|
298
|
+
}
|
|
299
|
+
return out;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/** What changed between two snapshots, table by table. */
|
|
303
|
+
function diff(before, after) {
|
|
304
|
+
const changes = [];
|
|
305
|
+
for (const name of Object.keys(after)) {
|
|
306
|
+
const a = before[name] || { count: 0, rows: new Map(), tooBig: false };
|
|
307
|
+
const b = after[name];
|
|
308
|
+
if (a.tooBig || b.tooBig) {
|
|
309
|
+
if (a.count !== b.count) changes.push({ table: name, kind: 'count', from: a.count, to: b.count });
|
|
310
|
+
continue;
|
|
311
|
+
}
|
|
312
|
+
for (const [key, row] of b.rows) {
|
|
313
|
+
if (!a.rows.has(key)) {
|
|
314
|
+
changes.push({ table: name, kind: 'added', key: key, row: row });
|
|
315
|
+
continue;
|
|
316
|
+
}
|
|
317
|
+
const old = a.rows.get(key);
|
|
318
|
+
const columns = Object.keys(row).filter((c) => JSON.stringify(old[c]) !== JSON.stringify(row[c]))
|
|
319
|
+
.map((c) => ({ name: c, from: old[c], to: row[c] }));
|
|
320
|
+
if (columns.length) changes.push({ table: name, kind: 'changed', key: key, columns: columns, row: row });
|
|
321
|
+
}
|
|
322
|
+
for (const [key, row] of a.rows) if (!b.rows.has(key)) changes.push({ table: name, kind: 'removed', key: key, row: row });
|
|
323
|
+
}
|
|
324
|
+
return changes;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
// Tables that keep one row per delivery on purpose: a new row there is the
|
|
328
|
+
// app noting that a webhook arrived, not a customer getting something twice.
|
|
329
|
+
const LOG_TABLE = /(^|_)(logs?|events?|webhooks?|deliveries|delivery|audits?|history|histories|attempts?|inbox|outbox)(_|$)/i;
|
|
330
|
+
|
|
331
|
+
// Columns that only say when something last happened. Named exactly: a date
|
|
332
|
+
// that says how long a plan lasts (plan_until, expires_at, current_period_end)
|
|
333
|
+
// moving again is the customer getting the plan twice, and must never be
|
|
334
|
+
// waved through as a timestamp.
|
|
335
|
+
const TOUCH_COLUMN = /^(updated_at|modified_at|updated|modified|last_modified|last_seen_at|last_webhook_at|last_event_at|received_at|processed_at|synced_at|touched_at)$/i;
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* Each change, said as noise or as something a customer would notice. Only
|
|
339
|
+
* the second kind is a finding; the first is shown so nothing is hidden.
|
|
340
|
+
*/
|
|
341
|
+
function classify(change) {
|
|
342
|
+
if (change.kind === 'added' && LOG_TABLE.test(change.table)) return 'logged';
|
|
343
|
+
if (change.kind === 'count' && LOG_TABLE.test(change.table) && change.to > change.from) return 'logged';
|
|
344
|
+
if (change.kind === 'changed' && change.columns.every((c) => TOUCH_COLUMN.test(c.name))) return 'touched';
|
|
345
|
+
return 'business';
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
function shortValue(v) {
|
|
349
|
+
if (v === null || v === undefined) return 'empty';
|
|
350
|
+
const text = typeof v === 'string' ? v : JSON.stringify(v);
|
|
351
|
+
return text.length > 40 ? text.slice(0, 37) + '...' : text;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/** One change in a line a person can read. */
|
|
355
|
+
function changeLine(change) {
|
|
356
|
+
if (change.kind === 'added') return change.table + ': a new row';
|
|
357
|
+
if (change.kind === 'removed') return change.table + ': a row was deleted';
|
|
358
|
+
if (change.kind === 'count') return change.table + ': ' + change.from + ' rows -> ' + change.to + ' (too big to compare row by row)';
|
|
359
|
+
// What a customer would notice first; a timestamp that moved with it is
|
|
360
|
+
// named, not spelled out.
|
|
361
|
+
const told = change.columns.filter((c) => !TOUCH_COLUMN.test(c.name));
|
|
362
|
+
const touched = change.columns.filter((c) => TOUCH_COLUMN.test(c.name)).map((c) => c.name);
|
|
363
|
+
const shown = told.length ? told : change.columns;
|
|
364
|
+
// A secret is never spelled out, here or anywhere: found by the trace's
|
|
365
|
+
// check, a password_hash that changed was printed in the finding itself.
|
|
366
|
+
return change.table + '.' + shown.map((c) => c.name + ' ' + shortValue(traceValue(c.name, c.from)) + ' -> ' + shortValue(traceValue(c.name, c.to))).join(', ') +
|
|
367
|
+
(told.length && touched.length ? ' (and ' + touched.join(', ') + ')' : '');
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
// How long after a webhook its work is looked for. An app may answer first
|
|
371
|
+
// and do the work afterwards (a queue, a background job). Work that lands
|
|
372
|
+
// later than this would be put down to the next copy, so it is said in the
|
|
373
|
+
// report, and the wait is never shorter than this.
|
|
374
|
+
const MIN_WAIT_MS = 4000;
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* Reads until the database stops changing: at least `minMs` after it began,
|
|
378
|
+
* and two reads `quietMs` apart that agree. Found by the check: a handler that
|
|
379
|
+
* answered first and granted 1.5 s later settled at once, so its grant was
|
|
380
|
+
* missed - and a correct one's late first grant would have been blamed on the
|
|
381
|
+
* repeat.
|
|
382
|
+
*/
|
|
383
|
+
async function settle(client, schema, tables, options) {
|
|
384
|
+
const quietMs = (options && options.quietMs) || 1000;
|
|
385
|
+
const minMs = options && options.minMs !== undefined ? options.minMs : MIN_WAIT_MS;
|
|
386
|
+
const maxMs = (options && options.maxMs) || 20000;
|
|
387
|
+
const began = Date.now();
|
|
388
|
+
const until = began + maxMs;
|
|
389
|
+
const limit = options && options.rowLimit;
|
|
390
|
+
let last = await snapshot(client, schema, tables, limit);
|
|
391
|
+
while (Date.now() < until) {
|
|
392
|
+
await new Promise((r) => setTimeout(r, quietMs));
|
|
393
|
+
const next = await snapshot(client, schema, tables, limit);
|
|
394
|
+
if (!diff(last, next).length && Date.now() - began >= minMs) return { snap: next, settled: true };
|
|
395
|
+
last = next;
|
|
396
|
+
}
|
|
397
|
+
return { snap: last, settled: false };
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
const ok = (status) => status !== null && status >= 200 && status < 300;
|
|
401
|
+
|
|
402
|
+
// Events that say a payment was refunded, on either provider.
|
|
403
|
+
const REFUND_EVENT = /^(refund\.(created|processed)|payment\.refunded|charge\.refunded)$/;
|
|
404
|
+
|
|
405
|
+
// Columns whose values are never written into a trace, whatever the table.
|
|
406
|
+
const SECRET_COLUMN = /pass(word|wd)?|secret|token|api_?key|access_?key|private|signature|card|cvv|otp|(^|_)(pan|pin|ssn|aadhaar)(_|$)|(^|_)keys?$/i;
|
|
407
|
+
const SHORT = 200;
|
|
408
|
+
|
|
409
|
+
/** One value as kept in a trace: hidden when its column holds a secret, cut when long. */
|
|
410
|
+
function traceValue(column, value) {
|
|
411
|
+
if (SECRET_COLUMN.test(column)) return value === null || value === undefined ? value : '[hidden]';
|
|
412
|
+
if (typeof value === 'string' && value.length > SHORT) return value.slice(0, SHORT) + '...';
|
|
413
|
+
return value;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
function traceRow(row) {
|
|
417
|
+
const out = {};
|
|
418
|
+
for (const k of Object.keys(row || {})) out[k] = traceValue(k, row[k]);
|
|
419
|
+
return out;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* What a delivery wrote, row by row: the table, the row's primary key (null
|
|
424
|
+
* for a table without one), and the row before and after - or, for a changed
|
|
425
|
+
* row, each column that changed. Secrets are hidden.
|
|
426
|
+
*/
|
|
427
|
+
function writesOf(changes) {
|
|
428
|
+
return changes.map((c) => {
|
|
429
|
+
let key = null;
|
|
430
|
+
if (c.key && !/#\d+$/.test(c.key)) {
|
|
431
|
+
try { key = JSON.parse(c.key); } catch (err) { key = null; }
|
|
432
|
+
}
|
|
433
|
+
const out = { class: c.class || classify(c), table: c.table, kind: c.kind, key: key, line: changeLine(c) };
|
|
434
|
+
if (c.kind === 'added') out.after = traceRow(c.row);
|
|
435
|
+
if (c.kind === 'removed') out.before = traceRow(c.row);
|
|
436
|
+
if (c.kind === 'changed') out.columns = c.columns.map((x) => ({ name: x.name, from: traceValue(x.name, x.from), to: traceValue(x.name, x.to) }));
|
|
437
|
+
if (c.kind === 'count') { out.from = c.from; out.to = c.to; }
|
|
438
|
+
return out;
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/** The ids a webhook names: its event, its payment, its order, its object. */
|
|
443
|
+
function idsOf(hook) {
|
|
444
|
+
const json = parsed(hook.body) || {};
|
|
445
|
+
const out = [hook.headers && hook.headers['x-razorpay-event-id'], json.id];
|
|
446
|
+
const p = json.payload || {};
|
|
447
|
+
for (const k of Object.keys(p)) {
|
|
448
|
+
const e = p[k] && p[k].entity;
|
|
449
|
+
if (e) out.push(e.id, e.order_id, e.payment_id, e.subscription_id, e.invoice_id);
|
|
450
|
+
}
|
|
451
|
+
const o = json.data && json.data.object;
|
|
452
|
+
if (o) out.push(o.id, o.payment_intent, o.subscription, o.invoice, o.charge);
|
|
453
|
+
return out.filter((v) => typeof v === 'string' && v.length >= 6);
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
/**
|
|
457
|
+
* Whether a change while a copy was being judged is that copy's doing, as far
|
|
458
|
+
* as can be told: the very row the first delivery changed, a new row in a
|
|
459
|
+
* table the first delivery added to, or a row naming one of the webhook's own
|
|
460
|
+
* ids. Anything else happened at the same time and is not put down to it.
|
|
461
|
+
* Found on a blind test (round 18): the person bought a hoodie during the
|
|
462
|
+
* wrong-signature step, its new checkout row was blamed on the copy, and a
|
|
463
|
+
* refused copy was reported as a CRITICAL forged payment.
|
|
464
|
+
* With no record of the first delivery, everything counts, as before.
|
|
465
|
+
*/
|
|
466
|
+
function relatedTo(firstWrites, ids) {
|
|
467
|
+
if (!firstWrites) return () => true;
|
|
468
|
+
const rows = new Set(firstWrites.filter((w) => w.key).map((w) => w.table + '|' + JSON.stringify(w.key)));
|
|
469
|
+
const added = new Set(firstWrites.filter((w) => w.kind === 'added' || w.kind === 'count').map((w) => w.table));
|
|
470
|
+
return (change) => {
|
|
471
|
+
let key = null;
|
|
472
|
+
if (change.key && !/#\d+$/.test(change.key)) {
|
|
473
|
+
try { key = JSON.stringify(JSON.parse(change.key)); } catch (err) { key = null; }
|
|
474
|
+
}
|
|
475
|
+
if (key && rows.has(change.table + '|' + key)) return true;
|
|
476
|
+
if ((change.kind === 'added' || change.kind === 'count') && added.has(change.table)) return true;
|
|
477
|
+
const text = change.row ? JSON.stringify(change.row) : '';
|
|
478
|
+
return ids.some((id) => text.includes(id));
|
|
479
|
+
};
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
function sha256(body) {
|
|
483
|
+
return crypto.createHash('sha256').update(body).digest('hex');
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
/**
|
|
487
|
+
* The whole check for one captured webhook. `client` reads the app's
|
|
488
|
+
* database (schema `schema`), `target` is the app. Never called for live mode:
|
|
489
|
+
* the caller has already refused it.
|
|
490
|
+
*/
|
|
491
|
+
async function check(options) {
|
|
492
|
+
const { client, schema, target, hook } = options;
|
|
493
|
+
const say = options.say || (() => {});
|
|
494
|
+
const event = eventOf(hook);
|
|
495
|
+
const result = { provider: hook.provider, event: event, path: hook.path, firstStatus: hook.status, steps: [], findings: [], notTested: [] };
|
|
496
|
+
|
|
497
|
+
// The trace: every delivery of this webhook, when it went, what came back,
|
|
498
|
+
// and what it wrote. The body is never kept - its SHA-256 shows the repeat
|
|
499
|
+
// was the same bytes.
|
|
500
|
+
const bodySha256 = sha256(hook.body);
|
|
501
|
+
const firstDelivery = {
|
|
502
|
+
step: 'first', from: hook.provider, at: hook.receivedAt || new Date(hook.at).toISOString(), answeredAt: hook.answeredAt || null,
|
|
503
|
+
ms: hook.ms === undefined ? null : hook.ms, heldFor: hook.heldFor === undefined ? null : hook.heldFor,
|
|
504
|
+
status: hook.status, said: hook.said || '', signature: 'as the provider signed it', bodySha256: bodySha256,
|
|
505
|
+
writes: null, writesWhy: options.before ? '' : options.beforeWhy || 'the database was not read before this delivery',
|
|
506
|
+
};
|
|
507
|
+
result.trace = { eventId: event.id, about: event.about, bodySha256: bodySha256, deliveries: [firstDelivery] };
|
|
508
|
+
const delivered = (step, answer, signature, changes, settled) => ({
|
|
509
|
+
step: step, from: 'Kryptheon', at: answer.sentAt, answeredAt: answer.answeredAt, ms: answer.ms, status: answer.status,
|
|
510
|
+
said: answer.said, signature: signature, bodySha256: bodySha256, writes: writesOf(changes), settled: settled,
|
|
511
|
+
});
|
|
512
|
+
|
|
513
|
+
if (!ok(hook.status)) {
|
|
514
|
+
result.notTested.push('your app answered the payment\'s own webhook with ' + hook.status +
|
|
515
|
+
', so it never took the payment the first time - there was nothing to repeat. Fix that first.');
|
|
516
|
+
return result;
|
|
517
|
+
}
|
|
518
|
+
if (hook.provider === 'stripe' && Date.now() - hook.at > STRIPE_TOLERANCE_MS - 30000) {
|
|
519
|
+
result.notTested.push('this Stripe webhook is more than four and a half minutes old, and Stripe\'s own check refuses one ' +
|
|
520
|
+
'past five minutes. Make the test payment again and the check runs straight after it.');
|
|
521
|
+
return result;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
const tables = await tablesOf(client, schema);
|
|
525
|
+
say('Waiting for your database to settle after the first delivery ...');
|
|
526
|
+
const first = await settle(client, schema, tables, options);
|
|
527
|
+
if (options.before) {
|
|
528
|
+
firstDelivery.writes = writesOf(diff(options.before.snap, first.snap));
|
|
529
|
+
// Read once, before the first webhook of the payment: with more than one,
|
|
530
|
+
// what they wrote is seen together.
|
|
531
|
+
if (options.before.shared) firstDelivery.writesWhy = 'together with the other webhooks of this payment, which arrived with it';
|
|
532
|
+
} else if (options.knownFirstWrites) {
|
|
533
|
+
// Judged a second time: what the first delivery wrote was seen the first time.
|
|
534
|
+
firstDelivery.writes = options.knownFirstWrites;
|
|
535
|
+
firstDelivery.writesWhy = '';
|
|
536
|
+
}
|
|
537
|
+
if (!first.settled) {
|
|
538
|
+
result.notTested.push('your database kept changing for ' + Math.round(((options.maxMs) || 20000) / 1000) +
|
|
539
|
+
' seconds after the webhook, so a second change could not be told from the first.');
|
|
540
|
+
return result;
|
|
541
|
+
}
|
|
542
|
+
// A refund that took nothing back. Seen, not proven wrong: an app may
|
|
543
|
+
// handle refunds by hand. Found on a blind test (round 18): the app answered
|
|
544
|
+
// refund.created and refund.processed with "ignored" and wrote only its log,
|
|
545
|
+
// so the credits a refunded payment gave stayed - and nothing said so.
|
|
546
|
+
if (REFUND_EVENT.test(event.type) && firstDelivery.writes &&
|
|
547
|
+
!firstDelivery.writes.some((w) => w.class === 'business')) {
|
|
548
|
+
result.findings.push({ kind: 'refund-nothing', severity: 'MEDIUM', toCheck: true, status: hook.status, said: hook.said || '' });
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
// 1. The same webhook again.
|
|
552
|
+
say('Sending the same webhook again, exactly as ' + (hook.provider === 'razorpay' ? 'Razorpay' : 'Stripe') + ' would on a retry ...');
|
|
553
|
+
const again = await send(target, hook);
|
|
554
|
+
const second = await settle(client, schema, tables, options);
|
|
555
|
+
const related = relatedTo(firstDelivery.writes, idsOf(hook));
|
|
556
|
+
const judged = (c) => {
|
|
557
|
+
const cls = classify(c);
|
|
558
|
+
return Object.assign({ class: cls === 'business' && !related(c) ? 'elsewhere' : cls }, c);
|
|
559
|
+
};
|
|
560
|
+
const changes = diff(first.snap, second.snap).map(judged);
|
|
561
|
+
const business = changes.filter((c) => c.class === 'business');
|
|
562
|
+
result.steps.push({ step: 'repeat', status: again.status, changes: changes.map((c) => ({ class: c.class, line: changeLine(c) })) });
|
|
563
|
+
result.trace.deliveries.push(delivered('repeat', again, 'the same', changes, second.settled));
|
|
564
|
+
if (!second.settled) {
|
|
565
|
+
result.notTested.push('after the repeat your database kept changing, so what the repeat did could not be told apart.');
|
|
566
|
+
} else if (business.length) {
|
|
567
|
+
result.findings.push({
|
|
568
|
+
kind: 'repeat-grants',
|
|
569
|
+
severity: 'HIGH',
|
|
570
|
+
status: again.status,
|
|
571
|
+
changes: business.map(changeLine),
|
|
572
|
+
noise: changes.filter((c) => c.class !== 'business').map(changeLine),
|
|
573
|
+
});
|
|
574
|
+
} else if (!ok(again.status)) {
|
|
575
|
+
result.findings.push({ kind: 'repeat-refused', severity: 'MEDIUM', status: again.status, said: again.said });
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
// 2. The same bytes under a wrong signature.
|
|
579
|
+
say('Sending a copy with a wrong signature ...');
|
|
580
|
+
const forged = await send(target, hook, { badSignature: true });
|
|
581
|
+
const third = await settle(client, schema, tables, options);
|
|
582
|
+
const forgedChanges = diff(second.snap, third.snap).map(judged);
|
|
583
|
+
const forgedBusiness = forgedChanges.filter((c) => c.class === 'business');
|
|
584
|
+
result.steps.push({ step: 'wrong-signature', status: forged.status, changes: forgedChanges.map((c) => ({ class: c.class, line: changeLine(c) })) });
|
|
585
|
+
result.trace.deliveries.push(delivered('wrong-signature', forged, 'one character changed', forgedChanges, third.settled));
|
|
586
|
+
if (ok(forged.status) || forgedBusiness.length) {
|
|
587
|
+
result.findings.push({
|
|
588
|
+
kind: 'unsigned',
|
|
589
|
+
severity: forgedBusiness.length ? 'CRITICAL' : 'MEDIUM',
|
|
590
|
+
status: forged.status,
|
|
591
|
+
refused: !ok(forged.status),
|
|
592
|
+
changes: forgedBusiness.map(changeLine),
|
|
593
|
+
});
|
|
594
|
+
}
|
|
595
|
+
// What changed meanwhile but is not this webhook's, said so it is not lost.
|
|
596
|
+
const elsewhere = changes.concat(forgedChanges).filter((c) => c.class === 'elsewhere').map(changeLine);
|
|
597
|
+
if (elsewhere.length) result.elsewhere = elsewhere;
|
|
598
|
+
return result;
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
module.exports = {
|
|
602
|
+
isLocalAddress: isLocalAddress,
|
|
603
|
+
providerOf: providerOf,
|
|
604
|
+
modeOf: modeOf,
|
|
605
|
+
eventOf: eventOf,
|
|
606
|
+
startCapture: startCapture,
|
|
607
|
+
send: send,
|
|
608
|
+
spoiled: spoiled,
|
|
609
|
+
tablesOf: tablesOf,
|
|
610
|
+
snapshot: snapshot,
|
|
611
|
+
diff: diff,
|
|
612
|
+
classify: classify,
|
|
613
|
+
changeLine: changeLine,
|
|
614
|
+
settle: settle,
|
|
615
|
+
check: check,
|
|
616
|
+
writesOf: writesOf,
|
|
617
|
+
idsOf: idsOf,
|
|
618
|
+
relatedTo: relatedTo,
|
|
619
|
+
traceValue: traceValue,
|
|
620
|
+
sha256: sha256,
|
|
621
|
+
ROW_LIMIT: ROW_LIMIT,
|
|
622
|
+
BEFORE_FORWARD_MS: BEFORE_FORWARD_MS,
|
|
623
|
+
MIN_WAIT_MS: MIN_WAIT_MS,
|
|
624
|
+
LOG_TABLE: LOG_TABLE,
|
|
625
|
+
TOUCH_COLUMN: TOUCH_COLUMN,
|
|
626
|
+
};
|