kryptheon 0.1.63 → 0.1.65

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. */
@@ -210,6 +227,7 @@ function actionOf(r, known, roleNames) {
210
227
  data: Object.keys(data).length ? data : null, encoding: encodingOf(r), flow: r.file + ':' + r.line, from: { file: r.file, line: r.line },
211
228
  mapAuth: r.auth ? r.auth.status : map.STATUS.UNKNOWN, leftOut, unchecked: unchecked, fields: fields, fullData: fullData,
212
229
  guardRoles: r.role && r.role.status === map.STATUS.DETECTED && Array.isArray(r.role.roles) ? r.role.roles : null,
230
+ roleChecked: Boolean(r.role && r.role.status === map.STATUS.DETECTED),
213
231
  scopedByOrg: Boolean(r.db && r.db.some((d) => d.level === 'organisation')),
214
232
  };
215
233
  }
@@ -549,7 +567,7 @@ function draftPolicy(plan) {
549
567
  if ((!a.guardRoles || a.guardRoles.includes(r)) && !rolesOut[r].includes(perm)) rolesOut[r].push(perm);
550
568
  }
551
569
  }
552
- const noCheck = actions.filter((a) => !a.guardRoles).map((a) => a.method + ' ' + a.template);
570
+ const noCheck = actions.filter((a) => !a.guardRoles && !a.roleChecked).map((a) => a.method + ' ' + a.template);
553
571
  const json = {
554
572
  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
573
  notes: [
@@ -598,7 +616,7 @@ async function runRoles(root, options) {
598
616
  say('Signing in as ' + j.accounts.usable.length + ' accounts, and trying ' + j.actions.length + ' routes as each, across organizations and without signing in...');
599
617
  const result = await roles.checkAccess({
600
618
  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,
619
+ fetch: opts.fetch, say: say, stopped: opts.stopped, maxAttempts: opts.maxAttempts, anonymous: true, seed: true, tryUnruled: true,
602
620
  });
603
621
  roles.markHints(result.attempts);
604
622
  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,9 +725,22 @@ async function runInvalid(root, options) {
707
725
  '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
726
  out.findings.push(finding);
709
727
  };
728
+ const getTemplates = new Set(j.actions.filter((x) => x.method === 'GET' && x.params.length <= 1).map((x) => x.template));
729
+ // the request limit ends this check where it is: what was tried is kept, the routes it did not reach are said
730
+ let reaching = null;
731
+ try {
732
+ // 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)
733
+ 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
734
  for (const a of j.actions) {
711
735
  if (!/^(POST|PUT|PATCH)$/.test(a.method) || !(a.fields || []).length) continue;
712
736
  const what = a.method + ' ' + a.template;
737
+ // a change to a record or a singleton (settings): read first, one field made wrong at a time, the original values sent back
738
+ if (isUpdateRoute(a)) {
739
+ reaching = a;
740
+ if (await invalidOnUpdate({ session, out, who, getTemplates, say }, a) === 'down') return out;
741
+ continue;
742
+ }
743
+ reaching = a;
713
744
  const probes = probesOf(a);
714
745
  if (!probes.length) continue;
715
746
  if (a.params.length || a.method !== 'POST') {
@@ -790,6 +821,7 @@ async function runInvalid(root, options) {
790
821
  }
791
822
  }
792
823
  // A write aimed at a record that cannot exist: the app should say no (404, 400). A success is a write on nothing.
824
+ reaching = null;
793
825
  for (const a of j.actions) {
794
826
  if (!/^(POST|PUT|PATCH)$/.test(a.method) || a.params.length !== 1) continue;
795
827
  if (opts.stopped && opts.stopped()) return out;
@@ -811,9 +843,200 @@ async function runInvalid(root, options) {
811
843
  '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
844
  out.findings.push(finding);
813
845
  }
846
+ } catch (err) {
847
+ if (!authz.isRequestLimit(err)) throw err;
848
+ out.stoppedAt = err.limit;
849
+ const at = j.actions.indexOf(reaching);
850
+ 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' });
851
+ }
852
+ return out;
853
+ }
854
+
855
+ // --- bad values on a route that CHANGES a record ------------------------------------
856
+
857
+ // A field that holds a secret: never sent a wrong value, and never read back into a report.
858
+ const SECRET_FIELD = /^(password|passwd|secret|token|apikey|key|hash|salt|otp|pin|cvv)$/;
859
+
860
+ /** Whether a route is an update Kryptheon can read first: PUT|PATCH on a record (/things/{id}) or on a singleton (/settings). */
861
+ function isUpdateRoute(a) {
862
+ if (!/^(PUT|PATCH)$/.test(a.method) || !(a.fields || []).length) return false;
863
+ return !a.params.length || (a.params.length === 1 && a.segs[a.segs.length - 1] === '{id}');
864
+ }
865
+
866
+ /** Whether a value read back is the one that was sent (a number may come back as text, a long text cut short). */
867
+ function storedAs(got, sent) {
868
+ if (got === undefined) return false;
869
+ if (JSON.stringify(got) === JSON.stringify(sent)) return true;
870
+ if (typeof sent === 'number' && got !== null && got !== '' && typeof got !== 'object' && Number(got) === sent) return true;
871
+ if (typeof sent === 'string' && sent.length > 40 && typeof got === 'string' && got.slice(0, 40) === sent.slice(0, 40)) return true;
872
+ return false;
873
+ }
874
+
875
+ /**
876
+ * The tries on a field of an update, one wrong thing at a time, from the record's current values:
877
+ * the same blank, negative, above-100 and unchecked-field tries a create gets, and for every field a value of the wrong kind
878
+ * (an object, an array, null, and a number for a text or text for a number).
879
+ */
880
+ function updateProbesOf(a, base) {
881
+ const out = [];
882
+ const seen = new Set();
883
+ const add = (f, mode, value, why) => {
884
+ if (JSON.stringify(value) === JSON.stringify(base[f.name])) return;
885
+ // the same value for the same field once, whatever it is called
886
+ const k = f.name + '|' + JSON.stringify(value);
887
+ if (seen.has(k)) return;
888
+ seen.add(k);
889
+ out.push({ field: f, mode: mode, value: value, why: why });
890
+ };
891
+ const leave = (f) => base[f.name] === undefined || wordsOf(f.name).some((w) => SECRET_FIELD.test(w)) || /^(id|uuid)$/i.test(f.name);
892
+ for (const pr of probesOf(a)) if (pr.mode !== 'overdraw' && !leave(pr.field)) add(pr.field, pr.mode, pr.value, pr.why);
893
+ for (const f of a.fields) {
894
+ if (leave(f)) continue;
895
+ const kind = typeof f.sample === 'number' ? 'number' : typeof f.sample === 'boolean' ? 'boolean' : typeof base[f.name] === 'number' ? 'number' : 'text';
896
+ add(f, 'object', { 'kx-invalid': 'kx-invalid-object' }, 'an object where a ' + kind + ' belongs');
897
+ add(f, 'array', ['kx-invalid-array'], 'an array where a ' + kind + ' belongs');
898
+ add(f, 'null', null, 'null');
899
+ if (kind === 'text') add(f, 'number', 12345, 'a number (12345) where a text belongs');
900
+ else add(f, 'string', 'kx-invalid-not-a-' + kind, 'text where a ' + kind + ' belongs');
901
+ }
814
902
  return out;
815
903
  }
816
904
 
905
+ /**
906
+ * Bad values sent to one PUT|PATCH route, on a record or a singleton (settings, profile). The record is read first;
907
+ * the body starts from its current values for the fields the route reads, and one field at a time is made wrong.
908
+ * Judged from the app's own read before and after: a 500 = crashed; the bad value shown after = kept; some other
909
+ * value stored in its place = changed; refused with nothing changed = turned away. Every try that was taken is
910
+ * followed by the original values sent back, and the read says whether that worked (said in `left`).
911
+ * Returns 'down' when the app stopped answering, 'stuck' when a record could not be put back, else null.
912
+ */
913
+ async function invalidOnUpdate(env, a) {
914
+ const { session, out, who, getTemplates, say } = env;
915
+ const what = a.method + ' ' + a.template;
916
+ const singleton = !a.params.length;
917
+ const listTemplate = singleton ? null : '/' + a.segs.slice(0, a.params[0].index).join('/');
918
+ const no = (why) => { out.notTried.push({ what: what, why: why }); return null; };
919
+ 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');
920
+ if (!singleton && !getTemplates.has(listTemplate)) return no('there is no existing record to start from: no GET ' + listTemplate + ' lists those records');
921
+ const json = (b) => (a.encoding === 'form' ? { form: b } : { json: b });
922
+ // the records to start from: the singleton itself, or the first few of the list that have an id
923
+ let rows;
924
+ if (singleton) {
925
+ const r = await session.send('GET', a.template, {});
926
+ rows = r.status === 200 && r.json !== undefined ? [{ at: a.template, read: r.json }] : [];
927
+ } else {
928
+ const r = await session.send('GET', listTemplate, {});
929
+ const list = rowsOfReply(r) || [];
930
+ 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 }));
931
+ }
932
+ 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'));
933
+ const detail = !singleton && getTemplates.has(a.template);
934
+ const readNow = async (row) => {
935
+ if (singleton || detail) {
936
+ const r = await session.send('GET', row.at, {});
937
+ return r.status === 200 && r.json !== undefined ? r.json : undefined;
938
+ }
939
+ const r = await session.send('GET', listTemplate, {});
940
+ const list = rowsOfReply(r) || [];
941
+ return list.find((x) => x && String(x.id) === String(row.id));
942
+ };
943
+ // the starting body: each field the route reads that the record shows a plain value for
944
+ const bodyOf = (rec, only) => {
945
+ const b = {};
946
+ for (const f of a.fields) {
947
+ if (wordsOf(f.name).some((w) => SECRET_FIELD.test(w)) || /^(id|uuid)$/i.test(f.name)) continue;
948
+ if (only && !(f.kinds.length || wordsOf(f.name).some((x) => REQUIRED_NAME.test(x)))) continue;
949
+ const cur = valueIn(rec, f.name);
950
+ if (cur !== undefined && (cur === null || typeof cur !== 'object')) b[f.name] = cur;
951
+ }
952
+ return b;
953
+ };
954
+ let row = null;
955
+ let base = null;
956
+ let refused = null;
957
+ for (const candidate of rows) {
958
+ const rec = candidate.read;
959
+ for (const only of [false, true]) {
960
+ const b = bodyOf(rec, only);
961
+ if (!Object.keys(b).length || (only && JSON.stringify(b) === JSON.stringify(bodyOf(rec, false)))) continue;
962
+ const first = await session.send(a.method, candidate.at, json(b));
963
+ const rp = authz.replyOf(first);
964
+ if (rp.kind === 'accepted' || rp.kind === 'redirect') { row = candidate; base = b; break; }
965
+ refused = first.status;
966
+ }
967
+ if (row) break;
968
+ }
969
+ 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');
970
+ const target = row.at;
971
+ 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');
972
+ const probes = updateProbesOf(a, base);
973
+ for (const pr of probes) {
974
+ const f = pr.field;
975
+ const orig = base[f.name];
976
+ const body = Object.assign({}, base, { [f.name]: pr.value });
977
+ const res = await session.send(a.method, target, json(body));
978
+ const reply = authz.replyOf(res);
979
+ if (reply.kind === 'no-answer' && await appDown(session, singleton ? a.template : listTemplate)) {
980
+ out.findings.push(downFinding(who.email, what, f, pr, res));
981
+ out.appDown = what + ' with ' + f.name + ' ' + pr.why;
982
+ 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 });
983
+ return 'down';
984
+ }
985
+ const took = reply.kind === 'accepted' || reply.kind === 'redirect';
986
+ const crashed = reply.kind === 'server-error';
987
+ const after = took || crashed ? await readNow(row) : undefined;
988
+ const now = after === undefined ? undefined : valueIn(after, f.name);
989
+ const emptied = pr.mode === 'blank' && (now === null || now === '');
990
+ const kept = took && (pr.mode === 'number' ? JSON.stringify(now) === JSON.stringify(pr.value) : storedAs(now, pr.value) || emptied);
991
+ const changed = (took || crashed) && !kept && now !== undefined && JSON.stringify(now) !== JSON.stringify(orig) && String(now) !== String(orig);
992
+ let verdict = 'turned away';
993
+ if (crashed) verdict = 'crashed';
994
+ else if (took && kept) verdict = 'kept';
995
+ else if (took && changed) verdict = 'changed';
996
+ else if (took) verdict = 'taken, not shown';
997
+ 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 });
998
+ // put back whatever may have moved: every taken try, and a crash that left something behind
999
+ let putBack = null;
1000
+ if (took || (crashed && changed)) {
1001
+ const back = await session.send(a.method, target, json(base));
1002
+ const again = await readNow(row);
1003
+ const was = again === undefined ? undefined : valueIn(again, f.name);
1004
+ putBack = was === undefined ? !(kept || changed) : String(was) === String(orig) || JSON.stringify(was) === JSON.stringify(orig);
1005
+ 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'));
1006
+ if (!putBack) {
1007
+ 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' });
1008
+ return 'stuck';
1009
+ }
1010
+ }
1011
+ const wrongKind = /^(object|array|string|number)$/.test(pr.mode);
1012
+ // 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
1013
+ const required = f.kinds.some((k) => /^(required)$/.test(k)) || wordsOf(f.name).some((x) => REQUIRED_NAME.test(x));
1014
+ const worth = verdict === 'crashed' || (verdict === 'kept' && pr.mode !== 'number' && !(pr.mode === 'null' && !required)) || (verdict === 'changed' && /^(object|array|string)$/.test(pr.mode));
1015
+ if (!worth) continue;
1016
+ const claimed = (pr.mode === 'blank' && f.kinds.some((k) => /^(required|length)$/.test(k))) || (wrongKind && f.kinds.includes('type'));
1017
+ const longText = pr.mode === 'type' && /characters long/.test(pr.why);
1018
+ const finding = {
1019
+ kind: verdict === 'crashed' ? 'invalid-crash' : 'invalid-kept', where: what, field: f.name, from: f.file + ':' + f.line,
1020
+ status: verdict === 'crashed' || claimed ? 'confirmed' : 'verification required',
1021
+ severity: verdict === 'crashed' || longText ? 'LOW' : 'MEDIUM',
1022
+ headline: verdict === 'crashed'
1023
+ ? what + ' crashed (' + res.status + ') when ' + f.name + ' was ' + pr.why + (f.status === map.STATUS.NOT ? ': nothing in the code checks that field.' : '.')
1024
+ : verdict === 'kept'
1025
+ ? what + ' took ' + f.name + ' ' + pr.why + ' and saved it' + (claimed ? ', though the code checks that field.' : ': nothing in the code stops it.')
1026
+ : what + ' took ' + f.name + ' ' + pr.why + ' and stored "' + authz.clip(JSON.stringify(now), 60) + '" in its place: nothing in the code stops it.',
1027
+ 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) + '.',
1028
+ 'Sent ' + a.method + ' ' + target + ' with the record\'s own values and only ' + f.name + ' ' + pr.why + ': answered ' + res.status + ' (' + reply.note + ').']
1029
+ .concat(verdict === 'crashed' ? [] : ['Read it again: ' + f.name + ' = ' + authz.clip(JSON.stringify(now), 60) + '.'])
1030
+ .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.']),
1031
+ };
1032
+ 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, '',
1033
+ '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));
1034
+ out.findings.push(finding);
1035
+ }
1036
+ say(' ' + what + ': ' + probes.length + ' bad values tried');
1037
+ return null;
1038
+ }
1039
+
817
1040
  const GHOST_ID = '999999999';
818
1041
 
819
1042
 
@@ -1249,7 +1472,8 @@ async function runCreates(root, options) {
1249
1472
  // --- a limit used up -------------------------------------------------------------
1250
1473
 
1251
1474
  // 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;
1475
+ // use_limit, seat_limit, limit_total: a limit too (round 32: a one-use credit, use_limit 1, redeemed twice at once)
1476
+ const LIMIT_FIELD = /^(capacity|max_\w+|limit|\w+_limit|limit_\w+|quota|stock|seats_total|total_seats)$/i;
1253
1477
  const COUNTER_FIELD = /^(sold|uses|used|used_count|booked|taken|reserved|issued|redeemed|count)$/i;
1254
1478
  // a field of the create that says how many: what a capacity is used up by
1255
1479
  const AMOUNT_FIELD = /^(quantity|qty|seats|tickets|units|count)$/i;
@@ -1269,6 +1493,86 @@ function limitPairOf(rec) {
1269
1493
  return null;
1270
1494
  }
1271
1495
 
1496
+ const REFUND_STEP = /refund/i;
1497
+ const PAID_STATE = /captur|paid|succeed|settled|complete/i;
1498
+ const MONEY_FIELD = /amount|cents|paise|total|price/i;
1499
+
1500
+ /**
1501
+ * A refund larger than what was paid: for a route that refunds one record (POST /payments/{id}/refunds), a record
1502
+ * whose list shows it paid, a control refund of 1 first (taken, the route works for this account and record), then
1503
+ * one of twice what was paid plus one. Taken = the finding; refused = held. Blind round 32 (Northstar): a refund
1504
+ * of 999999 was stored against a payment of 12500. { ran, why, tried, findings, left }.
1505
+ */
1506
+ async function runRefundCap(root, options) {
1507
+ const opts = options || {};
1508
+ const plan = planOf(root, opts);
1509
+ if (!plan.ran) return { ran: false, why: plan.why };
1510
+ const j = plan.journey;
1511
+ const steps = j.actions.filter((a) => a.method === 'POST' && a.params.length === 1 && REFUND_STEP.test(a.template) &&
1512
+ Object.keys(a.fullData || {}).some((k) => MONEY_FIELD.test(k)));
1513
+ if (!steps.length) return { ran: false, why: 'no route that refunds a record by an amount' };
1514
+ let origin;
1515
+ try {
1516
+ origin = authz.assertLocalTarget(opts.origin);
1517
+ } catch (err) {
1518
+ return { ran: false, why: 'the app must be on this computer (localhost or 127.0.0.1): ' + (opts.origin || 'no address was given') };
1519
+ }
1520
+ if (!j.signIn) return { ran: false, why: j.signInWhy };
1521
+ 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 });
1522
+ const out = { ran: true, tried: [], findings: [], left: [] };
1523
+ const sessions = [];
1524
+ for (const a of j.accounts.usable) {
1525
+ const s = authz.makeSession(ctx, 'account', a.email, a.password);
1526
+ const f = { [j.signIn.emailField]: a.email, [j.signIn.passwordField]: a.password };
1527
+ const r = authz.replyOf(await s.send(j.signIn.method, j.signIn.path, j.signIn.encoding === 'form' ? { form: f } : { json: f }));
1528
+ if (r.kind !== 'refused' && r.kind !== 'server-error' && r.kind !== 'no-answer') sessions.push(s);
1529
+ }
1530
+ if (!sessions.length) return { ran: false, why: 'no account with a password in .env could sign in' };
1531
+ for (const a of steps) {
1532
+ const what = a.method + ' ' + a.template;
1533
+ const field = Object.keys(a.fullData).find((k) => MONEY_FIELD.test(k));
1534
+ const listTemplate = '/' + a.segs.slice(0, a.params[0].index).join('/');
1535
+ let verdict = null;
1536
+ for (const s of sessions) {
1537
+ if (verdict) break;
1538
+ const list = await s.send('GET', listTemplate, {});
1539
+ const rows = rowsOfReply(list) || [];
1540
+ 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]))));
1541
+ for (const rec of paid.slice(0, 3)) {
1542
+ const amountKey = Object.keys(rec).find((k) => MONEY_FIELD.test(k) && Number(rec[k]) > 0);
1543
+ if (!amountKey) continue;
1544
+ const at = a.template.replace('{id}', encodeURIComponent(String(rec.id)));
1545
+ const send = (n) => s.send('POST', at, a.encoding === 'form' ? { form: Object.assign({}, a.fullData, { [field]: n }) } : { json: Object.assign({}, a.fullData, { [field]: n }) });
1546
+ const control = await send(1);
1547
+ const rc = authz.replyOf(control);
1548
+ if (rc.kind !== 'accepted' && rc.kind !== 'redirect') continue;
1549
+ const over = Number(rec[amountKey]) * 2 + 1;
1550
+ const big = await send(over);
1551
+ const rb = authz.replyOf(big);
1552
+ const took = rb.kind === 'accepted' || rb.kind === 'redirect';
1553
+ 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');
1554
+ verdict = { what, by: s.email, record: rec.id, paid: Number(rec[amountKey]), over, status: big.status, took };
1555
+ out.tried.push(verdict);
1556
+ if (took) {
1557
+ const echoed = JSON.stringify(big.json || '').indexOf(String(over)) !== -1;
1558
+ const finding = {
1559
+ kind: 'refund-over', where: what, from: a.from ? a.from.file + ':' + a.from.line : what, status: echoed ? 'confirmed' : 'verification required', severity: 'HIGH',
1560
+ 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',
1561
+ proof: ['As ' + s.email + ', a refund of 1 for record ' + rec.id + ' was taken (' + control.status + '): the route works for this account.',
1562
+ 'Then a refund of ' + over + ' (twice the ' + rec[amountKey] + ' paid, plus 1): answered ' + big.status + (echoed ? ', and the answer names that amount.' : '.')],
1563
+ };
1564
+ finding.fixPrompt = ['Kryptheon tried this on my running app:', '', finding.headline + '.', '', 'Where: ' + finding.from, '', finding.proof.join('\n'), '',
1565
+ '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');
1566
+ out.findings.push(finding);
1567
+ }
1568
+ break;
1569
+ }
1570
+ }
1571
+ if (!verdict) out.tried.push({ what, verdict: 'not tried', why: 'no paid record that a refund of 1 was taken for' });
1572
+ }
1573
+ return out;
1574
+ }
1575
+
1272
1576
  /**
1273
1577
  * Each create of a costly record (POST /api/orders) whose body names a record that carries a limit and its count
1274
1578
  * (ticket_type_id: capacity and sold; promo_code: max_uses and uses), found through the route named for it
@@ -1430,8 +1734,14 @@ async function runSteps(root, options) {
1430
1734
  }
1431
1735
  return o;
1432
1736
  };
1737
+ // What one step costs: a list read per account, two sends and every list read twice. Sized to the app
1738
+ // (blind round 32: 7 steps x 8 accounts x 25 lists went past a fixed 200), never past 3000.
1739
+ ctx.limit = Math.min(3000, Math.max(ctx.limit, ctx.requests + 20 + items.length * (sessions.length + 3 + 2 * lists.length)));
1433
1740
  const out = { ran: true, tried: [], findings: [], left: [] };
1434
- for (const item of items) {
1741
+ let i = 0;
1742
+ try {
1743
+ for (; i < items.length; i++) {
1744
+ const item = items[i];
1435
1745
  const what = normRoute(item.route);
1436
1746
  const a = j.actions.find((x) => x.method + ' ' + x.template === what);
1437
1747
  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 +1787,426 @@ async function runSteps(root, options) {
1477
1787
  '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
1788
  out.findings.push(finding);
1479
1789
  }
1790
+ } catch (err) {
1791
+ if (!authz.isRequestLimit(err)) throw err;
1792
+ // keep what was found; say which steps were never sent twice
1793
+ 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' });
1794
+ out.stoppedAt = err.limit;
1795
+ }
1796
+ return out;
1797
+ }
1798
+
1799
+ // --- one step on one record, sent twice at the same moment ------------------------
1800
+
1801
+ // A record that has already come to the end of its life: the step is not aimed at it while another record is open.
1802
+ 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;
1803
+ // A field that tells one row from another, or when it was made: never part of "the same row twice".
1804
+ 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)$/;
1805
+ // a number in `col = col - $1`, `col = GREATEST(col - $1, 0)`, or { decrement: n }
1806
+ const ARITHMETIC = [
1807
+ /\b([a-z_]\w*)\s*=\s*(?:\w+\.)?\1\s*([-+])\s*[\w$:(]/i,
1808
+ /\b([a-z_]\w*)\s*=\s*\w+\(\s*(?:\w+\.)?\1\s*([-+])\s*[\w$:(]/i,
1809
+ ];
1810
+
1811
+ /** The records of a read's answer (an array, or the first array in an object), or null. */
1812
+ function rowsOfReply(res) {
1813
+ if (res.status !== 200) return null;
1814
+ return Array.isArray(res.json) ? res.json : res.json && typeof res.json === 'object' ? Object.values(res.json).find(Array.isArray) || null : null;
1815
+ }
1816
+
1817
+ /** The code of a write the map found, from its first line to the end of the call (or twelve lines). */
1818
+ function sourceAround(root, d) {
1819
+ try {
1820
+ const lines = fs.readFileSync(path.join(root, d.file), 'utf8').split('\n').slice(Math.max(0, d.line - 1), d.line + 11).join('\n');
1821
+ const end = lines.search(/\)\s*;/);
1822
+ return end < 0 ? lines : lines.slice(0, end + 2);
1823
+ } catch (err) {
1824
+ return d.sql || '';
1825
+ }
1826
+ }
1827
+
1828
+ /**
1829
+ * What a route's writes do, read from the map: rows it adds, numbers it moves (a stock taken down, a count
1830
+ * put up) and a status it sets. null when it does none of these (a rename is not a step to send twice).
1831
+ */
1832
+ function stepEffectOf(root, route) {
1833
+ const eff = { adds: [], moves: [], sets: [] };
1834
+ for (const d of route.db || []) {
1835
+ if (d.op === 'INSERT' || d.op === 'UPSERT') eff.adds.push(d.table);
1836
+ if (d.op !== 'UPDATE' && d.op !== 'UPSERT') continue;
1837
+ const src = sourceAround(root, d);
1838
+ for (const re of ARITHMETIC) {
1839
+ const m = re.exec(src);
1840
+ if (m) eff.moves.push({ col: m[1].toLowerCase(), sign: m[2] });
1841
+ }
1842
+ const inc = /\b(increment|decrement)\s*:/.exec(src);
1843
+ if (inc) eff.moves.push({ col: null, sign: inc[1] === 'decrement' ? '-' : '+' });
1844
+ if (d.setsState) eff.sets.push(d.setsState.col + " = '" + d.setsState.value + "'");
1845
+ else if (/\b(status|state|stage)\b\s*[:=]/i.test(src)) eff.sets.push('a status');
1846
+ }
1847
+ return eff.adds.length || eff.moves.length || eff.sets.length ? eff : null;
1848
+ }
1849
+
1850
+ /** Every number in a body or record worth counting by: its own amounts, and those of lines inside it. */
1851
+ function amountsIn(value, skipKey) {
1852
+ const out = new Set();
1853
+ 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); };
1854
+ const walk = (o, depth) => {
1855
+ if (!o || typeof o !== 'object' || depth > 2) return;
1856
+ for (const k of Object.keys(o)) {
1857
+ const v = o[k];
1858
+ if (Array.isArray(v)) v.forEach((x) => walk(x, depth + 1));
1859
+ else if (v && typeof v === 'object') walk(v, depth + 1);
1860
+ else if (!ROW_IDENTITY.test(k) && !/(^|_)id$|Id$/.test(k) && k.toLowerCase() !== String(skipKey).toLowerCase()) take(v);
1861
+ }
1862
+ };
1863
+ walk(value, 0);
1864
+ return out;
1865
+ }
1866
+
1867
+ /** The numbers of the rows in some lists that belong to a record (its own row, or rows pointing at it): path -> { key, value }. */
1868
+ function numbersOfRecord(snap, listTemplate, rec) {
1869
+ const out = new Map();
1870
+ const pointsAt = (row, list) => Object.keys(row).some((k) => k !== 'id' && pointerKey(k, listTemplate) && String(row[k]) === String(rec.id));
1871
+ for (const l of Object.keys(snap)) {
1872
+ const json = snap[l];
1873
+ const wrapper = Array.isArray(json) ? '' : json && typeof json === 'object' ? Object.keys(json).find((k) => Array.isArray(json[k])) : undefined;
1874
+ const rows = Array.isArray(json) ? json : wrapper ? json[wrapper] : [];
1875
+ for (const row of rows) {
1876
+ if (!row || typeof row !== 'object' || row.id === undefined) continue;
1877
+ if (!((l === listTemplate && String(row.id) === String(rec.id)) || pointsAt(row, l))) continue;
1878
+ 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] });
1879
+ }
1880
+ }
1881
+ return out;
1882
+ }
1883
+
1884
+ /** 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. */
1885
+ function pointerKey(key, listTemplate) {
1886
+ const flat = (w) => String(w).toLowerCase().replace(/[^a-z]/g, '');
1887
+ const stem = flat((listTemplate.split('/').pop() || '').replace(/ies$/, 'y').replace(/s$/, ''));
1888
+ return /(^|_)id$|[a-z]Id$|(^|_)ref$/.test(key) || (stem && flat(key) === stem);
1889
+ }
1890
+
1891
+ /**
1892
+ * What two copies of one step did that one copy would not have: rows made twice, a number that was not
1893
+ * negative and is, a count past its limit, a number of the record moved by twice what one step moves.
1894
+ * [{ kind, sure, text }] - empty when the lists show one effect, or none.
1895
+ */
1896
+ function doubleEffects(before, after, rec, listTemplate, body, stepTemplate) {
1897
+ const out = [];
1898
+ // A step that adds to a list of the record (/assets/{id}/reservations, /orders/{id}/items) makes a new
1899
+ // thing each time it is sent: two of them are two reservations, not one done twice. Only a number pushed
1900
+ // below zero or past its limit is a double effect there. (Harness, rounds 22: two reservations of 1 took
1901
+ // a stock of 2 to 0 and were called a double effect.)
1902
+ const last = stepTemplate ? String(stepTemplate).split('/').filter((s) => s && s !== '{id}').pop() || '' : '';
1903
+ const addsToList = /s$/i.test(last) && !STEP_VERB.test(last);
1904
+ // 1. two new rows tied to the record that say the same thing (two transfers of one payout) - not rows of
1905
+ // a log: each request writes its own audit or activity line (harness, rounds 27-28)
1906
+ for (const l of addsToList ? [] : Object.keys(after)) {
1907
+ if (HISTORY_LIST.test(String(l).split('/').filter(Boolean).pop() || '')) continue;
1908
+ const had = new Set((rowsOfReply({ status: 200, json: before[l] }) || []).map((x) => x && JSON.stringify(x.id)));
1909
+ const fresh = (rowsOfReply({ status: 200, json: after[l] }) || []).filter((x) => x && typeof x === 'object' && x.id !== undefined && !had.has(JSON.stringify(x.id)));
1910
+ const tied = fresh.filter((x) => Object.keys(x).some((k) => k !== 'id' && pointerKey(k, listTemplate) && String(x[k]) === String(rec.id)));
1911
+ if (tied.length < 2) continue;
1912
+ const keys = Object.keys(tied[0]).filter((k) => !ROW_IDENTITY.test(k));
1913
+ 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;
1914
+ 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(', ') + ')' });
1915
+ }
1916
+ // 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)
1917
+ const was = numbersOf(before);
1918
+ for (const [at, v] of numbersOf(after)) {
1919
+ if (!(was.has(at) && was.get(at) >= 0 && v < 0)) continue;
1920
+ if (String(at).split('.').pop().split('_').some((w) => CHANGE_WORDS.has(w.toLowerCase()))) continue;
1921
+ out.push({ kind: 'below-zero', sure: true, text: at + ' went ' + was.get(at) + ' -> ' + v + ', below zero' });
1922
+ }
1923
+ out.push(...pastLimit(before, after));
1924
+ // 3. a number of the record, or of a row pointing at it, moved by twice what one step moves
1925
+ const b = numbersOfRecord(before, listTemplate, rec);
1926
+ for (const [at, now] of addsToList ? [] : numbersOfRecord(after, listTemplate, rec)) {
1927
+ if (!b.has(at) || ROW_IDENTITY.test(now.key)) continue;
1928
+ const d = Math.abs(now.value - b.get(at).value);
1929
+ if (!d) continue;
1930
+ const units = new Set([...amountsIn(body, now.key), ...amountsIn(rec, now.key)]);
1931
+ // the unit has to be the record's own (a payout's amount paid twice): a unit that only the request names is
1932
+ // the change it asked for - two stock adjustments of +1 are +2 (round 32: PATCH /catalog/{id}/stock {amount})
1933
+ const recUnits = amountsIn(rec, now.key);
1934
+ const bodyUnits = amountsIn(body, now.key);
1935
+ const asked = (u) => bodyUnits.has(u) && !recUnits.has(u);
1936
+ if ([...units].some((u) => Math.abs(2 * u - d) < 1e-9 && !asked(u)) && !units.has(d)) {
1937
+ 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' });
1938
+ } else if (d === 2 && COUNTER_FIELD.test(now.key) && !units.has(2)) {
1939
+ 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' });
1940
+ }
1941
+ }
1942
+ return out;
1943
+ }
1944
+
1945
+ // Lists that write a line per request (two lines for two requests is the log doing its job).
1946
+ const HISTORY_LIST = /^(audit|audits|audit[-_]?logs?|logs?|activity|activities|activity[-_]?logs?|history|histories|timeline|notifications?|events[-_]?log)$/i;
1947
+ // A last path word that is a step done to the record, though it ends in "s" (process, access, address, pass, status).
1948
+ const STEP_VERB = /^(process|access|address|pass|bypass|status|progress|dismiss|redress|reassess|assess)$/i;
1949
+
1950
+ /** Counts that stand above their limit after and did not before: [{ kind, sure, text }]. */
1951
+ function pastLimit(before, after) {
1952
+ const out = [];
1953
+ const rowsAt = (snap, l) => rowsOfReply({ status: 200, json: snap[l] }) || [];
1954
+ for (const l of Object.keys(after)) {
1955
+ for (const row of rowsAt(after, l)) {
1956
+ if (!row || typeof row !== 'object') continue;
1957
+ const keys = Object.keys(row);
1958
+ for (const lim of keys.filter((k) => LIMIT_FIELD.test(k))) {
1959
+ for (const c of keys.filter((k) => COUNTER_FIELD.test(k))) {
1960
+ const L = Number(row[lim]);
1961
+ const C = Number(row[c]);
1962
+ if (!(Number.isFinite(L) && Number.isFinite(C) && L > 0 && C > L)) continue;
1963
+ const old = row.id === undefined ? null : rowsAt(before, l).find((x) => x && JSON.stringify(x.id) === JSON.stringify(row.id));
1964
+ if (old && Number(old[c]) > Number(old[lim])) continue;
1965
+ 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' });
1966
+ }
1967
+ }
1968
+ }
1969
+ }
1970
+ return out;
1971
+ }
1972
+
1973
+ const CLAIM_QTY = /^(quantity|qty|units|seats|count|amount_units)$/i;
1974
+
1975
+ /**
1976
+ * Two new records that both claim the thing with the fewest units left, for a step that takes a number down:
1977
+ * the column the step decrements is found in another list (stock_on_hand in /api/catalog), its smallest positive
1978
+ * value picked, and the create route of the step's own records (POST /api/orders) sent twice with the field that
1979
+ * names a row of that list set to it, and a quantity field (if any) set to all the units left. Each fits alone;
1980
+ * both together do not. { ids, label } or null when there is no such create, list or field.
1981
+ */
1982
+ async function scarceClaim(s, c, j, lists, snap, taken) {
1983
+ const cols = c.effect.moves.filter((m) => m.sign === '-' && m.col).map((m) => m.col);
1984
+ if (!cols.length) return null;
1985
+ const create = j.actions.find((x) => x.method === 'POST' && x.template === c.listTemplate && !x.params.length);
1986
+ if (!create || !create.fullData) return null;
1987
+ const now = await snap(s);
1988
+ for (const l of lists) {
1989
+ if (l === c.listTemplate) continue;
1990
+ const rows = (rowsOfReply({ status: 200, json: now[l] }) || []).filter((r) => r && r.id !== undefined);
1991
+ if (!rows.length) continue;
1992
+ const col = cols.find((k) => rows[0][k] !== undefined);
1993
+ if (!col) continue;
1994
+ const ids = new Set(rows.map((r) => String(r.id)));
1995
+ // the field of the create body that names a row of this list: its value is one of that list's ids
1996
+ const pointer = Object.keys(create.fullData).find((k) => /(^|_)id$|Id$/.test(k) && ids.has(String(create.fullData[k])));
1997
+ if (!pointer) continue;
1998
+ const scarce = rows.filter((r) => Number(r[col]) > 0).sort((x, y) => Number(x[col]) - Number(y[col]))[0];
1999
+ if (!scarce) continue;
2000
+ const units = Number(scarce[col]);
2001
+ const qty = Object.keys(create.fullData).find((k) => CLAIM_QTY.test(k));
2002
+ if (!qty && units > 1) continue; // one unit per record and more than one left: two records cannot overdraw it
2003
+ const body = Object.assign({}, create.fullData, { [pointer]: scarce.id }, qty ? { [qty]: units } : {});
2004
+ const made = [];
2005
+ for (let n = 0; n < 2; n++) {
2006
+ const before = rowsOfReply(await s.send('GET', c.listTemplate, {})) || [];
2007
+ const res = await s.send('POST', c.listTemplate, create.encoding === 'form' ? { form: body } : { json: body });
2008
+ if (!taken(res)) return null;
2009
+ const after = rowsOfReply(await s.send('GET', c.listTemplate, {})) || [];
2010
+ const had = new Set(before.map((r) => r && String(r.id)));
2011
+ const fresh = after.find((r) => r && r.id !== undefined && !had.has(String(r.id)));
2012
+ 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);
2013
+ if (id === undefined || id === null) return null;
2014
+ made.push(id);
2015
+ }
2016
+ return { ids: made, label: l.split('/').pop() + ' ' + scarce.id + ' (' + col + ' ' + units + ')' };
2017
+ }
2018
+ return null;
2019
+ }
2020
+
2021
+ /**
2022
+ * The map's steps on one record (POST|PUT|PATCH /things/{id}/verb that change a status, money or a count),
2023
+ * sent twice at the same moment for ONE record, as a double click or two tabs do; and, where the step takes a
2024
+ * number down, for TWO records that compete for the same stock. Judged from the app's own lists before and
2025
+ * after: both answers taken AND the effect twice (two new rows tied to the record, a number moved by twice what
2026
+ * one step moves, a number below zero, a count past its limit) = a finding. One taken and one refused, or both
2027
+ * taken with one effect, = held. Nothing is deleted; every record sent is listed as left. Round 33.
2028
+ * { ran, why, tried, findings, left, stoppedAt }.
2029
+ */
2030
+ async function runStepsAtOnce(root, options) {
2031
+ const opts = options || {};
2032
+ const plan = planOf(root, opts);
2033
+ if (!plan.ran) return { ran: false, why: plan.why };
2034
+ const j = plan.journey;
2035
+ const lists = j.actions.filter((x) => x.method === 'GET' && !x.params.length).map((x) => x.template);
2036
+ const candidates = [];
2037
+ // /orders/{id}/confirm is a step on a record, though a bare "confirm" address is sign-up plumbing for the other checks
2038
+ const known = allowedByName(plan.map);
2039
+ 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))
2040
+ .map((r) => actionOf(r, known, null)).filter((a) => !a.leftOut.length && !j.actions.some((x) => x.method === a.method && x.template === a.template));
2041
+ for (const a of j.actions.concat(confirms)) {
2042
+ 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;
2043
+ const route = (plan.map.routes || []).find((r) => !r.unmounted && r.method === a.method && normRoute(r.path || '') === a.template);
2044
+ const effect = route && stepEffectOf(root, route);
2045
+ const listTemplate = '/' + a.segs.slice(0, a.params[0].index).join('/');
2046
+ if (effect && lists.includes(listTemplate)) candidates.push({ a, effect, listTemplate, route });
2047
+ }
2048
+ 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' };
2049
+ let origin;
2050
+ try {
2051
+ origin = authz.assertLocalTarget(opts.origin);
2052
+ } catch (err) {
2053
+ return { ran: false, why: 'the app must be on this computer (localhost or 127.0.0.1): ' + (opts.origin || 'no address was given') };
2054
+ }
2055
+ if (!j.signIn) return { ran: false, why: j.signInWhy };
2056
+ 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 });
2057
+ const sessions = [];
2058
+ for (const a of j.accounts.usable) {
2059
+ const s = authz.makeSession(ctx, 'account', a.email, a.password);
2060
+ const f = { [j.signIn.emailField]: a.email, [j.signIn.passwordField]: a.password };
2061
+ const r = authz.replyOf(await s.send(j.signIn.method, j.signIn.path, j.signIn.encoding === 'form' ? { form: f } : { json: f }));
2062
+ if (r.kind !== 'refused' && r.kind !== 'server-error' && r.kind !== 'no-answer') sessions.push(s);
2063
+ }
2064
+ if (!sessions.length) return { ran: false, why: 'no account with a password in .env could sign in' };
2065
+ const snap = async (s) => {
2066
+ const o = {};
2067
+ for (const t of lists) {
2068
+ const r = await s.send('GET', t, {});
2069
+ if (r.status === 200 && r.json !== undefined) o[t] = r.json;
2070
+ }
2071
+ return o;
2072
+ };
2073
+ const taken = (res) => { const r = authz.replyOf(res); return r.kind === 'accepted' || r.kind === 'redirect'; };
2074
+ // What one step costs here: per account a list and a read of every list, two sends and another read; and the same
2075
+ // again for two records. Sized to the app, never past 3000 (the limit of the whole run is kept: ctx.limit only goes up).
2076
+ 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))));
2077
+ const out = { ran: true, tried: [], findings: [], left: [] };
2078
+ const headFor = (c) => c.a.method + ' ' + c.a.template;
2079
+ const finish = (c, what, s, atText, answers, effects, mode, body) => {
2080
+ const sure = effects.some((e) => e.sure);
2081
+ const finding = {
2082
+ 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',
2083
+ headline: mode === 'compete'
2084
+ ? '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.'
2085
+ : 'Sent twice at the same moment for one record, ' + what + ' was taken both times and its effect happened twice: ' + effects[0].text + '.',
2086
+ proof: ['As ' + s.email + ', read ' + (mode === 'compete' ? 'the lists' : 'GET ' + c.listTemplate + ' and the other lists') + ' before.',
2087
+ '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 ') + '.']
2088
+ .concat(effects.slice(0, 3).map((e) => 'Read the lists again: ' + e.text + '.')),
2089
+ };
2090
+ 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, '',
2091
+ '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));
2092
+ out.findings.push(finding);
2093
+ };
2094
+ let i = 0;
2095
+ try {
2096
+ for (; i < candidates.length; i++) {
2097
+ const c = candidates[i];
2098
+ const a = c.a;
2099
+ const what = headFor(c);
2100
+ const t = { what, verdict: 'no account was let in' };
2101
+ out.tried.push(t);
2102
+ const route = c.route;
2103
+ const shared = c.effect.moves.some((m) => m.sign === '-' || (m.col && COUNTER_FIELD.test(m.col)));
2104
+ const sets = (route.db || []).find((d) => d.setsState);
2105
+ const target = sets ? sets.setsState.value : null;
2106
+ const bodyOf = () => (a.encoding === 'form' ? { form: a.fullData || {} } : { json: a.fullData || {} });
2107
+ const atOf = (rec) => a.template.replace('{id}', encodeURIComponent(String(rec.id)));
2108
+ const open = (recs, statusKey) => recs.filter((x) => x && x.id !== undefined && !(statusKey && (ENDED_STATUS.test(String(x[statusKey])) || (target && String(x[statusKey]) === target))));
2109
+ for (const s of sessions) {
2110
+ const recs = rowsOfReply(await s.send('GET', c.listTemplate, {}));
2111
+ const usable = (recs || []).filter((x) => x && typeof x === 'object' && x.id !== undefined);
2112
+ if (!usable.length) { t.verdict = 'no record at GET ' + c.listTemplate + ' to aim it at'; continue; }
2113
+ const statusKey = Object.keys(usable[0]).find((k) => /^(status|state|stage)$/i.test(k));
2114
+ const fresh = open(usable, statusKey);
2115
+ const rec = fresh[0] || usable[0];
2116
+ const at = atOf(rec);
2117
+ const before = await snap(s);
2118
+ const [r1, r2] = await Promise.all([s.send(a.method, at, bodyOf()), s.send(a.method, at, bodyOf())]);
2119
+ const answers = [r1.status, r2.status];
2120
+ const both = taken(r1) && taken(r2);
2121
+ t.by = s.email;
2122
+ t.record = rec.id;
2123
+ t.answers = answers;
2124
+ if (!taken(r1) && !taken(r2)) {
2125
+ t.verdict = 'refused both (' + answers.join(' and ') + ')';
2126
+ // not this account's to change: the next one may be; any other refusal is about the step
2127
+ if ([401, 403, 404].includes(r1.status) && [401, 403, 404].includes(r2.status)) continue;
2128
+ break;
2129
+ }
2130
+ const after = await snap(s);
2131
+ const effects = both ? doubleEffects(before, after, rec, c.listTemplate, a.fullData || {}, a.template) : [];
2132
+ t.verdict = effects.length ? 'done twice' : both ? 'held: both taken, one effect' : 'held: one taken, one refused';
2133
+ 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'));
2134
+ if (effects.length) finish(c, what, s, a.method + ' ' + at, answers, effects, 'once', a.fullData);
2135
+ // two records for the same stock: only where the step takes a number down
2136
+ if (shared) {
2137
+ // Best: two NEW records that both claim the thing with the fewest units left, each for all of them, then
2138
+ // the step for both at once (blind round 32: one seeded order per product, so two existing orders never
2139
+ // competed for the same last unit, and C24 - stock below zero - was missed).
2140
+ const scarce = await scarceClaim(s, c, j, lists, snap, taken);
2141
+ if (scarce) {
2142
+ const tc = { what, mode: 'compete', claimed: scarce.label, verdict: 'not tried' };
2143
+ out.tried.push(tc);
2144
+ const was = await snap(s);
2145
+ const [q1, q2] = await Promise.all(scarce.ids.map((id) => s.send(a.method, atOf({ id }), bodyOf())));
2146
+ tc.records = scarce.ids;
2147
+ tc.answers = [q1.status, q2.status];
2148
+ 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 + ')');
2149
+ for (const id of scarce.ids) out.left.push(c.listTemplate + ' record ' + id + ' made by Kryptheon to claim ' + scarce.label + ' - never deleted');
2150
+ if (taken(q1) && taken(q2)) {
2151
+ const now = await snap(s);
2152
+ const was0 = numbersOf(was);
2153
+ const effects2 = [];
2154
+ for (const [at2, v] of numbersOf(now)) {
2155
+ if (!(was0.has(at2) && was0.get(at2) >= 0 && v < 0)) continue;
2156
+ if (String(at2).split('.').pop().split('_').some((w) => CHANGE_WORDS.has(w.toLowerCase()))) continue;
2157
+ effects2.push({ kind: 'below-zero', sure: true, text: at2 + ' went ' + was0.get(at2) + ' -> ' + v + ', below zero' });
2158
+ }
2159
+ effects2.push(...pastLimit(was, now));
2160
+ tc.verdict = effects2.length ? 'went past what there was' : 'held: both taken, the shared number stayed within its limit';
2161
+ 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);
2162
+ break;
2163
+ }
2164
+ tc.verdict = 'held: ' + (taken(q1) || taken(q2) ? 'one taken, one refused' : 'both refused') + ' (records made to claim ' + scarce.label + ')';
2165
+ if (taken(q1) || taken(q2)) break;
2166
+ }
2167
+ // otherwise two open records already there: not ones Kryptheon made, and the next pair when one is refused as not found
2168
+ const others = open(usable, statusKey).filter((x) => String(x.id) !== String(rec.id) && !/Kryptheon test|kx-/i.test(JSON.stringify(x))).slice(0, 8);
2169
+ const tc = { what, mode: 'compete', verdict: 'not tried: fewer than two other open records to compete' };
2170
+ out.tried.push(tc);
2171
+ if (others.length < 2) break;
2172
+ // two records that name the same thing (the same product, the same slot) compete best
2173
+ let pair = [others[0], others[1]];
2174
+ search: for (let x = 0; x < others.length; x++) {
2175
+ for (let y = x + 1; y < others.length; y++) {
2176
+ 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; }
2177
+ }
2178
+ }
2179
+ const was = await snap(s);
2180
+ const [q1, q2] = await Promise.all(pair.map((x) => s.send(a.method, atOf(x), bodyOf())));
2181
+ tc.records = pair.map((x) => x.id);
2182
+ tc.answers = [q1.status, q2.status];
2183
+ 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 + ')');
2184
+ if (!(taken(q1) && taken(q2))) { tc.verdict = 'held: ' + (taken(q1) || taken(q2) ? 'one taken, one refused' : 'both refused'); break; }
2185
+ const now = await snap(s);
2186
+ const was0 = numbersOf(was);
2187
+ const effects2 = [];
2188
+ for (const [at2, v] of numbersOf(now)) {
2189
+ if (!(was0.has(at2) && was0.get(at2) >= 0 && v < 0)) continue;
2190
+ if (String(at2).split('.').pop().split('_').some((w) => CHANGE_WORDS.has(w.toLowerCase()))) continue;
2191
+ effects2.push({ kind: 'below-zero', sure: true, text: at2 + ' went ' + was0.get(at2) + ' -> ' + v + ', below zero' });
2192
+ }
2193
+ effects2.push(...pastLimit(was, now));
2194
+ tc.verdict = effects2.length ? 'went past what there was' : 'held: both taken, the shared number stayed within its limit';
2195
+ if (effects2.length) finish(c, what, s, a.method + ' ' + atOf(pair[0]) + ' and ' + atOf(pair[1]), [q1.status, q2.status], effects2, 'compete', a.fullData);
2196
+ }
2197
+ break;
2198
+ }
2199
+ }
2200
+ } catch (err) {
2201
+ if (!authz.isRequestLimit(err)) throw err;
2202
+ // keep what was found; say which steps were never sent twice
2203
+ for (const c of candidates.slice(i)) {
2204
+ const mine = out.tried.find((x) => x.what === headFor(c) && !x.mode);
2205
+ if (mine && mine.verdict !== 'no account was let in') continue;
2206
+ 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' });
2207
+ }
2208
+ out.stoppedAt = err.limit;
2209
+ }
1480
2210
  return out;
1481
2211
  }
1482
2212
 
@@ -1615,7 +2345,72 @@ function reconcile(toCheck, verdicts) {
1615
2345
  return { shown, held };
1616
2346
  }
1617
2347
 
1618
- function verdictsFor(toCheck, wall, invalid, roleRun, forged, webhooks, steps) {
2348
+ // An address on no real site: nothing is ever sent to it, it is only read back from the app's answer.
2349
+ const OUTSIDE = 'https://kryptheon.invalid/landing';
2350
+
2351
+ /**
2352
+ * The map's open redirects (an address the request names, sent back as the place to go), tried on the
2353
+ * running app: the address is set to an outside one, and the answer's Location - or a redirect field of a
2354
+ * JSON answer - is read. Pointing there = confirmed; anywhere else = held. The sign-in route is tried with
2355
+ * an account's own sign-in, as a person clicking a link would. Blind round 32 (Northstar). { ran, why, tried, findings }.
2356
+ */
2357
+ async function runRedirects(root, options) {
2358
+ const opts = options || {};
2359
+ const plan = planOf(root, opts);
2360
+ if (!plan.ran) return { ran: false, why: plan.why };
2361
+ const j = plan.journey;
2362
+ const items = (opts.toCheck || map.toCheck(plan.map)).filter((t) => t.kind === 'open-redirect');
2363
+ if (!items.length) return { ran: false, why: 'the map found no redirect to an address the request names' };
2364
+ let origin;
2365
+ try {
2366
+ origin = authz.assertLocalTarget(opts.origin);
2367
+ } catch (err) {
2368
+ return { ran: false, why: 'the app must be on this computer (localhost or 127.0.0.1): ' + (opts.origin || 'no address was given') };
2369
+ }
2370
+ const account = j.accounts.usable[0];
2371
+ const ctx = authz.makeContext({ baseUrl: origin, allowOrigins: [origin], fetch: opts.fetch, passwords: account ? { [account.email]: account.password } : {}, maxRequests: 20 + items.length * 3 });
2372
+ const out = { ran: true, tried: [], findings: [] };
2373
+ const signInBody = () => (account && j.signIn ? { [j.signIn.emailField]: account.email, [j.signIn.passwordField]: account.password } : null);
2374
+ for (const item of items) {
2375
+ const what = normRoute(item.route);
2376
+ const [method, path] = what.split(' ');
2377
+ const route = (plan.map.routes || []).find((r) => r.method + ' ' + normRoute(r.path || '') === what);
2378
+ const rd = route && (route.redirects || [])[0];
2379
+ if (!rd || /\{/.test(path || '') || !/^(query|body)$/.test(rd.from)) {
2380
+ 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' });
2381
+ continue;
2382
+ }
2383
+ const isSignIn = j.signIn && j.signIn.method === method && normRoute(j.signIn.path) === path;
2384
+ const s = authz.makeSession(ctx, 'account', account ? account.email : 'nobody', account ? account.password : '');
2385
+ if (!isSignIn && account && j.signIn) {
2386
+ const f = signInBody();
2387
+ await s.send(j.signIn.method, j.signIn.path, j.signIn.encoding === 'form' ? { form: f } : { json: f });
2388
+ }
2389
+ const body = Object.assign({}, isSignIn ? signInBody() : {}, rd.from === 'body' ? { [rd.field]: OUTSIDE } : {});
2390
+ const at = path + (rd.from === 'query' ? '?' + encodeURIComponent(rd.field) + '=' + encodeURIComponent(OUTSIDE) : '');
2391
+ const sendOpts = method === 'GET' || method === 'HEAD' ? {} : (isSignIn && j.signIn.encoding === 'form') || (route && route.validation && /form/.test(String(route.encoding || ''))) ? { form: body } : { json: body };
2392
+ const res = await s.send(method, at, sendOpts);
2393
+ const loc = String(res.location || '');
2394
+ const inJson = res.json && typeof res.json === 'object' && Object.values(res.json).some((v) => typeof v === 'string' && v.indexOf('kryptheon.invalid') !== -1);
2395
+ const away = (res.status >= 300 && res.status < 400 && /^https?:\/\/kryptheon\.invalid/i.test(loc)) || inJson;
2396
+ out.tried.push({ what, field: rd.from + '.' + rd.field, status: res.status, location: loc, verdict: away ? 'sent away' : 'held' });
2397
+ if (!away) continue;
2398
+ const finding = {
2399
+ kind: 'open-redirect', where: what, from: item.where, status: 'confirmed', severity: 'MEDIUM',
2400
+ 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',
2401
+ proof: [
2402
+ (isSignIn ? 'Signed in as ' + account.email + ' through ' : 'As ' + (account ? account.email : 'nobody') + ', sent ') + method + ' ' + path + ' with ' + rd.from + '.' + rd.field + ' = ' + OUTSIDE + '.',
2403
+ inJson ? 'The answer (' + res.status + ') named that address as the place to go.' : 'The answer was ' + res.status + ' with Location: ' + authz.clip(loc, 120) + '.',
2404
+ ],
2405
+ };
2406
+ finding.fixPrompt = ['Kryptheon tried this on my running app:', '', finding.headline + '.', '', 'Where: ' + finding.from, '', finding.proof.join('\n'), '',
2407
+ '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');
2408
+ out.findings.push(finding);
2409
+ }
2410
+ return out;
2411
+ }
2412
+
2413
+ function verdictsFor(toCheck, wall, invalid, roleRun, forged, webhooks, steps, redirects) {
1619
2414
  const out = [];
1620
2415
  const attempts = wall && wall.ran ? wall.result.attempts : [];
1621
2416
  const failures = (wall && wall.findings) || [];
@@ -1625,7 +2420,20 @@ function verdictsFor(toCheck, wall, invalid, roleRun, forged, webhooks, steps) {
1625
2420
  const route = normRoute(t.route);
1626
2421
  const v = { route: t.route, kind: t.kind, headline: t.headline };
1627
2422
  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') {
2423
+ if (t.kind === 'open-redirect') {
2424
+ const hit = redirects && redirects.ran ? redirects.findings.find((f) => f.where === route) : null;
2425
+ const tried = redirects && redirects.ran ? redirects.tried.find((x) => x.what === route) : null;
2426
+ if (hit) {
2427
+ v.verdict = 'confirmed';
2428
+ v.finding = hit.headline;
2429
+ } else if (tried && tried.verdict === 'held') {
2430
+ v.verdict = 'held';
2431
+ v.strength = 'strong';
2432
+ } else {
2433
+ v.verdict = 'not tried';
2434
+ v.why = tried ? tried.why || tried.verdict : redirects && !redirects.ran ? redirects.why : 'the redirect was not tried';
2435
+ }
2436
+ } else if (t.kind === 'no-state-check') {
1629
2437
  const hit = steps && steps.ran ? steps.findings.find((f) => f.where === route) : null;
1630
2438
  const tried = steps && steps.ran ? steps.tried.find((x) => x.what === route) : null;
1631
2439
  if (hit) {
@@ -1662,14 +2470,20 @@ function verdictsFor(toCheck, wall, invalid, roleRun, forged, webhooks, steps) {
1662
2470
  }
1663
2471
  } else if (kind === 'role') {
1664
2472
  // "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') : [];
2473
+ // a try of a change no rule names (as the fewest permissions) is never "held" nor "confirmed": no rule says who may
2474
+ const tries = roleRun && roleRun.ran ? roleRun.result.attempts.filter((a) => !a.notTried && a.kind === 'role' && !a.unruled && a.method + ' ' + a.template === route) : [];
2475
+ const rf = roleRun && roleRun.ran ? roleRun.findings.filter((f) => f.where === route && f.kind !== 'tenant' && f.kind !== 'signin' && !f.unruled) : [];
2476
+ const unruledTries = roleRun && roleRun.ran ? roleRun.result.attempts.filter((a) => !a.notTried && a.unruled && a.method + ' ' + a.template === route) : [];
2477
+ const unruledFinding = roleRun && roleRun.ran ? roleRun.findings.find((f) => f.where === route && f.unruled) : null;
1667
2478
  if (!roleRun || !roleRun.ran) {
1668
2479
  v.verdict = 'not tried';
1669
2480
  v.why = 'it needs the roles run (--roles) and your permissions-policy.json';
1670
2481
  } else if (rf.length) {
1671
2482
  v.verdict = 'confirmed';
1672
2483
  v.finding = rf[0].headline;
2484
+ } else if (!tries.length && unruledTries.length) {
2485
+ v.verdict = 'not judged';
2486
+ 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
2487
  } else if (tries.length && tries.every((a) => a.verdict === V.DENIED || a.verdict === V.ALLOWED)) {
1674
2488
  v.verdict = 'held';
1675
2489
  v.strength = heldStrength(tries);
@@ -1807,33 +2621,71 @@ async function runApi(root, options) {
1807
2621
  const shared = Object.assign({}, opts, { map: m, say: say });
1808
2622
  // jobs first, on the app as it was seeded: the tries after them cancel and refund the records a job works on
1809
2623
  // (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 }));
2624
+ // A check that reaches its request limit stops there and says so; the others still run and report.
2625
+ // Blind round 32: one check's limit threw out of here, and the person got a stack trace and no report.
2626
+ const contained = async (label, run) => {
2627
+ try {
2628
+ return await run();
2629
+ } catch (err) {
2630
+ if (!authz.isRequestLimit(err)) throw err;
2631
+ say(' ' + label + ': stopped at its limit of ' + err.limit + ' requests');
2632
+ 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' };
2633
+ }
2634
+ };
2635
+ const jobsRun = await contained('Jobs run twice', () => runJobs(root, shared));
2636
+ const wall = await contained('Across organizations and without a session', () => runWall(root, shared));
2637
+ const forged = await contained('Who is calling, as the request says', () => runForged(root, shared));
2638
+ const webhooks = await contained('Webhooks', () => webhook.run(root, shared));
2639
+ const exportsRun = await contained('Exports', () => csv.run(root, shared));
2640
+ const keptRun = await contained('Changes kept', () => runKept(root, shared));
2641
+ const createsRun = await contained('Creates sent twice at once', () => runCreates(root, shared));
2642
+ const limitsRun = await contained('Limits', () => runLimits(root, shared));
2643
+ const stepsRun = await contained('Steps sent twice', () => runSteps(root, Object.assign({}, shared, { toCheck: toCheck })));
2644
+ const refundsRun = await contained('Refunds above what was paid', () => runRefundCap(root, shared));
2645
+ const redirectsRun = await contained('Redirects to an address the request names', () => runRedirects(root, Object.assign({}, shared, { toCheck: toCheck })));
2646
+ const onceRun = await contained('Steps sent twice at once', () => runStepsAtOnce(root, shared));
1819
2647
  let roleRun = null;
1820
- if (opts.roles) roleRun = await runRoles(root, shared);
2648
+ if (opts.roles) roleRun = await contained('By role', () => runRoles(root, shared));
1821
2649
  // 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);
2650
+ const invalid = await contained('Bad values', () => runInvalid(root, shared));
2651
+ const verdicts = verdictsFor(toCheck, wall, invalid, roleRun, forged, webhooks, stepsRun, redirectsRun);
1824
2652
  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) => {
2653
+ 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
2654
  const k = f.kind + '|' + f.where + '|' + (f.field || '');
1827
2655
  return seen.has(k) ? false : (seen.add(k), true);
1828
2656
  });
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) : []);
2657
+ 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
2658
  return {
1831
2659
  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,
2660
+ 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
2661
  notTried: (wall.plan ? wall.plan.notTried : []).concat(invalid.ran ? invalid.notTried : []),
2662
+ writes: writesOf(m, wall),
1834
2663
  };
1835
2664
  }
1836
2665
 
2666
+ /**
2667
+ * Every route that writes a table, the tables it writes, and whether a try across organizations reached it - so a
2668
+ * database finding that is "to verify" (a row naming another organization's record) can say which routes were and
2669
+ * were not tried on the running app. [{ route, tables, acrossOrgs: 'tried' | 'not tried', why }]
2670
+ */
2671
+ function writesOf(m, wall) {
2672
+ const attempts = wall && wall.ran ? wall.result.attempts : [];
2673
+ const notes = wall && wall.plan && wall.plan.journey ? wall.plan.journey.notTried : [];
2674
+ const out = [];
2675
+ for (const r of (m && m.routes) || []) {
2676
+ if (r.unmounted || !r.path) continue;
2677
+ const tables = [];
2678
+ for (const d of r.db || []) if (d.op !== 'SELECT' && d.table && !tables.includes(d.table)) tables.push(d.table);
2679
+ if (!tables.length) continue;
2680
+ const route = r.method + ' ' + normRoute(r.path);
2681
+ const mine = attempts.filter((a) => a.kind === 'tenant' && a.method + ' ' + a.template === route);
2682
+ const ran = mine.some((a) => !a.notTried);
2683
+ const note = notes.find((n) => normRoute(n.what) === route);
2684
+ 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') });
2685
+ }
2686
+ return out;
2687
+ }
2688
+
1837
2689
  /** The report as it is kept on disk: no passwords, no raw tries, no plan with accounts. */
1838
2690
  function reportOf(run) {
1839
2691
  const wallTries = run.wall && run.wall.ran ? run.wall.result.attempts.filter((a) => !a.notTried).length : 0;
@@ -1843,7 +2695,7 @@ function reportOf(run) {
1843
2695
  tries: { acrossOrganizationsAndNoSession: wallTries, badValues: run.invalid && run.invalid.ran ? run.invalid.tried.length : 0 },
1844
2696
  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
2697
  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,
2698
+ verdicts: run.verdicts, left: run.left, notTried: run.notTried, writes: run.writes || [],
1847
2699
  };
1848
2700
  }
1849
2701
 
@@ -1862,14 +2714,22 @@ function saveReport(dir, run) {
1862
2714
  function apiLines(run) {
1863
2715
  if (!run.ran) return ['', ' Nothing was tried: ' + run.why, ''];
1864
2716
  const out = run.wall && run.wall.ran ? wallLines(run.wall) : ['', ' Across organizations and without a session: not tried (' + (run.wall ? run.wall.why : '') + ')', ''];
2717
+ // a check that stopped at its request limit prints nothing of its own: say so, so its silence is not read as "clear"
2718
+ 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);
2719
+ for (const r of stopped) out.push(' NOT FINISHED ' + r.label + ': ' + r.why + '.');
2720
+ 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.');
2721
+ 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.');
2722
+ if (stopped.length || (run.steps && run.steps.stoppedAt) || (run.stepsAtOnce && run.stepsAtOnce.stoppedAt)) out.push('');
1865
2723
  const counts = { confirmed: 0, held: 0, 'not judged': 0, 'not tried': 0 };
1866
2724
  for (const v of run.verdicts) counts[v.verdict] = (counts[v.verdict] || 0) + 1;
1867
2725
  if (run.roles && run.roles.ran) {
1868
- const rt = run.roles.result.attempts.filter((a) => !a.notTried && a.kind === 'role');
2726
+ const rt = run.roles.result.attempts.filter((a) => !a.notTried && a.kind === 'role' && !a.unruled);
1869
2727
  const n = (expected, verdict) => rt.filter((a) => a.expected === expected && a.verdict === verdict).length;
1870
2728
  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');
2729
+ const ur = run.roles.result.attempts.filter((a) => !a.notTried && a.unruled);
2730
+ 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
2731
  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 + ')' : ''));
2732
+ out.push(' ' + (f.unruled ? 'TO CHECK' : 'PROBLEM') + ' ' + f.severity + ' ' + f.headline + (f.from ? ' (' + f.from + ')' : ''));
1873
2733
  for (const l of (f.proof || []).slice(0, 6)) out.push(' ' + l);
1874
2734
  }
1875
2735
  out.push('');
@@ -1942,20 +2802,44 @@ function apiLines(run) {
1942
2802
  }
1943
2803
  out.push('');
1944
2804
  }
2805
+ if (run.refunds && run.refunds.ran) {
2806
+ 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 + ')'));
2807
+ for (const f of run.refunds.findings) {
2808
+ out.push(' ' + (f.status === 'confirmed' ? 'PROBLEM' : 'TO CHECK') + ' ' + f.severity + ' ' + f.headline + ' (' + f.from + ')');
2809
+ for (const l of f.proof) out.push(' ' + l);
2810
+ }
2811
+ out.push('');
2812
+ }
2813
+ if (run.redirects && run.redirects.ran) {
2814
+ 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 : ''));
2815
+ for (const f of run.redirects.findings) {
2816
+ out.push(' PROBLEM ' + f.severity + ' ' + f.headline + ' (' + f.from + ')');
2817
+ for (const l of f.proof) out.push(' ' + l);
2818
+ }
2819
+ out.push('');
2820
+ }
1945
2821
  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 + ')' : ''));
2822
+ 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
2823
  for (const f of run.steps.findings) {
1948
2824
  out.push(' PROBLEM ' + f.severity + ' ' + f.headline + ' (' + f.from + ')');
1949
2825
  for (const l of f.proof) out.push(' ' + l);
1950
2826
  }
1951
2827
  out.push('');
1952
2828
  }
2829
+ if (run.stepsAtOnce && run.stepsAtOnce.ran) {
2830
+ 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 : ''));
2831
+ for (const f of run.stepsAtOnce.findings) {
2832
+ out.push(' ' + (f.status === 'confirmed' ? 'PROBLEM' : 'TO CHECK') + ' ' + f.severity + ' ' + f.headline + ' (' + f.from + ')');
2833
+ for (const l of f.proof) out.push(' ' + l);
2834
+ }
2835
+ out.push('');
2836
+ }
1953
2837
  if (run.invalid && run.invalid.ran && run.invalid.appDown) {
1954
2838
  out.push(' THE APP STOPPED ANSWERING after ' + run.invalid.appDown + '. Nothing more was sent; start it again before any other check.');
1955
2839
  out.push('');
1956
2840
  }
1957
2841
  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');
2842
+ 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
2843
  for (const f of run.invalid.findings) {
1960
2844
  out.push(' PROBLEM ' + f.severity + ' ' + f.headline + ' (' + f.from + ')');
1961
2845
  for (const l of f.proof) out.push(' ' + l);
@@ -1981,4 +2865,4 @@ function apiLines(run) {
1981
2865
  return out;
1982
2866
  }
1983
2867
 
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 };
2868
+ 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 };