kryptheon 0.1.57 → 0.1.61

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
@@ -117,10 +117,26 @@ const NUMBER_WORDS = new Set(['quantity', 'qty', 'count', 'weight', 'kg', 'amoun
117
117
  * same field name, on another route), a number when the code turns it into one.
118
118
  */
119
119
  function sampleValue(field, known, roleNames) {
120
+ // a list of items the code reads names off (lines = [{ product_id, quantity }]): one item
121
+ if (field.items && field.items.length) {
122
+ const item = {};
123
+ for (const n of field.items) {
124
+ const v = sampleValue({ name: n }, known, roleNames);
125
+ if (v !== null) item[n] = v;
126
+ }
127
+ return [item];
128
+ }
120
129
  if (roleNames && roleNames.length && wordsOf(field.name).includes('role')) return roleNames[0];
121
130
  const en = (field.checks || []).find((c) => c.kind === 'enum' && c.values && c.values.length);
122
131
  if (en) return en.values[0];
123
132
  if (known && known.has(field.name)) return known.get(field.name)[0];
133
+ // 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
+ const declared = known && known.types && known.types[String(field.name).toLowerCase()];
135
+ if (declared && declared.length) {
136
+ if (declared.every((x) => /^bool(ean)?$/.test(x))) return true;
137
+ if (declared.every((x) => /^(int|int2|int4|int8|integer|smallint|bigint|serial|bigserial|numeric|decimal|real|float|float4|float8|double|money)$/.test(x))) return 1;
138
+ if (declared.every((x) => /^(date|timestamp|timestamptz)$/.test(x))) return new Date().toISOString().slice(0, 10);
139
+ }
124
140
  const w = wordsOf(field.name);
125
141
  const last = w[w.length - 1] || '';
126
142
  if (w.includes('role')) return null; // a role is never made up: the field is left out
@@ -128,6 +144,8 @@ function sampleValue(field, known, roleNames) {
128
144
  if (/^(date|day)$/.test(last) || last === 'at' || last === 'on') return new Date().toISOString().slice(0, 10);
129
145
  if (/^(is|has)$/.test(w[0]) || /^(enabled|active|private|public)$/.test(last)) return true;
130
146
  if (last === 'id' && w.length > 1) return 1;
147
+ // a list of ids (work_order_ids): one id in a list, never text
148
+ if (last === 'ids' && w.length > 1) return [1];
131
149
  if (w.some((x) => NUMBER_WORDS.has(x))) return 1;
132
150
  if ((field.checks || []).some((c) => /^(type|range)$/.test(c.kind) && /Number|parse(Int|Float)|isFinite|isInteger/.test(c.code || ''))) return 1;
133
151
  if (w.some((x) => /^(url|link|website)$/.test(x))) return 'https://kryptheon.invalid/test';
@@ -143,6 +161,7 @@ function allowedByName(m) {
143
161
  for (const c of f.checks || []) if (c.kind === 'enum' && c.values && c.values.length && !out.has(f.name)) out.set(f.name, c.values);
144
162
  }
145
163
  }
164
+ out.types = m.columnTypes || {};
146
165
  return out;
147
166
  }
148
167
 
@@ -171,6 +190,16 @@ function actionOf(r, known, roleNames) {
171
190
  fields.push(info);
172
191
  if (f.status === map.STATUS.NOT) unchecked.push({ name: f.name, line: f.line, file: f.file });
173
192
  }
193
+ // from_location_id and to_location_id: two of the same kind of record get two different ids (round 25:
194
+ // a transfer from a location to itself was turned away before anything else was looked at)
195
+ const seenKind = new Map();
196
+ for (const k of Object.keys(data)) {
197
+ const m = /(?:^|_)([a-z]+)_id$/i.exec(k);
198
+ if (!m || typeof data[k] !== 'number') continue;
199
+ const n = seenKind.get(m[1].toLowerCase()) || 0;
200
+ seenKind.set(m[1].toLowerCase(), n + 1);
201
+ if (n) data[k] = data[k] + n;
202
+ }
174
203
  const fullData = Object.assign({}, data);
175
204
  if (/^(PATCH|PUT)$/.test(r.method)) {
176
205
  const plain = Object.keys(data).find((k) => typeof data[k] === 'string' && !wordsOf(k).some((x) => KEEP_OUT_OF_CHANGES.test(x)) && !/@/.test(data[k]));
@@ -356,7 +385,8 @@ function proofOf(att) {
356
385
  if (att.changes && att.changes.length) {
357
386
  lines.push(' state: changed after the request (read before and after):');
358
387
  for (const c of att.changes.slice(0, 3)) lines.push(' ' + c);
359
- } else if (att.protectedPiecesSeen) lines.push(' state: the answer held ' + att.protectedPiecesSeen + ' piece(s) of records ' + (att.kind === 'signin' ? 'only a signed-in account sees' : 'of the other organization'));
388
+ } else if (att.pointedAt) lines.push(' state: saved, and this account\'s own lists now name the other organization\'s record' + (att.pointedAt.key ? ' (' + att.pointedAt.key + ')' : '') + ': ' + att.pointedAt.words.map((w) => JSON.stringify(w)).join(', '));
389
+ else if (att.protectedPiecesSeen) lines.push(' state: the answer held ' + att.protectedPiecesSeen + ' piece(s) of records ' + (att.kind === 'signin' ? 'only a signed-in account sees' : 'of the other organization'));
360
390
  else lines.push(' state: unchanged');
361
391
  return lines;
362
392
  }
@@ -381,16 +411,35 @@ function fixPrompt(f) {
381
411
  */
382
412
  function findingsOf(attempts, journey) {
383
413
  const groups = new Map();
414
+ // another organization's record named in the body and taken, with nothing any list shows moving: a thing to check
415
+ // (round 27: a dispatch run took the other organization's work order ids; no read of the app shows them)
416
+ const taken = (a) => a.kind === 'tenant' && a.verdict === V.AMBIGUOUS && a.namedInBody && (a.response.reading === 'accepted' || a.response.reading === 'redirect');
384
417
  for (const a of attempts) {
385
- if (a.notTried || a.verdict !== V.FAILURE || (a.kind !== 'tenant' && a.kind !== 'signin')) continue;
386
- const k = a.kind + '|' + a.method + ' ' + a.template;
418
+ if (a.notTried || !(a.verdict === V.FAILURE || taken(a)) || (a.kind !== 'tenant' && a.kind !== 'signin')) continue;
419
+ const k = a.kind + '|' + a.method + ' ' + a.template + '|' + (a.verdict === V.FAILURE ? 'p' : 'h');
387
420
  if (!groups.has(k)) groups.set(k, []);
388
421
  groups.get(k).push(a);
389
422
  }
390
423
  const out = [];
391
- for (const list of groups.values()) {
424
+ for (const [key, list] of groups) {
392
425
  const first = list[0];
393
426
  const where = first.method + ' ' + first.template;
427
+ if (key.endsWith('|h')) {
428
+ // said only where the same route is not already proven
429
+ if (groups.has(key.slice(0, -1) + 'p')) continue;
430
+ const action = (journey.actions || []).find((x) => x.method === first.method && x.template === first.template) || {};
431
+ const f = {
432
+ kind: 'tenant', where: where, from: action.from ? action.from.file + ':' + action.from.line : null, severity: 'MEDIUM', status: 'verification required',
433
+ headline: 'Sent another organization\'s record (' + first.namedInBody + ') to ' + where + ', the app took it (' + first.response.status + ') - to check, its effect was not visible.',
434
+ proof: [],
435
+ };
436
+ for (const a of list.slice(0, 3)) f.proof.push.apply(f.proof, proofOf(a));
437
+ if (list.length > 3) f.proof.push('... and ' + (list.length - 3) + ' more like it.');
438
+ f.proof.push('Nothing the app shows changed, so this is not proven: the app may have stored it, or ignored it. Check by hand whether ' + where + ' looks up ' + first.namedInBody + ' in the caller\'s own organization.');
439
+ f.fixPrompt = fixPrompt(f);
440
+ out.push(f);
441
+ continue;
442
+ }
394
443
  const action = (journey.actions || []).find((x) => x.method === first.method && x.template === first.template) || {};
395
444
  const unguarded = action.mapAuth === map.STATUS.NOT;
396
445
  const f = {
@@ -614,6 +663,48 @@ async function runInvalid(root, options) {
614
663
  const res = await session.send('GET', template, {});
615
664
  return res.status === 200 ? JSON.stringify(res.json !== undefined ? res.json : res.text) : null;
616
665
  };
666
+ // every list the account can read, as JSON: what a change by an amount shows up in
667
+ const lists = j.actions.filter((x) => x.method === 'GET' && !x.params.length).map((x) => x.template);
668
+ const snapLists = async () => {
669
+ const o = {};
670
+ for (const t of lists) {
671
+ const r = await session.send('GET', t, {});
672
+ if (r.status === 200 && r.json !== undefined) o[t] = r.json;
673
+ }
674
+ return o;
675
+ };
676
+ // quantity_delta = -999999: judged by whether anything in the app's lists went below zero; then the opposite change puts it back
677
+ const overdraw = async (a, f, pr, base, body, send) => {
678
+ const what = a.method + ' ' + a.template;
679
+ const before = await snapLists();
680
+ const res = await send(body);
681
+ const reply = authz.replyOf(res);
682
+ const took = reply.kind === 'accepted' || reply.kind === 'redirect';
683
+ // a new row holding exactly the change sent (a movement log's quantity_delta) is the request written down, not a balance;
684
+ // a row of a list (stock id 1) says more than a total (a dashboard's sum), so it is said first
685
+ const fell = (took ? belowZero(before, await snapLists()) : [])
686
+ .filter((x) => !(x.before === null && x.after === pr.value))
687
+ .sort((x, y) => (/\[id /.test(y.where) ? 1 : 0) - (/\[id /.test(x.where) ? 1 : 0));
688
+ let putBack = null;
689
+ if (took) {
690
+ await send(Object.assign({}, base, { [f.name]: -pr.value }));
691
+ const now = belowZero(before, await snapLists());
692
+ putBack = fell.every((x) => !now.some((y) => y.where === x.where));
693
+ }
694
+ out.tried.push({ what: what, field: f.name, mode: pr.mode, sent: pr.why, status: res.status, reading: reply.kind, kept: fell.length > 0, verdict: fell.length ? 'went below zero' : reply.kind === 'server-error' ? 'crashed' : took ? 'taken, not shown' : 'turned away', from: f.file + ':' + f.line });
695
+ if (took) out.left.push(what + ' with ' + f.name + ' = ' + pr.why + (putBack ? ', then the opposite change, which put it back' : ' (the opposite change did not put it back)'));
696
+ if (!fell.length) return;
697
+ const finding = {
698
+ kind: 'below-zero', where: what, field: f.name, from: f.file + ':' + f.line, status: 'confirmed', severity: 'HIGH',
699
+ headline: what + ' took ' + f.name + ' ' + pr.why + ', and ' + fell[0].where + ' went below zero (' + fell[0].before + ' -> ' + fell[0].after + '): nothing stops a change larger than what is there.',
700
+ proof: ['As ' + who.email + ', sent ' + what + ' with ' + f.name + ' ' + pr.why + ' and every other field valid.', 'The app answered ' + res.status + ' (' + reply.note + ').']
701
+ .concat(fell.slice(0, 3).map((x) => 'Read before and after: ' + x.where + ' ' + x.before + ' -> ' + x.after))
702
+ .concat([putBack ? 'The opposite change put it back.' : 'The opposite change did NOT put it back - look at it.']),
703
+ };
704
+ finding.fixPrompt = ['Kryptheon sent a change larger than what was there to my running app, and it took it:', '', finding.headline, '', 'Where: ' + finding.from, '',
705
+ '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');
706
+ out.findings.push(finding);
707
+ };
617
708
  for (const a of j.actions) {
618
709
  if (!/^(POST|PUT|PATCH)$/.test(a.method) || !(a.fields || []).length) continue;
619
710
  const what = a.method + ' ' + a.template;
@@ -643,6 +734,10 @@ async function runInvalid(root, options) {
643
734
  if (opts.stopped && opts.stopped()) return out;
644
735
  const f = pr.field;
645
736
  const body = Object.assign({}, base, { [f.name]: pr.value });
737
+ if (pr.mode === 'overdraw') {
738
+ await overdraw(a, f, pr, base, body, send);
739
+ continue;
740
+ }
646
741
  const before = await read(a.template);
647
742
  const res = await session.send(a.method, a.template, a.encoding === 'form' ? { form: body } : { json: body });
648
743
  const reply = authz.replyOf(res);
@@ -710,13 +805,196 @@ async function runInvalid(root, options) {
710
805
 
711
806
  const GHOST_ID = '999999999';
712
807
 
808
+
809
+ // --- a change answered as saved, and not kept ---------------------------------------
810
+
811
+ // Never changed to see if a change is kept: what decides who may do what, money, or a way in.
812
+ const NOT_TO_CHANGE = /^(id|uuid|email|mail|password|secret|token|key|url|link|role|roles|status|state|type|kind|owner|org|organization|tenant|team|workspace|plan|amount|price|cost|total|currency|deleted|archived|created|updated|limit|cents)$/i;
813
+
814
+ // A field that holds where a record is in its life: open, approved, closed.
815
+ const STATE_FIELD = /^(status|state|stage)$/i;
816
+
817
+ /** The value of a field in an answer, at the top or one level down (settings.timezone); camelCase and snake_case are the same. */
818
+ function valueIn(json, field) {
819
+ const flat = (k) => String(k).split('_').join('').toLowerCase();
820
+ const look = (o) => {
821
+ if (!o || typeof o !== 'object' || Array.isArray(o)) return undefined;
822
+ const k = Object.keys(o).find((x) => flat(x) === flat(field));
823
+ return k === undefined ? undefined : o[k];
824
+ };
825
+ const top = look(json);
826
+ if (top !== undefined) return top;
827
+ if (json && typeof json === 'object' && !Array.isArray(json)) for (const v of Object.values(json)) { const d = look(v); if (d !== undefined) return d; }
828
+ return undefined;
829
+ }
830
+
831
+ /** A plain text field of a read's answer that may be changed to see whether a change is kept, or null. */
832
+ function plainFieldOf(json) {
833
+ const pick = (o) => (o && typeof o === 'object' && !Array.isArray(o) ? Object.keys(o).find((k) => typeof o[k] === 'string' && o[k].length < 200 && !wordsOf(k).some((w) => NOT_TO_CHANGE.test(w))) : undefined);
834
+ const top = pick(json);
835
+ if (top) return top;
836
+ if (json && typeof json === 'object' && !Array.isArray(json)) for (const v of Object.values(json)) { const d = pick(v); if (d) return d; }
837
+ return null;
838
+ }
839
+
840
+ /**
841
+ * Each change route with no record in its address (PATCH /api/settings) that the app also reads at the same
842
+ * address: one plain field is changed by an account the app lets do it, then read back. Answered as done but
843
+ * still the old value = the change is not saved (confirmed when the answer even sent the new value back). A
844
+ * change that was kept is put back. { ran, why, tried, findings, left }.
845
+ */
846
+ async function runKept(root, options) {
847
+ const opts = options || {};
848
+ const plan = planOf(root, opts);
849
+ if (!plan.ran) return { ran: false, why: plan.why };
850
+ const j = plan.journey;
851
+ const reads = new Set((plan.map.routes || []).filter((r) => r.method === 'GET' && !r.unmounted && r.path).map((r) => normRoute(r.path)));
852
+ const routes = j.actions.filter((a) => /^(PATCH|PUT)$/.test(a.method) && !a.params.length && reads.has(a.template));
853
+ const states = j.actions.filter((a) => /^(PATCH|PUT|POST)$/.test(a.method) && a.params.length === 1 && (a.fields || []).some((f) => STATE_FIELD.test(f.name)));
854
+ if (!routes.length && !states.length) return { ran: false, why: 'no change route is also read at the same address, and none sets a status on one record' };
855
+ let origin;
856
+ try {
857
+ origin = authz.assertLocalTarget(opts.origin);
858
+ } catch (err) {
859
+ return { ran: false, why: 'the app must be on this computer (localhost or 127.0.0.1): ' + (opts.origin || 'no address was given') };
860
+ }
861
+ if (!j.signIn) return { ran: false, why: j.signInWhy };
862
+ const ctx = authz.makeContext({ baseUrl: origin, allowOrigins: [origin], fetch: opts.fetch, passwords: Object.fromEntries(j.accounts.usable.map((a) => [a.email, a.password])), maxRequests: 80 });
863
+ const sessions = [];
864
+ for (const a of j.accounts.usable) {
865
+ const s = authz.makeSession(ctx, 'account', a.email, a.password);
866
+ const f = { [j.signIn.emailField]: a.email, [j.signIn.passwordField]: a.password };
867
+ const r = authz.replyOf(await s.send(j.signIn.method, j.signIn.path, j.signIn.encoding === 'form' ? { form: f } : { json: f }));
868
+ if (r.kind !== 'refused' && r.kind !== 'server-error' && r.kind !== 'no-answer') sessions.push(s);
869
+ }
870
+ if (!sessions.length) return { ran: false, why: 'no account with a password in .env could sign in' };
871
+ const out = { ran: true, tried: [], findings: [], left: [] };
872
+ for (const a of routes) {
873
+ const what = a.method + ' ' + a.template;
874
+ const t = { what, verdict: 'no account was let in', field: null };
875
+ out.tried.push(t);
876
+ for (const s of sessions) {
877
+ const before = await s.send('GET', a.template, {});
878
+ if (before.status !== 200 || before.json === undefined) continue;
879
+ const field = (a.data && Object.keys(a.data).find((k) => typeof a.data[k] === 'string' && !wordsOf(k).some((w) => NOT_TO_CHANGE.test(w)) && valueIn(before.json, k) !== undefined)) || plainFieldOf(before.json);
880
+ if (!field) { t.verdict = 'no plain field to change in GET ' + a.template; break; }
881
+ const old = valueIn(before.json, field);
882
+ const changed = String(old).slice(0, 60) + ' kx-kept-' + Math.random().toString(36).slice(2, 7);
883
+ const body = { [field]: changed };
884
+ const res = await s.send(a.method, a.template, a.encoding === 'form' ? { form: body } : { json: body });
885
+ const reply = authz.replyOf(res);
886
+ if (reply.kind !== 'accepted' && reply.kind !== 'redirect') continue;
887
+ const after = await s.send('GET', a.template, {});
888
+ const now = after.status === 200 ? valueIn(after.json, field) : undefined;
889
+ t.field = field;
890
+ t.by = s.email;
891
+ if (now === changed) {
892
+ t.verdict = 'kept';
893
+ const back = await s.send(a.method, a.template, a.encoding === 'form' ? { form: { [field]: old } } : { json: { [field]: old } });
894
+ const again = await s.send('GET', a.template, {});
895
+ if (valueIn(again.json, field) !== old) out.left.push(what + ': ' + field + ' is now "' + changed + '" (it was "' + old + '"; putting it back answered ' + back.status + ')');
896
+ break;
897
+ }
898
+ t.verdict = 'not kept';
899
+ const echoed = JSON.stringify(res.json === undefined ? res.text : res.json).includes(changed);
900
+ const finding = {
901
+ kind: 'not-kept', where: what, field, from: a.from.file + ':' + a.from.line, status: echoed ? 'confirmed' : 'verification required', severity: 'MEDIUM',
902
+ headline: what + ' answered ' + res.status + (echoed ? ' and sent the new ' + field + ' back' : '') + ', but GET ' + a.template + ' still shows ' + field + ' = "' + now + '": the change is not saved.',
903
+ proof: ['As ' + s.email + ', read GET ' + a.template + ': ' + field + ' = "' + old + '".', 'Sent ' + what + ' with ' + field + ' = "' + changed + '": answered ' + res.status + ' ' + authz.clip(res.text, 100), 'Read GET ' + a.template + ' again: ' + field + ' = "' + now + '".'],
904
+ };
905
+ finding.fixPrompt = ['Kryptheon changed a setting of my running app and read it back:', '', finding.headline, '', 'Where: ' + finding.from, '',
906
+ 'First check this is real: change it in the app, reload, and look. If it is, save the change where the read takes it from (per organization or per account, as the page says), and have the read return what was saved instead of a fixed value. Change nothing else.', '', 'Afterwards I will run: npx kryptheon api'].join(String.fromCharCode(10));
907
+ out.findings.push(finding);
908
+ break;
909
+ }
910
+ }
911
+ // A status set on one record (PATCH /api/tickets/{id}/status { status }): a word that is no status is sent to a
912
+ // record the account can list, then read back on the record's own address or in the list. Kept = any word is a
913
+ // status, with no list of the allowed ones or of the moves between them (round 26 W26-07, round 27 R27-19). It is
914
+ // put back at once; only the status is sent, so nothing else moves.
915
+ const recordsIn = (r) => (r.status === 200 ? (Array.isArray(r.json) ? r.json : r.json && typeof r.json === 'object' ? Object.values(r.json).find(Array.isArray) : null) : null);
916
+ for (const a of states) {
917
+ const what = a.method + ' ' + a.template;
918
+ const field = a.fields.find((f) => STATE_FIELD.test(f.name)).name;
919
+ const t = { what, field, verdict: 'no account was let in' };
920
+ out.tried.push(t);
921
+ const listTemplate = '/' + a.segs.slice(0, a.params[0].index).join('/');
922
+ const detail = listTemplate + '/{id}';
923
+ for (const s of sessions) {
924
+ const recs = recordsIn(await s.send('GET', listTemplate, {}));
925
+ // the record's own address first: a list may leave the status out
926
+ const readBack = async (rec) => {
927
+ if (reads.has(detail)) {
928
+ const d = await s.send('GET', detail.replace('{id}', encodeURIComponent(String(rec.id))), {});
929
+ const v = d.status === 200 ? valueIn(d.json, field) : undefined;
930
+ if (v !== undefined) return v;
931
+ }
932
+ const again = recordsIn(await s.send('GET', listTemplate, {}));
933
+ const x = again && again.find((y) => y && String(y.id) === String(rec.id));
934
+ return x ? valueIn(x, field) : undefined;
935
+ };
936
+ const rec = recs && recs.find((x) => x && x.id !== undefined);
937
+ const old = rec ? await readBack(rec) : undefined;
938
+ if (old === undefined) { t.verdict = 'no record with a ' + field + ' at GET ' + listTemplate + (reads.has(detail) ? ' or GET ' + detail : ''); continue; }
939
+ const word = 'kx-no-such-' + field.toLowerCase();
940
+ const at = a.template.replace('{id}', encodeURIComponent(String(rec.id)));
941
+ const sendState = (v) => s.send(a.method, at, a.encoding === 'form' ? { form: { [field]: v } } : { json: { [field]: v } });
942
+ const res = await sendState(word);
943
+ const reply = authz.replyOf(res);
944
+ t.by = s.email;
945
+ t.record = rec.id;
946
+ if (reply.kind !== 'accepted' && reply.kind !== 'redirect') {
947
+ t.verdict = 'turned away (' + res.status + ')';
948
+ // not this account's to change: the next one may be; any other refusal is about the value
949
+ if ([401, 403, 404].includes(res.status)) continue;
950
+ break;
951
+ }
952
+ const now = await readBack(rec);
953
+ if (now !== word) {
954
+ t.verdict = 'answered ' + res.status + ', but ' + field + ' is still "' + now + '"';
955
+ break;
956
+ }
957
+ t.verdict = 'kept';
958
+ const back = await sendState(old);
959
+ const again = await readBack(rec);
960
+ const putBack = again === old;
961
+ if (!putBack) out.left.push(at + ': ' + field + ' is now "' + again + '" (it was "' + old + '"; putting it back answered ' + back.status + ')');
962
+ const finding = {
963
+ kind: 'state-any', where: what, field, from: a.from.file + ':' + a.from.line, status: 'confirmed', severity: 'MEDIUM',
964
+ headline: what + ' stored ' + field + ' "' + word + '", a word that is no ' + field + ': any value is taken, with no list of the allowed ones or of the moves between them.',
965
+ proof: ['As ' + s.email + ', read record ' + rec.id + ': ' + field + ' = "' + old + '".', 'Sent ' + a.method + ' ' + at + ' with only ' + field + ' = "' + word + '": answered ' + res.status + ' (' + reply.note + ').', 'Read it back: ' + field + ' = "' + now + '".', putBack ? 'Then put it back to "' + old + '".' : 'Putting it back did NOT work - look at it.'],
966
+ };
967
+ finding.fixPrompt = ['Kryptheon set a status on my running app that is not one of its statuses, and it was stored:', '', finding.headline, '', 'Where: ' + finding.from, '',
968
+ 'First check this is real. If it is, keep the list of allowed values of ' + field + ' and which one may follow which (for example open -> in_progress -> done), and refuse anything else with a 400 or 409, on the server, before anything is stored. Change nothing else.', '', 'Afterwards I will run: npx kryptheon api'].join(String.fromCharCode(10));
969
+ out.findings.push(finding);
970
+ break;
971
+ }
972
+ }
973
+ return out;
974
+ }
975
+
713
976
  // --- a job run twice ------------------------------------------------------------
714
977
 
715
- // A route that runs a job: /jobs/..., or one named for what jobs do.
716
- const JOB_PATH = /(^|\/)(jobs?|cron|tasks?\/run|reminders?|digest|notify[\w-]*|sync|overdue|batch)(\/|$)/i;
978
+ // A route that runs a job: /jobs/..., one named for what jobs do, or one that ends in "run" (/maintenance/run, round 27).
979
+ const JOB_PATH = /(^|\/)(jobs?|cron|tasks?\/run|reminders?|digest|notify[\w-]*|sync|overdue|batch)(\/|$)|\/run$/i;
717
980
  // What a job's answer counts: created 3, sent: 12, notified: 2.
718
981
  const COUNT_KEY = /^(created|inserted|sent|queued|notified|processed|affected|rows|count|total|emails|events|reminders|jobs)$/i;
719
982
 
983
+ /** The organizations the records in an answer are marked with (organization_id: 2), as text. */
984
+ function orgsIn(json) {
985
+ const out = new Set();
986
+ const walk = (v, depth) => {
987
+ if (depth > 4 || v == null || typeof v !== 'object') return;
988
+ if (Array.isArray(v)) return v.forEach((x) => walk(x, depth + 1));
989
+ for (const k of Object.keys(v)) {
990
+ if (roles.ORG_KEY.test(k) && ['string', 'number'].includes(typeof v[k])) out.add(String(v[k]));
991
+ else walk(v[k], depth + 1);
992
+ }
993
+ };
994
+ walk(json, 0);
995
+ return out;
996
+ }
997
+
720
998
  /** The counts a job's answer gives: { created: 3 }. */
721
999
  function countsIn(json) {
722
1000
  const out = {};
@@ -744,7 +1022,7 @@ async function runJobs(root, options) {
744
1022
  return { ran: false, why: 'the app must be on this computer (localhost or 127.0.0.1): ' + (opts.origin || 'no address was given') };
745
1023
  }
746
1024
  if (!j.signIn) return { ran: false, why: j.signInWhy };
747
- const ctx = authz.makeContext({ baseUrl: origin, allowOrigins: [origin], fetch: opts.fetch, passwords: Object.fromEntries(j.accounts.usable.map((a) => [a.email, a.password])), maxRequests: 80 });
1025
+ const ctx = authz.makeContext({ baseUrl: origin, allowOrigins: [origin], fetch: opts.fetch, passwords: Object.fromEntries(j.accounts.usable.map((a) => [a.email, a.password])), maxRequests: 200 });
748
1026
  const sessions = [];
749
1027
  for (const a of j.accounts.usable) {
750
1028
  const s = authz.makeSession(ctx, 'account', a.email, a.password);
@@ -753,6 +1031,7 @@ async function runJobs(root, options) {
753
1031
  if (r.kind !== 'refused' && r.kind !== 'server-error' && r.kind !== 'no-answer') sessions.push(s);
754
1032
  }
755
1033
  if (!sessions.length) return { ran: false, why: 'no account with a password in .env could sign in' };
1034
+ const lists = j.actions.filter((x) => x.method === 'GET' && !x.params.length).map((x) => x.template);
756
1035
  const out = { ran: true, tried: [], findings: [], left: [] };
757
1036
  for (const a of routes) {
758
1037
  const what = a.method + ' ' + a.template;
@@ -765,6 +1044,27 @@ async function runJobs(root, options) {
765
1044
  if (reply.kind === 'accepted' || reply.kind === 'redirect') { first = res; by = s; break; }
766
1045
  }
767
1046
  if (!first) { out.tried.push({ what, verdict: 'no account was let in' }); continue; }
1047
+ // The rows a job's answer gives, each marked with its organization (round 27 R27-22: /maintenance/run made every
1048
+ // organization's reminders). The caller's own organization is the one its own lists are marked with; one the job's answer holds that none of
1049
+ // them shows is another's (round 27: maya of organization 1 made organization 2's reminder). With no marks in the
1050
+ // lists, two organizations in one answer still tell.
1051
+ const orgs = orgsIn(first.json);
1052
+ const own = new Set();
1053
+ if (orgs.size) for (const t of lists) {
1054
+ const r = await by.send('GET', t, {});
1055
+ if (r.status === 200) for (const o of orgsIn(r.json)) own.add(o);
1056
+ }
1057
+ const foreign = Array.from(orgs).filter((o) => !own.has(o));
1058
+ if (own.size ? foreign.length > 0 : orgs.size >= 2) {
1059
+ const finding = {
1060
+ kind: 'tenant-job', where: what, from: a.from.file + ':' + a.from.line, status: 'confirmed', severity: 'HIGH',
1061
+ headline: 'Run once by ' + by.email + ', ' + what + ' answered with records of ' + (own.size ? 'organization ' + foreign.slice(0, 3).join(', ') + ', which none of ' + by.email + '\'s own lists shows (theirs: ' + Array.from(own).slice(0, 3).join(', ') + ')' : orgs.size + ' organizations (' + Array.from(orgs).slice(0, 4).join(', ') + ')') + ': the job works on, and shows, other organizations\' records.',
1062
+ proof: ['As ' + by.email + ', sent ' + what + ' once.', 'The answer: ' + authz.clip(first.text, 160)],
1063
+ };
1064
+ finding.fixPrompt = ['Kryptheon ran a job of my app as one organization\'s account:', '', finding.headline, '', 'Where: ' + finding.from, '',
1065
+ 'First check this is real. If it is, limit what the job reads and writes to the caller\'s organization (WHERE organization_id = the signed-in user\'s), or make it a job only the system runs, not a route an organization can call. Change nothing else.', '', 'Afterwards I will run: npx kryptheon api'].join(String.fromCharCode(10));
1066
+ out.findings.push(finding);
1067
+ }
768
1068
  const second = await send(by);
769
1069
  const c1 = countsIn(first.json);
770
1070
  const c2 = countsIn(second.json);
@@ -785,6 +1085,192 @@ async function runJobs(root, options) {
785
1085
  return out;
786
1086
  }
787
1087
 
1088
+ // --- a create sent twice at once ------------------------------------------------
1089
+
1090
+ // Records a second copy of costs something: money, a delivery, someone's time.
1091
+ const COSTLY_RECORD = /(^|[\/_-])(orders?|payments?|invoices?|deliver(y|ies)|shipments?|bookings?|reservations?|transfers?|refunds?|payouts?|charges?|subscriptions?|appointments?)$/i;
1092
+
1093
+ /**
1094
+ * Each create route for a costly record (POST /api/work-orders, /api/payments) that the app also lists at the same
1095
+ * address: the same body sent twice at once, as a double click sends it. Two new records with the same fields = no
1096
+ * guard against a repeated submit (round 26 W26-20/25, round 27 R27-13). One new record = held. Nothing is deleted;
1097
+ * what was made is listed as left. { ran, why, tried, findings, left }.
1098
+ */
1099
+ async function runCreates(root, options) {
1100
+ const opts = options || {};
1101
+ const plan = planOf(root, opts);
1102
+ if (!plan.ran) return { ran: false, why: plan.why };
1103
+ const j = plan.journey;
1104
+ const lists = new Set(j.actions.filter((x) => x.method === 'GET' && !x.params.length).map((x) => x.template));
1105
+ const routes = j.actions.filter((a) => a.method === 'POST' && !a.params.length && lists.has(a.template) && COSTLY_RECORD.test(a.template) && !JOB_PATH.test(a.template));
1106
+ if (!routes.length) return { ran: false, why: 'no create route of an order, payment or delivery is also listed at its address' };
1107
+ let origin;
1108
+ try {
1109
+ origin = authz.assertLocalTarget(opts.origin);
1110
+ } catch (err) {
1111
+ return { ran: false, why: 'the app must be on this computer (localhost or 127.0.0.1): ' + (opts.origin || 'no address was given') };
1112
+ }
1113
+ if (!j.signIn) return { ran: false, why: j.signInWhy };
1114
+ const ctx = authz.makeContext({ baseUrl: origin, allowOrigins: [origin], fetch: opts.fetch, passwords: Object.fromEntries(j.accounts.usable.map((a) => [a.email, a.password])), maxRequests: 120 });
1115
+ const sessions = [];
1116
+ for (const a of j.accounts.usable) {
1117
+ const s = authz.makeSession(ctx, 'account', a.email, a.password);
1118
+ const f = { [j.signIn.emailField]: a.email, [j.signIn.passwordField]: a.password };
1119
+ const r = authz.replyOf(await s.send(j.signIn.method, j.signIn.path, j.signIn.encoding === 'form' ? { form: f } : { json: f }));
1120
+ if (r.kind !== 'refused' && r.kind !== 'server-error' && r.kind !== 'no-answer') sessions.push(s);
1121
+ }
1122
+ if (!sessions.length) return { ran: false, why: 'no account with a password in .env could sign in' };
1123
+ const recordsIn = (r) => (r.status === 200 ? (Array.isArray(r.json) ? r.json : r.json && typeof r.json === 'object' ? Object.values(r.json).find(Array.isArray) : null) : null);
1124
+ const taken = (res) => { const r = authz.replyOf(res); return r.kind === 'accepted' || r.kind === 'redirect'; };
1125
+ const out = { ran: true, tried: [], findings: [], left: [] };
1126
+ for (const a of routes) {
1127
+ const what = a.method + ' ' + a.template;
1128
+ const t = { what, verdict: 'no account was let in' };
1129
+ out.tried.push(t);
1130
+ const minimal = {};
1131
+ for (const f of a.fields || []) if (f.kinds.length) minimal[f.name] = f.sample;
1132
+ const bodies = [a.fullData || {}].concat(Object.keys(minimal).length && JSON.stringify(minimal) !== JSON.stringify(a.fullData || {}) ? [minimal] : []);
1133
+ for (const s of sessions) {
1134
+ const before = recordsIn(await s.send('GET', a.template, {}));
1135
+ if (!before) continue;
1136
+ const had = new Set(before.map((x) => x && JSON.stringify(x.id)));
1137
+ let done = null;
1138
+ let answered = [];
1139
+ for (const body of bodies) {
1140
+ const send = () => s.send(a.method, a.template, a.encoding === 'form' ? { form: body } : { json: body });
1141
+ const [r1, r2] = await Promise.all([send(), send()]);
1142
+ answered = [r1.status, r2.status];
1143
+ if (!taken(r1) && !taken(r2)) continue;
1144
+ done = { body, r1, r2 };
1145
+ break;
1146
+ }
1147
+ // refused both times: the next account may be let in
1148
+ if (!done) { t.verdict = 'refused (' + answered.join(' and ') + ')'; continue; }
1149
+ const after = recordsIn(await s.send('GET', a.template, {})) || [];
1150
+ const made = after.filter((x) => x && x.id !== undefined && !had.has(JSON.stringify(x.id)));
1151
+ t.by = s.email;
1152
+ t.answers = [done.r1.status, done.r2.status];
1153
+ t.made = made.map((x) => x.id);
1154
+ if (made.length) out.left.push(what + ' sent twice at once as ' + s.email + ': record(s) ' + made.map((x) => x.id).join(', ') + ' (Kryptheon never deletes)');
1155
+ // the same fields the body set, on both records
1156
+ const keys = Object.keys(done.body).filter((k) => made.length && made.every((x) => valueIn(x, k) !== undefined));
1157
+ const alike = made.length >= 2 && keys.every((k) => JSON.stringify(valueIn(made[0], k)) === JSON.stringify(valueIn(made[1], k)));
1158
+ t.verdict = alike ? 'made twice' : made.length === 1 ? 'held: one record' : made.length ? 'made ' + made.length + ' different records' : 'nothing new in the list';
1159
+ if (t.verdict === 'made twice') {
1160
+ const finding = {
1161
+ kind: 'create-twice', where: what, from: a.from.file + ':' + a.from.line, status: 'verification required', severity: 'MEDIUM',
1162
+ headline: 'Sent twice at once, as a double click sends it, ' + what + ' made ' + made.length + ' records with the same ' + (keys.slice(0, 3).join(', ') || 'fields') + ' (' + made.map((x) => x.id).join(', ') + '): nothing stops a repeated submit.',
1163
+ proof: ['As ' + s.email + ', sent ' + what + ' twice at the same moment with the same body: ' + authz.clip(JSON.stringify(done.body), 120), 'Answered ' + done.r1.status + ' and ' + done.r2.status + '.', 'GET ' + a.template + ' before: ' + before.length + ' records; after: ' + after.length + ', new ' + made.map((x) => x.id).join(', ') + '.'],
1164
+ };
1165
+ finding.fixPrompt = ['Kryptheon sent the same create to my running app twice at once, as a double click does:', '', finding.headline, '', 'Where: ' + finding.from, '',
1166
+ 'First check this is real: is a second, identical record ever wanted here? If not, accept an idempotency key from the client (or a fingerprint of the request) and return the first record for a repeat, with a unique index behind it; and disable the submit button while the request is pending. Change nothing else.', '', 'Afterwards I will run: npx kryptheon api'].join(String.fromCharCode(10));
1167
+ out.findings.push(finding);
1168
+ }
1169
+ break;
1170
+ }
1171
+ }
1172
+ return out;
1173
+ }
1174
+
1175
+
1176
+ /**
1177
+ * The map's "a status set with no look at the status now" items, tried: the
1178
+ * step sent for one record, then sent again for the same record. Judged by
1179
+ * the numbers in the app's own lists between the two: when the second call
1180
+ * moves them too (stock added again), the step is done twice. Round 25:
1181
+ * POST /api/orders/:id/receive added the same order's lines to stock again.
1182
+ * { ran, why, tried, findings, left }.
1183
+ */
1184
+ async function runSteps(root, options) {
1185
+ const opts = options || {};
1186
+ const plan = planOf(root, opts);
1187
+ if (!plan.ran) return { ran: false, why: plan.why };
1188
+ const j = plan.journey;
1189
+ const items = (opts.toCheck || map.toCheck(plan.map)).filter((t) => t.kind === 'no-state-check');
1190
+ if (!items.length) return { ran: false, why: 'the map found no step that sets a status without looking at it' };
1191
+ let origin;
1192
+ try {
1193
+ origin = authz.assertLocalTarget(opts.origin);
1194
+ } catch (err) {
1195
+ return { ran: false, why: 'the app must be on this computer (localhost or 127.0.0.1): ' + (opts.origin || 'no address was given') };
1196
+ }
1197
+ if (!j.signIn) return { ran: false, why: j.signInWhy };
1198
+ const ctx = authz.makeContext({ baseUrl: origin, allowOrigins: [origin], fetch: opts.fetch, passwords: Object.fromEntries(j.accounts.usable.map((a) => [a.email, a.password])), maxRequests: 200 });
1199
+ const sessions = [];
1200
+ for (const a of j.accounts.usable) {
1201
+ const s = authz.makeSession(ctx, 'account', a.email, a.password);
1202
+ const f = { [j.signIn.emailField]: a.email, [j.signIn.passwordField]: a.password };
1203
+ const r = authz.replyOf(await s.send(j.signIn.method, j.signIn.path, j.signIn.encoding === 'form' ? { form: f } : { json: f }));
1204
+ if (r.kind !== 'refused' && r.kind !== 'server-error' && r.kind !== 'no-answer') sessions.push(s);
1205
+ }
1206
+ if (!sessions.length) return { ran: false, why: 'no account with a password in .env could sign in' };
1207
+ const lists = j.actions.filter((x) => x.method === 'GET' && !x.params.length).map((x) => x.template);
1208
+ const snap = async (s) => {
1209
+ const o = {};
1210
+ for (const t of lists) {
1211
+ const r = await s.send('GET', t, {});
1212
+ if (r.status === 200 && r.json !== undefined) o[t] = r.json;
1213
+ }
1214
+ return o;
1215
+ };
1216
+ const out = { ran: true, tried: [], findings: [], left: [] };
1217
+ for (const item of items) {
1218
+ const what = normRoute(item.route);
1219
+ const a = j.actions.find((x) => x.method + ' ' + x.template === what);
1220
+ 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; }
1221
+ // the records: the list the step's address starts with (/api/orders for /api/orders/{id}/receive)
1222
+ const listPath = a.segs.slice(0, a.params[0].index).join('/');
1223
+ const listTemplate = '/' + listPath;
1224
+ const route = (plan.map.routes || []).find((r) => r.method + ' ' + normRoute(r.path || '') === what);
1225
+ const sets = route && (route.db || []).find((d) => d.setsState);
1226
+ const target = sets ? sets.setsState.value : null;
1227
+ let done = null;
1228
+ for (const s of sessions) {
1229
+ const r = await s.send('GET', listTemplate, {});
1230
+ const recs = r.status === 200 ? (Array.isArray(r.json) ? r.json : r.json && typeof r.json === 'object' ? Object.values(r.json).find(Array.isArray) : null) : null;
1231
+ if (!recs || !recs.length) continue;
1232
+ // a record not already at the status the step sets: the first call is the step's own, the second the repeat
1233
+ const statusKey = Object.keys(recs[0]).find((k) => /^(status|state|stage)$/i.test(k));
1234
+ const rec = recs.find((x) => x && x.id !== undefined && (!statusKey || !target || String(x[statusKey]) !== target)) || recs.find((x) => x && x.id !== undefined);
1235
+ if (!rec) continue;
1236
+ const at = a.template.replace('{id}', encodeURIComponent(String(rec.id)));
1237
+ const send = () => s.send(a.method, at, a.encoding === 'form' ? { form: a.fullData || {} } : { json: a.fullData || {} });
1238
+ const first = await send();
1239
+ const r1 = authz.replyOf(first);
1240
+ if (r1.kind !== 'accepted' && r1.kind !== 'redirect') continue;
1241
+ const s1 = await snap(s);
1242
+ const second = await send();
1243
+ const r2 = authz.replyOf(second);
1244
+ const s2 = await snap(s);
1245
+ done = { s, rec, at, first, second, r2, moved: numbersMoved(s1, s2) };
1246
+ break;
1247
+ }
1248
+ if (!done) { out.tried.push({ what, verdict: 'no account was let in' }); continue; }
1249
+ const again = (done.r2.kind === 'accepted' || done.r2.kind === 'redirect') && done.moved.length > 0;
1250
+ out.tried.push({ what, by: done.s.email, record: done.rec.id, first: done.first.status, second: done.second.status, verdict: again ? 'done twice' : done.r2.kind === 'refused' ? 'refused the second time' : 'nothing moved the second time' });
1251
+ out.left.push(a.method + ' ' + done.at + ' sent twice as ' + done.s.email + ' (answered ' + done.first.status + ', then ' + done.second.status + ')' + (again ? ': its effect, twice, is still there - Kryptheon does not undo it' : ''));
1252
+ if (!again) continue;
1253
+ const finding = {
1254
+ kind: 'state-repeat', where: what, from: item.where, status: 'confirmed', severity: 'HIGH',
1255
+ headline: 'Sent twice for the same record, ' + what + ' did its step twice: ' + done.moved[0].where + ' went ' + done.moved[0].before + ' -> ' + done.moved[0].after + ' on the second call' + (target ? ', though the record was already ' + target : '') + '.',
1256
+ proof: ['As ' + done.s.email + ', sent ' + a.method + ' ' + done.at + ': answered ' + done.first.status + '.', 'Sent it again, unchanged: answered ' + done.second.status + '.']
1257
+ .concat(done.moved.slice(0, 3).map((x) => 'Read between the two calls and after the second: ' + x.where + ' ' + x.before + ' -> ' + x.after)),
1258
+ };
1259
+ finding.fixPrompt = ['Kryptheon sent the same step twice to my running app, and it was done twice:', '', finding.headline, '', 'Where: ' + finding.from, '',
1260
+ '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));
1261
+ out.findings.push(finding);
1262
+ }
1263
+ return out;
1264
+ }
1265
+
1266
+ /** The numbers that differ between two reads of the same lists: [{ where, before, after }]. */
1267
+ function numbersMoved(before, after) {
1268
+ const b = numbersOf(before);
1269
+ const out = [];
1270
+ for (const [at, v] of numbersOf(after)) if (b.has(at) && b.get(at) !== v) out.push({ where: at, before: b.get(at), after: v });
1271
+ return out;
1272
+ }
1273
+
788
1274
 
789
1275
  /** Whether the app has stopped answering: a request that got no answer, and then a plain read that gets none either. */
790
1276
  async function appDown(session, template) {
@@ -815,6 +1301,33 @@ const NOT_PLAIN_TEXT = /^(email|mail|date|day|at|on|url|link|website|phone|mobil
815
1301
  * blank - a text field the code requires, or is plainly required by its name, is left empty
816
1302
  * below - a number field gets -1
817
1303
  */
1304
+ const CHANGE_WORDS = new Set(['delta', 'change', 'adjust', 'adjustment', 'diff', 'difference', 'increment', 'decrement']);
1305
+ const OVERDRAW = 999999;
1306
+
1307
+ /** Every number in some lists, by where it is: '/api/stock.items[id 1].on_hand' -> 18. Rows are known by their id. */
1308
+ function numbersOf(snap) {
1309
+ const out = new Map();
1310
+ const walk = (v, at) => {
1311
+ if (typeof v === 'number') out.set(at, v);
1312
+ else if (Array.isArray(v)) v.forEach((x, i) => walk(x, at + '[' + (x && typeof x === 'object' && x.id !== undefined ? 'id ' + x.id : i) + ']'));
1313
+ else if (v && typeof v === 'object') for (const k of Object.keys(v)) walk(v[k], at + '.' + k);
1314
+ };
1315
+ for (const t of Object.keys(snap)) walk(snap[t], t);
1316
+ return out;
1317
+ }
1318
+
1319
+ /**
1320
+ * Numbers in the app's lists that are below zero after and were not before:
1321
+ * [{ where, before, after }]. Records are known by their id, so a row that
1322
+ * moves in a list is still the same row.
1323
+ */
1324
+ function belowZero(before, after) {
1325
+ const b = numbersOf(before);
1326
+ const out = [];
1327
+ for (const [at, v] of numbersOf(after)) if (v < 0 && !(b.get(at) < 0)) out.push({ where: at, before: b.has(at) ? b.get(at) : null, after: v });
1328
+ return out;
1329
+ }
1330
+
818
1331
  function probesOf(a) {
819
1332
  const out = [];
820
1333
  for (const f of a.fields || []) {
@@ -827,7 +1340,10 @@ function probesOf(a) {
827
1340
  if (typeof f.sample === 'string' && !f.kinds.includes('enum') && !w.some((x) => NOT_PLAIN_TEXT.test(x)) && (checked || w.some((x) => REQUIRED_NAME.test(x)))) {
828
1341
  out.push({ field: f, mode: 'blank', value: '', why: 'left blank' });
829
1342
  }
830
- if (typeof f.sample === 'number' && w[w.length - 1] !== 'id' && (w.some((x) => NUMBER_WORDS.has(x)) || f.kinds.includes('range'))) {
1343
+ // a change by an amount (quantity_delta): a negative one is normal, one larger than anything held is not (round 25)
1344
+ if (typeof f.sample === 'number' && w.some((x) => CHANGE_WORDS.has(x))) {
1345
+ out.push({ field: f, mode: 'overdraw', value: -OVERDRAW, why: 'a negative change larger than anything held (-' + OVERDRAW + ')' });
1346
+ } else if (typeof f.sample === 'number' && w[w.length - 1] !== 'id' && (w.some((x) => NUMBER_WORDS.has(x)) || f.kinds.includes('range'))) {
831
1347
  out.push({ field: f, mode: 'below', value: -1, why: 'a negative number (-1)' });
832
1348
  }
833
1349
  }
@@ -849,7 +1365,34 @@ const normRoute = (name) => String(name).replace(/:\w+\*?/g, '{id}');
849
1365
  * organizations; no-role <- the roles run (needs the owner's permissions file);
850
1366
  * a webhook is the payments check's.
851
1367
  */
852
- function verdictsFor(toCheck, wall, invalid, roleRun, forged, webhooks) {
1368
+ /**
1369
+ * How much a "held" says. Strong: some try was refused, or it was a write (a write is held only when refused and
1370
+ * the stored state did not change). Weak: every try was a read the app answered, just without the other side's
1371
+ * records - which may only mean there were none (round 24: notes of a return that had no notes).
1372
+ */
1373
+ function heldStrength(tries) {
1374
+ const denied = tries.filter((a) => a.verdict === V.DENIED);
1375
+ return denied.some((a) => a.method !== 'GET' || (a.response && a.response.reading === 'refused')) ? 'strong' : 'weak';
1376
+ }
1377
+
1378
+ /**
1379
+ * The confidence step between the map and the running app (round 26: five map items the run itself had held were
1380
+ * still shown as HIGH and MEDIUM problems - three of them the answer key's decoys). A map item the running app
1381
+ * strongly held is no longer a thing to fix: it moves to `held`, with what was tried. One held weakly stays, with
1382
+ * why it says little. Everything else stays as it is. { shown, held }
1383
+ */
1384
+ function reconcile(toCheck, verdicts) {
1385
+ const shown = [];
1386
+ const held = [];
1387
+ for (const t of toCheck || []) {
1388
+ const v = (verdicts || []).find((x) => x.route === t.route && x.kind === t.kind);
1389
+ if (v && v.verdict === 'held' && v.strength === 'strong') held.push(Object.assign({}, t, { verdict: v }));
1390
+ else shown.push(Object.assign({}, t, v ? { verdict: v } : {}));
1391
+ }
1392
+ return { shown, held };
1393
+ }
1394
+
1395
+ function verdictsFor(toCheck, wall, invalid, roleRun, forged, webhooks, steps) {
853
1396
  const out = [];
854
1397
  const attempts = wall && wall.ran ? wall.result.attempts : [];
855
1398
  const failures = (wall && wall.findings) || [];
@@ -859,9 +1402,23 @@ function verdictsFor(toCheck, wall, invalid, roleRun, forged, webhooks) {
859
1402
  const route = normRoute(t.route);
860
1403
  const v = { route: t.route, kind: t.kind, headline: t.headline };
861
1404
  const kind = /^no-sign-in$/.test(t.kind) ? 'signin' : /^tenant-/.test(t.kind) ? 'tenant' : /^no-role$/.test(t.kind) ? 'role' : null;
862
- if (t.kind === 'identity-from-request') {
1405
+ if (t.kind === 'no-state-check') {
1406
+ const hit = steps && steps.ran ? steps.findings.find((f) => f.where === route) : null;
1407
+ const tried = steps && steps.ran ? steps.tried.find((x) => x.what === route) : null;
1408
+ if (hit) {
1409
+ v.verdict = 'confirmed';
1410
+ v.finding = hit.headline;
1411
+ } else if (tried && /^(refused the second time|nothing moved the second time)$/.test(tried.verdict)) {
1412
+ v.verdict = 'held';
1413
+ v.strength = 'strong';
1414
+ } else {
1415
+ v.verdict = 'not tried';
1416
+ v.why = tried ? tried.why || tried.verdict : steps && !steps.ran ? steps.why : 'the step was not sent twice';
1417
+ }
1418
+ } else if (t.kind === 'identity-from-request') {
863
1419
  const hit = forged && forged.ran && forged.findings.find((f) => f.from === t.where);
864
1420
  v.verdict = hit ? 'confirmed' : forged && forged.ran && forged.tried.length ? 'held' : 'not tried';
1421
+ if (v.verdict === 'held') v.strength = 'strong';
865
1422
  if (hit) v.finding = hit.headline;
866
1423
  if (v.verdict === 'not tried') v.why = forged ? forged.why || 'not tried' : 'the forged identity was not tried';
867
1424
  } else if (/^webhook/.test(t.kind)) {
@@ -872,6 +1429,7 @@ function verdictsFor(toCheck, wall, invalid, roleRun, forged, webhooks) {
872
1429
  v.finding = hit.headline;
873
1430
  } else if (hook && hook.control && hook.control.took && hook.tried.some((x) => x.mode === 'none') && !hook.findings.some((f) => /^webhook-(unsigned|forged)$/.test(f.kind))) {
874
1431
  v.verdict = 'held';
1432
+ v.strength = 'strong';
875
1433
  } else if (hook && hook.findings.some((f) => /^webhook-(unsigned|forged)$/.test(f.kind))) {
876
1434
  v.verdict = 'not judged';
877
1435
  v.why = 'the app answered success without a valid signature, but no change was seen in ' + hook.list;
@@ -891,6 +1449,8 @@ function verdictsFor(toCheck, wall, invalid, roleRun, forged, webhooks) {
891
1449
  v.finding = rf[0].headline;
892
1450
  } else if (tries.length && tries.every((a) => a.verdict === V.DENIED || a.verdict === V.ALLOWED)) {
893
1451
  v.verdict = 'held';
1452
+ v.strength = heldStrength(tries);
1453
+ if (v.strength === 'weak') v.why = 'it answered without the protected records, which may only mean there were none to show';
894
1454
  } else if (tries.length) {
895
1455
  v.verdict = 'not judged';
896
1456
  v.why = (tries.find((a) => a.verdict === V.AMBIGUOUS) || tries[0]).reason;
@@ -910,6 +1470,8 @@ function verdictsFor(toCheck, wall, invalid, roleRun, forged, webhooks) {
910
1470
  v.finding = hit.headline;
911
1471
  } else if (mine.length && mine.some((a) => a.verdict === V.DENIED || a.verdict === V.ALLOWED) && !mine.some((a) => a.verdict === V.AMBIGUOUS)) {
912
1472
  v.verdict = 'held';
1473
+ v.strength = heldStrength(mine);
1474
+ if (v.strength === 'weak') v.why = 'it answered without the other organization\'s records, which may only mean it had none to show';
913
1475
  } else if (mine.length) {
914
1476
  v.verdict = 'not judged';
915
1477
  v.why = (mine.find((a) => a.verdict === V.AMBIGUOUS) || mine[0]).reason;
@@ -1023,22 +1585,25 @@ async function runApi(root, options) {
1023
1585
  const wall = await runWall(root, shared);
1024
1586
  const forged = await runForged(root, shared);
1025
1587
  const webhooks = await webhook.run(root, shared);
1026
- // the export and the jobs come before the bad values, which may stop the app
1027
1588
  const exportsRun = await csv.run(root, shared);
1028
1589
  const jobsRun = await runJobs(root, shared);
1029
- const invalid = await runInvalid(root, shared);
1590
+ const keptRun = await runKept(root, shared);
1591
+ const createsRun = await runCreates(root, shared);
1592
+ const stepsRun = await runSteps(root, Object.assign({}, shared, { toCheck: toCheck }));
1030
1593
  let roleRun = null;
1031
1594
  if (opts.roles) roleRun = await runRoles(root, shared);
1032
- const verdicts = verdictsFor(toCheck, wall, invalid, roleRun, forged, webhooks);
1595
+ // the bad values always last: one of them may stop the app (round 24 lost its whole roles run that way)
1596
+ const invalid = await runInvalid(root, shared);
1597
+ const verdicts = verdictsFor(toCheck, wall, invalid, roleRun, forged, webhooks, stepsRun);
1033
1598
  const seen = new Set();
1034
- 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(roleRun && roleRun.ran ? roleRun.findings : []).concat(invalid.ran ? invalid.findings : []).filter((f) => {
1599
+ 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(stepsRun.ran ? stepsRun.findings : []).concat(roleRun && roleRun.ran ? roleRun.findings : []).concat(invalid.ran ? invalid.findings : []).filter((f) => {
1035
1600
  const k = f.kind + '|' + f.where + '|' + (f.field || '');
1036
1601
  return seen.has(k) ? false : (seen.add(k), true);
1037
1602
  });
1038
- const left = (webhooks.ran ? webhooks.left : []).concat(exportsRun.ran ? exportsRun.left : []).concat(jobsRun.ran ? jobsRun.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) : []);
1603
+ 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(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) : []);
1039
1604
  return {
1040
1605
  ran: wall.ran || invalid.ran, why: wall.ran || invalid.ran ? null : wall.why, checkedAt: new Date().toISOString(),
1041
- origin: opts.origin, wall: wall, forged: forged, webhooks: webhooks, exports: exportsRun, jobs: jobsRun, invalid: invalid, roles: roleRun, verdicts: verdicts, findings: findings, left: left,
1606
+ origin: opts.origin, wall: wall, forged: forged, webhooks: webhooks, exports: exportsRun, jobs: jobsRun, kept: keptRun, creates: createsRun, steps: stepsRun, invalid: invalid, roles: roleRun, verdicts: verdicts, findings: findings, left: left,
1042
1607
  notTried: (wall.plan ? wall.plan.notTried : []).concat(invalid.ran ? invalid.notTried : []),
1043
1608
  };
1044
1609
  }
@@ -1098,7 +1663,10 @@ function apiLines(run) {
1098
1663
  out.push(' Webhook ' + h.route + ': not tried - ' + h.notTried);
1099
1664
  continue;
1100
1665
  }
1101
- out.push(' Webhook ' + h.route + ' (' + h.signing.header + ', ' + h.signing.algorithm + ' ' + h.signing.encoding + ' over ' + h.signing.signs + '): a correctly signed event was taken; ' + h.tried.filter((t) => t !== h.control && t.took).length + ' of ' + (h.tried.length - 1) + ' other tries were taken, ' + h.tried.filter((t) => t.changed && t !== h.control).length + ' changed a record');
1666
+ const how = h.signing.open
1667
+ ? ' (it checks no signature at all): an event with none was taken'
1668
+ : ' (' + h.signing.header + ', ' + h.signing.algorithm + ' ' + h.signing.encoding + ' over ' + h.signing.signs + '): a correctly signed ' + (h.neutral ? 'event of a type it does not act on' : 'event') + ' was taken';
1669
+ out.push(' Webhook ' + h.route + how + '; ' + h.tried.filter((t) => t !== h.control && t.took).length + ' of ' + (h.tried.length - 1) + ' other tries were taken, ' + h.tried.filter((t) => t.changed && t !== h.control).length + ' changed a record' + (h.list ? '' : ' (no list to read a change from)'));
1102
1670
  for (const f of h.findings) {
1103
1671
  out.push(' ' + (f.status === 'confirmed' ? 'PROBLEM' : 'TO CHECK') + ' ' + f.severity + ' ' + f.headline + ' (' + f.from + ')');
1104
1672
  for (const l of f.proof) out.push(' ' + l);
@@ -1116,6 +1684,14 @@ function apiLines(run) {
1116
1684
  }
1117
1685
  out.push('');
1118
1686
  }
1687
+ if (run.kept && run.kept.ran) {
1688
+ for (const t of run.kept.tried) out.push(' Change ' + t.what + (t.field ? ' (' + t.field + ')' : '') + ': ' + t.verdict);
1689
+ for (const f of run.kept.findings) {
1690
+ out.push(' ' + (f.status === 'confirmed' ? 'PROBLEM' : 'TO CHECK') + ' ' + f.severity + ' ' + f.headline + ' (' + f.from + ')');
1691
+ for (const l of f.proof) out.push(' ' + l);
1692
+ }
1693
+ out.push('');
1694
+ }
1119
1695
  if (run.jobs && run.jobs.ran) {
1120
1696
  for (const t of run.jobs.tried) out.push(' Job ' + t.what + ' run twice: ' + t.verdict + (t.first ? ' (' + JSON.stringify(t.first) + ', then ' + JSON.stringify(t.second) + ')' : ''));
1121
1697
  for (const f of run.jobs.findings) {
@@ -1124,12 +1700,28 @@ function apiLines(run) {
1124
1700
  }
1125
1701
  out.push('');
1126
1702
  }
1703
+ if (run.creates && run.creates.ran) {
1704
+ for (const t of run.creates.tried) out.push(' Create ' + t.what + ' sent twice at once: ' + t.verdict + (t.answers ? ' (answered ' + t.answers.join(' and ') + (t.made && t.made.length ? '; new ' + t.made.join(', ') : '') + ')' : ''));
1705
+ for (const f of run.creates.findings) {
1706
+ out.push(' TO CHECK ' + f.severity + ' ' + f.headline + ' (' + f.from + ')');
1707
+ for (const l of f.proof) out.push(' ' + l);
1708
+ }
1709
+ out.push('');
1710
+ }
1711
+ if (run.steps && run.steps.ran) {
1712
+ 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 + ')' : ''));
1713
+ for (const f of run.steps.findings) {
1714
+ out.push(' PROBLEM ' + f.severity + ' ' + f.headline + ' (' + f.from + ')');
1715
+ for (const l of f.proof) out.push(' ' + l);
1716
+ }
1717
+ out.push('');
1718
+ }
1127
1719
  if (run.invalid && run.invalid.ran && run.invalid.appDown) {
1128
1720
  out.push(' THE APP STOPPED ANSWERING after ' + run.invalid.appDown + '. Nothing more was sent; start it again before any other check.');
1129
1721
  out.push('');
1130
1722
  }
1131
1723
  if (run.invalid && run.invalid.ran) {
1132
- 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 === 'taken, not shown').length + ' taken with nothing to show');
1724
+ 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');
1133
1725
  for (const f of run.invalid.findings) {
1134
1726
  out.push(' PROBLEM ' + f.severity + ' ' + f.headline + ' (' + f.from + ')');
1135
1727
  for (const l of f.proof) out.push(' ' + l);
@@ -1141,7 +1733,9 @@ function apiLines(run) {
1141
1733
  }
1142
1734
  if (run.verdicts.length) {
1143
1735
  out.push(' What the map pointed at (' + run.verdicts.length + '): ' + counts.confirmed + ' confirmed on the running app, ' + counts.held + ' held, ' + counts['not judged'] + ' not judged, ' + counts['not tried'] + ' not tried');
1144
- for (const v of run.verdicts) out.push(' ' + v.verdict.toUpperCase() + ' ' + v.route + ' - ' + v.kind + (v.why ? ' (' + v.why + ')' : ''));
1736
+ for (const v of run.verdicts) out.push(' ' + (v.verdict === 'held' && v.strength === 'weak' ? 'HELD WEAKLY' : v.verdict.toUpperCase()) + ' ' + v.route + ' - ' + v.kind + (v.why ? ' (' + v.why + ')' : ''));
1737
+ const strong = run.verdicts.filter((v) => v.verdict === 'held' && v.strength === 'strong').length;
1738
+ if (strong) out.push(' The ' + strong + ' held for certain (refused, or nothing changed) are no longer things to fix: the dashboard keeps them apart.');
1145
1739
  out.push('');
1146
1740
  }
1147
1741
  const extra = run.invalid && run.invalid.ran ? run.invalid.left : [];
@@ -1153,4 +1747,4 @@ function apiLines(run) {
1153
1747
  return out;
1154
1748
  }
1155
1749
 
1156
- module.exports = { 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 };
1750
+ module.exports = { reconcile, heldStrength, runSteps, numbersMoved, runKept, runCreates, COSTLY_RECORD, 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 };