kryptheon 0.1.56 → 0.1.58

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
@@ -24,6 +24,8 @@ const roles = require('./kryptheon-roles.js');
24
24
  const replay = require('./kryptheon-replay.js');
25
25
  const secrets = require('./kryptheon-secrets.js');
26
26
  const authz = require('./kryptheon-authz.js');
27
+ const webhook = require('./kryptheon-webhook.js');
28
+ const csv = require('./kryptheon-csv.js');
27
29
 
28
30
  const EMAIL = /[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/g;
29
31
  const SIGN_IN = /(^|\/)(log-?in|sign-?in|session|auth\/(login|signin|token))$/i;
@@ -115,10 +117,26 @@ const NUMBER_WORDS = new Set(['quantity', 'qty', 'count', 'weight', 'kg', 'amoun
115
117
  * same field name, on another route), a number when the code turns it into one.
116
118
  */
117
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
+ }
118
129
  if (roleNames && roleNames.length && wordsOf(field.name).includes('role')) return roleNames[0];
119
130
  const en = (field.checks || []).find((c) => c.kind === 'enum' && c.values && c.values.length);
120
131
  if (en) return en.values[0];
121
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
+ }
122
140
  const w = wordsOf(field.name);
123
141
  const last = w[w.length - 1] || '';
124
142
  if (w.includes('role')) return null; // a role is never made up: the field is left out
@@ -141,6 +159,7 @@ function allowedByName(m) {
141
159
  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);
142
160
  }
143
161
  }
162
+ out.types = m.columnTypes || {};
144
163
  return out;
145
164
  }
146
165
 
@@ -169,6 +188,16 @@ function actionOf(r, known, roleNames) {
169
188
  fields.push(info);
170
189
  if (f.status === map.STATUS.NOT) unchecked.push({ name: f.name, line: f.line, file: f.file });
171
190
  }
191
+ // from_location_id and to_location_id: two of the same kind of record get two different ids (round 25:
192
+ // a transfer from a location to itself was turned away before anything else was looked at)
193
+ const seenKind = new Map();
194
+ for (const k of Object.keys(data)) {
195
+ const m = /(?:^|_)([a-z]+)_id$/i.exec(k);
196
+ if (!m || typeof data[k] !== 'number') continue;
197
+ const n = seenKind.get(m[1].toLowerCase()) || 0;
198
+ seenKind.set(m[1].toLowerCase(), n + 1);
199
+ if (n) data[k] = data[k] + n;
200
+ }
172
201
  const fullData = Object.assign({}, data);
173
202
  if (/^(PATCH|PUT)$/.test(r.method)) {
174
203
  const plain = Object.keys(data).find((k) => typeof data[k] === 'string' && !wordsOf(k).some((x) => KEEP_OUT_OF_CHANGES.test(x)) && !/@/.test(data[k]));
@@ -206,6 +235,11 @@ function journeyFromMap(m, root, env, roleNames) {
206
235
  notTried.push({ what, why: 'another sign-in route - signing in is done once, through ' + (s.none ? 'none found' : s.method + ' ' + s.path) });
207
236
  continue;
208
237
  }
238
+ // the 'who am I' route answers each account with itself: nothing of another organization's to aim at
239
+ if (r.method === 'GET' && IDENTITY_ROUTE.test(r.path)) {
240
+ notTried.push({ what, why: 'the "who am I" route: each account is answered with itself, so there is nothing of another to compare (the forged-identity try uses it)' });
241
+ continue;
242
+ }
209
243
  if (PLUMBING.test(r.path)) {
210
244
  notTried.push({ what, why: 'sign-up, password or health plumbing - not an action to try as another account' });
211
245
  continue;
@@ -349,7 +383,8 @@ function proofOf(att) {
349
383
  if (att.changes && att.changes.length) {
350
384
  lines.push(' state: changed after the request (read before and after):');
351
385
  for (const c of att.changes.slice(0, 3)) lines.push(' ' + c);
352
- } 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'));
386
+ } 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(', '));
387
+ 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'));
353
388
  else lines.push(' state: unchanged');
354
389
  return lines;
355
390
  }
@@ -607,6 +642,48 @@ async function runInvalid(root, options) {
607
642
  const res = await session.send('GET', template, {});
608
643
  return res.status === 200 ? JSON.stringify(res.json !== undefined ? res.json : res.text) : null;
609
644
  };
645
+ // every list the account can read, as JSON: what a change by an amount shows up in
646
+ const lists = j.actions.filter((x) => x.method === 'GET' && !x.params.length).map((x) => x.template);
647
+ const snapLists = async () => {
648
+ const o = {};
649
+ for (const t of lists) {
650
+ const r = await session.send('GET', t, {});
651
+ if (r.status === 200 && r.json !== undefined) o[t] = r.json;
652
+ }
653
+ return o;
654
+ };
655
+ // quantity_delta = -999999: judged by whether anything in the app's lists went below zero; then the opposite change puts it back
656
+ const overdraw = async (a, f, pr, base, body, send) => {
657
+ const what = a.method + ' ' + a.template;
658
+ const before = await snapLists();
659
+ const res = await send(body);
660
+ const reply = authz.replyOf(res);
661
+ const took = reply.kind === 'accepted' || reply.kind === 'redirect';
662
+ // a new row holding exactly the change sent (a movement log's quantity_delta) is the request written down, not a balance;
663
+ // a row of a list (stock id 1) says more than a total (a dashboard's sum), so it is said first
664
+ const fell = (took ? belowZero(before, await snapLists()) : [])
665
+ .filter((x) => !(x.before === null && x.after === pr.value))
666
+ .sort((x, y) => (/\[id /.test(y.where) ? 1 : 0) - (/\[id /.test(x.where) ? 1 : 0));
667
+ let putBack = null;
668
+ if (took) {
669
+ await send(Object.assign({}, base, { [f.name]: -pr.value }));
670
+ const now = belowZero(before, await snapLists());
671
+ putBack = fell.every((x) => !now.some((y) => y.where === x.where));
672
+ }
673
+ 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 });
674
+ 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)'));
675
+ if (!fell.length) return;
676
+ const finding = {
677
+ kind: 'below-zero', where: what, field: f.name, from: f.file + ':' + f.line, status: 'confirmed', severity: 'HIGH',
678
+ 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.',
679
+ proof: ['As ' + who.email + ', sent ' + what + ' with ' + f.name + ' ' + pr.why + ' and every other field valid.', 'The app answered ' + res.status + ' (' + reply.note + ').']
680
+ .concat(fell.slice(0, 3).map((x) => 'Read before and after: ' + x.where + ' ' + x.before + ' -> ' + x.after))
681
+ .concat([putBack ? 'The opposite change put it back.' : 'The opposite change did NOT put it back - look at it.']),
682
+ };
683
+ finding.fixPrompt = ['Kryptheon sent a change larger than what was there to my running app, and it took it:', '', finding.headline, '', 'Where: ' + finding.from, '',
684
+ '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');
685
+ out.findings.push(finding);
686
+ };
610
687
  for (const a of j.actions) {
611
688
  if (!/^(POST|PUT|PATCH)$/.test(a.method) || !(a.fields || []).length) continue;
612
689
  const what = a.method + ' ' + a.template;
@@ -636,9 +713,19 @@ async function runInvalid(root, options) {
636
713
  if (opts.stopped && opts.stopped()) return out;
637
714
  const f = pr.field;
638
715
  const body = Object.assign({}, base, { [f.name]: pr.value });
716
+ if (pr.mode === 'overdraw') {
717
+ await overdraw(a, f, pr, base, body, send);
718
+ continue;
719
+ }
639
720
  const before = await read(a.template);
640
721
  const res = await session.send(a.method, a.template, a.encoding === 'form' ? { form: body } : { json: body });
641
722
  const reply = authz.replyOf(res);
723
+ if (reply.kind === 'no-answer' && await appDown(session, a.template)) {
724
+ out.findings.push(downFinding(who.email, what, f, pr, res));
725
+ out.appDown = what + ' with ' + f.name + ' ' + pr.why;
726
+ 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 });
727
+ return out;
728
+ }
642
729
  const after = await read(a.template);
643
730
  const took = reply.kind === 'accepted' || reply.kind === 'redirect';
644
731
  const shown = before !== null && after !== null;
@@ -697,6 +784,301 @@ async function runInvalid(root, options) {
697
784
 
698
785
  const GHOST_ID = '999999999';
699
786
 
787
+
788
+ // --- a change answered as saved, and not kept ---------------------------------------
789
+
790
+ // Never changed to see if a change is kept: what decides who may do what, money, or a way in.
791
+ 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;
792
+
793
+ /** The value of a field in an answer, at the top or one level down (settings.timezone); camelCase and snake_case are the same. */
794
+ function valueIn(json, field) {
795
+ const flat = (k) => String(k).split('_').join('').toLowerCase();
796
+ const look = (o) => {
797
+ if (!o || typeof o !== 'object' || Array.isArray(o)) return undefined;
798
+ const k = Object.keys(o).find((x) => flat(x) === flat(field));
799
+ return k === undefined ? undefined : o[k];
800
+ };
801
+ const top = look(json);
802
+ if (top !== undefined) return top;
803
+ if (json && typeof json === 'object' && !Array.isArray(json)) for (const v of Object.values(json)) { const d = look(v); if (d !== undefined) return d; }
804
+ return undefined;
805
+ }
806
+
807
+ /** A plain text field of a read's answer that may be changed to see whether a change is kept, or null. */
808
+ function plainFieldOf(json) {
809
+ 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);
810
+ const top = pick(json);
811
+ if (top) return top;
812
+ if (json && typeof json === 'object' && !Array.isArray(json)) for (const v of Object.values(json)) { const d = pick(v); if (d) return d; }
813
+ return null;
814
+ }
815
+
816
+ /**
817
+ * Each change route with no record in its address (PATCH /api/settings) that the app also reads at the same
818
+ * address: one plain field is changed by an account the app lets do it, then read back. Answered as done but
819
+ * still the old value = the change is not saved (confirmed when the answer even sent the new value back). A
820
+ * change that was kept is put back. { ran, why, tried, findings, left }.
821
+ */
822
+ async function runKept(root, options) {
823
+ const opts = options || {};
824
+ const plan = planOf(root, opts);
825
+ if (!plan.ran) return { ran: false, why: plan.why };
826
+ const j = plan.journey;
827
+ const reads = new Set((plan.map.routes || []).filter((r) => r.method === 'GET' && !r.unmounted && r.path).map((r) => r.path));
828
+ const routes = j.actions.filter((a) => /^(PATCH|PUT)$/.test(a.method) && !a.params.length && reads.has(a.template));
829
+ if (!routes.length) return { ran: false, why: 'no change route is also read at the same address' };
830
+ let origin;
831
+ try {
832
+ origin = authz.assertLocalTarget(opts.origin);
833
+ } catch (err) {
834
+ return { ran: false, why: 'the app must be on this computer (localhost or 127.0.0.1): ' + (opts.origin || 'no address was given') };
835
+ }
836
+ if (!j.signIn) return { ran: false, why: j.signInWhy };
837
+ 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 });
838
+ const sessions = [];
839
+ for (const a of j.accounts.usable) {
840
+ const s = authz.makeSession(ctx, 'account', a.email, a.password);
841
+ const f = { [j.signIn.emailField]: a.email, [j.signIn.passwordField]: a.password };
842
+ const r = authz.replyOf(await s.send(j.signIn.method, j.signIn.path, j.signIn.encoding === 'form' ? { form: f } : { json: f }));
843
+ if (r.kind !== 'refused' && r.kind !== 'server-error' && r.kind !== 'no-answer') sessions.push(s);
844
+ }
845
+ if (!sessions.length) return { ran: false, why: 'no account with a password in .env could sign in' };
846
+ const out = { ran: true, tried: [], findings: [], left: [] };
847
+ for (const a of routes) {
848
+ const what = a.method + ' ' + a.template;
849
+ const t = { what, verdict: 'no account was let in', field: null };
850
+ out.tried.push(t);
851
+ for (const s of sessions) {
852
+ const before = await s.send('GET', a.template, {});
853
+ if (before.status !== 200 || before.json === undefined) continue;
854
+ 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);
855
+ if (!field) { t.verdict = 'no plain field to change in GET ' + a.template; break; }
856
+ const old = valueIn(before.json, field);
857
+ const changed = String(old).slice(0, 60) + ' kx-kept-' + Math.random().toString(36).slice(2, 7);
858
+ const body = { [field]: changed };
859
+ const res = await s.send(a.method, a.template, a.encoding === 'form' ? { form: body } : { json: body });
860
+ const reply = authz.replyOf(res);
861
+ if (reply.kind !== 'accepted' && reply.kind !== 'redirect') continue;
862
+ const after = await s.send('GET', a.template, {});
863
+ const now = after.status === 200 ? valueIn(after.json, field) : undefined;
864
+ t.field = field;
865
+ t.by = s.email;
866
+ if (now === changed) {
867
+ t.verdict = 'kept';
868
+ const back = await s.send(a.method, a.template, a.encoding === 'form' ? { form: { [field]: old } } : { json: { [field]: old } });
869
+ const again = await s.send('GET', a.template, {});
870
+ if (valueIn(again.json, field) !== old) out.left.push(what + ': ' + field + ' is now "' + changed + '" (it was "' + old + '"; putting it back answered ' + back.status + ')');
871
+ break;
872
+ }
873
+ t.verdict = 'not kept';
874
+ const echoed = JSON.stringify(res.json === undefined ? res.text : res.json).includes(changed);
875
+ const finding = {
876
+ kind: 'not-kept', where: what, field, from: a.from.file + ':' + a.from.line, status: echoed ? 'confirmed' : 'verification required', severity: 'MEDIUM',
877
+ 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.',
878
+ 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 + '".'],
879
+ };
880
+ finding.fixPrompt = ['Kryptheon changed a setting of my running app and read it back:', '', finding.headline, '', 'Where: ' + finding.from, '',
881
+ '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));
882
+ out.findings.push(finding);
883
+ break;
884
+ }
885
+ }
886
+ return out;
887
+ }
888
+
889
+ // --- a job run twice ------------------------------------------------------------
890
+
891
+ // A route that runs a job: /jobs/..., or one named for what jobs do.
892
+ const JOB_PATH = /(^|\/)(jobs?|cron|tasks?\/run|reminders?|digest|notify[\w-]*|sync|overdue|batch)(\/|$)/i;
893
+ // What a job's answer counts: created 3, sent: 12, notified: 2.
894
+ const COUNT_KEY = /^(created|inserted|sent|queued|notified|processed|affected|rows|count|total|emails|events|reminders|jobs)$/i;
895
+
896
+ /** The counts a job's answer gives: { created: 3 }. */
897
+ function countsIn(json) {
898
+ const out = {};
899
+ if (!json || typeof json !== 'object' || Array.isArray(json)) return out;
900
+ for (const k of Object.keys(json)) if (COUNT_KEY.test(k) && typeof json[k] === 'number') out[k] = json[k];
901
+ return out;
902
+ }
903
+
904
+ /**
905
+ * Each job route run twice in a row by an account it lets in, nothing changed in between. The same
906
+ * non-zero count the second time means the job made the same things again (a second notification
907
+ * for the same overdue return): a thing to check, said with both answers. { ran, why, tried, findings, left }.
908
+ */
909
+ async function runJobs(root, options) {
910
+ const opts = options || {};
911
+ const plan = planOf(root, opts);
912
+ if (!plan.ran) return { ran: false, why: plan.why };
913
+ const j = plan.journey;
914
+ const routes = j.actions.filter((a) => a.method === 'POST' && !a.params.length && JOB_PATH.test(a.template));
915
+ if (!routes.length) return { ran: false, why: 'the map found no route that runs a job' };
916
+ let origin;
917
+ try {
918
+ origin = authz.assertLocalTarget(opts.origin);
919
+ } catch (err) {
920
+ return { ran: false, why: 'the app must be on this computer (localhost or 127.0.0.1): ' + (opts.origin || 'no address was given') };
921
+ }
922
+ if (!j.signIn) return { ran: false, why: j.signInWhy };
923
+ 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 });
924
+ const sessions = [];
925
+ for (const a of j.accounts.usable) {
926
+ const s = authz.makeSession(ctx, 'account', a.email, a.password);
927
+ const f = { [j.signIn.emailField]: a.email, [j.signIn.passwordField]: a.password };
928
+ const r = authz.replyOf(await s.send(j.signIn.method, j.signIn.path, j.signIn.encoding === 'form' ? { form: f } : { json: f }));
929
+ if (r.kind !== 'refused' && r.kind !== 'server-error' && r.kind !== 'no-answer') sessions.push(s);
930
+ }
931
+ if (!sessions.length) return { ran: false, why: 'no account with a password in .env could sign in' };
932
+ const out = { ran: true, tried: [], findings: [], left: [] };
933
+ for (const a of routes) {
934
+ const what = a.method + ' ' + a.template;
935
+ const send = (s) => s.send(a.method, a.template, a.encoding === 'form' ? { form: a.fullData || {} } : { json: a.fullData || {} });
936
+ let first = null;
937
+ let by = null;
938
+ for (const s of sessions) {
939
+ const res = await send(s);
940
+ const reply = authz.replyOf(res);
941
+ if (reply.kind === 'accepted' || reply.kind === 'redirect') { first = res; by = s; break; }
942
+ }
943
+ if (!first) { out.tried.push({ what, verdict: 'no account was let in' }); continue; }
944
+ const second = await send(by);
945
+ const c1 = countsIn(first.json);
946
+ const c2 = countsIn(second.json);
947
+ const again = Object.keys(c1).filter((k) => c1[k] > 0 && c2[k] === c1[k]);
948
+ out.tried.push({ what, by: by.email, first: c1, second: c2, verdict: again.length ? 'made the same again' : Object.keys(c1).length ? 'held' : 'no count to compare' });
949
+ out.left.push(what + ' run twice as ' + by.email + (Object.keys(c1).length ? ' (' + JSON.stringify(c1) + ', then ' + JSON.stringify(c2) + ')' : ''));
950
+ if (!again.length) continue;
951
+ const k = again[0];
952
+ const finding = {
953
+ kind: 'job-repeat', where: what, from: a.from.file + ':' + a.from.line, status: 'verification required', severity: 'MEDIUM',
954
+ headline: 'Run twice in a row, ' + what + ' made the same ' + c2[k] + ' again (' + k + ': ' + c1[k] + ', then ' + k + ': ' + c2[k] + '), with nothing changed in between: the same reminders or records may be made every time it runs.',
955
+ proof: ['As ' + by.email + ', sent ' + what + ' twice, one right after the other.', 'first answer: ' + authz.clip(first.text, 120), 'second answer: ' + authz.clip(second.text, 120)],
956
+ };
957
+ finding.fixPrompt = ['Kryptheon ran a job of my app twice in a row:', '', finding.headline, '', 'Where: ' + finding.from, '',
958
+ 'First check this is real: look at what the job writes. If running it again makes the same rows again, make it idempotent - remember what it already did (a unique key on the item and the run, or a "notified_at" on the item) and skip what was done. Change nothing else.', '', 'Afterwards I will run: npx kryptheon api'].join(String.fromCharCode(10));
959
+ out.findings.push(finding);
960
+ }
961
+ return out;
962
+ }
963
+
964
+ /**
965
+ * The map's "a status set with no look at the status now" items, tried: the
966
+ * step sent for one record, then sent again for the same record. Judged by
967
+ * the numbers in the app's own lists between the two: when the second call
968
+ * moves them too (stock added again), the step is done twice. Round 25:
969
+ * POST /api/orders/:id/receive added the same order's lines to stock again.
970
+ * { ran, why, tried, findings, left }.
971
+ */
972
+ async function runSteps(root, options) {
973
+ const opts = options || {};
974
+ const plan = planOf(root, opts);
975
+ if (!plan.ran) return { ran: false, why: plan.why };
976
+ const j = plan.journey;
977
+ const items = (opts.toCheck || map.toCheck(plan.map)).filter((t) => t.kind === 'no-state-check');
978
+ if (!items.length) return { ran: false, why: 'the map found no step that sets a status without looking at it' };
979
+ let origin;
980
+ try {
981
+ origin = authz.assertLocalTarget(opts.origin);
982
+ } catch (err) {
983
+ return { ran: false, why: 'the app must be on this computer (localhost or 127.0.0.1): ' + (opts.origin || 'no address was given') };
984
+ }
985
+ if (!j.signIn) return { ran: false, why: j.signInWhy };
986
+ 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 });
987
+ const sessions = [];
988
+ for (const a of j.accounts.usable) {
989
+ const s = authz.makeSession(ctx, 'account', a.email, a.password);
990
+ const f = { [j.signIn.emailField]: a.email, [j.signIn.passwordField]: a.password };
991
+ const r = authz.replyOf(await s.send(j.signIn.method, j.signIn.path, j.signIn.encoding === 'form' ? { form: f } : { json: f }));
992
+ if (r.kind !== 'refused' && r.kind !== 'server-error' && r.kind !== 'no-answer') sessions.push(s);
993
+ }
994
+ if (!sessions.length) return { ran: false, why: 'no account with a password in .env could sign in' };
995
+ const lists = j.actions.filter((x) => x.method === 'GET' && !x.params.length).map((x) => x.template);
996
+ const snap = async (s) => {
997
+ const o = {};
998
+ for (const t of lists) {
999
+ const r = await s.send('GET', t, {});
1000
+ if (r.status === 200 && r.json !== undefined) o[t] = r.json;
1001
+ }
1002
+ return o;
1003
+ };
1004
+ const out = { ran: true, tried: [], findings: [], left: [] };
1005
+ for (const item of items) {
1006
+ const what = normRoute(item.route);
1007
+ const a = j.actions.find((x) => x.method + ' ' + x.template === what);
1008
+ 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; }
1009
+ // the records: the list the step's address starts with (/api/orders for /api/orders/{id}/receive)
1010
+ const listPath = a.segs.slice(0, a.params[0].index).join('/');
1011
+ const listTemplate = '/' + listPath;
1012
+ const route = (plan.map.routes || []).find((r) => r.method + ' ' + normRoute(r.path || '') === what);
1013
+ const sets = route && (route.db || []).find((d) => d.setsState);
1014
+ const target = sets ? sets.setsState.value : null;
1015
+ let done = null;
1016
+ for (const s of sessions) {
1017
+ const r = await s.send('GET', listTemplate, {});
1018
+ 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;
1019
+ if (!recs || !recs.length) continue;
1020
+ // a record not already at the status the step sets: the first call is the step's own, the second the repeat
1021
+ const statusKey = Object.keys(recs[0]).find((k) => /^(status|state|stage)$/i.test(k));
1022
+ const rec = recs.find((x) => x && x.id !== undefined && (!statusKey || !target || String(x[statusKey]) !== target)) || recs.find((x) => x && x.id !== undefined);
1023
+ if (!rec) continue;
1024
+ const at = a.template.replace('{id}', encodeURIComponent(String(rec.id)));
1025
+ const send = () => s.send(a.method, at, a.encoding === 'form' ? { form: a.fullData || {} } : { json: a.fullData || {} });
1026
+ const first = await send();
1027
+ const r1 = authz.replyOf(first);
1028
+ if (r1.kind !== 'accepted' && r1.kind !== 'redirect') continue;
1029
+ const s1 = await snap(s);
1030
+ const second = await send();
1031
+ const r2 = authz.replyOf(second);
1032
+ const s2 = await snap(s);
1033
+ done = { s, rec, at, first, second, r2, moved: numbersMoved(s1, s2) };
1034
+ break;
1035
+ }
1036
+ if (!done) { out.tried.push({ what, verdict: 'no account was let in' }); continue; }
1037
+ const again = (done.r2.kind === 'accepted' || done.r2.kind === 'redirect') && done.moved.length > 0;
1038
+ 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' });
1039
+ 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' : ''));
1040
+ if (!again) continue;
1041
+ const finding = {
1042
+ kind: 'state-repeat', where: what, from: item.where, status: 'confirmed', severity: 'HIGH',
1043
+ 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 : '') + '.',
1044
+ proof: ['As ' + done.s.email + ', sent ' + a.method + ' ' + done.at + ': answered ' + done.first.status + '.', 'Sent it again, unchanged: answered ' + done.second.status + '.']
1045
+ .concat(done.moved.slice(0, 3).map((x) => 'Read between the two calls and after the second: ' + x.where + ' ' + x.before + ' -> ' + x.after)),
1046
+ };
1047
+ finding.fixPrompt = ['Kryptheon sent the same step twice to my running app, and it was done twice:', '', finding.headline, '', 'Where: ' + finding.from, '',
1048
+ '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));
1049
+ out.findings.push(finding);
1050
+ }
1051
+ return out;
1052
+ }
1053
+
1054
+ /** The numbers that differ between two reads of the same lists: [{ where, before, after }]. */
1055
+ function numbersMoved(before, after) {
1056
+ const b = numbersOf(before);
1057
+ const out = [];
1058
+ 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 });
1059
+ return out;
1060
+ }
1061
+
1062
+
1063
+ /** Whether the app has stopped answering: a request that got no answer, and then a plain read that gets none either. */
1064
+ async function appDown(session, template) {
1065
+ const again = await session.send('GET', template, {});
1066
+ if (again.status !== 0) return false;
1067
+ const root = await session.send('GET', '/', {});
1068
+ return root.status === 0;
1069
+ }
1070
+
1071
+ function downFinding(email, what, f, pr, res) {
1072
+ const finding = {
1073
+ kind: 'app-down', where: what, field: f.name, from: f.file + ':' + f.line, status: 'confirmed', severity: 'HIGH',
1074
+ headline: what + ' with ' + f.name + ' ' + pr.why + ' stopped the app: it answered nothing afterwards, to that or any request. One request like this takes the whole app down for everyone.',
1075
+ proof: ['As ' + email + ', sent ' + what + ' with ' + f.name + ' ' + pr.why + ' and every other field valid.', 'No answer (' + (res.error || 'the connection failed') + '), and no answer to a plain read of ' + what.split(' ')[1] + ' and of / right after.', 'Nothing more was sent: the rest of the bad-value tries were not made.'],
1076
+ };
1077
+ finding.fixPrompt = ['Kryptheon sent one bad value to my running app and the whole app stopped:', '', finding.headline, '', 'Where: ' + finding.from, '',
1078
+ 'First check this is real: start the app, send that request, and see the process exit. Then find why an error in this handler is not caught - an async handler whose rejected promise reaches nothing (Express 4 does not catch those), or a database error thrown outside try/catch. Catch it and answer 400 for a value that is not what the field should hold, and add a last-resort error handler so one bad request can never stop the server. Change nothing else.', '', 'Afterwards I will run: npx kryptheon api'].join(String.fromCharCode(10));
1079
+ return finding;
1080
+ }
1081
+
700
1082
  // Names of fields that are plainly required, so a blank one is worth trying even where the code checks nothing.
701
1083
  const REQUIRED_NAME = /^(name|title|subject|address|city|street|postcode|postal|zip|recipient|reason|label)$/;
702
1084
  const NOT_PLAIN_TEXT = /^(email|mail|date|day|at|on|url|link|website|phone|mobile|role|status|state|currency|country|id|key|token)$/;
@@ -707,6 +1089,33 @@ const NOT_PLAIN_TEXT = /^(email|mail|date|day|at|on|url|link|website|phone|mobil
707
1089
  * blank - a text field the code requires, or is plainly required by its name, is left empty
708
1090
  * below - a number field gets -1
709
1091
  */
1092
+ const CHANGE_WORDS = new Set(['delta', 'change', 'adjust', 'adjustment', 'diff', 'difference', 'increment', 'decrement']);
1093
+ const OVERDRAW = 999999;
1094
+
1095
+ /** Every number in some lists, by where it is: '/api/stock.items[id 1].on_hand' -> 18. Rows are known by their id. */
1096
+ function numbersOf(snap) {
1097
+ const out = new Map();
1098
+ const walk = (v, at) => {
1099
+ if (typeof v === 'number') out.set(at, v);
1100
+ else if (Array.isArray(v)) v.forEach((x, i) => walk(x, at + '[' + (x && typeof x === 'object' && x.id !== undefined ? 'id ' + x.id : i) + ']'));
1101
+ else if (v && typeof v === 'object') for (const k of Object.keys(v)) walk(v[k], at + '.' + k);
1102
+ };
1103
+ for (const t of Object.keys(snap)) walk(snap[t], t);
1104
+ return out;
1105
+ }
1106
+
1107
+ /**
1108
+ * Numbers in the app's lists that are below zero after and were not before:
1109
+ * [{ where, before, after }]. Records are known by their id, so a row that
1110
+ * moves in a list is still the same row.
1111
+ */
1112
+ function belowZero(before, after) {
1113
+ const b = numbersOf(before);
1114
+ const out = [];
1115
+ 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 });
1116
+ return out;
1117
+ }
1118
+
710
1119
  function probesOf(a) {
711
1120
  const out = [];
712
1121
  for (const f of a.fields || []) {
@@ -719,7 +1128,10 @@ function probesOf(a) {
719
1128
  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)))) {
720
1129
  out.push({ field: f, mode: 'blank', value: '', why: 'left blank' });
721
1130
  }
722
- if (typeof f.sample === 'number' && w[w.length - 1] !== 'id' && (w.some((x) => NUMBER_WORDS.has(x)) || f.kinds.includes('range'))) {
1131
+ // a change by an amount (quantity_delta): a negative one is normal, one larger than anything held is not (round 25)
1132
+ if (typeof f.sample === 'number' && w.some((x) => CHANGE_WORDS.has(x))) {
1133
+ out.push({ field: f, mode: 'overdraw', value: -OVERDRAW, why: 'a negative change larger than anything held (-' + OVERDRAW + ')' });
1134
+ } else if (typeof f.sample === 'number' && w[w.length - 1] !== 'id' && (w.some((x) => NUMBER_WORDS.has(x)) || f.kinds.includes('range'))) {
723
1135
  out.push({ field: f, mode: 'below', value: -1, why: 'a negative number (-1)' });
724
1136
  }
725
1137
  }
@@ -741,7 +1153,7 @@ const normRoute = (name) => String(name).replace(/:\w+\*?/g, '{id}');
741
1153
  * organizations; no-role <- the roles run (needs the owner's permissions file);
742
1154
  * a webhook is the payments check's.
743
1155
  */
744
- function verdictsFor(toCheck, wall, invalid, roleRun) {
1156
+ function verdictsFor(toCheck, wall, invalid, roleRun, forged, webhooks, steps) {
745
1157
  const out = [];
746
1158
  const attempts = wall && wall.ran ? wall.result.attempts : [];
747
1159
  const failures = (wall && wall.findings) || [];
@@ -751,9 +1163,38 @@ function verdictsFor(toCheck, wall, invalid, roleRun) {
751
1163
  const route = normRoute(t.route);
752
1164
  const v = { route: t.route, kind: t.kind, headline: t.headline };
753
1165
  const kind = /^no-sign-in$/.test(t.kind) ? 'signin' : /^tenant-/.test(t.kind) ? 'tenant' : /^no-role$/.test(t.kind) ? 'role' : null;
754
- if (/^webhook/.test(t.kind)) {
755
- v.verdict = 'not tried';
756
- v.why = 'a webhook: the payments check sends it, signed and unsigned (npx kryptheon payments)';
1166
+ if (t.kind === 'no-state-check') {
1167
+ const hit = steps && steps.ran ? steps.findings.find((f) => f.where === route) : null;
1168
+ const tried = steps && steps.ran ? steps.tried.find((x) => x.what === route) : null;
1169
+ if (hit) {
1170
+ v.verdict = 'confirmed';
1171
+ v.finding = hit.headline;
1172
+ } else if (tried && /^(refused the second time|nothing moved the second time)$/.test(tried.verdict)) {
1173
+ v.verdict = 'held';
1174
+ } else {
1175
+ v.verdict = 'not tried';
1176
+ v.why = tried ? tried.why || tried.verdict : steps && !steps.ran ? steps.why : 'the step was not sent twice';
1177
+ }
1178
+ } else if (t.kind === 'identity-from-request') {
1179
+ const hit = forged && forged.ran && forged.findings.find((f) => f.from === t.where);
1180
+ v.verdict = hit ? 'confirmed' : forged && forged.ran && forged.tried.length ? 'held' : 'not tried';
1181
+ if (hit) v.finding = hit.headline;
1182
+ if (v.verdict === 'not tried') v.why = forged ? forged.why || 'not tried' : 'the forged identity was not tried';
1183
+ } else if (/^webhook/.test(t.kind)) {
1184
+ const hook = webhooks && webhooks.ran ? webhooks.hooks.find((x) => x.route === t.route) : null;
1185
+ const hit = hook && hook.findings.find((f) => /^webhook-(unsigned|forged)$/.test(f.kind) && f.status === 'confirmed');
1186
+ if (hit) {
1187
+ v.verdict = 'confirmed';
1188
+ v.finding = hit.headline;
1189
+ } 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))) {
1190
+ v.verdict = 'held';
1191
+ } else if (hook && hook.findings.some((f) => /^webhook-(unsigned|forged)$/.test(f.kind))) {
1192
+ v.verdict = 'not judged';
1193
+ v.why = 'the app answered success without a valid signature, but no change was seen in ' + hook.list;
1194
+ } else {
1195
+ v.verdict = 'not tried';
1196
+ v.why = (hook && hook.notTried ? hook.notTried : webhooks && !webhooks.ran ? webhooks.why : 'a webhook this run did not send to') + '; a provider\'s own webhook is the payments check\'s (npx kryptheon payments)';
1197
+ }
757
1198
  } else if (kind === 'role') {
758
1199
  // "held" only when the roles run really tried this route as a role the policy forbids
759
1200
  const tries = roleRun && roleRun.ran ? roleRun.result.attempts.filter((a) => !a.notTried && a.kind === 'role' && a.method + ' ' + a.template === route) : [];
@@ -799,6 +1240,90 @@ function verdictsFor(toCheck, wall, invalid, roleRun) {
799
1240
  return out;
800
1241
  }
801
1242
 
1243
+ // --- who is calling, as the request says: forged on the running app ----------------
1244
+
1245
+ const IDENTITY_ROUTE = /(^|\/)(me|whoami|session|profile|account)$/i;
1246
+
1247
+ /** The id of the person an identity answer names: { id, email } or null. */
1248
+ function personIn(json) {
1249
+ const look = (v, depth) => {
1250
+ if (!v || typeof v !== 'object' || depth > 2 || Array.isArray(v)) return null;
1251
+ if (['string', 'number'].includes(typeof v.id) && typeof v.email === 'string') return { id: v.id, email: v.email };
1252
+ for (const k of Object.keys(v)) {
1253
+ const f = look(v[k], depth + 1);
1254
+ if (f) return f;
1255
+ }
1256
+ return null;
1257
+ };
1258
+ return look(json, 0);
1259
+ }
1260
+
1261
+ /**
1262
+ * For each sign-in check the map says takes who is calling from a cookie or header holding the bare
1263
+ * user id: with no password at all, a request carrying another account's id there. Answered as that
1264
+ * account = confirmed. Reads only (the app's "who am I" route). { ran, why, findings, tried }.
1265
+ */
1266
+ async function runForged(root, options) {
1267
+ const opts = options || {};
1268
+ const say = typeof opts.say === 'function' ? opts.say : () => {};
1269
+ const plan = planOf(root, opts);
1270
+ if (!plan.ran) return { ran: false, why: plan.why };
1271
+ const items = map.toCheck(plan.map).filter((t) => t.kind === 'identity-from-request');
1272
+ if (!items.length) return { ran: false, why: 'no sign-in check takes who is calling from a bare id in a cookie or header' };
1273
+ let origin;
1274
+ try {
1275
+ origin = authz.assertLocalTarget(opts.origin);
1276
+ } catch (err) {
1277
+ return { ran: false, why: 'the app must be on this computer (localhost or 127.0.0.1): ' + (opts.origin || 'no address was given') };
1278
+ }
1279
+ const j = plan.journey;
1280
+ if (!j.signIn) return { ran: false, why: j.signInWhy };
1281
+ const me = (plan.map.routes || []).find((r) => r.method === 'GET' && r.path && IDENTITY_ROUTE.test(r.path) && r.auth && r.auth.status === map.STATUS.DETECTED);
1282
+ if (!me) return { ran: false, why: 'no "who am I" route (GET /api/me or the like) behind the sign-in check, so an answer cannot be tied to a person' };
1283
+ const accounts = j.accounts.usable.slice(0, 4);
1284
+ if (accounts.length < 1) return { ran: false, why: 'no account has a password in .env' };
1285
+ const passwords = {};
1286
+ for (const a of accounts) passwords[a.email] = a.password;
1287
+ const ctx = authz.makeContext({ baseUrl: origin, allowOrigins: [origin], fetch: opts.fetch, passwords: passwords, maxRequests: 60 });
1288
+ // who each account is, as the app says when they sign in properly
1289
+ const people = [];
1290
+ for (const a of accounts) {
1291
+ const s = authz.makeSession(ctx, 'account', a.email, a.password);
1292
+ const fields = { [j.signIn.emailField]: a.email, [j.signIn.passwordField]: a.password };
1293
+ const r = authz.replyOf(await s.send(j.signIn.method, j.signIn.path, j.signIn.encoding === 'form' ? { form: fields } : { json: fields }));
1294
+ if (r.kind === 'refused' || r.kind === 'server-error' || r.kind === 'no-answer') continue;
1295
+ const res = await s.send('GET', me.path, {});
1296
+ const who = res.status === 200 ? personIn(res.json) : null;
1297
+ if (who && who.email.toLowerCase() === a.email.toLowerCase()) people.push(who);
1298
+ }
1299
+ if (!people.length) return { ran: false, why: 'signed in, but ' + me.path + ' did not say who anyone is' };
1300
+ say('Asking ' + me.path + ' with nobody signed in, carrying another person\'s id where the sign-in check reads it...');
1301
+ const out = { ran: true, findings: [], tried: [], route: me.path };
1302
+ for (const item of items) {
1303
+ const { kind, name } = item.identity;
1304
+ for (const person of people) {
1305
+ const forger = authz.makeSession(ctx, 'forger', '(nobody signed in)', null);
1306
+ if (kind === 'cookie') forger.jar.set(name, String(person.id));
1307
+ const res = await forger.send('GET', me.path, kind === 'header' ? { headers: { [name]: String(person.id) } } : {});
1308
+ const got = res.status === 200 ? personIn(res.json) : null;
1309
+ const isThem = !!(got && String(got.id) === String(person.id) && got.email.toLowerCase() === person.email.toLowerCase());
1310
+ out.tried.push({ by: kind + ' ' + name, as: person.email, status: res.status, verdict: isThem ? 'answered as them' : 'refused' });
1311
+ if (!isThem) continue;
1312
+ const sent = kind === 'cookie' ? 'cookie ' + name + '=' + person.id : 'header ' + name + ': ' + person.id;
1313
+ const finding = {
1314
+ kind: 'forged-identity', where: 'GET ' + me.path, from: item.where, status: 'confirmed', severity: 'CRITICAL',
1315
+ headline: 'Anyone can be any user: with no password and nobody signed in, a request carrying ' + sent + ' was answered as ' + person.email + '. ' + item.headline + '.',
1316
+ proof: ['With nobody signed in (no password sent), set ' + sent + ' - the id ' + me.path + ' gives ' + person.email + ' when they sign in.', 'sent: GET ' + me.path, 'answer: ' + res.status + ' ' + authz.clip(res.text, 160)],
1317
+ };
1318
+ finding.fixPrompt = ['Kryptheon showed that anyone can act as any user of my running app:', '', finding.headline, '', 'Where: ' + item.where, '',
1319
+ 'First check this is real: with no session, send that request yourself. If it is, ' + map.ADVICE['identity-from-request'] + ' Change nothing else.', '', 'Afterwards I will run: npx kryptheon api'].join(String.fromCharCode(10));
1320
+ out.findings.push(finding);
1321
+ break; // one person proves it for this check
1322
+ }
1323
+ }
1324
+ return out;
1325
+ }
1326
+
802
1327
  /**
803
1328
  * The whole run on an app on this computer: across organizations and without
804
1329
  * a session, then bad values, then (only with the owner's permissions file and
@@ -812,19 +1337,26 @@ async function runApi(root, options) {
812
1337
  const toCheck = opts.toCheck || map.toCheck(m);
813
1338
  const shared = Object.assign({}, opts, { map: m, say: say });
814
1339
  const wall = await runWall(root, shared);
815
- const invalid = await runInvalid(root, shared);
1340
+ const forged = await runForged(root, shared);
1341
+ const webhooks = await webhook.run(root, shared);
1342
+ const exportsRun = await csv.run(root, shared);
1343
+ const jobsRun = await runJobs(root, shared);
1344
+ const keptRun = await runKept(root, shared);
1345
+ const stepsRun = await runSteps(root, Object.assign({}, shared, { toCheck: toCheck }));
816
1346
  let roleRun = null;
817
1347
  if (opts.roles) roleRun = await runRoles(root, shared);
818
- const verdicts = verdictsFor(toCheck, wall, invalid, roleRun);
1348
+ // the bad values always last: one of them may stop the app (round 24 lost its whole roles run that way)
1349
+ const invalid = await runInvalid(root, shared);
1350
+ const verdicts = verdictsFor(toCheck, wall, invalid, roleRun, forged, webhooks, stepsRun);
819
1351
  const seen = new Set();
820
- const findings = (wall.ran ? wall.findings : []).concat(roleRun && roleRun.ran ? roleRun.findings : []).concat(invalid.ran ? invalid.findings : []).filter((f) => {
1352
+ 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(stepsRun.ran ? stepsRun.findings : []).concat(roleRun && roleRun.ran ? roleRun.findings : []).concat(invalid.ran ? invalid.findings : []).filter((f) => {
821
1353
  const k = f.kind + '|' + f.where + '|' + (f.field || '');
822
1354
  return seen.has(k) ? false : (seen.add(k), true);
823
1355
  });
824
- const left = (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) : []);
1356
+ const left = (webhooks.ran ? webhooks.left : []).concat(exportsRun.ran ? exportsRun.left : []).concat(jobsRun.ran ? jobsRun.left : []).concat(keptRun.ran ? keptRun.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) : []);
825
1357
  return {
826
1358
  ran: wall.ran || invalid.ran, why: wall.ran || invalid.ran ? null : wall.why, checkedAt: new Date().toISOString(),
827
- origin: opts.origin, wall: wall, invalid: invalid, roles: roleRun, verdicts: verdicts, findings: findings, left: left,
1359
+ origin: opts.origin, wall: wall, forged: forged, webhooks: webhooks, exports: exportsRun, jobs: jobsRun, kept: keptRun, steps: stepsRun, invalid: invalid, roles: roleRun, verdicts: verdicts, findings: findings, left: left,
828
1360
  notTried: (wall.plan ? wall.plan.notTried : []).concat(invalid.ran ? invalid.notTried : []),
829
1361
  };
830
1362
  }
@@ -837,6 +1369,7 @@ function reportOf(run) {
837
1369
  accounts: run.wall && run.wall.ran ? run.wall.result.accounts.map((a) => a.email) : run.invalid && run.invalid.ran ? [run.invalid.account] : [],
838
1370
  tries: { acrossOrganizationsAndNoSession: wallTries, badValues: run.invalid && run.invalid.ran ? run.invalid.tried.length : 0 },
839
1371
  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 })),
1372
+ 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 })) })) : [],
840
1373
  verdicts: run.verdicts, left: run.left, notTried: run.notTried,
841
1374
  };
842
1375
  }
@@ -868,8 +1401,69 @@ function apiLines(run) {
868
1401
  }
869
1402
  out.push('');
870
1403
  }
1404
+ if (run.forged && run.forged.ran) {
1405
+ const got = run.forged.tried.filter((t) => t.verdict === 'answered as them').length;
1406
+ out.push(' Who is calling, as the request says (' + run.forged.route + ', nobody signed in): ' + got + ' answered as another person, ' + (run.forged.tried.length - got) + ' refused');
1407
+ for (const f of run.forged.findings) {
1408
+ out.push(' PROBLEM ' + f.severity + ' ' + f.headline + ' (' + f.from + ')');
1409
+ for (const l of f.proof) out.push(' ' + l);
1410
+ }
1411
+ out.push('');
1412
+ }
1413
+ if (run.webhooks && run.webhooks.ran) {
1414
+ for (const h of run.webhooks.hooks) {
1415
+ if (h.notTried) {
1416
+ out.push(' Webhook ' + h.route + ': not tried - ' + h.notTried);
1417
+ continue;
1418
+ }
1419
+ 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');
1420
+ for (const f of h.findings) {
1421
+ out.push(' ' + (f.status === 'confirmed' ? 'PROBLEM' : 'TO CHECK') + ' ' + f.severity + ' ' + f.headline + ' (' + f.from + ')');
1422
+ for (const l of f.proof) out.push(' ' + l);
1423
+ }
1424
+ }
1425
+ out.push('');
1426
+ } else if (run.webhooks && run.webhooks.why && !/no webhook route/.test(run.webhooks.why)) {
1427
+ out.push(' Webhooks not tried: ' + run.webhooks.why, '');
1428
+ }
1429
+ if (run.exports && run.exports.ran) {
1430
+ for (const t of run.exports.tried) out.push(' Export ' + t.what + ': ' + (t.verdict === 'raw' ? 'writes ' + t.field + ' as typed' : t.verdict === 'escaped' ? t.field + ' is written with a mark in front of "=" - held' : 'a marked value made through the app did not appear in it - not judged'));
1431
+ for (const f of run.exports.findings) {
1432
+ out.push(' PROBLEM ' + f.severity + ' ' + f.headline + ' (' + f.from + ')');
1433
+ for (const l of f.proof) out.push(' ' + l);
1434
+ }
1435
+ out.push('');
1436
+ }
1437
+ if (run.kept && run.kept.ran) {
1438
+ for (const t of run.kept.tried) out.push(' Change ' + t.what + (t.field ? ' (' + t.field + ')' : '') + ': ' + t.verdict);
1439
+ for (const f of run.kept.findings) {
1440
+ out.push(' ' + (f.status === 'confirmed' ? 'PROBLEM' : 'TO CHECK') + ' ' + f.severity + ' ' + f.headline + ' (' + f.from + ')');
1441
+ for (const l of f.proof) out.push(' ' + l);
1442
+ }
1443
+ out.push('');
1444
+ }
1445
+ if (run.jobs && run.jobs.ran) {
1446
+ 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) + ')' : ''));
1447
+ for (const f of run.jobs.findings) {
1448
+ out.push(' TO CHECK ' + f.severity + ' ' + f.headline + ' (' + f.from + ')');
1449
+ for (const l of f.proof) out.push(' ' + l);
1450
+ }
1451
+ out.push('');
1452
+ }
1453
+ if (run.steps && run.steps.ran) {
1454
+ 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 + ')' : ''));
1455
+ for (const f of run.steps.findings) {
1456
+ out.push(' PROBLEM ' + f.severity + ' ' + f.headline + ' (' + f.from + ')');
1457
+ for (const l of f.proof) out.push(' ' + l);
1458
+ }
1459
+ out.push('');
1460
+ }
1461
+ if (run.invalid && run.invalid.ran && run.invalid.appDown) {
1462
+ out.push(' THE APP STOPPED ANSWERING after ' + run.invalid.appDown + '. Nothing more was sent; start it again before any other check.');
1463
+ out.push('');
1464
+ }
871
1465
  if (run.invalid && run.invalid.ran) {
872
- 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');
1466
+ 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');
873
1467
  for (const f of run.invalid.findings) {
874
1468
  out.push(' PROBLEM ' + f.severity + ' ' + f.headline + ' (' + f.from + ')');
875
1469
  for (const l of f.proof) out.push(' ' + l);
@@ -893,4 +1487,4 @@ function apiLines(run) {
893
1487
  return out;
894
1488
  }
895
1489
 
896
- module.exports = { 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 };
1490
+ module.exports = { runSteps, numbersMoved, runKept, 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 };