kryptheon 0.1.64 → 0.1.66

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/kryptheon-api.js CHANGED
@@ -31,7 +31,24 @@ const EMAIL = /[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/g;
31
31
  const SIGN_IN = /(^|\/)(log-?in|sign-?in|session|auth\/(login|signin|token))$/i;
32
32
  const SIGN_OUT = /(^|\/)(log-?out|sign-?out)$/i;
33
33
  // Addresses that are sign-up, password and account plumbing: not actions to try as another account.
34
- const PLUMBING = /(^|\/)(sign-?up|register|password|forgot|reset|verify|confirm|callback|oauth|health|healthz|ready|live|favicon)(\/|$)/i;
34
+ const PLUMBING_WORDS = /(^|\/)(sign-?up|register|password|forgot|reset|verify|confirm|callback|oauth|health|healthz|ready|live|favicon)(\/|$)/i;
35
+ // "confirm" and "verify" are an account's own step (/auth/confirm, /email/verify) - but a step on a record
36
+ // (/orders/:id/confirm, /payments/{id}/verify) is a business action like approve. Blind round 32: confirming an
37
+ // order, the step that takes stock, was never tried across organizations or twice at once.
38
+ const ACCOUNT_PATH = /(^|\/)(auth|account|accounts|email|emails|user|users|session|sessions|signup|sign-up|login|invite|invites|invitation|invitations|password|me|profile)(\/|$)/i;
39
+ const PLUMBING = {
40
+ test(p) {
41
+ const path = String(p || '');
42
+ if (!PLUMBING_WORDS.test(path)) return false;
43
+ const m = /(^|\/)(verify|confirm)(\/|$)/i.exec(path);
44
+ if (!m) return true;
45
+ // only confirm/verify matched: plumbing unless it is a step on a record outside the account's own paths
46
+ const rest = path.replace(/(^|\/)(verify|confirm)(\/|$)/ig, '/');
47
+ if (PLUMBING_WORDS.test(rest)) return true;
48
+ const onRecord = /\/(:\w+|\{\w+\})\/(verify|confirm)(\/|$)/i.test(path) || /\/(:\w+|\{\w+\})\//.test(path.slice(0, m.index + 1));
49
+ return !onRecord || ACCOUNT_PATH.test(path);
50
+ },
51
+ };
35
52
  const MAX_FILE = 400 * 1024;
36
53
 
37
54
  /** The sign-in route of the map: { method, path, emailField, passwordField, encoding } or null with why. */
@@ -129,6 +146,8 @@ function sampleValue(field, known, roleNames) {
129
146
  if (roleNames && roleNames.length && wordsOf(field.name).includes('role')) return roleNames[0];
130
147
  const en = (field.checks || []).find((c) => c.kind === 'enum' && c.values && c.values.length);
131
148
  if (en) return en.values[0];
149
+ // read by the code as Number(...) / parseInt(...): a number (round 33)
150
+ if (field.numeric) return 1;
132
151
  if (known && known.has(field.name)) return known.get(field.name)[0];
133
152
  // the column of the same name, as the app's own CREATE TABLE declares it (round 25: text into a boolean, NaN into an integer)
134
153
  const declared = known && known.types && known.types[String(field.name).toLowerCase()];
@@ -210,6 +229,7 @@ function actionOf(r, known, roleNames) {
210
229
  data: Object.keys(data).length ? data : null, encoding: encodingOf(r), flow: r.file + ':' + r.line, from: { file: r.file, line: r.line },
211
230
  mapAuth: r.auth ? r.auth.status : map.STATUS.UNKNOWN, leftOut, unchecked: unchecked, fields: fields, fullData: fullData,
212
231
  guardRoles: r.role && r.role.status === map.STATUS.DETECTED && Array.isArray(r.role.roles) ? r.role.roles : null,
232
+ roleChecked: Boolean(r.role && r.role.status === map.STATUS.DETECTED),
213
233
  scopedByOrg: Boolean(r.db && r.db.some((d) => d.level === 'organisation')),
214
234
  };
215
235
  }
@@ -549,7 +569,7 @@ function draftPolicy(plan) {
549
569
  if ((!a.guardRoles || a.guardRoles.includes(r)) && !rolesOut[r].includes(perm)) rolesOut[r].push(perm);
550
570
  }
551
571
  }
552
- const noCheck = actions.filter((a) => !a.guardRoles).map((a) => a.method + ' ' + a.template);
572
+ const noCheck = actions.filter((a) => !a.guardRoles && !a.roleChecked).map((a) => a.method + ' ' + a.template);
553
573
  const json = {
554
574
  draft: 'Written by Kryptheon on ' + new Date().toISOString().slice(0, 10) + ' from your code. It lists what your code does TODAY, not what you want. Edit every role to what you want, delete this line, and rename the file to ' + roles.POLICY_FILE + '.',
555
575
  notes: [
@@ -598,7 +618,7 @@ async function runRoles(root, options) {
598
618
  say('Signing in as ' + j.accounts.usable.length + ' accounts, and trying ' + j.actions.length + ' routes as each, across organizations and without signing in...');
599
619
  const result = await roles.checkAccess({
600
620
  origin: origin, policy: plan.policy.policy, journey: { signIn: j.signIn, actions: j.actions }, accounts: j.accounts.usable,
601
- fetch: opts.fetch, say: say, stopped: opts.stopped, maxAttempts: opts.maxAttempts, anonymous: true, seed: true,
621
+ fetch: opts.fetch, say: say, stopped: opts.stopped, maxAttempts: opts.maxAttempts, anonymous: true, seed: true, tryUnruled: true,
602
622
  });
603
623
  roles.markHints(result.attempts);
604
624
  const findings = roles.findingsOf(result.attempts.filter((a) => a.kind !== 'signin'), plan.policy.policy).concat(findingsOf(result.attempts.filter((a) => a.kind === 'signin'), j));
@@ -707,13 +727,53 @@ async function runInvalid(root, options) {
707
727
  'First check this is real. If it is, before applying ' + f.name + ', read what is held now (with a row lock, in the same transaction) and refuse with a 400 or 409 when the result would be below zero. Change nothing else.', '', 'Afterwards I will run: npx kryptheon api'].join('\n');
708
728
  out.findings.push(finding);
709
729
  };
730
+ const getTemplates = new Set(j.actions.filter((x) => x.method === 'GET' && x.params.length <= 1).map((x) => x.template));
731
+ // the request limit ends this check where it is: what was tried is kept, the routes it did not reach are said
732
+ let reaching = null;
733
+ try {
734
+ // what the updates cost: a read, a send, a read and a send back with a read for each wrong value (a run that was given a cap keeps it)
735
+ if (opts.maxRequests === undefined) ctx.limit = Math.min(3000, ctx.limit + j.actions.filter(isUpdateRoute).reduce((n, x) => n + 6 + x.fields.length * 6 * 5, 0));
710
736
  for (const a of j.actions) {
711
737
  if (!/^(POST|PUT|PATCH)$/.test(a.method) || !(a.fields || []).length) continue;
712
738
  const what = a.method + ' ' + a.template;
739
+ // a change to a record or a singleton (settings): read first, one field made wrong at a time, the original values sent back
740
+ if (isUpdateRoute(a)) {
741
+ reaching = a;
742
+ if (await invalidOnUpdate({ session, out, who, getTemplates, say }, a) === 'down') return out;
743
+ continue;
744
+ }
745
+ reaching = a;
713
746
  const probes = probesOf(a);
714
747
  if (!probes.length) continue;
715
748
  if (a.params.length || a.method !== 'POST') {
716
- out.notTried.push({ what: what, why: 'it changes an existing record, and bad values are only tried on routes that create one' });
749
+ // A step on one record that changes it by an amount (PATCH /stock/{id}/adjust, delta): only a change larger than
750
+ // what is held, at a record its own list shows, then the opposite change, then the starting change undone (round 33:
751
+ // stock adjusted to -999 was never tried, as the route changes an existing record). Nothing else is sent there.
752
+ const over = probes.filter((pr) => pr.mode === 'overdraw');
753
+ const listT = a.params.length === 1 ? a.template.split('/{id}')[0] : null;
754
+ const rec = over.length && listT && getTemplates.has(listT) ? ((rowsOfReply(await session.send('GET', listT, {})) || []).find((r) => r && r.id !== undefined)) : null;
755
+ if (!rec) {
756
+ out.notTried.push({ what: what, why: 'it changes an existing record, and bad values are only tried on routes that create one' });
757
+ continue;
758
+ }
759
+ reaching = a;
760
+ const at = a.template.replace('{id}', encodeURIComponent(String(rec.id)));
761
+ const sendAt = (b) => session.send(a.method, at, a.encoding === 'form' ? { form: b } : { json: b });
762
+ const base = a.fullData || {};
763
+ const first = await sendAt(base);
764
+ if (!isTaken(first)) {
765
+ out.notTried.push({ what: what, why: 'the app refused the request (' + first.status + ') before any bad value was added, so no bad value could be judged' });
766
+ continue;
767
+ }
768
+ for (const pr of over) {
769
+ if (opts.stopped && opts.stopped()) return out;
770
+ await overdraw(a, pr.field, pr, base, Object.assign({}, base, { [pr.field.name]: pr.value }), sendAt);
771
+ }
772
+ // the starting change, undone
773
+ const undo = Object.assign({}, base);
774
+ for (const pr of over) if (typeof base[pr.field.name] === 'number') undo[pr.field.name] = -base[pr.field.name];
775
+ const undone = await sendAt(undo);
776
+ out.left.push(what.replace('{id}', rec.id) + ' with a valid body (' + Object.keys(base).join(', ') + ') - the starting point of the tries' + (isTaken(undone) ? ', undone by the opposite change' : ' (the opposite change was refused: ' + undone.status + ')'));
717
777
  continue;
718
778
  }
719
779
  // A bad value means something only if the same request without it is taken. The full body first; if the app
@@ -790,6 +850,7 @@ async function runInvalid(root, options) {
790
850
  }
791
851
  }
792
852
  // A write aimed at a record that cannot exist: the app should say no (404, 400). A success is a write on nothing.
853
+ reaching = null;
793
854
  for (const a of j.actions) {
794
855
  if (!/^(POST|PUT|PATCH)$/.test(a.method) || a.params.length !== 1) continue;
795
856
  if (opts.stopped && opts.stopped()) return out;
@@ -811,9 +872,200 @@ async function runInvalid(root, options) {
811
872
  'First check this is real: look at what the handler does when the record is not found. If it saves or answers success anyway, look the record up first and answer 404 when it is not there (and, for a table that points at it, add a foreign key). Change nothing else.', '', 'Afterwards I will run: npx kryptheon api'].join(String.fromCharCode(10));
812
873
  out.findings.push(finding);
813
874
  }
875
+ } catch (err) {
876
+ if (!authz.isRequestLimit(err)) throw err;
877
+ out.stoppedAt = err.limit;
878
+ const at = j.actions.indexOf(reaching);
879
+ for (const x of at < 0 ? [] : j.actions.slice(at)) if (/^(POST|PUT|PATCH)$/.test(x.method) && (x.fields || []).length) out.notTried.push({ what: x.method + ' ' + x.template, why: 'the request limit (' + err.limit + ') was reached first; what was tried before it is kept' });
880
+ }
881
+ return out;
882
+ }
883
+
884
+ // --- bad values on a route that CHANGES a record ------------------------------------
885
+
886
+ // A field that holds a secret: never sent a wrong value, and never read back into a report.
887
+ const SECRET_FIELD = /^(password|passwd|secret|token|apikey|key|hash|salt|otp|pin|cvv)$/;
888
+
889
+ /** Whether a route is an update Kryptheon can read first: PUT|PATCH on a record (/things/{id}) or on a singleton (/settings). */
890
+ function isUpdateRoute(a) {
891
+ if (!/^(PUT|PATCH)$/.test(a.method) || !(a.fields || []).length) return false;
892
+ return !a.params.length || (a.params.length === 1 && a.segs[a.segs.length - 1] === '{id}');
893
+ }
894
+
895
+ /** Whether a value read back is the one that was sent (a number may come back as text, a long text cut short). */
896
+ function storedAs(got, sent) {
897
+ if (got === undefined) return false;
898
+ if (JSON.stringify(got) === JSON.stringify(sent)) return true;
899
+ if (typeof sent === 'number' && got !== null && got !== '' && typeof got !== 'object' && Number(got) === sent) return true;
900
+ if (typeof sent === 'string' && sent.length > 40 && typeof got === 'string' && got.slice(0, 40) === sent.slice(0, 40)) return true;
901
+ return false;
902
+ }
903
+
904
+ /**
905
+ * The tries on a field of an update, one wrong thing at a time, from the record's current values:
906
+ * the same blank, negative, above-100 and unchecked-field tries a create gets, and for every field a value of the wrong kind
907
+ * (an object, an array, null, and a number for a text or text for a number).
908
+ */
909
+ function updateProbesOf(a, base) {
910
+ const out = [];
911
+ const seen = new Set();
912
+ const add = (f, mode, value, why) => {
913
+ if (JSON.stringify(value) === JSON.stringify(base[f.name])) return;
914
+ // the same value for the same field once, whatever it is called
915
+ const k = f.name + '|' + JSON.stringify(value);
916
+ if (seen.has(k)) return;
917
+ seen.add(k);
918
+ out.push({ field: f, mode: mode, value: value, why: why });
919
+ };
920
+ const leave = (f) => base[f.name] === undefined || wordsOf(f.name).some((w) => SECRET_FIELD.test(w)) || /^(id|uuid)$/i.test(f.name);
921
+ for (const pr of probesOf(a)) if (pr.mode !== 'overdraw' && !leave(pr.field)) add(pr.field, pr.mode, pr.value, pr.why);
922
+ for (const f of a.fields) {
923
+ if (leave(f)) continue;
924
+ const kind = typeof f.sample === 'number' ? 'number' : typeof f.sample === 'boolean' ? 'boolean' : typeof base[f.name] === 'number' ? 'number' : 'text';
925
+ add(f, 'object', { 'kx-invalid': 'kx-invalid-object' }, 'an object where a ' + kind + ' belongs');
926
+ add(f, 'array', ['kx-invalid-array'], 'an array where a ' + kind + ' belongs');
927
+ add(f, 'null', null, 'null');
928
+ if (kind === 'text') add(f, 'number', 12345, 'a number (12345) where a text belongs');
929
+ else add(f, 'string', 'kx-invalid-not-a-' + kind, 'text where a ' + kind + ' belongs');
930
+ }
814
931
  return out;
815
932
  }
816
933
 
934
+ /**
935
+ * Bad values sent to one PUT|PATCH route, on a record or a singleton (settings, profile). The record is read first;
936
+ * the body starts from its current values for the fields the route reads, and one field at a time is made wrong.
937
+ * Judged from the app's own read before and after: a 500 = crashed; the bad value shown after = kept; some other
938
+ * value stored in its place = changed; refused with nothing changed = turned away. Every try that was taken is
939
+ * followed by the original values sent back, and the read says whether that worked (said in `left`).
940
+ * Returns 'down' when the app stopped answering, 'stuck' when a record could not be put back, else null.
941
+ */
942
+ async function invalidOnUpdate(env, a) {
943
+ const { session, out, who, getTemplates, say } = env;
944
+ const what = a.method + ' ' + a.template;
945
+ const singleton = !a.params.length;
946
+ const listTemplate = singleton ? null : '/' + a.segs.slice(0, a.params[0].index).join('/');
947
+ const no = (why) => { out.notTried.push({ what: what, why: why }); return null; };
948
+ if (singleton && !getTemplates.has(a.template)) return no('there is no existing record to start from: nothing reads ' + a.template + ' to get the current values');
949
+ if (!singleton && !getTemplates.has(listTemplate)) return no('there is no existing record to start from: no GET ' + listTemplate + ' lists those records');
950
+ const json = (b) => (a.encoding === 'form' ? { form: b } : { json: b });
951
+ // the records to start from: the singleton itself, or the first few of the list that have an id
952
+ let rows;
953
+ if (singleton) {
954
+ const r = await session.send('GET', a.template, {});
955
+ rows = r.status === 200 && r.json !== undefined ? [{ at: a.template, read: r.json }] : [];
956
+ } else {
957
+ const r = await session.send('GET', listTemplate, {});
958
+ const list = rowsOfReply(r) || [];
959
+ rows = list.filter((x) => x && typeof x === 'object' && x.id !== undefined).slice(0, 3).map((x) => ({ at: a.template.replace('{id}', encodeURIComponent(String(x.id))), read: x, id: x.id }));
960
+ }
961
+ if (!rows.length) return no('there is no existing record to start from: ' + (singleton ? 'GET ' + a.template + ' did not answer with one' : 'GET ' + listTemplate + ' lists no record with an id'));
962
+ const detail = !singleton && getTemplates.has(a.template);
963
+ const readNow = async (row) => {
964
+ if (singleton || detail) {
965
+ const r = await session.send('GET', row.at, {});
966
+ return r.status === 200 && r.json !== undefined ? r.json : undefined;
967
+ }
968
+ const r = await session.send('GET', listTemplate, {});
969
+ const list = rowsOfReply(r) || [];
970
+ return list.find((x) => x && String(x.id) === String(row.id));
971
+ };
972
+ // the starting body: each field the route reads that the record shows a plain value for
973
+ const bodyOf = (rec, only) => {
974
+ const b = {};
975
+ for (const f of a.fields) {
976
+ if (wordsOf(f.name).some((w) => SECRET_FIELD.test(w)) || /^(id|uuid)$/i.test(f.name)) continue;
977
+ if (only && !(f.kinds.length || wordsOf(f.name).some((x) => REQUIRED_NAME.test(x)))) continue;
978
+ const cur = valueIn(rec, f.name);
979
+ if (cur !== undefined && (cur === null || typeof cur !== 'object')) b[f.name] = cur;
980
+ }
981
+ return b;
982
+ };
983
+ let row = null;
984
+ let base = null;
985
+ let refused = null;
986
+ for (const candidate of rows) {
987
+ const rec = candidate.read;
988
+ for (const only of [false, true]) {
989
+ const b = bodyOf(rec, only);
990
+ if (!Object.keys(b).length || (only && JSON.stringify(b) === JSON.stringify(bodyOf(rec, false)))) continue;
991
+ const first = await session.send(a.method, candidate.at, json(b));
992
+ const rp = authz.replyOf(first);
993
+ if (rp.kind === 'accepted' || rp.kind === 'redirect') { row = candidate; base = b; break; }
994
+ refused = first.status;
995
+ }
996
+ if (row) break;
997
+ }
998
+ if (!row) return no(refused === null ? 'there is no existing record to start from: none of the fields the route reads (' + a.fields.map((f) => f.name).join(', ') + ') is in what the app shows for it' : 'the app refused the record\'s own current values (' + refused + ') before any bad value was added, so no bad value could be judged');
999
+ const target = row.at;
1000
+ out.left.push(what + ' at ' + target + ' with the record\'s own current values (' + Object.keys(base).join(', ') + ') - the starting point of the bad-value tries; nothing was changed');
1001
+ const probes = updateProbesOf(a, base);
1002
+ for (const pr of probes) {
1003
+ const f = pr.field;
1004
+ const orig = base[f.name];
1005
+ const body = Object.assign({}, base, { [f.name]: pr.value });
1006
+ const res = await session.send(a.method, target, json(body));
1007
+ const reply = authz.replyOf(res);
1008
+ if (reply.kind === 'no-answer' && await appDown(session, singleton ? a.template : listTemplate)) {
1009
+ out.findings.push(downFinding(who.email, what, f, pr, res));
1010
+ out.appDown = what + ' with ' + f.name + ' ' + pr.why;
1011
+ out.tried.push({ what: what, field: f.name, mode: pr.mode, sent: pr.why, status: 0, reading: 'no-answer', kept: false, verdict: 'stopped the app', from: f.file + ':' + f.line, update: true });
1012
+ return 'down';
1013
+ }
1014
+ const took = reply.kind === 'accepted' || reply.kind === 'redirect';
1015
+ const crashed = reply.kind === 'server-error';
1016
+ const after = took || crashed ? await readNow(row) : undefined;
1017
+ const now = after === undefined ? undefined : valueIn(after, f.name);
1018
+ const emptied = pr.mode === 'blank' && (now === null || now === '');
1019
+ const kept = took && (pr.mode === 'number' ? JSON.stringify(now) === JSON.stringify(pr.value) : storedAs(now, pr.value) || emptied);
1020
+ const changed = (took || crashed) && !kept && now !== undefined && JSON.stringify(now) !== JSON.stringify(orig) && String(now) !== String(orig);
1021
+ let verdict = 'turned away';
1022
+ if (crashed) verdict = 'crashed';
1023
+ else if (took && kept) verdict = 'kept';
1024
+ else if (took && changed) verdict = 'changed';
1025
+ else if (took) verdict = 'taken, not shown';
1026
+ out.tried.push({ what: what, field: f.name, mode: pr.mode, sent: pr.why, status: res.status, reading: reply.kind, kept: kept, verdict: verdict, from: f.file + ':' + f.line, update: true });
1027
+ // put back whatever may have moved: every taken try, and a crash that left something behind
1028
+ let putBack = null;
1029
+ if (took || (crashed && changed)) {
1030
+ const back = await session.send(a.method, target, json(base));
1031
+ const again = await readNow(row);
1032
+ const was = again === undefined ? undefined : valueIn(again, f.name);
1033
+ putBack = was === undefined ? !(kept || changed) : String(was) === String(orig) || JSON.stringify(was) === JSON.stringify(orig);
1034
+ out.left.push(what + ' at ' + target + ' with ' + f.name + ' = ' + pr.why + ' (' + (kept ? 'now shown by the app' : changed ? 'it stored "' + authz.clip(JSON.stringify(now), 60) + '"' : 'its effect was not visible') + '); then the original values were sent back, answered ' + back.status + (putBack ? ', which put it back' : ' - it did NOT put it back, look at it'));
1035
+ if (!putBack) {
1036
+ out.notTried.push({ what: what, why: 'stopped: the record could not be put back after ' + f.name + ' ' + pr.why + ', so nothing more was sent to it' });
1037
+ return 'stuck';
1038
+ }
1039
+ }
1040
+ const wrongKind = /^(object|array|string|number)$/.test(pr.mode);
1041
+ // a crash is always a finding; a value stored is one where the app is expected to refuse it: null is a plain "no value" unless the field is required
1042
+ const required = f.kinds.some((k) => /^(required)$/.test(k)) || wordsOf(f.name).some((x) => REQUIRED_NAME.test(x));
1043
+ const worth = verdict === 'crashed' || (verdict === 'kept' && pr.mode !== 'number' && !(pr.mode === 'null' && !required)) || (verdict === 'changed' && /^(object|array|string)$/.test(pr.mode));
1044
+ if (!worth) continue;
1045
+ const claimed = (pr.mode === 'blank' && f.kinds.some((k) => /^(required|length)$/.test(k))) || (wrongKind && f.kinds.includes('type'));
1046
+ const longText = pr.mode === 'type' && /characters long/.test(pr.why);
1047
+ const finding = {
1048
+ kind: verdict === 'crashed' ? 'invalid-crash' : 'invalid-kept', where: what, field: f.name, from: f.file + ':' + f.line,
1049
+ status: verdict === 'crashed' || claimed ? 'confirmed' : 'verification required',
1050
+ severity: verdict === 'crashed' || longText ? 'LOW' : 'MEDIUM',
1051
+ headline: verdict === 'crashed'
1052
+ ? what + ' crashed (' + res.status + ') when ' + f.name + ' was ' + pr.why + (f.status === map.STATUS.NOT ? ': nothing in the code checks that field.' : '.')
1053
+ : verdict === 'kept'
1054
+ ? what + ' took ' + f.name + ' ' + pr.why + ' and saved it' + (claimed ? ', though the code checks that field.' : ': nothing in the code stops it.')
1055
+ : what + ' took ' + f.name + ' ' + pr.why + ' and stored "' + authz.clip(JSON.stringify(now), 60) + '" in its place: nothing in the code stops it.',
1056
+ proof: ['As ' + who.email + ', read ' + (singleton ? 'GET ' + a.template : 'the record ' + (row.id !== undefined ? row.id : '') + ' (GET ' + (detail ? a.template : listTemplate) + ')') + ': ' + f.name + ' = ' + authz.clip(JSON.stringify(orig), 60) + '.',
1057
+ 'Sent ' + a.method + ' ' + target + ' with the record\'s own values and only ' + f.name + ' ' + pr.why + ': answered ' + res.status + ' (' + reply.note + ').']
1058
+ .concat(verdict === 'crashed' ? [] : ['Read it again: ' + f.name + ' = ' + authz.clip(JSON.stringify(now), 60) + '.'])
1059
+ .concat(putBack === null ? [] : [putBack ? 'Then sent the original values back: ' + f.name + ' is "' + authz.clip(String(orig), 60) + '" again.' : 'Putting the original values back did NOT work - look at it.']),
1060
+ };
1061
+ finding.fixPrompt = ['Kryptheon sent a bad value to a change route of my running app and it was not turned away:', '', finding.headline, '', 'Where: ' + finding.from, '',
1062
+ 'First check this is real' + (finding.status === 'confirmed' ? '' : ' (the app may mean to accept it)') + '. If it is, check ' + f.name + ' where the handler reads it: reject a value that is not what the field should hold (blank, negative, the wrong type - an object, an array, a number for a text), on the server, with a 400 and a message, before anything is stored or used (a call such as .trim() on it). Change nothing else.', '', 'Afterwards I will run: npx kryptheon api'].join(String.fromCharCode(10));
1063
+ out.findings.push(finding);
1064
+ }
1065
+ say(' ' + what + ': ' + probes.length + ' bad values tried');
1066
+ return null;
1067
+ }
1068
+
817
1069
  const GHOST_ID = '999999999';
818
1070
 
819
1071
 
@@ -1249,7 +1501,8 @@ async function runCreates(root, options) {
1249
1501
  // --- a limit used up -------------------------------------------------------------
1250
1502
 
1251
1503
  // A record's limit and the count that runs up to it: capacity and sold, max_uses and uses.
1252
- const LIMIT_FIELD = /^(capacity|max_\w+|limit|quota|stock|seats_total|total_seats)$/i;
1504
+ // use_limit, seat_limit, limit_total: a limit too (round 32: a one-use credit, use_limit 1, redeemed twice at once)
1505
+ const LIMIT_FIELD = /^(capacity|max_\w+|limit|\w+_limit|limit_\w+|quota|stock|seats_total|total_seats)$/i;
1253
1506
  const COUNTER_FIELD = /^(sold|uses|used|used_count|booked|taken|reserved|issued|redeemed|count)$/i;
1254
1507
  // a field of the create that says how many: what a capacity is used up by
1255
1508
  const AMOUNT_FIELD = /^(quantity|qty|seats|tickets|units|count)$/i;
@@ -1269,6 +1522,86 @@ function limitPairOf(rec) {
1269
1522
  return null;
1270
1523
  }
1271
1524
 
1525
+ const REFUND_STEP = /refund/i;
1526
+ const PAID_STATE = /captur|paid|succeed|settled|complete/i;
1527
+ const MONEY_FIELD = /amount|cents|paise|total|price/i;
1528
+
1529
+ /**
1530
+ * A refund larger than what was paid: for a route that refunds one record (POST /payments/{id}/refunds), a record
1531
+ * whose list shows it paid, a control refund of 1 first (taken, the route works for this account and record), then
1532
+ * one of twice what was paid plus one. Taken = the finding; refused = held. Blind round 32 (Northstar): a refund
1533
+ * of 999999 was stored against a payment of 12500. { ran, why, tried, findings, left }.
1534
+ */
1535
+ async function runRefundCap(root, options) {
1536
+ const opts = options || {};
1537
+ const plan = planOf(root, opts);
1538
+ if (!plan.ran) return { ran: false, why: plan.why };
1539
+ const j = plan.journey;
1540
+ const steps = j.actions.filter((a) => a.method === 'POST' && a.params.length === 1 && REFUND_STEP.test(a.template) &&
1541
+ Object.keys(a.fullData || {}).some((k) => MONEY_FIELD.test(k)));
1542
+ if (!steps.length) return { ran: false, why: 'no route that refunds a record by an amount' };
1543
+ let origin;
1544
+ try {
1545
+ origin = authz.assertLocalTarget(opts.origin);
1546
+ } catch (err) {
1547
+ return { ran: false, why: 'the app must be on this computer (localhost or 127.0.0.1): ' + (opts.origin || 'no address was given') };
1548
+ }
1549
+ if (!j.signIn) return { ran: false, why: j.signInWhy };
1550
+ const ctx = authz.makeContext({ baseUrl: origin, allowOrigins: [origin], fetch: opts.fetch, passwords: Object.fromEntries(j.accounts.usable.map((a) => [a.email, a.password])), maxRequests: 40 + steps.length * j.accounts.usable.length * 6 });
1551
+ const out = { ran: true, tried: [], findings: [], left: [] };
1552
+ const sessions = [];
1553
+ for (const a of j.accounts.usable) {
1554
+ const s = authz.makeSession(ctx, 'account', a.email, a.password);
1555
+ const f = { [j.signIn.emailField]: a.email, [j.signIn.passwordField]: a.password };
1556
+ const r = authz.replyOf(await s.send(j.signIn.method, j.signIn.path, j.signIn.encoding === 'form' ? { form: f } : { json: f }));
1557
+ if (r.kind !== 'refused' && r.kind !== 'server-error' && r.kind !== 'no-answer') sessions.push(s);
1558
+ }
1559
+ if (!sessions.length) return { ran: false, why: 'no account with a password in .env could sign in' };
1560
+ for (const a of steps) {
1561
+ const what = a.method + ' ' + a.template;
1562
+ const field = Object.keys(a.fullData).find((k) => MONEY_FIELD.test(k));
1563
+ const listTemplate = '/' + a.segs.slice(0, a.params[0].index).join('/');
1564
+ let verdict = null;
1565
+ for (const s of sessions) {
1566
+ if (verdict) break;
1567
+ const list = await s.send('GET', listTemplate, {});
1568
+ const rows = rowsOfReply(list) || [];
1569
+ const paid = rows.filter((r) => r && r.id !== undefined && Object.keys(r).some((k) => /^(status|state)$/i.test(k) && PAID_STATE.test(String(r[k]))));
1570
+ for (const rec of paid.slice(0, 3)) {
1571
+ const amountKey = Object.keys(rec).find((k) => MONEY_FIELD.test(k) && Number(rec[k]) > 0);
1572
+ if (!amountKey) continue;
1573
+ const at = a.template.replace('{id}', encodeURIComponent(String(rec.id)));
1574
+ const send = (n) => s.send('POST', at, a.encoding === 'form' ? { form: Object.assign({}, a.fullData, { [field]: n }) } : { json: Object.assign({}, a.fullData, { [field]: n }) });
1575
+ const control = await send(1);
1576
+ const rc = authz.replyOf(control);
1577
+ if (rc.kind !== 'accepted' && rc.kind !== 'redirect') continue;
1578
+ const over = Number(rec[amountKey]) * 2 + 1;
1579
+ const big = await send(over);
1580
+ const rb = authz.replyOf(big);
1581
+ const took = rb.kind === 'accepted' || rb.kind === 'redirect';
1582
+ out.left.push(what.replace('{id}', rec.id) + ' as ' + s.email + ': a refund of 1 (answered ' + control.status + ')' + (took ? ' and one of ' + over + ' (answered ' + big.status + ')' : '') + ' - Kryptheon never deletes them');
1583
+ verdict = { what, by: s.email, record: rec.id, paid: Number(rec[amountKey]), over, status: big.status, took };
1584
+ out.tried.push(verdict);
1585
+ if (took) {
1586
+ const echoed = JSON.stringify(big.json || '').indexOf(String(over)) !== -1;
1587
+ const finding = {
1588
+ kind: 'refund-over', where: what, from: a.from ? a.from.file + ':' + a.from.line : what, status: echoed ? 'confirmed' : 'verification required', severity: 'HIGH',
1589
+ headline: what + ' took a refund of ' + over + ' for ' + listTemplate.split('/').pop().replace(/s$/, '') + ' ' + rec.id + ', which was paid ' + rec[amountKey] + ': nothing caps a refund at what was paid',
1590
+ proof: ['As ' + s.email + ', a refund of 1 for record ' + rec.id + ' was taken (' + control.status + '): the route works for this account.',
1591
+ 'Then a refund of ' + over + ' (twice the ' + rec[amountKey] + ' paid, plus 1): answered ' + big.status + (echoed ? ', and the answer names that amount.' : '.')],
1592
+ };
1593
+ finding.fixPrompt = ['Kryptheon tried this on my running app:', '', finding.headline + '.', '', 'Where: ' + finding.from, '', finding.proof.join('\n'), '',
1594
+ 'Before saving a refund, add up what has already been refunded for this payment and refuse (409) any refund that would take the total above what was paid. Do it in one transaction (or one statement), so two refunds sent at once cannot both pass the check.'].join('\n');
1595
+ out.findings.push(finding);
1596
+ }
1597
+ break;
1598
+ }
1599
+ }
1600
+ if (!verdict) out.tried.push({ what, verdict: 'not tried', why: 'no paid record that a refund of 1 was taken for' });
1601
+ }
1602
+ return out;
1603
+ }
1604
+
1272
1605
  /**
1273
1606
  * Each create of a costly record (POST /api/orders) whose body names a record that carries a limit and its count
1274
1607
  * (ticket_type_id: capacity and sold; promo_code: max_uses and uses), found through the route named for it
@@ -1430,8 +1763,14 @@ async function runSteps(root, options) {
1430
1763
  }
1431
1764
  return o;
1432
1765
  };
1766
+ // What one step costs: a list read per account, two sends and every list read twice. Sized to the app
1767
+ // (blind round 32: 7 steps x 8 accounts x 25 lists went past a fixed 200), never past 3000.
1768
+ ctx.limit = Math.min(3000, Math.max(ctx.limit, ctx.requests + 20 + items.length * (sessions.length + 3 + 2 * lists.length)));
1433
1769
  const out = { ran: true, tried: [], findings: [], left: [] };
1434
- for (const item of items) {
1770
+ let i = 0;
1771
+ try {
1772
+ for (; i < items.length; i++) {
1773
+ const item = items[i];
1435
1774
  const what = normRoute(item.route);
1436
1775
  const a = j.actions.find((x) => x.method + ' ' + x.template === what);
1437
1776
  if (!a || a.params.length !== 1) { out.tried.push({ what, verdict: 'not tried', why: 'it is not a step on one record (/things/{id}/step)' }); continue; }
@@ -1477,6 +1816,472 @@ async function runSteps(root, options) {
1477
1816
  'First check this is real. If it is, make the step look at the record\'s status first: change it only from the status it must come from (UPDATE ... WHERE id = $1 AND status = <the one before>, and do the rest only when a row was changed), and answer 409 otherwise. A second call must change nothing. Change nothing else.', '', 'Afterwards I will run: npx kryptheon api'].join(String.fromCharCode(10));
1478
1817
  out.findings.push(finding);
1479
1818
  }
1819
+ } catch (err) {
1820
+ if (!authz.isRequestLimit(err)) throw err;
1821
+ // keep what was found; say which steps were never sent twice
1822
+ for (const item of items.slice(i)) out.tried.push({ what: normRoute(item.route), verdict: 'not tried', why: 'the request limit (' + err.limit + ') was reached first' });
1823
+ out.stoppedAt = err.limit;
1824
+ }
1825
+ return out;
1826
+ }
1827
+
1828
+ // --- one step on one record, sent twice at the same moment ------------------------
1829
+
1830
+ // A record that has already come to the end of its life: the step is not aimed at it while another record is open.
1831
+ const ENDED_STATUS = /^(done|closed|cancel+ed|paid|complete[d]?|executed|received|shipped|delivered|refunded|void(ed)?|expired|redeemed|used|failed|rejected|archived|settled|finished)$/i;
1832
+ // A field that tells one row from another, or when it was made: never part of "the same row twice".
1833
+ const ROW_IDENTITY = /^(id|uuid)$|(^|_)(at|on|date|time|timestamp|created|updated|hash|token|seq|serial|ref|reference|number|key|version|etag)$|(At|On|Date|Time|Ref|Number|Token|Hash|Key)$/;
1834
+ // a number in `col = col - $1`, `col = GREATEST(col - $1, 0)`, or { decrement: n }
1835
+ const ARITHMETIC = [
1836
+ /\b([a-z_]\w*)\s*=\s*(?:\w+\.)?\1\s*([-+])\s*[\w$:(]/i,
1837
+ /\b([a-z_]\w*)\s*=\s*\w+\(\s*(?:\w+\.)?\1\s*([-+])\s*[\w$:(]/i,
1838
+ ];
1839
+
1840
+ /** The records of a read's answer (an array, or the first array in an object), or null. */
1841
+ function rowsOfReply(res) {
1842
+ if (res.status !== 200) return null;
1843
+ return Array.isArray(res.json) ? res.json : res.json && typeof res.json === 'object' ? Object.values(res.json).find(Array.isArray) || null : null;
1844
+ }
1845
+
1846
+ /** The code of a write the map found, from its first line to the end of the call (or twelve lines). */
1847
+ function sourceAround(root, d) {
1848
+ try {
1849
+ const lines = fs.readFileSync(path.join(root, d.file), 'utf8').split('\n').slice(Math.max(0, d.line - 1), d.line + 11).join('\n');
1850
+ const end = lines.search(/\)\s*;/);
1851
+ return end < 0 ? lines : lines.slice(0, end + 2);
1852
+ } catch (err) {
1853
+ return d.sql || '';
1854
+ }
1855
+ }
1856
+
1857
+ /**
1858
+ * What a route's writes do, read from the map: rows it adds, numbers it moves (a stock taken down, a count
1859
+ * put up) and a status it sets. null when it does none of these (a rename is not a step to send twice).
1860
+ */
1861
+ function stepEffectOf(root, route) {
1862
+ const eff = { adds: [], moves: [], sets: [] };
1863
+ for (const d of route.db || []) {
1864
+ if (d.op === 'INSERT' || d.op === 'UPSERT') eff.adds.push(d.table);
1865
+ if (d.op !== 'UPDATE' && d.op !== 'UPSERT') continue;
1866
+ const src = sourceAround(root, d);
1867
+ for (const re of ARITHMETIC) {
1868
+ const m = re.exec(src);
1869
+ if (m) eff.moves.push({ col: m[1].toLowerCase(), sign: m[2] });
1870
+ }
1871
+ const inc = /\b(increment|decrement)\s*:/.exec(src);
1872
+ if (inc) eff.moves.push({ col: null, sign: inc[1] === 'decrement' ? '-' : '+' });
1873
+ if (d.setsState) eff.sets.push(d.setsState.col + " = '" + d.setsState.value + "'");
1874
+ else if (/\b(status|state|stage)\b\s*[:=]/i.test(src)) eff.sets.push('a status');
1875
+ }
1876
+ return eff.adds.length || eff.moves.length || eff.sets.length ? eff : null;
1877
+ }
1878
+
1879
+ /** Every number in a body or record worth counting by: its own amounts, and those of lines inside it. */
1880
+ function amountsIn(value, skipKey) {
1881
+ const out = new Set();
1882
+ const take = (v) => { const n = typeof v === 'string' && /^-?\d+(\.\d+)?$/.test(v) ? Number(v) : v; if (typeof n === 'number' && Number.isFinite(n) && n > 0) out.add(n); };
1883
+ const walk = (o, depth) => {
1884
+ if (!o || typeof o !== 'object' || depth > 2) return;
1885
+ for (const k of Object.keys(o)) {
1886
+ const v = o[k];
1887
+ if (Array.isArray(v)) v.forEach((x) => walk(x, depth + 1));
1888
+ else if (v && typeof v === 'object') walk(v, depth + 1);
1889
+ else if (!ROW_IDENTITY.test(k) && !/(^|_)id$|Id$/.test(k) && k.toLowerCase() !== String(skipKey).toLowerCase()) take(v);
1890
+ }
1891
+ };
1892
+ walk(value, 0);
1893
+ return out;
1894
+ }
1895
+
1896
+ /** The numbers of the rows in some lists that belong to a record (its own row, or rows pointing at it): path -> { key, value }. */
1897
+ function numbersOfRecord(snap, listTemplate, rec) {
1898
+ const out = new Map();
1899
+ const pointsAt = (row, list) => Object.keys(row).some((k) => k !== 'id' && pointerKey(k, listTemplate) && String(row[k]) === String(rec.id));
1900
+ for (const l of Object.keys(snap)) {
1901
+ const json = snap[l];
1902
+ const wrapper = Array.isArray(json) ? '' : json && typeof json === 'object' ? Object.keys(json).find((k) => Array.isArray(json[k])) : undefined;
1903
+ const rows = Array.isArray(json) ? json : wrapper ? json[wrapper] : [];
1904
+ for (const row of rows) {
1905
+ if (!row || typeof row !== 'object' || row.id === undefined) continue;
1906
+ if (!((l === listTemplate && String(row.id) === String(rec.id)) || pointsAt(row, l))) continue;
1907
+ for (const k of Object.keys(row)) if (typeof row[k] === 'number') out.set(l + (wrapper ? '.' + wrapper : '') + '[id ' + row.id + '].' + k, { key: k, value: row[k] });
1908
+ }
1909
+ }
1910
+ return out;
1911
+ }
1912
+
1913
+ /** Whether a field of a row says "this row belongs to that record" (payout_id, payoutId, payout) for the list the step's record lives in. */
1914
+ function pointerKey(key, listTemplate) {
1915
+ const flat = (w) => String(w).toLowerCase().replace(/[^a-z]/g, '');
1916
+ const stem = flat((listTemplate.split('/').pop() || '').replace(/ies$/, 'y').replace(/s$/, ''));
1917
+ return /(^|_)id$|[a-z]Id$|(^|_)ref$/.test(key) || (stem && flat(key) === stem);
1918
+ }
1919
+
1920
+ /**
1921
+ * What two copies of one step did that one copy would not have: rows made twice, a number that was not
1922
+ * negative and is, a count past its limit, a number of the record moved by twice what one step moves.
1923
+ * [{ kind, sure, text }] - empty when the lists show one effect, or none.
1924
+ */
1925
+ function doubleEffects(before, after, rec, listTemplate, body, stepTemplate) {
1926
+ const out = [];
1927
+ // A step that adds to a list of the record (/assets/{id}/reservations, /orders/{id}/items) makes a new
1928
+ // thing each time it is sent: two of them are two reservations, not one done twice. Only a number pushed
1929
+ // below zero or past its limit is a double effect there. (Harness, rounds 22: two reservations of 1 took
1930
+ // a stock of 2 to 0 and were called a double effect.)
1931
+ const last = stepTemplate ? String(stepTemplate).split('/').filter((s) => s && s !== '{id}').pop() || '' : '';
1932
+ const addsToList = /s$/i.test(last) && !STEP_VERB.test(last);
1933
+ // 1. two new rows tied to the record that say the same thing (two transfers of one payout) - not rows of
1934
+ // a log: each request writes its own audit or activity line (harness, rounds 27-28)
1935
+ for (const l of addsToList ? [] : Object.keys(after)) {
1936
+ if (HISTORY_LIST.test(String(l).split('/').filter(Boolean).pop() || '')) continue;
1937
+ // the log as one array of a summary address (GET /dashboard -> { metrics, activity: [...] }) - round 33
1938
+ const aj = after[l];
1939
+ const compared = rowsOfReply({ status: 200, json: aj });
1940
+ const arrayKey = aj && typeof aj === 'object' && !Array.isArray(aj) ? Object.keys(aj).find((k) => aj[k] === compared) : undefined;
1941
+ if (arrayKey && wordsOf(arrayKey).some((w) => HISTORY_LIST.test(w))) continue;
1942
+ const had = new Set((rowsOfReply({ status: 200, json: before[l] }) || []).map((x) => x && JSON.stringify(x.id)));
1943
+ const fresh = (rowsOfReply({ status: 200, json: after[l] }) || []).filter((x) => x && typeof x === 'object' && x.id !== undefined && !had.has(JSON.stringify(x.id)));
1944
+ const tied = fresh.filter((x) => Object.keys(x).some((k) => k !== 'id' && pointerKey(k, listTemplate) && String(x[k]) === String(rec.id)));
1945
+ if (tied.length < 2) continue;
1946
+ const keys = Object.keys(tied[0]).filter((k) => !ROW_IDENTITY.test(k));
1947
+ if (!tied.every((x) => keys.every((k) => JSON.stringify(x[k]) === JSON.stringify(tied[0][k])) && Object.keys(x).filter((k) => !ROW_IDENTITY.test(k)).length === keys.length)) continue;
1948
+ out.push({ kind: 'rows', sure: true, text: 'GET ' + l + ' gained ' + tied.length + ' rows with the same ' + (keys.slice(0, 4).join(', ') || 'fields') + ', each tied to record ' + rec.id + ' (' + tied.map((x) => x.id).join(', ') + ')' });
1949
+ }
1950
+ // 2. a number that was held and went below zero (a new row's own negative change is the step written down, not a balance)
1951
+ const was = numbersOf(before);
1952
+ for (const [at, v] of numbersOf(after)) {
1953
+ if (!(was.has(at) && was.get(at) >= 0 && v < 0)) continue;
1954
+ if (String(at).split('.').pop().split('_').some((w) => CHANGE_WORDS.has(w.toLowerCase()))) continue;
1955
+ out.push({ kind: 'below-zero', sure: true, text: at + ' went ' + was.get(at) + ' -> ' + v + ', below zero' });
1956
+ }
1957
+ out.push(...pastLimit(before, after));
1958
+ // 3. a number of the record, or of a row pointing at it, moved by twice what one step moves
1959
+ const b = numbersOfRecord(before, listTemplate, rec);
1960
+ for (const [at, now] of addsToList ? [] : numbersOfRecord(after, listTemplate, rec)) {
1961
+ if (!b.has(at) || ROW_IDENTITY.test(now.key)) continue;
1962
+ const d = Math.abs(now.value - b.get(at).value);
1963
+ if (!d) continue;
1964
+ const units = new Set([...amountsIn(body, now.key), ...amountsIn(rec, now.key)]);
1965
+ // the unit has to be the record's own (a payout's amount paid twice): a unit that only the request names is
1966
+ // the change it asked for - two stock adjustments of +1 are +2 (round 32: PATCH /catalog/{id}/stock {amount})
1967
+ const recUnits = amountsIn(rec, now.key);
1968
+ const bodyUnits = amountsIn(body, now.key);
1969
+ const asked = (u) => bodyUnits.has(u) && !recUnits.has(u);
1970
+ if ([...units].some((u) => Math.abs(2 * u - d) < 1e-9 && !asked(u)) && !units.has(d)) {
1971
+ out.push({ kind: 'moved-twice', sure: true, text: at + ' went ' + b.get(at).value + ' -> ' + now.value + ': twice the ' + (d / 2) + ' one step moves it by' });
1972
+ } else if (d === 2 && COUNTER_FIELD.test(now.key) && !units.has(2)) {
1973
+ out.push({ kind: 'moved-twice', sure: false, text: at + ' went ' + b.get(at).value + ' -> ' + now.value + ': a count that goes up by one went up by two' });
1974
+ }
1975
+ }
1976
+ return out;
1977
+ }
1978
+
1979
+ // Lists that write a line per request (two lines for two requests is the log doing its job).
1980
+ // A word of an address or a body field about who may do what (round 33): such a change is the roles check's to try.
1981
+ const ACCESS_WORD = /^(role|roles|permission|permissions|privilege|privileges|grant|revoke|promote|demote|elevate|password|passwd|passcode|passphrase|pin|secret|token|otp|totp|mfa|2fa|apikey|credential|credentials)$/;
1982
+ const HISTORY_LIST = /^(audit|audits|audit[-_]?logs?|logs?|activity|activities|activity[-_]?logs?|history|histories|timeline|notifications?|events[-_]?log)$/i;
1983
+ // A last path word that is a step done to the record, though it ends in "s" (process, access, address, pass, status).
1984
+ const STEP_VERB = /^(process|access|address|pass|bypass|status|progress|dismiss|redress|reassess|assess)$/i;
1985
+
1986
+ /** Counts that stand above their limit after and did not before: [{ kind, sure, text }]. */
1987
+ function pastLimit(before, after) {
1988
+ const out = [];
1989
+ const rowsAt = (snap, l) => rowsOfReply({ status: 200, json: snap[l] }) || [];
1990
+ for (const l of Object.keys(after)) {
1991
+ for (const row of rowsAt(after, l)) {
1992
+ if (!row || typeof row !== 'object') continue;
1993
+ const keys = Object.keys(row);
1994
+ for (const lim of keys.filter((k) => LIMIT_FIELD.test(k))) {
1995
+ for (const c of keys.filter((k) => COUNTER_FIELD.test(k))) {
1996
+ const L = Number(row[lim]);
1997
+ const C = Number(row[c]);
1998
+ if (!(Number.isFinite(L) && Number.isFinite(C) && L > 0 && C > L)) continue;
1999
+ const old = row.id === undefined ? null : rowsAt(before, l).find((x) => x && JSON.stringify(x.id) === JSON.stringify(row.id));
2000
+ if (old && Number(old[c]) > Number(old[lim])) continue;
2001
+ out.push({ kind: 'past-limit', sure: true, text: 'GET ' + l + (row.id !== undefined ? ' record ' + row.id : '') + ': ' + c + ' ' + (old ? old[c] + ' -> ' : '') + C + ' of ' + lim + ' ' + L + ', past its limit' });
2002
+ }
2003
+ }
2004
+ }
2005
+ }
2006
+ return out;
2007
+ }
2008
+
2009
+ const CLAIM_QTY = /^(quantity|qty|units|seats|count|amount_units)$/i;
2010
+
2011
+ /**
2012
+ * Two new records that both claim the thing with the fewest units left, for a step that takes a number down:
2013
+ * the column the step decrements is found in another list (stock_on_hand in /api/catalog), its smallest positive
2014
+ * value picked, and the create route of the step's own records (POST /api/orders) sent twice with the field that
2015
+ * names a row of that list set to it, and a quantity field (if any) set to all the units left. Each fits alone;
2016
+ * both together do not. { ids, label } or null when there is no such create, list or field.
2017
+ */
2018
+ async function scarceClaim(s, c, j, lists, snap, taken) {
2019
+ const cols = c.effect.moves.filter((m) => m.sign === '-' && m.col).map((m) => m.col);
2020
+ if (!cols.length) return null;
2021
+ const create = j.actions.find((x) => x.method === 'POST' && x.template === c.listTemplate && !x.params.length);
2022
+ if (!create || !create.fullData) return null;
2023
+ const now = await snap(s);
2024
+ for (const l of lists) {
2025
+ if (l === c.listTemplate) continue;
2026
+ const rows = (rowsOfReply({ status: 200, json: now[l] }) || []).filter((r) => r && r.id !== undefined);
2027
+ if (!rows.length) continue;
2028
+ const col = cols.find((k) => rows[0][k] !== undefined);
2029
+ if (!col) continue;
2030
+ const ids = new Set(rows.map((r) => String(r.id)));
2031
+ // The fields of the create body, and of its first line when it carries a list of lines (blind round 33: items:
2032
+ // [{ productId, quantity }]): [{ path, key, value }]
2033
+ const leaves = [];
2034
+ for (const k of Object.keys(create.fullData)) {
2035
+ const v = create.fullData[k];
2036
+ if (Array.isArray(v) && v[0] && typeof v[0] === 'object' && !Array.isArray(v[0])) for (const k2 of Object.keys(v[0])) leaves.push({ path: [k, 0, k2], key: k2, value: v[0][k2] });
2037
+ else if (v === null || typeof v !== 'object') leaves.push({ path: [k], key: k, value: v });
2038
+ }
2039
+ const flat = (k) => String(k).replace(/_/g, '').toLowerCase();
2040
+ const isPointer = (k) => /(^|_)id$|Id$/.test(k);
2041
+ const scarce = rows.filter((r) => Number(r[col]) > 0).sort((x, y) => Number(x[col]) - Number(y[col]))[0];
2042
+ if (!scarce) continue;
2043
+ // What each pointer of the body is set to. Named like a field of the row (productId -> product_id, warehouseId ->
2044
+ // warehouse_id: the stock found by product and place), that field's value - every such field, so the new record
2045
+ // claims exactly that row. Only when none is named so, the one pointer whose value is a row's own id (round 32:
2046
+ // productId = a catalog row's id). A value that merely equals some row's id is not taken as naming it.
2047
+ const sets = [];
2048
+ for (const l2 of leaves) {
2049
+ if (!isPointer(l2.key)) continue;
2050
+ const rk = Object.keys(scarce).find((x) => x !== 'id' && flat(x) === flat(l2.key));
2051
+ if (rk && scarce[rk] !== null && scarce[rk] !== undefined) sets.push({ path: l2.path, value: scarce[rk] });
2052
+ }
2053
+ if (!sets.length) {
2054
+ const byId = leaves.find((l2) => isPointer(l2.key) && ids.has(String(l2.value)));
2055
+ if (byId) sets.push({ path: byId.path, value: scarce.id });
2056
+ }
2057
+ if (!sets.length) continue;
2058
+ const units = Number(scarce[col]);
2059
+ const qty = leaves.find((l2) => CLAIM_QTY.test(l2.key));
2060
+ if (!qty && units > 1) continue; // one unit per record and more than one left: two records cannot overdraw it
2061
+ if (qty) sets.push({ path: qty.path, value: units });
2062
+ // one line only, the one claiming the scarce row
2063
+ const body = JSON.parse(JSON.stringify(create.fullData));
2064
+ for (const k of Object.keys(body)) if (Array.isArray(body[k]) && body[k].length > 1 && sets.some((x) => x.path[0] === k && x.path.length === 3)) body[k] = body[k].slice(0, 1);
2065
+ for (const x of sets) {
2066
+ let o = body;
2067
+ for (const p of x.path.slice(0, -1)) o = o[p];
2068
+ o[x.path[x.path.length - 1]] = x.value;
2069
+ }
2070
+ const made = [];
2071
+ for (let n = 0; n < 2; n++) {
2072
+ const before = rowsOfReply(await s.send('GET', c.listTemplate, {})) || [];
2073
+ const res = await s.send('POST', c.listTemplate, create.encoding === 'form' ? { form: body } : { json: body });
2074
+ if (!taken(res)) return null;
2075
+ const after = rowsOfReply(await s.send('GET', c.listTemplate, {})) || [];
2076
+ const had = new Set(before.map((r) => r && String(r.id)));
2077
+ const fresh = after.find((r) => r && r.id !== undefined && !had.has(String(r.id)));
2078
+ const id = fresh ? fresh.id : res.json && (res.json.id !== undefined ? res.json.id : Object.values(res.json).find((v) => v && typeof v === 'object' && v.id !== undefined) && Object.values(res.json).find((v) => v && typeof v === 'object' && v.id !== undefined).id);
2079
+ if (id === undefined || id === null) return null;
2080
+ made.push(id);
2081
+ }
2082
+ return { ids: made, label: l.split('/').pop() + ' ' + scarce.id + ' (' + col + ' ' + units + ')' };
2083
+ }
2084
+ return null;
2085
+ }
2086
+
2087
+ /**
2088
+ * The map's steps on one record (POST|PUT|PATCH /things/{id}/verb that change a status, money or a count),
2089
+ * sent twice at the same moment for ONE record, as a double click or two tabs do; and, where the step takes a
2090
+ * number down, for TWO records that compete for the same stock. Judged from the app's own lists before and
2091
+ * after: both answers taken AND the effect twice (two new rows tied to the record, a number moved by twice what
2092
+ * one step moves, a number below zero, a count past its limit) = a finding. One taken and one refused, or both
2093
+ * taken with one effect, = held. Nothing is deleted; every record sent is listed as left. Round 33.
2094
+ * { ran, why, tried, findings, left, stoppedAt }.
2095
+ */
2096
+ async function runStepsAtOnce(root, options) {
2097
+ const opts = options || {};
2098
+ const plan = planOf(root, opts);
2099
+ if (!plan.ran) return { ran: false, why: plan.why };
2100
+ const j = plan.journey;
2101
+ const lists = j.actions.filter((x) => x.method === 'GET' && !x.params.length).map((x) => x.template);
2102
+ const candidates = [];
2103
+ // /orders/{id}/confirm is a step on a record, though a bare "confirm" address is sign-up plumbing for the other checks
2104
+ const known = allowedByName(plan.map);
2105
+ const confirms = (plan.map.routes || []).filter((r) => !r.unmounted && r.path && /^(POST|PUT|PATCH)$/.test(r.method) && /\/:\w+\*?\/confirm$/i.test(r.path) && !SIGN_IN.test(r.path) && !SIGN_OUT.test(r.path))
2106
+ .map((r) => actionOf(r, known, null)).filter((a) => !a.leftOut.length && !j.actions.some((x) => x.method === a.method && x.template === a.template));
2107
+ // A change of who may do what - a role, a permission, a password, a key - is never sent twice at once here: the roles
2108
+ // check tries it, as the account it is about, and puts it back. Not blind, round 33: PATCH /team/1/role {"role":"support"}
2109
+ // was sent twice at the signed-in owner's own record, demoting it, and every check after it ran as support.
2110
+ const setAside = [];
2111
+ for (const a of j.actions.concat(confirms)) {
2112
+ if (!/^(POST|PUT|PATCH)$/.test(a.method) || a.params.length !== 1 || a.segs.length !== a.params[0].index + 2 || JOB_PATH.test(a.template)) continue;
2113
+ const words = [].concat(...a.segs.filter((s) => s !== '{id}').map((s) => wordsOf(s)), ...Object.keys(a.fullData || {}).map((k) => wordsOf(k)));
2114
+ if (words.some((w) => ACCESS_WORD.test(w))) {
2115
+ setAside.push({ what: a.method + ' ' + a.template, verdict: 'not sent: it changes who may do what (a role, a permission or a credential) - the roles check tries that, and puts it back' });
2116
+ continue;
2117
+ }
2118
+ const route = (plan.map.routes || []).find((r) => !r.unmounted && r.method === a.method && normRoute(r.path || '') === a.template);
2119
+ const effect = route && stepEffectOf(root, route);
2120
+ const listTemplate = '/' + a.segs.slice(0, a.params[0].index).join('/');
2121
+ if (effect && lists.includes(listTemplate)) candidates.push({ a, effect, listTemplate, route });
2122
+ }
2123
+ if (!candidates.length) return { ran: false, why: 'the map found no step on one record (POST /things/{id}/verb) that changes a status, money or a count, with a list of those records to aim it at' };
2124
+ let origin;
2125
+ try {
2126
+ origin = authz.assertLocalTarget(opts.origin);
2127
+ } catch (err) {
2128
+ return { ran: false, why: 'the app must be on this computer (localhost or 127.0.0.1): ' + (opts.origin || 'no address was given') };
2129
+ }
2130
+ if (!j.signIn) return { ran: false, why: j.signInWhy };
2131
+ const ctx = authz.makeContext({ baseUrl: origin, allowOrigins: [origin], fetch: opts.fetch, passwords: Object.fromEntries(j.accounts.usable.map((a) => [a.email, a.password])), maxRequests: opts.maxRequests || 200 });
2132
+ const sessions = [];
2133
+ for (const a of j.accounts.usable) {
2134
+ const s = authz.makeSession(ctx, 'account', a.email, a.password);
2135
+ const f = { [j.signIn.emailField]: a.email, [j.signIn.passwordField]: a.password };
2136
+ const r = authz.replyOf(await s.send(j.signIn.method, j.signIn.path, j.signIn.encoding === 'form' ? { form: f } : { json: f }));
2137
+ if (r.kind !== 'refused' && r.kind !== 'server-error' && r.kind !== 'no-answer') sessions.push(s);
2138
+ }
2139
+ if (!sessions.length) return { ran: false, why: 'no account with a password in .env could sign in' };
2140
+ const snap = async (s) => {
2141
+ const o = {};
2142
+ for (const t of lists) {
2143
+ const r = await s.send('GET', t, {});
2144
+ if (r.status === 200 && r.json !== undefined) o[t] = r.json;
2145
+ }
2146
+ return o;
2147
+ };
2148
+ const taken = (res) => { const r = authz.replyOf(res); return r.kind === 'accepted' || r.kind === 'redirect'; };
2149
+ // What one step costs here: per account a list and a read of every list, two sends and another read; and the same
2150
+ // again for two records. Sized to the app, never past 3000 (the limit of the whole run is kept: ctx.limit only goes up).
2151
+ if (opts.maxRequests === undefined) ctx.limit = Math.min(3000, Math.max(ctx.limit, ctx.requests + 20 + candidates.length * (sessions.length * (lists.length + 3) + 2 * (2 * lists.length + 4))));
2152
+ const out = { ran: true, tried: setAside.slice(), findings: [], left: [] };
2153
+ const headFor = (c) => c.a.method + ' ' + c.a.template;
2154
+ const finish = (c, what, s, atText, answers, effects, mode, body) => {
2155
+ const sure = effects.some((e) => e.sure);
2156
+ const finding = {
2157
+ kind: mode === 'compete' ? 'step-compete' : 'step-at-once', where: what, from: c.a.from.file + ':' + c.a.from.line, status: sure ? 'confirmed' : 'verification required', severity: 'HIGH',
2158
+ headline: mode === 'compete'
2159
+ ? 'Sent for two records at the same moment, ' + what + ' was taken for both, and ' + effects[0].text + ': nothing stops two steps from taking the same last unit.'
2160
+ : 'Sent twice at the same moment for one record, ' + what + ' was taken both times and its effect happened twice: ' + effects[0].text + '.',
2161
+ proof: ['As ' + s.email + ', read ' + (mode === 'compete' ? 'the lists' : 'GET ' + c.listTemplate + ' and the other lists') + ' before.',
2162
+ 'Sent ' + atText + ' at the same moment with the same body' + (body && Object.keys(body).length ? ': ' + authz.clip(JSON.stringify(body), 100) : '') + ': answered ' + answers.join(' and ') + '.']
2163
+ .concat(effects.slice(0, 3).map((e) => 'Read the lists again: ' + e.text + '.')),
2164
+ };
2165
+ finding.fixPrompt = ['Kryptheon sent the same step to my running app twice at the same moment, and it was done twice:', '', finding.headline, '', 'Where: ' + finding.from, '',
2166
+ 'First check this is real. If it is, make the check and the change one atomic statement: UPDATE ... SET status = <next> WHERE id = $1 AND status = <the one before> (or ... AND stock >= $2 for a number), do the rest only when a row was changed, and answer 409 when none was; or take a row lock (SELECT ... FOR UPDATE) in the same transaction before reading. Put a unique index behind anything that must exist once. Change nothing else.', '', 'Afterwards I will run: npx kryptheon api'].join(String.fromCharCode(10));
2167
+ out.findings.push(finding);
2168
+ };
2169
+ let i = 0;
2170
+ try {
2171
+ for (; i < candidates.length; i++) {
2172
+ const c = candidates[i];
2173
+ const a = c.a;
2174
+ const what = headFor(c);
2175
+ const t = { what, verdict: 'no account was let in' };
2176
+ out.tried.push(t);
2177
+ const route = c.route;
2178
+ const shared = c.effect.moves.some((m) => m.sign === '-' || (m.col && COUNTER_FIELD.test(m.col)));
2179
+ const sets = (route.db || []).find((d) => d.setsState);
2180
+ const target = sets ? sets.setsState.value : null;
2181
+ const bodyOf = () => (a.encoding === 'form' ? { form: a.fullData || {} } : { json: a.fullData || {} });
2182
+ const atOf = (rec) => a.template.replace('{id}', encodeURIComponent(String(rec.id)));
2183
+ const open = (recs, statusKey) => recs.filter((x) => x && x.id !== undefined && !(statusKey && (ENDED_STATUS.test(String(x[statusKey])) || (target && String(x[statusKey]) === target))));
2184
+ for (const s of sessions) {
2185
+ const recs = rowsOfReply(await s.send('GET', c.listTemplate, {}));
2186
+ const usable = (recs || []).filter((x) => x && typeof x === 'object' && x.id !== undefined);
2187
+ if (!usable.length) { t.verdict = 'no record at GET ' + c.listTemplate + ' to aim it at'; continue; }
2188
+ const statusKey = Object.keys(usable[0]).find((k) => /^(status|state|stage)$/i.test(k));
2189
+ const fresh = open(usable, statusKey);
2190
+ const rec = fresh[0] || usable[0];
2191
+ const at = atOf(rec);
2192
+ const before = await snap(s);
2193
+ const [r1, r2] = await Promise.all([s.send(a.method, at, bodyOf()), s.send(a.method, at, bodyOf())]);
2194
+ const answers = [r1.status, r2.status];
2195
+ const both = taken(r1) && taken(r2);
2196
+ t.by = s.email;
2197
+ t.record = rec.id;
2198
+ t.answers = answers;
2199
+ if (!taken(r1) && !taken(r2)) {
2200
+ t.verdict = 'refused both (' + answers.join(' and ') + ')';
2201
+ // not this account's to change: the next one may be; any other refusal is about the step
2202
+ if ([401, 403, 404].includes(r1.status) && [401, 403, 404].includes(r2.status)) continue;
2203
+ break;
2204
+ }
2205
+ const after = await snap(s);
2206
+ const effects = both ? doubleEffects(before, after, rec, c.listTemplate, a.fullData || {}, a.template) : [];
2207
+ t.verdict = effects.length ? 'done twice' : both ? 'held: both taken, one effect' : 'held: one taken, one refused';
2208
+ out.left.push(what.replace('{id}', rec.id) + ' sent twice at the same moment as ' + s.email + ' (answered ' + answers.join(' and ') + ')' + (effects.length ? ': its effect, twice, is still there - Kryptheon does not undo it' : ': ' + (both ? 'taken twice' : 'taken once') + ', Kryptheon does not undo it'));
2209
+ if (effects.length) finish(c, what, s, a.method + ' ' + at, answers, effects, 'once', a.fullData);
2210
+ // two records for the same stock: only where the step takes a number down
2211
+ if (shared) {
2212
+ // Best: two NEW records that both claim the thing with the fewest units left, each for all of them, then
2213
+ // the step for both at once (blind round 32: one seeded order per product, so two existing orders never
2214
+ // competed for the same last unit, and C24 - stock below zero - was missed).
2215
+ const scarce = await scarceClaim(s, c, j, lists, snap, taken);
2216
+ if (scarce) {
2217
+ const tc = { what, mode: 'compete', claimed: scarce.label, verdict: 'not tried' };
2218
+ out.tried.push(tc);
2219
+ const was = await snap(s);
2220
+ const [q1, q2] = await Promise.all(scarce.ids.map((id) => s.send(a.method, atOf({ id }), bodyOf())));
2221
+ tc.records = scarce.ids;
2222
+ tc.answers = [q1.status, q2.status];
2223
+ for (const [q, id] of [[q1, scarce.ids[0]], [q2, scarce.ids[1]]]) if (taken(q)) out.left.push(what.replace('{id}', id) + ' sent at the same moment as ' + s.email + ' for a record Kryptheon made to claim ' + scarce.label + ' (answered ' + q.status + ')');
2224
+ for (const id of scarce.ids) out.left.push(c.listTemplate + ' record ' + id + ' made by Kryptheon to claim ' + scarce.label + ' - never deleted');
2225
+ if (taken(q1) && taken(q2)) {
2226
+ const now = await snap(s);
2227
+ const was0 = numbersOf(was);
2228
+ const effects2 = [];
2229
+ for (const [at2, v] of numbersOf(now)) {
2230
+ if (!(was0.has(at2) && was0.get(at2) >= 0 && v < 0)) continue;
2231
+ if (String(at2).split('.').pop().split('_').some((w) => CHANGE_WORDS.has(w.toLowerCase()))) continue;
2232
+ effects2.push({ kind: 'below-zero', sure: true, text: at2 + ' went ' + was0.get(at2) + ' -> ' + v + ', below zero' });
2233
+ }
2234
+ effects2.push(...pastLimit(was, now));
2235
+ tc.verdict = effects2.length ? 'went past what there was' : 'held: both taken, the shared number stayed within its limit';
2236
+ if (effects2.length) finish(c, what, s, a.method + ' ' + atOf({ id: scarce.ids[0] }) + ' and ' + atOf({ id: scarce.ids[1] }) + ' (two records made to claim ' + scarce.label + ')', [q1.status, q2.status], effects2, 'compete', a.fullData);
2237
+ break;
2238
+ }
2239
+ tc.verdict = 'held: ' + (taken(q1) || taken(q2) ? 'one taken, one refused' : 'both refused') + ' (records made to claim ' + scarce.label + ')';
2240
+ if (taken(q1) || taken(q2)) break;
2241
+ }
2242
+ // otherwise two open records already there: not ones Kryptheon made, and the next pair when one is refused as not found
2243
+ const others = open(usable, statusKey).filter((x) => String(x.id) !== String(rec.id) && !/Kryptheon test|kx-/i.test(JSON.stringify(x))).slice(0, 8);
2244
+ const tc = { what, mode: 'compete', verdict: 'not tried: fewer than two other open records to compete' };
2245
+ out.tried.push(tc);
2246
+ if (others.length < 2) break;
2247
+ // two records that name the same thing (the same product, the same slot) compete best
2248
+ let pair = [others[0], others[1]];
2249
+ search: for (let x = 0; x < others.length; x++) {
2250
+ for (let y = x + 1; y < others.length; y++) {
2251
+ if (Object.keys(others[x]).some((k) => k !== 'id' && pointerKey(k, c.listTemplate) && others[x][k] !== null && others[x][k] !== undefined && JSON.stringify(others[x][k]) === JSON.stringify(others[y][k]))) { pair = [others[x], others[y]]; break search; }
2252
+ }
2253
+ }
2254
+ const was = await snap(s);
2255
+ const [q1, q2] = await Promise.all(pair.map((x) => s.send(a.method, atOf(x), bodyOf())));
2256
+ tc.records = pair.map((x) => x.id);
2257
+ tc.answers = [q1.status, q2.status];
2258
+ for (const [q, x] of [[q1, pair[0]], [q2, pair[1]]]) if (taken(q)) out.left.push(what.replace('{id}', x.id) + ' sent at the same moment as ' + s.email + ' beside record ' + pair.find((y) => y !== x).id + ' (answered ' + q.status + ')');
2259
+ if (!(taken(q1) && taken(q2))) { tc.verdict = 'held: ' + (taken(q1) || taken(q2) ? 'one taken, one refused' : 'both refused'); break; }
2260
+ const now = await snap(s);
2261
+ const was0 = numbersOf(was);
2262
+ const effects2 = [];
2263
+ for (const [at2, v] of numbersOf(now)) {
2264
+ if (!(was0.has(at2) && was0.get(at2) >= 0 && v < 0)) continue;
2265
+ if (String(at2).split('.').pop().split('_').some((w) => CHANGE_WORDS.has(w.toLowerCase()))) continue;
2266
+ effects2.push({ kind: 'below-zero', sure: true, text: at2 + ' went ' + was0.get(at2) + ' -> ' + v + ', below zero' });
2267
+ }
2268
+ effects2.push(...pastLimit(was, now));
2269
+ tc.verdict = effects2.length ? 'went past what there was' : 'held: both taken, the shared number stayed within its limit';
2270
+ if (effects2.length) finish(c, what, s, a.method + ' ' + atOf(pair[0]) + ' and ' + atOf(pair[1]), [q1.status, q2.status], effects2, 'compete', a.fullData);
2271
+ }
2272
+ break;
2273
+ }
2274
+ }
2275
+ } catch (err) {
2276
+ if (!authz.isRequestLimit(err)) throw err;
2277
+ // keep what was found; say which steps were never sent twice
2278
+ for (const c of candidates.slice(i)) {
2279
+ const mine = out.tried.find((x) => x.what === headFor(c) && !x.mode);
2280
+ if (mine && mine.verdict !== 'no account was let in') continue;
2281
+ if (mine) { mine.verdict = 'not tried'; mine.why = 'the request limit (' + err.limit + ') was reached first'; } else out.tried.push({ what: headFor(c), verdict: 'not tried', why: 'the request limit (' + err.limit + ') was reached first' });
2282
+ }
2283
+ out.stoppedAt = err.limit;
2284
+ }
1480
2285
  return out;
1481
2286
  }
1482
2287
 
@@ -1615,7 +2420,72 @@ function reconcile(toCheck, verdicts) {
1615
2420
  return { shown, held };
1616
2421
  }
1617
2422
 
1618
- function verdictsFor(toCheck, wall, invalid, roleRun, forged, webhooks, steps) {
2423
+ // An address on no real site: nothing is ever sent to it, it is only read back from the app's answer.
2424
+ const OUTSIDE = 'https://kryptheon.invalid/landing';
2425
+
2426
+ /**
2427
+ * The map's open redirects (an address the request names, sent back as the place to go), tried on the
2428
+ * running app: the address is set to an outside one, and the answer's Location - or a redirect field of a
2429
+ * JSON answer - is read. Pointing there = confirmed; anywhere else = held. The sign-in route is tried with
2430
+ * an account's own sign-in, as a person clicking a link would. Blind round 32 (Northstar). { ran, why, tried, findings }.
2431
+ */
2432
+ async function runRedirects(root, options) {
2433
+ const opts = options || {};
2434
+ const plan = planOf(root, opts);
2435
+ if (!plan.ran) return { ran: false, why: plan.why };
2436
+ const j = plan.journey;
2437
+ const items = (opts.toCheck || map.toCheck(plan.map)).filter((t) => t.kind === 'open-redirect');
2438
+ if (!items.length) return { ran: false, why: 'the map found no redirect to an address the request names' };
2439
+ let origin;
2440
+ try {
2441
+ origin = authz.assertLocalTarget(opts.origin);
2442
+ } catch (err) {
2443
+ return { ran: false, why: 'the app must be on this computer (localhost or 127.0.0.1): ' + (opts.origin || 'no address was given') };
2444
+ }
2445
+ const account = j.accounts.usable[0];
2446
+ const ctx = authz.makeContext({ baseUrl: origin, allowOrigins: [origin], fetch: opts.fetch, passwords: account ? { [account.email]: account.password } : {}, maxRequests: 20 + items.length * 3 });
2447
+ const out = { ran: true, tried: [], findings: [] };
2448
+ const signInBody = () => (account && j.signIn ? { [j.signIn.emailField]: account.email, [j.signIn.passwordField]: account.password } : null);
2449
+ for (const item of items) {
2450
+ const what = normRoute(item.route);
2451
+ const [method, path] = what.split(' ');
2452
+ const route = (plan.map.routes || []).find((r) => r.method + ' ' + normRoute(r.path || '') === what);
2453
+ const rd = route && (route.redirects || [])[0];
2454
+ if (!rd || /\{/.test(path || '') || !/^(query|body)$/.test(rd.from)) {
2455
+ out.tried.push({ what, verdict: 'not tried', why: !rd ? 'the redirect was not read' : 'the address is in ' + rd.from + ' or the path needs a record' });
2456
+ continue;
2457
+ }
2458
+ const isSignIn = j.signIn && j.signIn.method === method && normRoute(j.signIn.path) === path;
2459
+ const s = authz.makeSession(ctx, 'account', account ? account.email : 'nobody', account ? account.password : '');
2460
+ if (!isSignIn && account && j.signIn) {
2461
+ const f = signInBody();
2462
+ await s.send(j.signIn.method, j.signIn.path, j.signIn.encoding === 'form' ? { form: f } : { json: f });
2463
+ }
2464
+ const body = Object.assign({}, isSignIn ? signInBody() : {}, rd.from === 'body' ? { [rd.field]: OUTSIDE } : {});
2465
+ const at = path + (rd.from === 'query' ? '?' + encodeURIComponent(rd.field) + '=' + encodeURIComponent(OUTSIDE) : '');
2466
+ const sendOpts = method === 'GET' || method === 'HEAD' ? {} : (isSignIn && j.signIn.encoding === 'form') || (route && route.validation && /form/.test(String(route.encoding || ''))) ? { form: body } : { json: body };
2467
+ const res = await s.send(method, at, sendOpts);
2468
+ const loc = String(res.location || '');
2469
+ const inJson = res.json && typeof res.json === 'object' && Object.values(res.json).some((v) => typeof v === 'string' && v.indexOf('kryptheon.invalid') !== -1);
2470
+ const away = (res.status >= 300 && res.status < 400 && /^https?:\/\/kryptheon\.invalid/i.test(loc)) || inJson;
2471
+ out.tried.push({ what, field: rd.from + '.' + rd.field, status: res.status, location: loc, verdict: away ? 'sent away' : 'held' });
2472
+ if (!away) continue;
2473
+ const finding = {
2474
+ kind: 'open-redirect', where: what, from: item.where, status: 'confirmed', severity: 'MEDIUM',
2475
+ headline: what + ' sends the browser to any address given as ' + rd.field + ': a link to your app ' + (isSignIn ? 'signs a person in and then ' : '') + 'lands them on another site',
2476
+ proof: [
2477
+ (isSignIn ? 'Signed in as ' + account.email + ' through ' : 'As ' + (account ? account.email : 'nobody') + ', sent ') + method + ' ' + path + ' with ' + rd.from + '.' + rd.field + ' = ' + OUTSIDE + '.',
2478
+ inJson ? 'The answer (' + res.status + ') named that address as the place to go.' : 'The answer was ' + res.status + ' with Location: ' + authz.clip(loc, 120) + '.',
2479
+ ],
2480
+ };
2481
+ finding.fixPrompt = ['Kryptheon tried this on my running app:', '', finding.headline + '.', '', 'Where: ' + finding.from, '', finding.proof.join('\n'), '',
2482
+ 'Send the browser only to an address on this site: accept a path that starts with a single "/" (not "//", not "/\\\\", no "http"), or a name from a fixed list of places, and fall back to "/" for anything else. Change nothing else in the route.'].join('\n');
2483
+ out.findings.push(finding);
2484
+ }
2485
+ return out;
2486
+ }
2487
+
2488
+ function verdictsFor(toCheck, wall, invalid, roleRun, forged, webhooks, steps, redirects) {
1619
2489
  const out = [];
1620
2490
  const attempts = wall && wall.ran ? wall.result.attempts : [];
1621
2491
  const failures = (wall && wall.findings) || [];
@@ -1625,7 +2495,20 @@ function verdictsFor(toCheck, wall, invalid, roleRun, forged, webhooks, steps) {
1625
2495
  const route = normRoute(t.route);
1626
2496
  const v = { route: t.route, kind: t.kind, headline: t.headline };
1627
2497
  const kind = /^no-sign-in$/.test(t.kind) ? 'signin' : /^tenant-/.test(t.kind) ? 'tenant' : /^no-role$/.test(t.kind) ? 'role' : null;
1628
- if (t.kind === 'no-state-check') {
2498
+ if (t.kind === 'open-redirect') {
2499
+ const hit = redirects && redirects.ran ? redirects.findings.find((f) => f.where === route) : null;
2500
+ const tried = redirects && redirects.ran ? redirects.tried.find((x) => x.what === route) : null;
2501
+ if (hit) {
2502
+ v.verdict = 'confirmed';
2503
+ v.finding = hit.headline;
2504
+ } else if (tried && tried.verdict === 'held') {
2505
+ v.verdict = 'held';
2506
+ v.strength = 'strong';
2507
+ } else {
2508
+ v.verdict = 'not tried';
2509
+ v.why = tried ? tried.why || tried.verdict : redirects && !redirects.ran ? redirects.why : 'the redirect was not tried';
2510
+ }
2511
+ } else if (t.kind === 'no-state-check') {
1629
2512
  const hit = steps && steps.ran ? steps.findings.find((f) => f.where === route) : null;
1630
2513
  const tried = steps && steps.ran ? steps.tried.find((x) => x.what === route) : null;
1631
2514
  if (hit) {
@@ -1662,14 +2545,20 @@ function verdictsFor(toCheck, wall, invalid, roleRun, forged, webhooks, steps) {
1662
2545
  }
1663
2546
  } else if (kind === 'role') {
1664
2547
  // "held" only when the roles run really tried this route as a role the policy forbids
1665
- const tries = roleRun && roleRun.ran ? roleRun.result.attempts.filter((a) => !a.notTried && a.kind === 'role' && a.method + ' ' + a.template === route) : [];
1666
- const rf = roleRun && roleRun.ran ? roleRun.findings.filter((f) => f.where === route && f.kind !== 'tenant' && f.kind !== 'signin') : [];
2548
+ // a try of a change no rule names (as the fewest permissions) is never "held" nor "confirmed": no rule says who may
2549
+ const tries = roleRun && roleRun.ran ? roleRun.result.attempts.filter((a) => !a.notTried && a.kind === 'role' && !a.unruled && a.method + ' ' + a.template === route) : [];
2550
+ const rf = roleRun && roleRun.ran ? roleRun.findings.filter((f) => f.where === route && f.kind !== 'tenant' && f.kind !== 'signin' && !f.unruled) : [];
2551
+ const unruledTries = roleRun && roleRun.ran ? roleRun.result.attempts.filter((a) => !a.notTried && a.unruled && a.method + ' ' + a.template === route) : [];
2552
+ const unruledFinding = roleRun && roleRun.ran ? roleRun.findings.find((f) => f.where === route && f.unruled) : null;
1667
2553
  if (!roleRun || !roleRun.ran) {
1668
2554
  v.verdict = 'not tried';
1669
2555
  v.why = 'it needs the roles run (--roles) and your permissions-policy.json';
1670
2556
  } else if (rf.length) {
1671
2557
  v.verdict = 'confirmed';
1672
2558
  v.finding = rf[0].headline;
2559
+ } else if (!tries.length && unruledTries.length) {
2560
+ v.verdict = 'not judged';
2561
+ v.why = unruledFinding ? 'to check: ' + unruledFinding.headline : 'no permission in ' + roles.POLICY_FILE + ' names it; tried only as the role with the fewest permissions (' + unruledTries[0].reason + ')';
1673
2562
  } else if (tries.length && tries.every((a) => a.verdict === V.DENIED || a.verdict === V.ALLOWED)) {
1674
2563
  v.verdict = 'held';
1675
2564
  v.strength = heldStrength(tries);
@@ -1807,33 +2696,71 @@ async function runApi(root, options) {
1807
2696
  const shared = Object.assign({}, opts, { map: m, say: say });
1808
2697
  // jobs first, on the app as it was seeded: the tries after them cancel and refund the records a job works on
1809
2698
  // (round 28: the settlement job found no paid order left and made nothing either time)
1810
- const jobsRun = await runJobs(root, shared);
1811
- const wall = await runWall(root, shared);
1812
- const forged = await runForged(root, shared);
1813
- const webhooks = await webhook.run(root, shared);
1814
- const exportsRun = await csv.run(root, shared);
1815
- const keptRun = await runKept(root, shared);
1816
- const createsRun = await runCreates(root, shared);
1817
- const limitsRun = await runLimits(root, shared);
1818
- const stepsRun = await runSteps(root, Object.assign({}, shared, { toCheck: toCheck }));
2699
+ // A check that reaches its request limit stops there and says so; the others still run and report.
2700
+ // Blind round 32: one check's limit threw out of here, and the person got a stack trace and no report.
2701
+ const contained = async (label, run) => {
2702
+ try {
2703
+ return await run();
2704
+ } catch (err) {
2705
+ if (!authz.isRequestLimit(err)) throw err;
2706
+ say(' ' + label + ': stopped at its limit of ' + err.limit + ' requests');
2707
+ return { ran: false, stopped: true, label: label, limit: err.limit, why: 'stopped at its limit of ' + err.limit + ' requests before it finished, so nothing it found is reported' };
2708
+ }
2709
+ };
2710
+ const jobsRun = await contained('Jobs run twice', () => runJobs(root, shared));
2711
+ const wall = await contained('Across organizations and without a session', () => runWall(root, shared));
2712
+ const forged = await contained('Who is calling, as the request says', () => runForged(root, shared));
2713
+ const webhooks = await contained('Webhooks', () => webhook.run(root, shared));
2714
+ const exportsRun = await contained('Exports', () => csv.run(root, shared));
2715
+ const keptRun = await contained('Changes kept', () => runKept(root, shared));
2716
+ const createsRun = await contained('Creates sent twice at once', () => runCreates(root, shared));
2717
+ const limitsRun = await contained('Limits', () => runLimits(root, shared));
2718
+ const stepsRun = await contained('Steps sent twice', () => runSteps(root, Object.assign({}, shared, { toCheck: toCheck })));
2719
+ const refundsRun = await contained('Refunds above what was paid', () => runRefundCap(root, shared));
2720
+ const redirectsRun = await contained('Redirects to an address the request names', () => runRedirects(root, Object.assign({}, shared, { toCheck: toCheck })));
2721
+ const onceRun = await contained('Steps sent twice at once', () => runStepsAtOnce(root, shared));
1819
2722
  let roleRun = null;
1820
- if (opts.roles) roleRun = await runRoles(root, shared);
2723
+ if (opts.roles) roleRun = await contained('By role', () => runRoles(root, shared));
1821
2724
  // the bad values always last: one of them may stop the app (round 24 lost its whole roles run that way)
1822
- const invalid = await runInvalid(root, shared);
1823
- const verdicts = verdictsFor(toCheck, wall, invalid, roleRun, forged, webhooks, stepsRun);
2725
+ const invalid = await contained('Bad values', () => runInvalid(root, shared));
2726
+ const verdicts = verdictsFor(toCheck, wall, invalid, roleRun, forged, webhooks, stepsRun, redirectsRun);
1824
2727
  const seen = new Set();
1825
- const findings = (wall.ran ? wall.findings : []).concat(forged.ran ? forged.findings : []).concat(webhooks.ran ? webhooks.findings : []).concat(exportsRun.ran ? exportsRun.findings : []).concat(jobsRun.ran ? jobsRun.findings : []).concat(keptRun.ran ? keptRun.findings : []).concat(createsRun.ran ? createsRun.findings : []).concat(limitsRun.ran ? limitsRun.findings : []).concat(stepsRun.ran ? stepsRun.findings : []).concat(roleRun && roleRun.ran ? roleRun.findings : []).concat(invalid.ran ? invalid.findings : []).filter((f) => {
2728
+ const findings = (wall.ran ? wall.findings : []).concat(redirectsRun.ran ? redirectsRun.findings : []).concat(refundsRun.ran ? refundsRun.findings : []).concat(forged.ran ? forged.findings : []).concat(webhooks.ran ? webhooks.findings : []).concat(exportsRun.ran ? exportsRun.findings : []).concat(jobsRun.ran ? jobsRun.findings : []).concat(keptRun.ran ? keptRun.findings : []).concat(createsRun.ran ? createsRun.findings : []).concat(limitsRun.ran ? limitsRun.findings : []).concat(stepsRun.ran ? stepsRun.findings : []).concat(onceRun.ran ? onceRun.findings : []).concat(roleRun && roleRun.ran ? roleRun.findings : []).concat(invalid.ran ? invalid.findings : []).filter((f) => {
1826
2729
  const k = f.kind + '|' + f.where + '|' + (f.field || '');
1827
2730
  return seen.has(k) ? false : (seen.add(k), true);
1828
2731
  });
1829
- const left = (webhooks.ran ? webhooks.left : []).concat(exportsRun.ran ? exportsRun.left : []).concat(jobsRun.ran ? jobsRun.left : []).concat(keptRun.ran ? keptRun.left : []).concat(createsRun.ran ? createsRun.left : []).concat(limitsRun.ran ? limitsRun.left : []).concat(stepsRun.ran ? stepsRun.left : []).concat(wall.ran ? wall.result.seeded || [] : []).concat(roleRun && roleRun.ran ? roleRun.result.seeded || [] : []).concat(wall.ran ? leftBehind(wall.result.attempts) : []).concat(invalid.ran ? invalid.left : []).concat(roleRun && roleRun.ran ? leftBehind(roleRun.result.attempts) : []);
2732
+ const left = (webhooks.ran ? webhooks.left : []).concat(refundsRun.ran ? refundsRun.left : []).concat(exportsRun.ran ? exportsRun.left : []).concat(jobsRun.ran ? jobsRun.left : []).concat(keptRun.ran ? keptRun.left : []).concat(createsRun.ran ? createsRun.left : []).concat(limitsRun.ran ? limitsRun.left : []).concat(stepsRun.ran ? stepsRun.left : []).concat(onceRun.ran ? onceRun.left : []).concat(wall.ran ? wall.result.seeded || [] : []).concat(roleRun && roleRun.ran ? roleRun.result.seeded || [] : []).concat(wall.ran ? leftBehind(wall.result.attempts) : []).concat(invalid.ran ? invalid.left : []).concat(roleRun && roleRun.ran ? leftBehind(roleRun.result.attempts) : []);
1830
2733
  return {
1831
2734
  ran: wall.ran || invalid.ran, why: wall.ran || invalid.ran ? null : wall.why, checkedAt: new Date().toISOString(),
1832
- origin: opts.origin, wall: wall, forged: forged, webhooks: webhooks, exports: exportsRun, jobs: jobsRun, kept: keptRun, creates: createsRun, limits: limitsRun, steps: stepsRun, invalid: invalid, roles: roleRun, verdicts: verdicts, findings: findings, left: left,
2735
+ origin: opts.origin, wall: wall, forged: forged, webhooks: webhooks, exports: exportsRun, jobs: jobsRun, kept: keptRun, creates: createsRun, limits: limitsRun, steps: stepsRun, redirects: redirectsRun, refunds: refundsRun, stepsAtOnce: onceRun, invalid: invalid, roles: roleRun, verdicts: verdicts, findings: findings, left: left,
1833
2736
  notTried: (wall.plan ? wall.plan.notTried : []).concat(invalid.ran ? invalid.notTried : []),
2737
+ writes: writesOf(m, wall),
1834
2738
  };
1835
2739
  }
1836
2740
 
2741
+ /**
2742
+ * Every route that writes a table, the tables it writes, and whether a try across organizations reached it - so a
2743
+ * database finding that is "to verify" (a row naming another organization's record) can say which routes were and
2744
+ * were not tried on the running app. [{ route, tables, acrossOrgs: 'tried' | 'not tried', why }]
2745
+ */
2746
+ function writesOf(m, wall) {
2747
+ const attempts = wall && wall.ran ? wall.result.attempts : [];
2748
+ const notes = wall && wall.plan && wall.plan.journey ? wall.plan.journey.notTried : [];
2749
+ const out = [];
2750
+ for (const r of (m && m.routes) || []) {
2751
+ if (r.unmounted || !r.path) continue;
2752
+ const tables = [];
2753
+ for (const d of r.db || []) if (d.op !== 'SELECT' && d.table && !tables.includes(d.table)) tables.push(d.table);
2754
+ if (!tables.length) continue;
2755
+ const route = r.method + ' ' + normRoute(r.path);
2756
+ const mine = attempts.filter((a) => a.kind === 'tenant' && a.method + ' ' + a.template === route);
2757
+ const ran = mine.some((a) => !a.notTried);
2758
+ const note = notes.find((n) => normRoute(n.what) === route);
2759
+ out.push({ route, tables, acrossOrgs: ran ? 'tried' : 'not tried', why: ran ? null : (mine.find((a) => a.notTried) || {}).reason || (note && note.why) || (wall && !wall.ran ? wall.why : 'no try across organizations was aimed at it') });
2760
+ }
2761
+ return out;
2762
+ }
2763
+
1837
2764
  /** The report as it is kept on disk: no passwords, no raw tries, no plan with accounts. */
1838
2765
  function reportOf(run) {
1839
2766
  const wallTries = run.wall && run.wall.ran ? run.wall.result.attempts.filter((a) => !a.notTried).length : 0;
@@ -1843,7 +2770,7 @@ function reportOf(run) {
1843
2770
  tries: { acrossOrganizationsAndNoSession: wallTries, badValues: run.invalid && run.invalid.ran ? run.invalid.tried.length : 0 },
1844
2771
  findings: run.findings.map((f) => ({ kind: f.kind, severity: f.severity, status: f.status, where: f.where, from: f.from, headline: f.headline, proof: f.proof, fixPrompt: f.fixPrompt })),
1845
2772
  webhooks: run.webhooks && run.webhooks.ran ? run.webhooks.hooks.map((h) => ({ route: h.route, from: h.from, signing: h.signing || null, notTried: h.notTried, control: h.control ? { status: h.control.status, took: h.control.took } : null, tries: h.tried.map((t) => ({ what: t.label + ' ' + t.type, record: t.record, status: t.status, changed: t.changed })) })) : [],
1846
- verdicts: run.verdicts, left: run.left, notTried: run.notTried,
2773
+ verdicts: run.verdicts, left: run.left, notTried: run.notTried, writes: run.writes || [],
1847
2774
  };
1848
2775
  }
1849
2776
 
@@ -1862,14 +2789,22 @@ function saveReport(dir, run) {
1862
2789
  function apiLines(run) {
1863
2790
  if (!run.ran) return ['', ' Nothing was tried: ' + run.why, ''];
1864
2791
  const out = run.wall && run.wall.ran ? wallLines(run.wall) : ['', ' Across organizations and without a session: not tried (' + (run.wall ? run.wall.why : '') + ')', ''];
2792
+ // a check that stopped at its request limit prints nothing of its own: say so, so its silence is not read as "clear"
2793
+ const stopped = [run.jobs, run.forged, run.webhooks, run.redirects, run.refunds, run.exports, run.kept, run.creates, run.limits, run.steps, run.stepsAtOnce, run.roles].filter((r) => r && r.stopped);
2794
+ for (const r of stopped) out.push(' NOT FINISHED ' + r.label + ': ' + r.why + '.');
2795
+ if (run.steps && run.steps.stoppedAt) out.push(' NOT FINISHED Steps sent twice: the request limit (' + run.steps.stoppedAt + ') was reached; the steps it did not reach are listed below as not tried.');
2796
+ if (run.stepsAtOnce && run.stepsAtOnce.stoppedAt) out.push(' NOT FINISHED Steps sent twice at once: the request limit (' + run.stepsAtOnce.stoppedAt + ') was reached; the steps it did not reach are listed below as not tried.');
2797
+ if (stopped.length || (run.steps && run.steps.stoppedAt) || (run.stepsAtOnce && run.stepsAtOnce.stoppedAt)) out.push('');
1865
2798
  const counts = { confirmed: 0, held: 0, 'not judged': 0, 'not tried': 0 };
1866
2799
  for (const v of run.verdicts) counts[v.verdict] = (counts[v.verdict] || 0) + 1;
1867
2800
  if (run.roles && run.roles.ran) {
1868
- const rt = run.roles.result.attempts.filter((a) => !a.notTried && a.kind === 'role');
2801
+ const rt = run.roles.result.attempts.filter((a) => !a.notTried && a.kind === 'role' && !a.unruled);
1869
2802
  const n = (expected, verdict) => rt.filter((a) => a.expected === expected && a.verdict === verdict).length;
1870
2803
  out.push(' By role (your permissions-policy.json): ' + n('deny', V.DENIED) + ' refused as the policy says, ' + n('deny', V.FAILURE) + ' got through that the policy forbids, ' + n('allow', V.ALLOWED) + ' allowed as the policy says, ' + rt.filter((a) => a.verdict === V.AMBIGUOUS).length + ' could not be judged');
2804
+ const ur = run.roles.result.attempts.filter((a) => !a.notTried && a.unruled);
2805
+ if (ur.length) out.push(' Changes no permission names, tried as the role with the fewest permissions: ' + ur.filter((a) => a.verdict === V.DENIED).length + ' refused, ' + ur.filter((a) => a.verdict !== V.DENIED).length + ' not refused (not judged: no rule says who may)');
1871
2806
  for (const f of run.roles.findings.filter((x) => x.kind !== 'tenant' && x.kind !== 'signin')) {
1872
- out.push(' PROBLEM ' + f.severity + ' ' + f.headline + (f.from ? ' (' + f.from + ')' : ''));
2807
+ out.push(' ' + (f.unruled ? 'TO CHECK' : 'PROBLEM') + ' ' + f.severity + ' ' + f.headline + (f.from ? ' (' + f.from + ')' : ''));
1873
2808
  for (const l of (f.proof || []).slice(0, 6)) out.push(' ' + l);
1874
2809
  }
1875
2810
  out.push('');
@@ -1942,20 +2877,44 @@ function apiLines(run) {
1942
2877
  }
1943
2878
  out.push('');
1944
2879
  }
2880
+ if (run.refunds && run.refunds.ran) {
2881
+ for (const t of run.refunds.tried) out.push(' Refund ' + t.what + (t.record !== undefined ? ' (record ' + t.record + ', paid ' + t.paid + ', a refund of ' + t.over + ')' : '') + ': ' + (t.took === undefined ? t.verdict + (t.why ? ' - ' + t.why : '') : t.took ? 'taken (' + t.status + ')' : 'refused (' + t.status + ')'));
2882
+ for (const f of run.refunds.findings) {
2883
+ out.push(' ' + (f.status === 'confirmed' ? 'PROBLEM' : 'TO CHECK') + ' ' + f.severity + ' ' + f.headline + ' (' + f.from + ')');
2884
+ for (const l of f.proof) out.push(' ' + l);
2885
+ }
2886
+ out.push('');
2887
+ }
2888
+ if (run.redirects && run.redirects.ran) {
2889
+ for (const t of run.redirects.tried) out.push(' Redirect ' + t.what + (t.field ? ' (' + t.field + ' = an outside address)' : '') + ': ' + t.verdict + (t.status ? ' (answered ' + t.status + (t.location ? ' to ' + authz.clip(t.location, 60) : '') + ')' : t.why ? ' - ' + t.why : ''));
2890
+ for (const f of run.redirects.findings) {
2891
+ out.push(' PROBLEM ' + f.severity + ' ' + f.headline + ' (' + f.from + ')');
2892
+ for (const l of f.proof) out.push(' ' + l);
2893
+ }
2894
+ out.push('');
2895
+ }
1945
2896
  if (run.steps && run.steps.ran) {
1946
- for (const t of run.steps.tried) out.push(' Step ' + t.what + ' sent twice: ' + t.verdict + (t.record !== undefined ? ' (record ' + t.record + ', answered ' + t.first + ', then ' + t.second + ')' : ''));
2897
+ for (const t of run.steps.tried) out.push(' Step ' + t.what + ' sent twice: ' + t.verdict + (t.record !== undefined ? ' (record ' + t.record + ', answered ' + t.first + ', then ' + t.second + ')' : '') + (t.record === undefined && t.why ? ' - ' + t.why : ''));
1947
2898
  for (const f of run.steps.findings) {
1948
2899
  out.push(' PROBLEM ' + f.severity + ' ' + f.headline + ' (' + f.from + ')');
1949
2900
  for (const l of f.proof) out.push(' ' + l);
1950
2901
  }
1951
2902
  out.push('');
1952
2903
  }
2904
+ if (run.stepsAtOnce && run.stepsAtOnce.ran) {
2905
+ for (const t of run.stepsAtOnce.tried) out.push(' Step ' + t.what + ' sent ' + (t.mode === 'compete' ? 'for two records' : 'twice') + ' at once: ' + t.verdict + (t.record !== undefined ? ' (record ' + t.record + ', answered ' + t.answers.join(' and ') + ')' : t.records ? ' (records ' + t.records.join(' and ') + ', answered ' + t.answers.join(' and ') + ')' : '') + (t.why ? ' - ' + t.why : ''));
2906
+ for (const f of run.stepsAtOnce.findings) {
2907
+ out.push(' ' + (f.status === 'confirmed' ? 'PROBLEM' : 'TO CHECK') + ' ' + f.severity + ' ' + f.headline + ' (' + f.from + ')');
2908
+ for (const l of f.proof) out.push(' ' + l);
2909
+ }
2910
+ out.push('');
2911
+ }
1953
2912
  if (run.invalid && run.invalid.ran && run.invalid.appDown) {
1954
2913
  out.push(' THE APP STOPPED ANSWERING after ' + run.invalid.appDown + '. Nothing more was sent; start it again before any other check.');
1955
2914
  out.push('');
1956
2915
  }
1957
2916
  if (run.invalid && run.invalid.ran) {
1958
- out.push(' Bad values (as ' + run.invalid.account + '): ' + run.invalid.tried.filter((t) => t.verdict === 'turned away').length + ' turned away, ' + run.invalid.tried.filter((t) => t.verdict === 'kept').length + ' kept, ' + run.invalid.tried.filter((t) => t.verdict === 'crashed').length + ' crashed, ' + run.invalid.tried.filter((t) => t.verdict === 'went below zero').length + ' went below zero, ' + run.invalid.tried.filter((t) => t.verdict === 'taken, not shown').length + ' taken with nothing to show');
2917
+ out.push(' Bad values (as ' + run.invalid.account + '): ' + run.invalid.tried.filter((t) => t.verdict === 'turned away').length + ' turned away, ' + run.invalid.tried.filter((t) => t.verdict === 'kept').length + ' kept, ' + run.invalid.tried.filter((t) => t.verdict === 'crashed').length + ' crashed, ' + run.invalid.tried.filter((t) => t.verdict === 'went below zero').length + ' went below zero, ' + (run.invalid.tried.some((t) => t.verdict === 'changed') ? run.invalid.tried.filter((t) => t.verdict === 'changed').length + ' stored as something else, ' : '') + run.invalid.tried.filter((t) => t.verdict === 'taken, not shown').length + ' taken with nothing to show');
1959
2918
  for (const f of run.invalid.findings) {
1960
2919
  out.push(' PROBLEM ' + f.severity + ' ' + f.headline + ' (' + f.from + ')');
1961
2920
  for (const l of f.proof) out.push(' ' + l);
@@ -1981,4 +2940,4 @@ function apiLines(run) {
1981
2940
  return out;
1982
2941
  }
1983
2942
 
1984
- module.exports = { reconcile, heldStrength, runSteps, numbersMoved, runKept, runCreates, COSTLY_RECORD, runLimits, limitPairOf, valueIn, plainFieldOf, runJobs, countsIn, JOB_PATH, runForged, personIn, REPORT_FILE, verdictsFor, runApi, reportOf, saveReport, apiLines, badValueFor, runInvalid, signInOf, emailsIn, accountsOf, wordsOf, sampleValue, allowedByName, actionOf, journeyFromMap, planOf, planLines, wallPolicy, runWall, findingsOf, fixPrompt, leftBehind, wallLines, DRAFT_FILE, permissionName, draftPolicy, writeDraft, runRoles };
2943
+ module.exports = { reconcile, isUpdateRoute, storedAs, updateProbesOf, heldStrength, runSteps, runRedirects, OUTSIDE, runRefundCap, PLUMBING, runStepsAtOnce, doubleEffects, numbersMoved, runKept, runCreates, COSTLY_RECORD, runLimits, limitPairOf, valueIn, plainFieldOf, runJobs, countsIn, JOB_PATH, runForged, personIn, REPORT_FILE, verdictsFor, runApi, reportOf, saveReport, apiLines, badValueFor, runInvalid, signInOf, emailsIn, accountsOf, wordsOf, sampleValue, allowedByName, actionOf, journeyFromMap, planOf, planLines, wallPolicy, runWall, findingsOf, fixPrompt, leftBehind, wallLines, DRAFT_FILE, permissionName, draftPolicy, writeDraft, runRoles };