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.
@@ -302,8 +302,8 @@
302
302
  });
303
303
  }
304
304
 
305
- var CHECK_NAME = { regression: 'Recorded flows', frontend: 'Frontend code', data: 'Database', edge: 'Input and edge cases', reliability: 'Reliability' };
306
- var CHECK_TONE = { regression: 'sun', frontend: 'amber', data: 'dusk', edge: 'ember', reliability: 'ember' };
305
+ var CHECK_NAME = { regression: 'Recorded flows', frontend: 'Frontend code', data: 'Database', edge: 'Input and edge cases', reliability: 'Reliability', payments: 'Payments' };
306
+ var CHECK_TONE = { regression: 'sun', frontend: 'amber', data: 'dusk', edge: 'ember', reliability: 'ember', payments: 'ember' };
307
307
 
308
308
  function counts() {
309
309
  var confirmed = state.findings.filter(function (f) { return f.status === 'confirmed'; }).length;
@@ -867,7 +867,13 @@
867
867
  head.appendChild(icon(openRow === f.id ? 'change' : 'arrow', 18));
868
868
  head.addEventListener('click', function () { openRow = openRow === f.id ? null : f.id; renderFindings(); });
869
869
  card.appendChild(head);
870
- if (probeAsking && probeAsking.finding && probeAsking.finding.id === f.id) {
870
+ if (paymentsAsking && paymentsAsking.finding && paymentsAsking.finding.id === f.id) {
871
+ var askingPay = el('div', 'controls f-quick');
872
+ askingPay.appendChild(paymentsBox('Yes, wait for one more test payment', function (body) {
873
+ runJob('/api/payments', Object.assign({ id: f.id }, body)).then(function (done) { heard(f, (done && done.result) || {}); }, function () {});
874
+ }));
875
+ card.appendChild(askingPay);
876
+ } else if (probeAsking && probeAsking.finding && probeAsking.finding.id === f.id) {
871
877
  var askingProbe = el('div', 'controls f-quick');
872
878
  askingProbe.appendChild(probeConsentBox(probeAsking.check, 'Yes, try it again', function (body) {
873
879
  runJob('/api/recheck', Object.assign({ id: f.id }, body)).then(function (done) { heard(f, (done && done.result) || {}); }, function () {});
@@ -884,7 +890,7 @@
884
890
  var qre = button('sm', 'refresh', 'Re-check', function () { recheck(f); });
885
891
  qre.setAttribute('data-action', 'recheck');
886
892
  qre.disabled = busy;
887
- quick.appendChild(withCommand(qre, f.check === 'data' ? 'npx kryptheon-night' : 'npx kryptheon recheck ' + f.id));
893
+ quick.appendChild(withCommand(qre, f.check === 'data' ? 'npx kryptheon-night' : f.check === 'payments' ? 'npx kryptheon payments' : 'npx kryptheon recheck ' + f.id));
888
894
  quick.appendChild(whenTag('after every change'));
889
895
  card.appendChild(quick);
890
896
  }
@@ -998,6 +1004,46 @@
998
1004
  box.appendChild(row);
999
1005
  return box;
1000
1006
  }
1007
+ // The payment check's yes: its words, the app on this computer it sends to,
1008
+ // and - only when Razorpay's mode cannot be read from the app's .env - the
1009
+ // person's own word that it is test mode. For a re-check, the finding.
1010
+ var paymentsAsking = null;
1011
+ function paymentsBox(yesLabel, onYes) {
1012
+ var check = state.checks.filter(function (c) { return c.id === 'payments'; })[0];
1013
+ var ask = (check && check.payments) || { app: 'http://localhost:3000', lines: [] };
1014
+ var box = el('div', 'consent');
1015
+ box.appendChild(el('div', 'd-step', 'YOUR APP PROCESSES THE COPIES'));
1016
+ box.appendChild(linesBox('help', ask.lines));
1017
+ var appLabel = el('label', 'tick', 'Your app on this computer: ');
1018
+ var app = el('input', 'field');
1019
+ app.type = 'text';
1020
+ app.value = ask.app;
1021
+ app.setAttribute('aria-label', 'Your app\'s address on this computer');
1022
+ appLabel.appendChild(app);
1023
+ box.appendChild(appLabel);
1024
+ var label = el('label', 'tick');
1025
+ var tick = el('input');
1026
+ tick.type = 'checkbox';
1027
+ label.appendChild(tick);
1028
+ label.appendChild(document.createTextNode(' It is Razorpay\'s TEST mode (only needed when your .env has no rzp_test_ key)'));
1029
+ box.appendChild(label);
1030
+ var row = el('div', 'd-actions');
1031
+ var yes = button('sun sm', 'check', yesLabel, function () {
1032
+ var connection = dbInput.value.trim();
1033
+ if (!connection) { toast('Paste your DEVELOPMENT database\'s connection string in "Connect your database" first.'); dbInput.focus(); return; }
1034
+ var body = { consent: true, connection: connection, app: app.value.trim(), testMode: tick.checked };
1035
+ paymentsAsking = null;
1036
+ renderChecks();
1037
+ renderFindings();
1038
+ onYes(body);
1039
+ });
1040
+ yes.setAttribute('data-action', 'payments');
1041
+ yes.disabled = busy;
1042
+ row.appendChild(yes);
1043
+ row.appendChild(button('sm', null, 'Cancel', function () { paymentsAsking = null; renderChecks(); renderFindings(); }));
1044
+ box.appendChild(row);
1045
+ return box;
1046
+ }
1001
1047
  function checkSwitch(c) {
1002
1048
  var sw = el('label', 'switch');
1003
1049
  var input = el('input');
@@ -1045,7 +1091,21 @@
1045
1091
  (lr.stopped ? ' (stopped)' : '') + ', ' + lr.held + ' held, ' + lr.notTested + ' not tested, ' +
1046
1092
  plural(lr.testData, 'test write') + ' left in your app.'));
1047
1093
  }
1048
- if (c.asks && c.enabled) {
1094
+ if (c.id === 'payments' && c.enabled) {
1095
+ if (paymentsAsking && !paymentsAsking.finding) {
1096
+ card.appendChild(paymentsBox('Yes, start and wait for my test payment', function (body) {
1097
+ runJob('/api/payments', body).then(function (done) {
1098
+ var r = (done && done.result) || {};
1099
+ toast(r.ok ? 'Finished: ' + (r.summary || 'done') : 'It could not finish' + (r.why ? ' - ' + r.why : ''));
1100
+ }, function () {});
1101
+ }));
1102
+ } else {
1103
+ var pay = button('sm', 'play', 'Check payments - start, then pay', function () { paymentsAsking = {}; renderChecks(); });
1104
+ pay.setAttribute('data-action', 'payments');
1105
+ pay.disabled = busy;
1106
+ card.appendChild(withCommand(pay, 'npx kryptheon payments'));
1107
+ }
1108
+ } else if (c.asks && c.enabled) {
1049
1109
  if (probeAsking && probeAsking.check === c.id && !probeAsking.finding) {
1050
1110
  card.appendChild(probeConsentBox(c.id, 'Yes, add test data and run', function (body) {
1051
1111
  runJob('/api/probe', Object.assign({ id: c.id }, body)).then(function (done) {
@@ -1070,6 +1130,16 @@
1070
1130
  box.appendChild(card);
1071
1131
  });
1072
1132
  byId('naBadge').textContent = String(missing);
1133
+ var nc = byId('notCheckedList');
1134
+ clear(nc);
1135
+ (state.notChecked || []).forEach(function (n) {
1136
+ var row = el('div', 'nc-row');
1137
+ row.appendChild(el('div', 'c-name', n.what));
1138
+ row.appendChild(el('div', 'c-what', n.why));
1139
+ row.appendChild(el('div', 'c-what nc-yourself', 'Check it yourself: ' + n.yourself));
1140
+ nc.appendChild(row);
1141
+ });
1142
+ byId('notCheckedBadge').textContent = String((state.notChecked || []).length);
1073
1143
  }
1074
1144
 
1075
1145
  function renderRuns() {
@@ -1130,6 +1200,8 @@
1130
1200
  // An input finding is re-checked by typing its value again, which adds
1131
1201
  // test data to the app: the same yes as the check's own button, first.
1132
1202
  if (f.check === 'edge' || f.check === 'reliability') { probeAsking = { check: f.check, finding: f }; openRow = null; renderFindings(); return; }
1203
+ // A payment finding needs a new payment: the same yes, and one more test payment.
1204
+ if (f.check === 'payments') { paymentsAsking = { finding: f }; openRow = null; renderFindings(); return; }
1133
1205
  runJob('/api/recheck', { id: f.id }).then(function (done) { heard(f, (done && done.result) || {}); }, function () {});
1134
1206
  }
1135
1207
  function heard(f, r) {
@@ -1320,6 +1392,6 @@
1320
1392
  // while a yes is being asked for, and not while someone is typing.
1321
1393
  setInterval(function () {
1322
1394
  var typing = document.activeElement === dbInput || document.activeElement === recordInput;
1323
- if (!busy && !openRow && !pending && !probeAsking && !typing) load();
1395
+ if (!busy && !openRow && !pending && !probeAsking && !paymentsAsking && !typing) load();
1324
1396
  }, 5000);
1325
1397
  })();
@@ -239,6 +239,11 @@
239
239
  .check .c-name { font-weight: 800; font-stretch: 74%; font-size: 1.2rem; line-height: 1.05; text-transform: uppercase; }
240
240
  .check .c-what { font-size: 13.5px; line-height: 1.6; color: var(--muted); }
241
241
  .check .btn { align-self: flex-start; margin-top: auto; }
242
+ .not-checked { border-top: 1px solid var(--line); }
243
+ .nc-row { padding: 14px 4px; border-bottom: 1px solid var(--line); display: grid; gap: 6px; }
244
+ .nc-row .c-name { font-weight: 700; }
245
+ .nc-row .c-what { font-size: 13.5px; line-height: 1.6; color: var(--muted); }
246
+ .nc-row .nc-yourself { color: var(--ink); }
242
247
  .na-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); border-top: 1px solid var(--line); }
243
248
  .na { display: flex; align-items: center; gap: 12px; padding: 14px 4px; font-weight: 700; font-stretch: 74%; text-transform: uppercase; color: var(--muted); border-bottom: 1px solid var(--line); }
244
249
  .na .na-lock { display: block; margin-top: 2px; font-size: 9px; font-weight: 400; color: rgba(18, 22, 29, 0.42); }
@@ -428,6 +433,10 @@
428
433
  <div class="card-head"><div><h3>Coming soon</h3><div class="c-sub">Being built, in this order - never shown as passed</div></div><span class="badge" id="naBadge">0</span></div>
429
434
  <div class="na-grid" id="naList"></div>
430
435
  </div>
436
+ <div class="card gap" id="notCheckedCard">
437
+ <div class="card-head"><div><h3>Not checked by Kryptheon</h3><div class="c-sub">No check here looks at these - a clean report says nothing about them. Check them yourself.</div></div><span class="badge" id="notCheckedBadge">0</span></div>
438
+ <div class="not-checked" id="notCheckedList"></div>
439
+ </div>
431
440
  </section>
432
441
 
433
442
  <section class="view" id="view-runs" hidden>
@@ -25,6 +25,8 @@ const { spawnSync } = require('child_process');
25
25
  const store = require('./kryptheon-store.js');
26
26
  const changes = require('./kryptheon-changes.js');
27
27
  const autostart = require('./kryptheon-autostart.js');
28
+ const selectors = require('./kryptheon-selectors.js');
29
+ const replay = require('./kryptheon-replay.js');
28
30
  const VERSION = require('./package.json').version;
29
31
 
30
32
  /**
@@ -49,7 +51,9 @@ const CHECKS = [
49
51
  { id: 'edge', label: 'Input and edge cases', available: true, asks: true,
50
52
  what: 'Replays your recorded forms with one field at a time given an unusual value - empty, 5000 characters, an HTML tag, emoji, a negative number, a number just past the lowest or highest the page allows, a date that does not exist - and reports only what it can prove: a page error, a 5xx, a bad value saved, a tag shown as HTML. It adds test data to your app, so it runs only when you press its button.' },
51
53
  { id: 'reliability', label: 'Reliability', available: true, asks: true,
52
- what: 'Replays each recorded flow three times and reports one that sometimes works and sometimes fails, with the failed run as proof; and clicks each step that saves twice, quickly, reporting when that saves twice, with both answers as proof; on an app on this computer, sends each save twice at the same moment, reporting a server that makes two records; and looks for what each save added on the page right after it, reporting a page that shows it only when opened again; and at phone size clicks every button with saving blocked, reporting a click that makes the page throw an error. It adds test data to your app, so it runs only when you press its button.' },
54
+ what: 'Replays each recorded flow and reports one that sometimes works and sometimes fails, with the failed run as proof; and clicks each step that saves twice, quickly, reporting when that saves twice, with both answers as proof; on an app on this computer, sends each save twice at the same moment, reporting a server that makes two records or fails; and looks for what each save added on the page right after it, reporting a page that shows it only when opened again; after an edit, opens the form again and reports a field that kept its old value; and at phone size clicks every button with saving blocked, reporting a click - or a page as it loads - that throws an error. It adds test data to your app, so it runs only when you press its button.' },
55
+ { id: 'payments', label: 'Payments: a webhook sent twice', available: true,
56
+ what: 'Real payments in Razorpay\'s or Stripe\'s TEST mode, through a proxy on this computer, each checked as it comes until you press Stop. Its signed webhook is sent to your app again, then once with a wrong signature, and your database is read before and after each - reporting the plan, credits or order given a second time, a repeat answered with an error, or a forged webhook accepted. Live mode is never sent again. It runs only when you press its button.' },
53
57
  { id: 'api', label: 'API', available: false },
54
58
  { id: 'backend', label: 'Backend', available: false },
55
59
  { id: 'synthetic', label: 'Synthetic test users', available: false },
@@ -57,6 +61,24 @@ const CHECKS = [
57
61
 
58
62
  const CHECK_IDS = new Set(CHECKS.map((c) => c.id));
59
63
 
64
+ // What no check here looks at, said plainly so a clean report is never read
65
+ // as "all of it is fine". Each blind round's misses that no check covers are
66
+ // here (rounds 19 and 20: roles, payment rules, counts and searches).
67
+ const NOT_CHECKED = [
68
+ { what: 'Who can do what',
69
+ why: 'Whether a lower role - a member, a customer - can open, change or delete what only a higher role should, or reach another account\'s record by changing an id in the address. Kryptheon replays each recording as the person who made it; it never tries one account\'s actions as another.',
70
+ yourself: 'Sign in as each role and open the higher role\'s pages and actions directly, by their address.' },
71
+ { what: 'Payment rules beyond a repeat',
72
+ why: 'Only the same webhook sent again, and a copy with a wrong signature, are tried. Not tried: a webhook with no signature at all, an amount or currency that does not match the order, buying again while a plan is active, or when a refund takes access away.',
73
+ yourself: 'Read your webhook handler for each of these, and test them in your provider\'s test mode.' },
74
+ { what: 'Whether numbers and lists are right',
75
+ why: 'A count or total that leaves something out, a search or filter that drops results. Kryptheon sees what a page shows, not what it should show.',
76
+ yourself: 'Compare each count and search result with what you know is in your data.' },
77
+ { what: 'Permissions decided in your server code',
78
+ why: 'The database check tests rules the database itself enforces (row level security, grants). An app that decides who sees what in its own server code - with plain Postgres and no such rules - is not tested there.',
79
+ yourself: 'Same as "Who can do what": try each role\'s requests against each other role\'s records.' },
80
+ ];
81
+
60
82
  function readJson(file) {
61
83
  try {
62
84
  let raw = fs.readFileSync(file, 'utf8');
@@ -167,9 +189,12 @@ function riskyPartsLine(f) {
167
189
  }
168
190
 
169
191
  /** When this flow last passed, from the run history, or null. */
170
- function lastPassOf(history, title) {
192
+ function lastPassOf(history, title, specFile) {
193
+ // By file too when both say one: round 18 had four recordings all titled
194
+ // "Round 18 shop", and one's pass was read as another's.
195
+ const same = (x) => x.title === title && (!specFile || !x.specFile || x.specFile === specFile);
171
196
  for (let i = history.length - 1; i >= 0; i--) {
172
- const t = (history[i].tests || []).find((x) => x.title === title);
197
+ const t = (history[i].tests || []).find(same);
173
198
  if (t && t.status === 'passed') return history[i].runAt;
174
199
  }
175
200
  return null;
@@ -245,6 +270,70 @@ const PROBE_WHAT = {
245
270
  const PROBE_WORDS = { reliability: { marker: false } };
246
271
  const PROBE_CHECKS = ['edge', 'reliability'];
247
272
 
273
+ // Loaded when it is first needed, so a page that never shows payments never
274
+ // reads it (and pg is only ever loaded by a run).
275
+ function paymentsRun() {
276
+ return require('./kryptheon-payments-run.js');
277
+ }
278
+
279
+ /**
280
+ * Every payment run kept in the store's payments folder, oldest first. A
281
+ * store from before the traces has only the last report.
282
+ */
283
+ function readPaymentRuns(paths, last) {
284
+ const dir = path.join(paths.dir, paymentsRun().TRACE_DIR);
285
+ let names = [];
286
+ try {
287
+ names = fs.readdirSync(dir).filter((n) => /\.json$/.test(n)).sort();
288
+ } catch (err) {
289
+ names = [];
290
+ }
291
+ const runs = names.map((n) => readJson(path.join(dir, n))).filter((r) => r && Array.isArray(r.results));
292
+ return runs.length ? runs : last ? [last] : [];
293
+ }
294
+
295
+ /** The words before the payment check's yes, and the app it would start from. */
296
+ function paymentsAsk(saved, last) {
297
+ const run = paymentsRun();
298
+ const local = saved && require('./kryptheon-payments.js').isLocalAddress(saved) ? saved : '';
299
+ const app = local || (last && last.app) || 'http://localhost:3000';
300
+ return { app: app, schema: (last && last.schema) || 'public', port: run.DEFAULT_PORT, lines: run.consentLines(app, (last && last.schema) || 'public', run.DEFAULT_PORT) };
301
+ }
302
+
303
+ /**
304
+ * What a fresh payment check says about one payment finding. Fixed only when
305
+ * the run tested a webhook of the same kind and the finding is gone; a run
306
+ * that tested nothing, or tested another event, proves nothing about it.
307
+ */
308
+ function paymentsVerdict(target, before, after, ran, report) {
309
+ const tested = ((report && report.results) || []).filter((r) => !(r.notTested || []).length);
310
+ // Judged by the re-check's own run: older runs stay in the store, so the
311
+ // list after it still holds the finding until this verdict is kept.
312
+ const foundAgain = tested.some((r) => target.where === r.path + ' (' + r.event.type + ')' &&
313
+ (r.findings || []).some((f) => paymentsRun().findingLines(r, f)[0].replace(/^(CRITICAL|HIGH|MEDIUM)\s+/, '') === target.headline));
314
+ let verdict;
315
+ let why = '';
316
+ if (!ran || !ran.ok) {
317
+ verdict = 'could not confirm';
318
+ why = (ran && ran.why) || 'the payment check could not run';
319
+ } else if (foundAgain) {
320
+ verdict = 'still open';
321
+ } else if (!tested.some((r) => target.where === r.path + ' (' + r.event.type + ')')) {
322
+ verdict = 'could not confirm';
323
+ why = tested.length ? 'that run tested a different webhook, not ' + target.where : 'no webhook was tested in that run';
324
+ } else {
325
+ verdict = 'fixed';
326
+ }
327
+ const had = new Set(before.map((f) => f.id));
328
+ return {
329
+ id: target.id, at: new Date().toISOString(), check: 'payments', where: target.where, headline: target.headline,
330
+ verdict: verdict, why: why,
331
+ newProblems: after.filter((f) => !had.has(f.id) && f.check === 'payments').map((f) => ({
332
+ id: f.id, check: f.check, severity: f.severity, status: f.status, where: f.where, headline: f.headline,
333
+ })),
334
+ };
335
+ }
336
+
248
337
  /**
249
338
  * The order someone new should go in, one step at a time. A step is "done"
250
339
  * only when it has actually run and left a record - never because nothing was
@@ -426,6 +515,33 @@ function nightlyLine(nightly) {
426
515
  * evidence, confidence, where, what, and the fix - so the page can show them
427
516
  * the same way whichever check found them.
428
517
  */
518
+ /**
519
+ * The step a failed run stopped at, when its selector rests on data that
520
+ * moves (kryptheon-selectors.js): { line, selector, why }, or null.
521
+ */
522
+ function movingStep(root, t) {
523
+ const line = t.failure && t.failure.line;
524
+ if (!line || !t.specFile) return null;
525
+ // More than one of it on the page is a different reason, already said.
526
+ if (/has \d+ of them/.test(String(t.failure.plainLanguage || ''))) return null;
527
+ let source;
528
+ try {
529
+ source = fs.readFileSync(path.resolve(root, String(t.specFile)), 'utf8');
530
+ } catch (err) {
531
+ return null;
532
+ }
533
+ const hit = selectors.findFragileSelectors(source).find((f) => f.line === line && f.reasons.some((r) => r.kind === 'number' || r.kind === 'data'));
534
+ // A row of a list picked by what it says: the words are a record's, and
535
+ // records change (blind round 20: "J Juniper Alex Parker Brush").
536
+ const text = String(source.split('\n')[line - 1] || '');
537
+ if (!hit && replay.ROW_PICK.test(text)) {
538
+ return { line: line, selector: selectors.selectorChain(text.trim()), why: 'it picks a row by what that record says, which changes when the record does' };
539
+ }
540
+ if (!hit) return null;
541
+ const reason = hit.reasons.find((r) => r.kind === 'number' || r.kind === 'data');
542
+ return { line: line, selector: hit.selector, why: reason.why + (reason.kind === 'number' ? ' that can change as data is added' : '') };
543
+ }
544
+
429
545
  function buildState(root, env) {
430
546
  const paths = store.pathsFor(root, env);
431
547
  const config = readConfig(paths);
@@ -446,17 +562,32 @@ function buildState(root, env) {
446
562
  if (lastRun && config.enabled.regression) {
447
563
  for (const t of lastRun.tests || []) {
448
564
  if (t.status !== 'failed' && t.status !== 'timedOut') continue;
565
+ // A recording that has never passed has not broken: nothing is known
566
+ // to have worked. Said as a thing to check - the recording or the app -
567
+ // as the replay's own summary says ("0 broken, 1 has no baseline").
568
+ // Found on a blind test (round 18): it was listed as "HIGH confirmed -
569
+ // broke" while the summary said nothing was broken.
570
+ const passedBefore = lastPassOf(history, t.title, t.specFile);
571
+ // Never passed, and stopped on a step whose selector holds a number or
572
+ // a date: the recording caught data that moves. Found on blind round
573
+ // 19: three recordings clicked "Projects3" and "fgff gffgddg 0" - counts
574
+ // that grew with every replay - and each was only "the recording or
575
+ // the app?".
576
+ const moved = !passedBefore && movingStep(root, t);
449
577
  findings.push({
450
578
  check: 'regression',
451
- severity: 'HIGH',
452
- status: 'confirmed',
453
- evidence: 'runtime confirmed',
454
- confidence: 'high',
579
+ severity: passedBefore ? 'HIGH' : moved ? 'LOW' : 'MEDIUM',
580
+ status: passedBefore ? 'confirmed' : 'verification required',
581
+ evidence: passedBefore ? 'runtime confirmed' : 'seen once',
582
+ confidence: passedBefore ? 'high' : 'medium',
455
583
  where: String((t.failure && t.failure.file) || t.specFile || 'tests').split('\\').join('/') +
456
584
  (t.failure && t.failure.line ? ':' + t.failure.line : ''),
457
- headline: 'The recorded flow "' + t.title + '" broke.',
585
+ headline: passedBefore ? 'The recorded flow "' + t.title + '" broke.'
586
+ : moved ? 'The recorded flow "' + t.title + '" has never passed: it stops at line ' + moved.line + ', ' + moved.selector + ', and ' + moved.why +
587
+ ' - most likely the recording, not the app. Record that step again on something that does not change.'
588
+ : 'The recorded flow "' + t.title + '" has never passed - the recording or the app?',
458
589
  detail: (t.failure && t.failure.plainLanguage) || '',
459
- fixPrompt: flowPrompt(t, lastPassOf(history, t.title)),
590
+ fixPrompt: flowPrompt(t, passedBefore),
460
591
  when: lastRun.runAt,
461
592
  });
462
593
  }
@@ -507,6 +638,48 @@ function buildState(root, env) {
507
638
  }
508
639
  }
509
640
 
641
+ // The payment check: every run kept in the store, not only the last. A
642
+ // finding stays until a re-check says FIXED. Found on a blind test (round
643
+ // 18): credits given twice was found by one payment, the next payment (a
644
+ // different product on the same webhook) held, and the finding vanished.
645
+ const paymentsLast = readJson(path.join(paths.dir, paymentsRun().REPORT_FILE));
646
+ const paymentRuns = readPaymentRuns(paths, paymentsLast);
647
+ const paymentsFixedAt = {};
648
+ for (const a of readHistory(paths.fixes, 500)) {
649
+ if (a.check === 'payments' && a.verdict === 'fixed' && (!paymentsFixedAt[a.id] || a.at > paymentsFixedAt[a.id])) paymentsFixedAt[a.id] = a.at;
650
+ }
651
+ const paymentFindings = new Map();
652
+ if (config.enabled.payments) for (const run of paymentRuns) {
653
+ for (const r of run.results || []) {
654
+ for (const f of r.findings || []) {
655
+ const words = paymentsRun().findingLines(r, f);
656
+ const item = {
657
+ check: 'payments',
658
+ severity: f.severity,
659
+ // A refund that took nothing back is seen, not proven wrong.
660
+ status: f.toCheck ? 'verification required' : 'confirmed',
661
+ evidence: f.toCheck ? 'seen once' : 'runtime confirmed',
662
+ confidence: f.toCheck ? 'medium' : 'high',
663
+ where: r.path + ' (' + r.event.type + ')',
664
+ headline: words[0].replace(/^(CRITICAL|HIGH|MEDIUM)\s+/, ''),
665
+ // What changed, then the whole trace: each delivery, when, its
666
+ // answer, and the rows it wrote - so the proof is on the finding.
667
+ detail: words.slice(2).map((l) => l.trim()).join('\n') +
668
+ (r.trace ? '\n\n' + paymentsRun().traceLines(r).join('\n') : '') +
669
+ (run.traceFile ? '\n\nKept on this computer: ' + run.traceFile : ''),
670
+ fixPrompt: paymentsRun().fixPrompt(r, f),
671
+ when: run.at,
672
+ kind: f.kind,
673
+ };
674
+ // The newest sighting of each finding is the one shown.
675
+ paymentFindings.set(changes.findingId(item), item);
676
+ }
677
+ }
678
+ }
679
+ for (const [id, item] of paymentFindings) {
680
+ if (!paymentsFixedAt[id] || paymentsFixedAt[id] < item.when) findings.push(item);
681
+ }
682
+
510
683
  let nightWhen = null;
511
684
  try {
512
685
  nightWhen = fs.statSync(paths.nightLast).mtime.toISOString();
@@ -570,7 +743,11 @@ function buildState(root, env) {
570
743
  f.id = changes.findingId(f);
571
744
  f.attempts = attempts.filter((a) => a.id === f.id).map((a) => ({ at: a.at, verdict: a.verdict, why: a.why || '', newProblems: (a.newProblems || []).length }));
572
745
  forAll.push({ id: f.id, severity: f.severity, headline: f.headline, where: f.where, prompt: f.fixPrompt || '' });
573
- if (f.fixPrompt && PROBE_CHECKS.includes(f.check)) {
746
+ if (f.fixPrompt && f.check === 'payments') {
747
+ // Proving it needs a new payment: the old webhook has been handled.
748
+ f.fixPrompt += '\n\nWhen you have fixed it, prove it: press Re-check on this finding in the Kryptheon dashboard ' +
749
+ '(or run npx kryptheon payments) and make one more test payment. It is fixed only when that says FIXED.';
750
+ } else if (f.fixPrompt && PROBE_CHECKS.includes(f.check)) {
574
751
  // Re-checking runs the flow in the app again, so it adds a little test
575
752
  // data: --yes says yes for an app on this computer, and is refused for
576
753
  // any other address - there the person must answer.
@@ -603,6 +780,12 @@ function buildState(root, env) {
603
780
  : 'last run held';
604
781
  }
605
782
  else if (c.id === 'frontend') state = !code ? 'never run' : (code.findings || []).length ? 'things to check' : 'nothing found';
783
+ else if (c.id === 'payments') {
784
+ state = !paymentsLast ? 'never run'
785
+ : findings.some((f) => f.check === 'payments') ? 'problems found'
786
+ : !(paymentsLast.results || []).some((r) => !(r.notTested || []).length) ? 'nothing was tested'
787
+ : 'last run held';
788
+ }
606
789
  else if (c.id === 'data') {
607
790
  const ranNightly = nightly && nightly.ranAt && !nightly.stopped;
608
791
  state = !night && !ranNightly ? 'never run here'
@@ -614,6 +797,9 @@ function buildState(root, env) {
614
797
  // A check that adds data to the app: never in "Run all", never on its
615
798
  // own - only its own button, after the page has shown these words.
616
799
  asks: c.asks && config.enabled[c.id] && recordings > 0 ? probeAsk(root, PROBE_WHAT[c.id], env, PROBE_WORDS[c.id]) : null,
800
+ // What the payment check says before its yes, and the app it starts
801
+ // from: the saved address when it is this computer's, else the usual one.
802
+ payments: c.id === 'payments' && config.enabled.payments ? paymentsAsk(readAppAddress(paths), paymentsLast) : null,
617
803
  lastRun: probeLast[c.id] ? {
618
804
  at: probeLast[c.id].endedAt, stopped: !!probeLast[c.id].stopped, runsDone: probeLast[c.id].runsDone, runsPlanned: probeLast[c.id].runsPlanned,
619
805
  held: (probeLast[c.id].held || []).length,
@@ -660,6 +846,7 @@ function buildState(root, env) {
660
846
  looks: looks,
661
847
  proven: proven,
662
848
  checks: checks,
849
+ notChecked: NOT_CHECKED,
663
850
  findings: findings,
664
851
  allFixPrompt: combinedPrompt(forAll),
665
852
  runs: history.slice().reverse().map((r) => ({ runAt: r.runAt, status: r.status, passed: r.passed, failed: r.failed, durationMs: r.durationMs })),
@@ -691,8 +878,10 @@ function buildState(root, env) {
691
878
 
692
879
  // What each run is called on the page, and the command that does the same in
693
880
  // a terminal - shown under every button, for when the page cannot be used.
694
- const RUN_TITLE = { frontend: 'Reading your code', regression: 'Replaying your recordings', edge: 'Trying unusual inputs in your forms', reliability: 'Replaying your flows three times, and double-clicking' };
695
- const RUN_COMMAND = { frontend: 'npx kryptheon code', regression: 'npx kryptheon check', edge: 'npx kryptheon edge', reliability: 'npx kryptheon reliability' };
881
+ const RUN_TITLE = { frontend: 'Reading your code', regression: 'Replaying your recordings', edge: 'Trying unusual inputs in your forms', reliability: 'Replaying your flows three times, and double-clicking',
882
+ payments: 'Waiting for test payments, and sending each webhook again' };
883
+ const RUN_COMMAND = { frontend: 'npx kryptheon code', regression: 'npx kryptheon check', edge: 'npx kryptheon edge', reliability: 'npx kryptheon reliability',
884
+ payments: 'npx kryptheon payments' };
696
885
  const DB_ACTIONS = {
697
886
  scan: { title: 'Checking your database', command: 'npx kryptheon-night' },
698
887
  install: { title: 'Setting up the nightly check', command: 'npx kryptheon-night install' },
@@ -1055,7 +1244,7 @@ function createServer(root, options) {
1055
1244
  if (!address) return json(res, 400, { ok: false, why: notAnAddress(input.url, 'http://localhost:3000') });
1056
1245
  const phone = input.phone === true;
1057
1246
  return begin('record', 'Recording ' + address + (phone ? ' at phone size' : ''),
1058
- 'npx kryptheon record ' + address + (phone ? ' --phone' : ''), (say) => opts.record(address, say, { phone: phone }));
1247
+ 'npx kryptheon record ' + address + (phone ? ' --phone' : ''), (say, control) => opts.record(address, say, { phone: phone, control: control }));
1059
1248
  }
1060
1249
  if (url.pathname === '/api/db' && typeof opts.db === 'function') {
1061
1250
  const action = String(input.action || '');
@@ -1087,6 +1276,43 @@ function createServer(root, options) {
1087
1276
  return begin('db', DB_ACTIONS[action].title, DB_ACTIONS[action].command,
1088
1277
  (say) => opts.db(action, connection, say), secretsOf(connection));
1089
1278
  }
1279
+ if (url.pathname === '/api/payments' && typeof opts.payments === 'function') {
1280
+ // One test payment, its webhook sent again. Its copies are processed
1281
+ // by the app, so only with the yes from the page, only for an app on
1282
+ // this computer, and the connection string is handled like the
1283
+ // database check's: given to this job only, scrubbed from its lines.
1284
+ if (!readConfig(store.pathsFor(root, env)).enabled.payments) return json(res, 400, { ok: false, why: 'the payment check is switched off' });
1285
+ if (input.consent !== true) return json(res, 400, { ok: false, why: 'its copies are processed by your app, so it runs only after you say yes' });
1286
+ const connection = connectionFrom(input.connection);
1287
+ if (!connection) return json(res, 400, { ok: false, why: 'paste your development database\'s connection string in "Connect your database" first' });
1288
+ const app = appAddress(typeof input.app === 'string' ? input.app : '');
1289
+ const full = app && !/^https?:\/\//i.test(app) ? 'http://' + app : app;
1290
+ if (!full || !require('./kryptheon-payments.js').isLocalAddress(full)) {
1291
+ return json(res, 400, { ok: false, why: 'the payment check only runs against an app on this computer - start it locally and give its address, like http://localhost:3000' });
1292
+ }
1293
+ const schema = typeof input.schema === 'string' && /^[A-Za-z_][A-Za-z0-9_]{0,62}$/.test(input.schema) ? input.schema : 'public';
1294
+ const run = { app: full, schema: schema, connection: connection, testMode: input.testMode === true };
1295
+ const id = input.id === undefined ? null : String(input.id);
1296
+ if (id !== null) {
1297
+ if (!/^[0-9a-f]{8}$/.test(id)) return json(res, 400, { ok: false, why: 'not a finding id' });
1298
+ const target = buildState(root, env).findings.find((f) => f.id === id && f.check === 'payments');
1299
+ if (!target) return json(res, 404, { ok: false, why: 'no open payment finding with that id' });
1300
+ return begin('recheck', 'Re-checking: ' + target.headline, RUN_COMMAND.payments, (say, control) => {
1301
+ const before = buildState(root, env).findings;
1302
+ return Promise.resolve(opts.payments(run, say, control)).then((ran) => {
1303
+ const report = readJson(path.join(store.pathsFor(root, env).dir, paymentsRun().REPORT_FILE));
1304
+ const attempt = paymentsVerdict(target, before, buildState(root, env).findings, ran, report);
1305
+ try {
1306
+ fs.appendFileSync(store.pathsFor(root, env).fixes, JSON.stringify(attempt) + '\n', 'utf8');
1307
+ } catch (err) {
1308
+ attempt.saveError = err.message;
1309
+ }
1310
+ return Object.assign({ ok: true, summary: attempt.verdict }, attempt);
1311
+ });
1312
+ }, secretsOf(connection));
1313
+ }
1314
+ return begin('payments', RUN_TITLE.payments, RUN_COMMAND.payments, (say, control) => opts.payments(run, say, control), secretsOf(connection));
1315
+ }
1090
1316
  if (url.pathname === '/api/app') {
1091
1317
  // The app's web address: saved for this project, so Record and the
1092
1318
  // outside check start from it. Empty clears it.
@@ -1157,6 +1383,8 @@ module.exports = {
1157
1383
  createJobs: createJobs,
1158
1384
  DB_ACTIONS: DB_ACTIONS,
1159
1385
  databaseVerdict: databaseVerdict,
1386
+ paymentsVerdict: paymentsVerdict,
1387
+ paymentsAsk: paymentsAsk,
1160
1388
  startSteps: startSteps,
1161
1389
  nextAction: nextAction,
1162
1390
  combinedPrompt: combinedPrompt,