kryptheon 0.1.57 → 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/README.md CHANGED
@@ -324,7 +324,9 @@ anything it could not put back.
324
324
  signature that changed a record is a confirmed problem. The secret is never
325
325
  printed or saved. A signature made from more than the body (a timestamp in
326
326
  front, a prefix) is said as not tried. A provider's own signed webhook still
327
- goes through `npx kryptheon payments`.
327
+ goes through `npx kryptheon payments`. The first correctly signed event is
328
+ also sent a second time, unchanged; changing the record again is a confirmed
329
+ problem - a provider resends events whenever it is not sure.
328
330
  - **Exports:** for a CSV export (a GET ending in `.csv`, or under `/export`),
329
331
  a record is made through your own create route with its text fields set to a
330
332
  marked value starting with `=`, and the file is downloaded. A cell written as
@@ -334,6 +336,11 @@ anything it could not put back.
334
336
  twice in a row. If its answer counts the same non-zero number both times
335
337
  (`created: 11`, then `created: 11`), it is a thing to check: the job may make
336
338
  the same reminders or records every time it runs.
339
+ - **Changes that are kept:** a change route that is also read at the same
340
+ address (`PATCH /api/settings` and `GET /api/settings`) gets one plain text
341
+ field changed - never a role, a status, an organization or money - by an
342
+ account it lets in, then read back. Answered as done but still the old value
343
+ is a problem; a change that was kept is put back.
337
344
  - **Bad values:** for each route that creates something, one field at a time
338
345
  gets a value that should be turned away - text for a number and 3000
339
346
  characters for a field the code never checks, a blank name or a negative
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
@@ -143,6 +159,7 @@ function allowedByName(m) {
143
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);
144
160
  }
145
161
  }
162
+ out.types = m.columnTypes || {};
146
163
  return out;
147
164
  }
148
165
 
@@ -171,6 +188,16 @@ function actionOf(r, known, roleNames) {
171
188
  fields.push(info);
172
189
  if (f.status === map.STATUS.NOT) unchecked.push({ name: f.name, line: f.line, file: f.file });
173
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
+ }
174
201
  const fullData = Object.assign({}, data);
175
202
  if (/^(PATCH|PUT)$/.test(r.method)) {
176
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]));
@@ -356,7 +383,8 @@ function proofOf(att) {
356
383
  if (att.changes && att.changes.length) {
357
384
  lines.push(' state: changed after the request (read before and after):');
358
385
  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'));
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'));
360
388
  else lines.push(' state: unchanged');
361
389
  return lines;
362
390
  }
@@ -614,6 +642,48 @@ async function runInvalid(root, options) {
614
642
  const res = await session.send('GET', template, {});
615
643
  return res.status === 200 ? JSON.stringify(res.json !== undefined ? res.json : res.text) : null;
616
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
+ };
617
687
  for (const a of j.actions) {
618
688
  if (!/^(POST|PUT|PATCH)$/.test(a.method) || !(a.fields || []).length) continue;
619
689
  const what = a.method + ' ' + a.template;
@@ -643,6 +713,10 @@ async function runInvalid(root, options) {
643
713
  if (opts.stopped && opts.stopped()) return out;
644
714
  const f = pr.field;
645
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
+ }
646
720
  const before = await read(a.template);
647
721
  const res = await session.send(a.method, a.template, a.encoding === 'form' ? { form: body } : { json: body });
648
722
  const reply = authz.replyOf(res);
@@ -710,6 +784,108 @@ async function runInvalid(root, options) {
710
784
 
711
785
  const GHOST_ID = '999999999';
712
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
+
713
889
  // --- a job run twice ------------------------------------------------------------
714
890
 
715
891
  // A route that runs a job: /jobs/..., or one named for what jobs do.
@@ -785,6 +961,104 @@ async function runJobs(root, options) {
785
961
  return out;
786
962
  }
787
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
+
788
1062
 
789
1063
  /** Whether the app has stopped answering: a request that got no answer, and then a plain read that gets none either. */
790
1064
  async function appDown(session, template) {
@@ -815,6 +1089,33 @@ const NOT_PLAIN_TEXT = /^(email|mail|date|day|at|on|url|link|website|phone|mobil
815
1089
  * blank - a text field the code requires, or is plainly required by its name, is left empty
816
1090
  * below - a number field gets -1
817
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
+
818
1119
  function probesOf(a) {
819
1120
  const out = [];
820
1121
  for (const f of a.fields || []) {
@@ -827,7 +1128,10 @@ function probesOf(a) {
827
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)))) {
828
1129
  out.push({ field: f, mode: 'blank', value: '', why: 'left blank' });
829
1130
  }
830
- 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'))) {
831
1135
  out.push({ field: f, mode: 'below', value: -1, why: 'a negative number (-1)' });
832
1136
  }
833
1137
  }
@@ -849,7 +1153,7 @@ const normRoute = (name) => String(name).replace(/:\w+\*?/g, '{id}');
849
1153
  * organizations; no-role <- the roles run (needs the owner's permissions file);
850
1154
  * a webhook is the payments check's.
851
1155
  */
852
- function verdictsFor(toCheck, wall, invalid, roleRun, forged, webhooks) {
1156
+ function verdictsFor(toCheck, wall, invalid, roleRun, forged, webhooks, steps) {
853
1157
  const out = [];
854
1158
  const attempts = wall && wall.ran ? wall.result.attempts : [];
855
1159
  const failures = (wall && wall.findings) || [];
@@ -859,7 +1163,19 @@ function verdictsFor(toCheck, wall, invalid, roleRun, forged, webhooks) {
859
1163
  const route = normRoute(t.route);
860
1164
  const v = { route: t.route, kind: t.kind, headline: t.headline };
861
1165
  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') {
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') {
863
1179
  const hit = forged && forged.ran && forged.findings.find((f) => f.from === t.where);
864
1180
  v.verdict = hit ? 'confirmed' : forged && forged.ran && forged.tried.length ? 'held' : 'not tried';
865
1181
  if (hit) v.finding = hit.headline;
@@ -1023,22 +1339,24 @@ async function runApi(root, options) {
1023
1339
  const wall = await runWall(root, shared);
1024
1340
  const forged = await runForged(root, shared);
1025
1341
  const webhooks = await webhook.run(root, shared);
1026
- // the export and the jobs come before the bad values, which may stop the app
1027
1342
  const exportsRun = await csv.run(root, shared);
1028
1343
  const jobsRun = await runJobs(root, shared);
1029
- const invalid = await runInvalid(root, shared);
1344
+ const keptRun = await runKept(root, shared);
1345
+ const stepsRun = await runSteps(root, Object.assign({}, shared, { toCheck: toCheck }));
1030
1346
  let roleRun = null;
1031
1347
  if (opts.roles) roleRun = await runRoles(root, shared);
1032
- const verdicts = verdictsFor(toCheck, wall, invalid, roleRun, forged, webhooks);
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);
1033
1351
  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) => {
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) => {
1035
1353
  const k = f.kind + '|' + f.where + '|' + (f.field || '');
1036
1354
  return seen.has(k) ? false : (seen.add(k), true);
1037
1355
  });
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) : []);
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) : []);
1039
1357
  return {
1040
1358
  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,
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,
1042
1360
  notTried: (wall.plan ? wall.plan.notTried : []).concat(invalid.ran ? invalid.notTried : []),
1043
1361
  };
1044
1362
  }
@@ -1116,6 +1434,14 @@ function apiLines(run) {
1116
1434
  }
1117
1435
  out.push('');
1118
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
+ }
1119
1445
  if (run.jobs && run.jobs.ran) {
1120
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) + ')' : ''));
1121
1447
  for (const f of run.jobs.findings) {
@@ -1124,12 +1450,20 @@ function apiLines(run) {
1124
1450
  }
1125
1451
  out.push('');
1126
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
+ }
1127
1461
  if (run.invalid && run.invalid.ran && run.invalid.appDown) {
1128
1462
  out.push(' THE APP STOPPED ANSWERING after ' + run.invalid.appDown + '. Nothing more was sent; start it again before any other check.');
1129
1463
  out.push('');
1130
1464
  }
1131
1465
  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');
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');
1133
1467
  for (const f of run.invalid.findings) {
1134
1468
  out.push(' PROBLEM ' + f.severity + ' ' + f.headline + ' (' + f.from + ')');
1135
1469
  for (const l of f.proof) out.push(' ' + l);
@@ -1153,4 +1487,4 @@ function apiLines(run) {
1153
1487
  return out;
1154
1488
  }
1155
1489
 
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 };
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 };
package/kryptheon-code.js CHANGED
@@ -112,7 +112,9 @@ function taglessColumns(root) {
112
112
  if (!entry.name.startsWith('.') && !SKIP_DIRS.has(entry.name)) walk(full, depth + 1);
113
113
  continue;
114
114
  }
115
- if (!/\.sql$/i.test(entry.name)) continue;
115
+ // and a server file that makes its own tables: CREATE TABLE in a string of server.js (round 25)
116
+ const script = /\.(c|m)?(js|ts)$/i.test(entry.name);
117
+ if (!/\.sql$/i.test(entry.name) && !script) continue;
116
118
  let sql;
117
119
  try {
118
120
  if (fs.statSync(full).size > 4 * MAX_BYTES) continue;
@@ -120,6 +122,7 @@ function taglessColumns(root) {
120
122
  } catch (err) {
121
123
  continue;
122
124
  }
125
+ if (script && !/create\s+table/i.test(sql)) continue;
123
126
  read++;
124
127
  sql = sql.replace(/--[^\n]*/g, ' ').replace(/\/\*[\s\S]*?\*\//g, ' ');
125
128
  const add = (name, type) => {
@@ -349,6 +352,13 @@ function classify(node, scope, code, depth, aliases) {
349
352
  const inner = argumentAliases(local.node, node.arguments, scope, local.scope, code, depth, aliases);
350
353
  if (returns.length) return worst(returns.map((r) => classify(r.node, r.scope, code, depth + 1, inner)));
351
354
  }
355
+ // ({ inventory: renderInventory, activity: renderActivity })[view](): as
356
+ // dangerous as the worst of the functions it can pick (blind round 25).
357
+ const picked = pickedFunctions(node, scope);
358
+ if (picked) {
359
+ const returns = picked.flatMap((fn) => returnsOf(fn).map((r) => ({ r: r, fn: fn })));
360
+ if (returns.length) return worst(returns.map((x) => classify(x.r.node, x.r.scope, code, depth + 1, argumentAliases(x.fn.node, node.arguments, scope, x.fn.scope, code, depth, aliases))));
361
+ }
352
362
  // String methods pass through whatever they were called on, and so do
353
363
  // the array methods that only pick or reorder items: notes.filter(...)
354
364
  // holds the same notes.
@@ -443,6 +453,23 @@ function localFunction(name, scope) {
443
453
  return null;
444
454
  }
445
455
 
456
+ /**
457
+ * The functions of this file's own that a call picks by name from an object
458
+ * written out right there - ({ a: renderA, b: renderB })[view]() - or null.
459
+ * Null too when any of them is not a function this file declares.
460
+ */
461
+ function pickedFunctions(call, scope) {
462
+ const callee = call.callee;
463
+ if (!callee || !/MemberExpression$/.test(callee.type) || !callee.computed || callee.object.type !== 'ObjectExpression') return null;
464
+ const fns = [];
465
+ for (const prop of callee.object.properties) {
466
+ const fn = prop.type === 'ObjectProperty' && prop.value.type === 'Identifier' ? localFunction(prop.value.name, scope) : null;
467
+ if (!fn) return null;
468
+ fns.push(fn);
469
+ }
470
+ return fns.length ? fns : null;
471
+ }
472
+
446
473
  /** Every value a function returns, with the scope to judge it in; nested functions excluded. */
447
474
  function returnsOf(fnPath) {
448
475
  if (fnPath.node.body.type !== 'BlockStatement') return [{ node: fnPath.node.body, scope: fnPath.scope }];
@@ -636,12 +663,33 @@ function riskyParts(node, scope, code, aliases, out, depth) {
636
663
  }
637
664
  if (node.type === 'ConditionalExpression') { go(node.consequent); go(node.alternate); return; }
638
665
  if (node.type === 'LogicalExpression') { go(node.left); go(node.right); return; }
666
+ if (node.type === 'AwaitExpression') { go(node.argument); return; }
639
667
  if (/CallExpression$/.test(node.type)) {
640
668
  const mapped = mapCallback(node, scope, code, depth, aliases);
641
669
  if (mapped && mapped.returns.length) {
642
670
  mapped.returns.forEach((r) => go(r.node, r.scope, mapped.inner));
643
671
  return;
644
672
  }
673
+ // a function of this file's own, or one picked from an object of them: the
674
+ // parts are inside what it returns (round 25 named only "html")
675
+ const own = node.callee.type === 'Identifier' ? localFunction(node.callee.name, scope) : null;
676
+ const fns = own ? [own] : pickedFunctions(node, scope);
677
+ const returns = (fns || []).flatMap((fn) => returnsOf(fn).map((r) => ({ r: r, fn: fn })));
678
+ if (returns.length) {
679
+ returns.forEach((x) => go(x.r.node, x.r.scope, argumentAliases(x.fn.node, node.arguments, scope, x.fn.scope, code, depth, aliases)));
680
+ return;
681
+ }
682
+ }
683
+ // const html = await render(): the parts are in what it was set to
684
+ if (node.type === 'Identifier' && !(aliases && Object.prototype.hasOwnProperty.call(aliases, node.name))) {
685
+ const binding = scope && scope.getBinding(node.name);
686
+ const decl = binding && binding.path && binding.path.node;
687
+ if (decl && decl.type === 'VariableDeclarator' && decl.id.type === 'Identifier' && decl.init && !(binding.constantViolations && binding.constantViolations.length) &&
688
+ (/CallExpression$/.test(decl.init.type) || decl.init.type === 'AwaitExpression')) {
689
+ const before = out.length;
690
+ go(decl.init, binding.path.scope);
691
+ if (out.length > before) return;
692
+ }
645
693
  }
646
694
  const text = code.slice(node.start, node.end).replace(/\s+/g, ' ').slice(0, 80);
647
695
  if (!out.some((p) => p.text === text)) out.push({ text: text, origin: origin, line: node.loc ? node.loc.start.line : null });
package/kryptheon-map.js CHANGED
@@ -1302,6 +1302,7 @@ const OTHER_DB = /^(knex|sequelize|typeorm|drizzle-orm|mongoose|mongodb|kysely|o
1302
1302
  function readSchema(ctx, root) {
1303
1303
  const tables = new Map();
1304
1304
  const rlsTables = new Set();
1305
+ const types = new Map(); // column name -> the types it is declared with, in any table
1305
1306
  const texts = [];
1306
1307
  (function walk(dir, depth) {
1307
1308
  if (depth > 6) return;
@@ -1351,8 +1352,17 @@ function readSchema(ctx, root) {
1351
1352
  }
1352
1353
  parts.push(cur);
1353
1354
  for (const p of parts) {
1354
- const w = p.trim().split(/\s+/)[0];
1355
- if (w && !/^(constraint|primary|foreign|unique|check|exclude|like|index|key)$/i.test(w)) cols.add(w.replace(/["`]/g, '').toLowerCase());
1355
+ const words = p.trim().split(/\s+/);
1356
+ const w = words[0];
1357
+ if (w && !/^(constraint|primary|foreign|unique|check|exclude|like|index|key)$/i.test(w)) {
1358
+ const col = w.replace(/["`]/g, '').toLowerCase();
1359
+ cols.add(col);
1360
+ // the column's type, so a request can send a value it can hold (round 25: "true kx-..." into a boolean)
1361
+ if (words[1]) {
1362
+ if (!types.has(col)) types.set(col, new Set());
1363
+ types.get(col).add(words[1].replace(/\(.*$/, '').toLowerCase());
1364
+ }
1365
+ }
1356
1366
  }
1357
1367
  tables.set(bare(m[1]), cols);
1358
1368
  }
@@ -1367,6 +1377,7 @@ function readSchema(ctx, root) {
1367
1377
  while ((m = rls.exec(text))) rlsTables.add(bare(m[1]));
1368
1378
  }
1369
1379
  tables.rls = rlsTables;
1380
+ tables.types = types;
1370
1381
  return tables;
1371
1382
  }
1372
1383
 
@@ -1434,7 +1445,9 @@ function parseSql(text) {
1434
1445
  }
1435
1446
  const upd = new RegExp('update\\s+(?:only\\s+)?' + name + '(?:\\s+(?:as\\s+)?(\\w+))?\\s+set\\b', 'gi');
1436
1447
  while ((m = upd.exec(sql))) {
1437
- out.push({ op: 'UPDATE', table: clean(m[1]), alias: m[2] && m[2].toLowerCase(), where: whereAfter(sql, upd.lastIndex) });
1448
+ const rest = sql.slice(upd.lastIndex);
1449
+ const w = rest.search(/\b(where|returning|from)\b/i);
1450
+ out.push({ op: 'UPDATE', table: clean(m[1]), alias: m[2] && m[2].toLowerCase(), where: whereAfter(sql, upd.lastIndex), set: w < 0 ? rest : rest.slice(0, w) });
1438
1451
  writes.add(clean(m[1]));
1439
1452
  }
1440
1453
  const del = new RegExp('delete\\s+from\\s+(?:only\\s+)?' + name + '(?:\\s+(?:as\\s+)?(?!where\\b|using\\b|returning\\b)(\\w+))?', 'gi');
@@ -1624,6 +1637,7 @@ function readDb(ctx, fnRef, schema, opts = {}) {
1624
1637
  const mod = fnRef.mod;
1625
1638
  const seenCalls = new Set();
1626
1639
  const recordScopes = new Map();
1640
+ const scopedLookups = []; // const po = await query('SELECT ... WHERE id=$1 AND organization_id=$2', [req.params.id, ...])
1627
1641
  const visitCall = (q) => {
1628
1642
  if (seenCalls.has(q.node)) return;
1629
1643
  seenCalls.add(q.node);
@@ -1659,8 +1673,32 @@ function readDb(ctx, fnRef, schema, opts = {}) {
1659
1673
  return;
1660
1674
  }
1661
1675
  const here = [];
1662
- for (const tx of texts) for (const st of parseSql(tx.text)) here.push(judgeSql(ctx, mod, line, st, tx, paramsArg, src, schema, recordScopes));
1676
+ const idOf = [];
1677
+ for (const tx of texts) {
1678
+ for (const st of parseSql(tx.text)) {
1679
+ const v = judgeSql(ctx, mod, line, st, tx, paramsArg, src, schema, recordScopes);
1680
+ // SET status = 'received': a step of the record's life; WHERE status = ...: a look at where it is now
1681
+ const stateSet = st.set && /(?:^|[\s,])"?(status|state|stage)"?\s*=\s*'([^']+)'/i.exec(st.set);
1682
+ if (stateSet) v.setsState = { col: stateSet[1].toLowerCase(), value: stateSet[2] };
1683
+ if (st.where && /\b(status|state|stage)\b/i.test(st.where)) v.readsState = true;
1684
+ here.push(v);
1685
+ idOf.push(idValueText(st, paramsArg, mod));
1686
+ }
1687
+ }
1663
1688
  joinWith(here);
1689
+ // UPDATE ... WHERE id=$1 after SELECT ... WHERE id=$1 AND organization_id=$2 of the same row,
1690
+ // which stops the route when nothing was found: limited by that lookup (round 25's receive)
1691
+ here.forEach((x, i) => {
1692
+ if (x.op === 'SELECT' || x.scoping.status !== STATUS.NOT || x.scoping.clientChosen || x.scoping.applies === false || !idOf[i]) return;
1693
+ const before = scopedLookups.find((l) => l.table === x.table && l.idText === idOf[i] && l.line < line && stopsWhenMissing(t, fnRef.path, l.name, l.line, line));
1694
+ if (before) x.scoping = { status: STATUS.DETECTED, evidence: { file: mod.rel, line, code: snippet(mod, line), how: 'the same ' + x.table + ' row was looked up with the organisation limit at ' + before.file + ':' + before.line + ', and the route stops when it is not found' } };
1695
+ });
1696
+ const lookupDecl = q.findParent((p) => p.isVariableDeclarator() || p.isFunction());
1697
+ here.forEach((x, i) => {
1698
+ if (x.op === 'SELECT' && !x.joinedTo && x.scoping.status === STATUS.DETECTED && idOf[i] && lookupDecl && lookupDecl.isVariableDeclarator() && t.isIdentifier(lookupDecl.node.id)) {
1699
+ scopedLookups.push({ table: x.table, idText: idOf[i], name: lookupDecl.node.id.name, line, file: mod.rel });
1700
+ }
1701
+ });
1664
1702
  out.push(...here);
1665
1703
  // const shipment = (await query('SELECT ... WHERE id=$1 AND org_id=$2')).rows[0]: remember how its row was found
1666
1704
  const decl = q.findParent((x) => x.isVariableDeclarator() || x.isFunction());
@@ -1701,9 +1739,22 @@ function readDb(ctx, fnRef, schema, opts = {}) {
1701
1739
  for (const st of parseSql(text)) out.push(judgeSql(ctx, mod, line, st, { text, dynamic: false }, { tagged: values }, src, schema));
1702
1740
  },
1703
1741
  });
1742
+ // if (po.status !== 'approved') ... / ['draft', 'sent'].includes(order.status): the code looks at where the record is now
1743
+ if (out.some((d) => d.setsState)) {
1744
+ const body = mod.source.slice(fnRef.path.node.start, fnRef.path.node.end);
1745
+ if (STATE_CHECK.some((re) => re.test(body))) for (const d of out) if (d.setsState) d.stateChecked = true;
1746
+ }
1704
1747
  return out;
1705
1748
  }
1706
1749
 
1750
+ // a status read and compared - never res.status(404), which is a call
1751
+ const STATE_CHECK = [
1752
+ /\.(status|state|stage)\b(?!\s*\()\s*(!==|===|!=|==)/,
1753
+ /(!==|===|!=|==)\s*[\w$.[\]]+\.(status|state|stage)\b(?!\s*\()/,
1754
+ /\.includes\(\s*[\w$.[\]]+\.(status|state|stage)\s*\)/,
1755
+ /switch\s*\(\s*[\w$.[\]]+\.(status|state|stage)\s*\)/,
1756
+ ];
1757
+
1707
1758
  /** The outermost call of a supabase chain that has .from('x') in it. */
1708
1759
  function fromCall(t, q) {
1709
1760
  let n = q.node.callee.object;
@@ -1835,6 +1886,40 @@ function isQueryWrapper(ctx, mod, calleePath) {
1835
1886
  return yes;
1836
1887
  }
1837
1888
 
1889
+ /** The code of the value a statement finds its row by - `id = $1` with [req.params.id] -> "req.params.id" - or null. */
1890
+ function idValueText(st, paramsArg, mod) {
1891
+ const m = st.where && /(?:^|[^\w.])(?:\w+\.)?"?id"?\s*=\s*\$(\d+)/i.exec(st.where);
1892
+ if (!m || !paramsArg || paramsArg.tagged) return null;
1893
+ const pa = unwrapTs(paramsArg);
1894
+ if (!pa.isArrayExpression()) return null;
1895
+ const el = pa.node.elements[Number(m[1]) - 1];
1896
+ if (!el || !memberChain(el)) return null;
1897
+ return mod.source.slice(el.start, el.end).replace(/\s+/g, '');
1898
+ }
1899
+
1900
+ /**
1901
+ * Whether, between the lookup at `from` and the write at `to`, the route
1902
+ * stops when the lookup named `name` found nothing: if (!po.rowCount) return ...
1903
+ */
1904
+ function stopsWhenMissing(t, fnPath, name, from, to) {
1905
+ let yes = false;
1906
+ fnPath.traverse({
1907
+ IfStatement(p) {
1908
+ const l = p.node.loc.start.line;
1909
+ if (yes || l < from || l > to) return;
1910
+ const test = p.node.test;
1911
+ const subject = test.type === 'UnaryExpression' && test.operator === '!' ? test.argument
1912
+ : test.type === 'BinaryExpression' && /^(===|==|<|<=)$/.test(test.operator) && test.right.type === 'NumericLiteral' && test.right.value <= 0 ? test.left : null;
1913
+ const chain = subject && memberChain(subject);
1914
+ if (!chain || chain[0] !== name || !(chain.length === 1 || /^(rowCount|length|rows)$/.test(chain[1]))) return;
1915
+ let stops = t.isReturnStatement(p.node.consequent) || t.isThrowStatement(p.node.consequent);
1916
+ p.get('consequent').traverse({ Function(f) { f.skip(); }, ReturnStatement() { stops = true; }, ThrowStatement() { stops = true; } });
1917
+ if (stops) yes = true;
1918
+ },
1919
+ });
1920
+ return yes;
1921
+ }
1922
+
1838
1923
  /** One SQL statement's touch on one table, judged for scoping. */
1839
1924
  function judgeSql(ctx, mod, line, st, tx, paramsArg, src, schema, recordScopes) {
1840
1925
  // the values behind $1, $2..., and the row each one was read from, if any
@@ -2036,7 +2121,9 @@ function readValidation(ctx, fnRef, elements, db) {
2036
2121
  const id = q.node.id;
2037
2122
  const init = q.node.init;
2038
2123
  if (!init) return;
2039
- let chain = memberChain(init.type === 'TSAsExpression' ? init.expression : init);
2124
+ // req.body || {} / req.body ?? {}: still the body (round 25 lost its sign-in, and so its whole API run, to this)
2125
+ const bare = init.type === 'TSAsExpression' ? init.expression : init;
2126
+ let chain = memberChain(bare.type === 'LogicalExpression' && (bare.operator === '||' || bare.operator === '??') && bare.right.type === 'ObjectExpression' ? bare.left : bare);
2040
2127
  // await req.json() / await req.formData() in a Fetch-style handler is the body
2041
2128
  const awaited = init.type === 'AwaitExpression' ? init.argument : init;
2042
2129
  if (!chain && awaited.type === 'CallExpression' && reqName) {
@@ -2210,9 +2297,40 @@ function readValidation(ctx, fnRef, elements, db) {
2210
2297
  if (el.kind === 'package' && /express-validator|celebrate|joi/.test(el.source || '')) schemas.push({ from: 'body', file: el.usedFile, line: el.usedLine, code: el.name, fields: null, why: 'checked by ' + el.source + ' middleware (its rules are not read)' });
2211
2298
  }
2212
2299
  const unread = (elements || []).some((e) => e.kind === 'package' && /express-validator|celebrate|joi/.test(e.source || ''));
2300
+ // a list of items in the body (lines = [{ product_id, quantity }]): the names read off each item, in
2301
+ // for (const line of lines) and lines.some((x) => ...) - so a request can send one item (round 25's transfer)
2302
+ const itemNames = new Map();
2303
+ const itemsOfLocal = (local, fnOrBody, itemName) => {
2304
+ const f = localField.get(local);
2305
+ if (!f || typeof f !== 'object') return;
2306
+ const k = key(f.from, f.name);
2307
+ if (!itemNames.has(k)) itemNames.set(k, new Set());
2308
+ fnOrBody.traverse({
2309
+ MemberExpression(m) {
2310
+ if (t.isIdentifier(m.node.object, { name: itemName }) && !m.node.computed && t.isIdentifier(m.node.property)) itemNames.get(k).add(m.node.property.name);
2311
+ },
2312
+ });
2313
+ };
2314
+ fn.traverse({
2315
+ ForOfStatement(q) {
2316
+ const right = q.node.right;
2317
+ const left = q.node.left;
2318
+ const v = left.type === 'VariableDeclaration' && left.declarations[0] && left.declarations[0].id;
2319
+ if (t.isIdentifier(right) && t.isIdentifier(v)) itemsOfLocal(right.name, q.get('body'), v.name);
2320
+ },
2321
+ CallExpression(q) {
2322
+ const c = q.node.callee;
2323
+ const cb = q.node.arguments[0];
2324
+ if (!t.isMemberExpression(c) || !t.isIdentifier(c.object) || !t.isIdentifier(c.property) || !/^(some|every|map|forEach|filter|find)$/.test(c.property.name)) return;
2325
+ if (!cb || !/Function/.test(cb.type) || !cb.params[0] || !t.isIdentifier(cb.params[0])) return;
2326
+ itemsOfLocal(c.object.name, q.get('arguments.0'), cb.params[0].name);
2327
+ },
2328
+ });
2213
2329
  const out = Array.from(fields.values()).map((e) => {
2214
2330
  let status = e.checks.length ? STATUS.DETECTED : (e.schemaUnread || unread ? STATUS.UNKNOWN : STATUS.NOT);
2215
- return { name: e.name, from: e.from, line: e.line, file: mod.rel, status, checks: e.checks, reachesWrite: e.reachesWrite || [], why: status === STATUS.UNKNOWN ? 'checked by a schema or middleware whose rules are not read' : null };
2331
+ const items = itemNames.get(key(e.from, e.name));
2332
+ return Object.assign({ name: e.name, from: e.from, line: e.line, file: mod.rel, status, checks: e.checks, reachesWrite: e.reachesWrite || [], why: status === STATUS.UNKNOWN ? 'checked by a schema or middleware whose rules are not read' : null },
2333
+ items && items.size ? { items: Array.from(items).sort() } : {});
2216
2334
  });
2217
2335
  return { fields: out.sort((a, b) => a.line - b.line), schemas };
2218
2336
  }
@@ -2646,7 +2764,10 @@ function nextRoutes(ctx, schema) {
2646
2764
 
2647
2765
  /** What kind of route this is, beside an ordinary one: a webhook, or a way to start a job. */
2648
2766
  function routeKind(r) {
2649
- if (/(^|\/)(webhooks?|hooks?|callbacks?|ipn|notify)(\/|$)/i.test(r.path || '') || r.signature.status === STATUS.DETECTED || r.signature.presenceOnly) return 'webhook';
2767
+ if (/(^|\/)(webhooks?|hooks?|callbacks?|ipn|notify)(\/|$)/i.test(r.path || '')) return 'webhook';
2768
+ // a sign-in compares a password hash with timingSafeEqual too: that is not a signed body (round 25)
2769
+ const signIn = /(^|\/)(log-?in|sign-?in|session|auth)(\/|$)/i.test(r.path || '');
2770
+ if (!signIn && (r.signature.status === STATUS.DETECTED || r.signature.presenceOnly)) return 'webhook';
2650
2771
  if (/(^|\/)(jobs?|cron|tasks?|workers?|queues?|schedules?)(\/|$)/i.test(r.path || '')) return 'job';
2651
2772
  return 'route';
2652
2773
  }
@@ -2758,7 +2879,9 @@ function mapProject(root, options) {
2758
2879
  seen.add(k);
2759
2880
  return true;
2760
2881
  });
2761
- return { ran: true, filesRead: files.length, routes, jobs, notRead, unreadable: ctx.unreadable, tablesKnown: schema.size };
2882
+ const columnTypes = {};
2883
+ for (const [col, set] of schema.types || []) columnTypes[col] = Array.from(set);
2884
+ return { ran: true, filesRead: files.length, routes, jobs, notRead, unreadable: ctx.unreadable, tablesKnown: schema.size, columnTypes };
2762
2885
  }
2763
2886
 
2764
2887
  // --- what is worth a look: read from code, never proven ---------------------------------
@@ -2775,6 +2898,7 @@ const ADVICE = {
2775
2898
  'tenant-from-request': 'take the organisation from the signed-in person\'s session, never from the request body or query string; for an id the request sends, look the record up with the organisation condition before using it.',
2776
2899
  'tenant-unscoped': 'add the signed-in person\'s organisation (or owner) to the statement - WHERE org_id = <the session\'s org> - or, for a row found by its id, check that the row belongs to that organisation before using it.',
2777
2900
  'no-role': 'allow only the roles that should be able to do this, and answer 403 for the others.',
2901
+ 'no-state-check': 'look at the record\'s status first and refuse the step when the record is not in the status it must come from (WHERE status = <the one before> in the same statement, or a check that answers 409). A second call must change nothing - no stock, money or log entry added again.',
2778
2902
  'identity-from-request': 'take who is calling only from something the server itself issued and can check - a signed session cookie (express-session, a signed JWT) - never from a bare user id in a cookie or a header the browser can set. Look the session up on the server and set req.user from that.',
2779
2903
  };
2780
2904
 
@@ -2833,6 +2957,12 @@ function toCheck(map) {
2833
2957
  const byOrg = open.some((d) => d.level !== 'owner');
2834
2958
  add(r, 'tenant-unscoped', open.some((d) => d.op !== 'SELECT') ? 'HIGH' : 'MEDIUM', name + ' ' + tables(open) + (byOrg ? ' with no limit to the signed-in person\'s organisation' : ' with no check that the rows belong to the signed-in person') + (fromRow ? ' (rows found by an id that was itself looked up without one)' : ''), open.map(at));
2835
2959
  }
2960
+ // SET status = 'received' with no look at what the status is now: sent twice, the step is done twice (round 25's receive)
2961
+ const steps = writes ? (r.db || []).filter((d) => d.setsState && !d.stateChecked && !(r.db || []).some((x) => x.readsState && x.table === d.table)) : [];
2962
+ if (steps.length) {
2963
+ const s = steps[0];
2964
+ add(r, 'no-state-check', 'MEDIUM', name + ' sets ' + s.table + '.' + s.setsState.col + " to '" + s.setsState.value + "' without looking at what it is now: sent again, or out of order, it does the step again", steps.map((d) => d.file + ':' + d.line + ' ' + d.sql));
2965
+ }
2836
2966
  if (checksRoles && writes && r.role.status === STATUS.NOT && SENSITIVE_PATH.test(r.path || '')) {
2837
2967
  add(r, 'no-role', 'MEDIUM', name + ' is a sensitive change, and no role check was found in front of it, while other routes of this app check roles', [r.file + ':' + r.line]);
2838
2968
  }
@@ -88,7 +88,23 @@ function checkPolicy(json) {
88
88
  // A draft Kryptheon wrote from the code says what the code does today, not what the owner wants.
89
89
  if (json.draft !== undefined) return { ok: false, problems: ['it is still marked "draft": it lists what your code does today, not what you want. Edit each role to what you want, then delete the "draft" line.'] };
90
90
  const roles = {};
91
- if (!json.roles || typeof json.roles !== 'object' || Array.isArray(json.roles)) problems.push('"roles" must say, for each role, the list of permissions it has.');
91
+ // the other common shape: "roles": ["owner", "viewer"] and "rules": [{ "action": "read:orders", "allow": ["owner", "viewer"] }] (round 25)
92
+ if (Array.isArray(json.roles) && Array.isArray(json.rules)) {
93
+ const names = json.roles.filter((r) => typeof r === 'string' && r.trim()).map((r) => r.trim());
94
+ if (names.length !== json.roles.length) problems.push('"roles" must be a list of role names.');
95
+ for (const rule of json.rules) {
96
+ if (!rule || typeof rule.action !== 'string' || !rule.action.trim() || !Array.isArray(rule.allow) || rule.allow.some((r) => typeof r !== 'string')) {
97
+ problems.push('each of "rules" must be { "action": "read:orders", "allow": ["owner", ...] }.');
98
+ break;
99
+ }
100
+ const unknown = rule.allow.find((r) => !names.includes(r.trim()) && r.trim() !== '*');
101
+ if (unknown) problems.push('rule "' + rule.action + '" allows "' + unknown + '", which is not in "roles".');
102
+ }
103
+ if (!problems.length) {
104
+ for (const name of names) roles[name] = json.rules.filter((rule) => rule.allow.some((r) => r.trim() === name || r.trim() === '*')).map((rule) => rule.action.trim());
105
+ if (names.length < 2) problems.push('it needs at least two roles to compare.');
106
+ }
107
+ } else if (!json.roles || typeof json.roles !== 'object' || Array.isArray(json.roles)) problems.push('"roles" must say, for each role, the list of permissions it has.');
92
108
  else {
93
109
  for (const name of Object.keys(json.roles)) {
94
110
  const list = json.roles[name];
@@ -102,8 +118,11 @@ function checkPolicy(json) {
102
118
  problems.push('"kryptheon.actions" must be a list of { "permission", "method", "path" }.');
103
119
  }
104
120
  if (problems.length) return { ok: false, problems: problems };
105
- const tenants = (typeof json.tenantBoundary === 'string' && json.tenantBoundary.trim() !== '') || json.tenants === true;
106
- return { ok: true, problems: [], policy: { roles: roles, tenants: tenants, boundary: typeof json.tenantBoundary === 'string' ? json.tenantBoundary : '', extra: k } };
121
+ // "tenant_rules": ["A user may only read or modify records belonging to their own workspace.", ...] says the same as "tenantBoundary"
122
+ const written = ['tenant_rules', 'tenantRules'].map((key) => json[key]).find((v) => Array.isArray(v) && v.some((x) => typeof x === 'string' && x.trim()));
123
+ const boundary = typeof json.tenantBoundary === 'string' ? json.tenantBoundary : written ? written.filter((x) => typeof x === 'string').join(' ') : '';
124
+ const tenants = boundary.trim() !== '' || json.tenants === true;
125
+ return { ok: true, problems: [], policy: { roles: roles, tenants: tenants, boundary: boundary, extra: k } };
107
126
  }
108
127
 
109
128
  /** Whether `role` has `permission` ("*" and "resource.*" are allowed in the file). */
@@ -209,6 +228,27 @@ function templateOfWritten(p) {
209
228
  return '/' + String(p || '').split(/[?#]/)[0].split('/').filter(Boolean).map((s) => (/^(\{[^}]*\}|:[A-Za-z_]\w*|<[^>]*>|\*)$/.test(s) ? '{id}' : s)).join('/');
210
229
  }
211
230
 
231
+ const VERB_WORDS = new Set([...READ_WORDS, 'create', 'add', 'update', 'edit', 'write', 'delete', 'remove', 'manage']);
232
+
233
+ /**
234
+ * A permission name split into what it is about and what it allows:
235
+ * "orders.read" -> orders / read; "read:purchase_orders" -> purchase, order / read;
236
+ * "orders:read" -> orders / read. Null when it is one word.
237
+ */
238
+ function partsOf(perm) {
239
+ const colon = String(perm).indexOf(':');
240
+ if (colon > 0) {
241
+ const left = wordsOf(perm.slice(0, colon));
242
+ const right = wordsOf(perm.slice(colon + 1));
243
+ if (!left.length || !right.length) return null;
244
+ // "orders:read" names the resource first; "read:orders" the action
245
+ if (right.every((w) => VERB_WORDS.has(w)) && !left.every((w) => VERB_WORDS.has(w))) return { resource: left, actions: right };
246
+ return { resource: right, actions: left };
247
+ }
248
+ const w = wordsOf(perm);
249
+ return w.length < 2 ? null : { resource: [w[0]], actions: w.slice(1) };
250
+ }
251
+
212
252
  /**
213
253
  * The permission an action needs, found from the words in its address and its
214
254
  * method - or null, or `ambiguous`. Strict on purpose: when two permissions
@@ -232,15 +272,16 @@ function permissionFor(method, pathOrTemplate, policy) {
232
272
  const verb = METHOD_VERB[m];
233
273
  const found = [];
234
274
  for (const perm of permissionsOf(policy)) {
235
- const w = wordsOf(perm);
236
- if (w.length < 2) continue;
237
- const resource = w[0];
238
- const actions = w.slice(1);
239
- if (!pathWords.has(resource)) continue;
275
+ const parts = partsOf(perm);
276
+ if (!parts) continue;
277
+ const resource = parts.resource;
278
+ const actions = parts.actions;
279
+ const hits = resource.filter((r) => pathWords.has(r)).length;
280
+ if (!hits) continue;
240
281
  const readish = actions.every((a) => READ_WORDS.has(a));
241
282
  if (isGet !== readish) continue;
242
- let score = 1;
243
- let lastHit = last.has(resource);
283
+ let score = hits;
284
+ let lastHit = resource.some((r) => last.has(r));
244
285
  for (const a of actions) {
245
286
  if (pathWords.has(a)) {
246
287
  score += 2;
@@ -521,7 +562,10 @@ async function checkAccess(input) {
521
562
  const me = await s.send('GET', p);
522
563
  if (me.status === 200 && me.json) {
523
564
  user = findUser(me.json) || (policy.anyRole ? findAnyUser(me.json) : null);
524
- if (user) break;
565
+ if (user) {
566
+ s.identityPath = p;
567
+ break;
568
+ }
525
569
  }
526
570
  }
527
571
  if (!user) {
@@ -697,7 +741,14 @@ async function checkAccess(input) {
697
741
  const listing = '/' + action.segs.slice(0, action.params[0].index).join('/');
698
742
  if (!getTemplates.has(listing)) {
699
743
  const mine = (seeded.get(listing) || []).filter((r) => r.org === orgKey);
700
- return mine.length ? mine : null;
744
+ if (mine.length) return mine;
745
+ // /team/:id, /users/:id with no list of them: the accounts signed in here are such records, with ids the app gave
746
+ if (PEOPLE.test(action.segs[action.params[0].index - 1] || '')) {
747
+ const people = accountsOf(orgKey).filter((ss) => ss.userId !== undefined && ss.userId !== null)
748
+ .map((ss) => ({ value: ss.userId, item: { id: ss.userId, email: ss.email, role: ss.role }, listing: listing, field: 'id' }));
749
+ return people.length ? people : null;
750
+ }
751
+ return null;
701
752
  }
702
753
  const items = await itemsOf(orgKey, listing);
703
754
  if (!items) return null;
@@ -721,7 +772,7 @@ async function checkAccess(input) {
721
772
  return text;
722
773
  };
723
774
  const markersOf = (value, known, requestText) =>
724
- Array.from(new Set(authz.stringsOf(value, 5))).filter((m) => !known.includes(m) && !requestText.includes(m)).slice(0, 60);
775
+ Array.from(new Set(authz.stringsOf(value, 5))).filter((m) => !known.includes(m) && !requestText.includes(m) && !wordsOfAnyRecord(value).has(m)).slice(0, 60);
725
776
 
726
777
  // 6. the tries
727
778
  const plan = [];
@@ -734,7 +785,9 @@ async function checkAccess(input) {
734
785
  plan.push({ action: action, actor: s, kind: 'role', expected: 'allow', org: s.org ? s.org.key : null });
735
786
  if (tenants && s.org) {
736
787
  if (aimable(action)) {
737
- for (const o of orgs.values()) if (o.key !== s.org.key) plan.push({ action: action, actor: s, kind: 'tenant', expected: 'deny', org: o.key });
788
+ // a write that names records only in its body: each of them aimed in its turn (from_location_id, then to_location_id)
789
+ const keys = action.method !== 'GET' && !action.params.length ? bodyRecords(action, action.data || {}, getTemplates).map((r) => r.key) : [null];
790
+ for (const o of orgs.values()) if (o.key !== s.org.key) for (const k of keys) plan.push({ action: action, actor: s, kind: 'tenant', expected: 'deny', org: o.key, bodyKey: k || undefined });
738
791
  } else if (!unaimed.has(action.method + ' ' + action.template)) {
739
792
  unaimed.add(action.method + ' ' + action.template);
740
793
  skipped.push({ what: action.method + ' ' + action.template, why: 'across organizations: nothing in the request names a record, so it cannot be aimed at another organization' });
@@ -815,16 +868,34 @@ async function checkAccess(input) {
815
868
  * The key in a request body that names another record (shipmentId, order_id),
816
869
  * with a stand-in for the address that lists those records: { key, like }.
817
870
  */
818
- function bodyRecord(action, body, getTemplates) {
819
- const key = Object.keys(body || {}).find((k) => /^[A-Za-z]+_?[iI]d$/.test(k) && k.toLowerCase() !== 'id' && ['string', 'number'].includes(typeof body[k]));
820
- if (!key) return null;
821
- const stem = singular(key.replace(/_?[iI]d$/, '').toLowerCase());
822
- const listing = Array.from(getTemplates || []).find((t) => !t.includes('{id}') && singular(t.split('/').pop().toLowerCase().replace(/[^a-z]/g, '')) === stem);
823
- if (!listing) return null;
824
- const segs = listing.split('/').filter(Boolean).concat('{id}');
825
- return { key: key, like: { template: listing + '/{id}', segs: segs, params: [{ index: segs.length - 1, value: body[key] }], by: action.by } };
871
+ function bodyRecord(action, body, getTemplates, want) {
872
+ const all = bodyRecords(action, body, getTemplates);
873
+ return (want ? all.find((r) => r.key === want) : all[0]) || null;
826
874
  }
827
875
 
876
+ /**
877
+ * Every key of a body that names another record and whose records the app
878
+ * lists, in the body's order: shipmentId, order_id - and from_location_id,
879
+ * to_location_id, each by its last word (round 25's transfer named its
880
+ * destination so, and was never aimed at another organization's location).
881
+ */
882
+ function bodyRecords(action, body, getTemplates) {
883
+ const out = [];
884
+ for (const key of Object.keys(body || {})) {
885
+ if (!/^[A-Za-z]+(?:_[A-Za-z]+)*_?[iI]d$/.test(key) || key.toLowerCase() === 'id' || !['string', 'number'].includes(typeof body[key])) continue;
886
+ const words = key.replace(/_?[iI]d$/, '').replace(/([a-z])([A-Z])/g, '$1_$2').toLowerCase().split('_').filter(Boolean);
887
+ const stem = singular(words[words.length - 1] || '');
888
+ const listing = Array.from(getTemplates || []).find((t) => !t.includes('{id}') && singular(t.split('/').pop().toLowerCase().replace(/[^a-z]/g, '')) === stem);
889
+ if (!listing) continue;
890
+ const segs = listing.split('/').filter(Boolean).concat('{id}');
891
+ out.push({ key: key, like: { template: listing + '/{id}', segs: segs, params: [{ index: segs.length - 1, value: body[key] }], by: action.by } });
892
+ }
893
+ return out;
894
+ }
895
+
896
+ // The last word of an address whose records are people.
897
+ const PEOPLE = /^(users?|team|teams|members?|memberships?|accounts?|staff|people|profiles?|employees?)$/i;
898
+
828
899
  /** One try: send, look, judge, put back. Returns the evidence, or null when the try had nothing to aim at. */
829
900
  async function tryOnce(x) {
830
901
  const { p, policy } = x;
@@ -869,7 +940,7 @@ async function tryOnce(x) {
869
940
  if (!isGet) {
870
941
  body = sampleBody(a);
871
942
  // a body that names another record (shipmentId, order_id) is pointed at the target's organization too
872
- const named = bodyRecord(a, body, x.getTemplates);
943
+ const named = bodyRecord(a, body, x.getTemplates, p.bodyKey);
873
944
  if (named) {
874
945
  const alt = await x.targetsFor(named.like, targetOrg);
875
946
  if (alt && alt.length) {
@@ -910,7 +981,15 @@ async function tryOnce(x) {
910
981
 
911
982
  // what the app holds now
912
983
  const reader = targetOrg !== null || p.kind === 'signin' || x.orgs.size === 0;
913
- const before = reader ? await x.snapshot(targetOrg) : {};
984
+ // a person's record: what they themselves are told about who they are shows the change (their role)
985
+ const person = !isGet && target.item && target.item.email ? x.sessions.find((ss) => ss.email === target.item.email && ss.identityPath) : null;
986
+ const withPerson = async (snap) => {
987
+ if (!person) return snap;
988
+ const r = await person.send('GET', person.identityPath);
989
+ if (r.status === 200 && r.json !== undefined) snap['who am I, as ' + person.email] = stable(r.json);
990
+ return snap;
991
+ };
992
+ const before = await withPerson(reader ? await x.snapshot(targetOrg) : {});
914
993
  // a write to an address that is also read (POST /returns/5/notes, GET /returns/5/notes) shows there
915
994
  const sameGet = reader && !isGet && a.params.length && x.getTemplates.has(a.template) ? pathNow : null;
916
995
  const readSame = async () => {
@@ -937,12 +1016,23 @@ async function tryOnce(x) {
937
1016
  }
938
1017
  }
939
1018
 
1019
+ // a write that names another organization's record in its body: our own lists, before and after
1020
+ const pointing = p.kind === 'tenant' && !isGet && bodyItem && actor.org && markers.length > 0;
1021
+ const ownBefore = pointing ? JSON.stringify(await x.snapshot(actor.org.key)) : '';
940
1022
  const res = await actor.send(a.method, pathNow, isGet ? {} : (a.encoding === 'form' ? { form: body } : { json: body }));
941
1023
  const reply = authz.replyOf(res);
942
1024
  const foreign = actor.org && isGet ? foreignRecords(res.json, actor.org.key) : [];
943
1025
  const ownMarked = actor.org && isGet ? ownRecords(res.json, actor.org.key) : 0;
944
- const found = markers.length ? authz.foundIn(res, markers).length : 0;
945
- const after = reader ? await x.snapshot(targetOrg) : {};
1026
+ let found = markers.length ? authz.foundIn(res, markers).length : 0;
1027
+ // their record's own words now in our lists: a record of ours points at theirs (round 25: a transfer whose
1028
+ // destination was another organization's location, shown by name in our list of transfers)
1029
+ let pointedAt = [];
1030
+ if (pointing && (reply.kind === 'accepted' || reply.kind === 'redirect')) {
1031
+ const ownAfter = JSON.stringify(await x.snapshot(actor.org.key));
1032
+ pointedAt = markers.filter((m) => ownAfter.includes(m) && !ownBefore.includes(m));
1033
+ found += pointedAt.length;
1034
+ }
1035
+ const after = await withPerson(reader ? await x.snapshot(targetOrg) : {});
946
1036
  if (sameBefore !== undefined) {
947
1037
  const sameAfter = await readSame();
948
1038
  if (sameAfter !== undefined) after[sameGet] = sameAfter;
@@ -956,6 +1046,7 @@ async function tryOnce(x) {
956
1046
  target: { organization: orgName(targetOrg), record: target.value },
957
1047
  changes: changes.slice(0, 6), protectedPiecesSeen: found,
958
1048
  });
1049
+ if (pointedAt.length) att.pointedAt = { key: p.bodyKey || Object.keys(body).find((k) => body[k] === target.value) || null, words: pointedAt.slice(0, 3) };
959
1050
  // An answer to a try that was aimed across organizations is judged for that. For any other try, another
960
1051
  // organization's records in the answer are a second finding, kept apart from what the role may do.
961
1052
  att.verdictInfo = judge({ expected: p.expected, isGet: isGet, status: res.status, reply: reply, found: found, foreign: p.kind === 'tenant' ? foreign : [], markersKnown: markersKnown || ownMarked > 0, changed: changed, kind: p.kind });
@@ -985,7 +1076,7 @@ async function tryOnce(x) {
985
1076
  const writers = (x.orgs.get(targetOrg) ? x.orgs.get(targetOrg).sessions : x.sessions).filter((s) => holds(policy, s.role, a.permission)).concat([actor]);
986
1077
  for (const who of writers) {
987
1078
  await who.send(a.method, pathNow, a.encoding === 'form' ? { form: back } : { json: back });
988
- const again = await x.snapshot(targetOrg);
1079
+ const again = await withPerson(await x.snapshot(targetOrg));
989
1080
  if (x.compare(before, again).length === 0) {
990
1081
  att.restored = true;
991
1082
  break;
@@ -1009,6 +1100,26 @@ async function tryOnce(x) {
1009
1100
  return att;
1010
1101
  }
1011
1102
 
1103
+ // Fields whose values are one of a few words every organization uses: "submitted", "warehouse", "INR".
1104
+ const SHARED_WORD_KEY = /(^|_)(status|state|stage|type|kind|role|level|priority|currency|unit|method|category|visibility)$/i;
1105
+
1106
+ /**
1107
+ * The values of a record that are a status or kind - words any organization's
1108
+ * records carry - and so prove nothing about whose record came back. Round 25:
1109
+ * "Only submitted purchase orders can be approved." held the other
1110
+ * organization's status "submitted", and a refusal was called a leak.
1111
+ */
1112
+ function wordsOfAnyRecord(value) {
1113
+ const out = new Set();
1114
+ (function walk(v, key) {
1115
+ if (typeof v === 'string') {
1116
+ if ((key && SHARED_WORD_KEY.test(key)) || /^[a-z]+(_[a-z]+)*$/.test(v)) out.add(v);
1117
+ } else if (Array.isArray(v)) v.forEach((x) => walk(x, key));
1118
+ else if (v && typeof v === 'object') Object.keys(v).forEach((k) => walk(v[k], k));
1119
+ })(value, null);
1120
+ return out;
1121
+ }
1122
+
1012
1123
  // A record's own mark of the organization it belongs to.
1013
1124
  const ORG_KEY = /^(org|organi[sz]ation|tenant|workspace|company)[_-]?id$/i;
1014
1125
 
@@ -1105,7 +1216,8 @@ function proofOf(att) {
1105
1216
  if (att.changes && att.changes.length) {
1106
1217
  lines.push(' state: changed after the request (read before and after):');
1107
1218
  for (const c of att.changes.slice(0, 3)) lines.push(' ' + c);
1108
- } else if (att.protectedPiecesSeen) lines.push(' state: the answer itself held ' + att.protectedPiecesSeen + ' piece(s) of records this account may not see');
1219
+ } 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(', '));
1220
+ else if (att.protectedPiecesSeen) lines.push(' state: the answer itself held ' + att.protectedPiecesSeen + ' piece(s) of records this account may not see');
1109
1221
  else lines.push(' state: unchanged');
1110
1222
  lines.push(' repeat: sign in as ' + att.actor.email + ', send ' + att.request.method + ' ' + att.request.path + (att.request.body && Object.keys(att.request.body).length ? ' with that body' : '') + ', then read the same lists as an allowed account');
1111
1223
  return lines;
@@ -69,10 +69,19 @@ function handlerAt(parser, ast, line) {
69
69
  if (!n.callee || n.callee.type !== 'MemberExpression') return;
70
70
  const verb = n.callee.property && n.callee.property.name;
71
71
  if (!/^(post|put|patch|all)$/.test(verb || '')) return;
72
+ const isFn = (a) => a && (a.type === 'ArrowFunctionExpression' || a.type === 'FunctionExpression');
72
73
  const argIndex = n.arguments.map((a) => a.type).lastIndexOf('ArrowFunctionExpression');
73
74
  const fnIndex = argIndex >= 0 ? argIndex : n.arguments.map((a) => a.type).lastIndexOf('FunctionExpression');
74
- if (fnIndex < 0) return;
75
- found = { path: p.get('arguments.' + fnIndex), node: n.arguments[fnIndex] };
75
+ if (fnIndex >= 0) {
76
+ found = { path: p.get('arguments.' + fnIndex), node: n.arguments[fnIndex] };
77
+ return;
78
+ }
79
+ // asyncRoute(async (req, res) => ...): the function inside the wrapper (round 25)
80
+ const last = n.arguments.length - 1;
81
+ const wrap = n.arguments[last];
82
+ if (wrap && wrap.type === 'CallExpression' && wrap.arguments.length === 1 && isFn(wrap.arguments[0])) {
83
+ found = { path: p.get('arguments.' + last + '.arguments.0'), node: wrap.arguments[0] };
84
+ }
76
85
  },
77
86
  });
78
87
  if (!found) return null;
@@ -310,9 +319,9 @@ async function run(root, options) {
310
319
  if (mode === 'wrong') headers[signing.header] = wrongSignature(signing, secret, text);
311
320
  return session.send('POST', route.path, { json: body, headers });
312
321
  };
313
- const attempt = async (mode, type, rec, amount, label) => {
322
+ const attempt = async (mode, type, rec, amount, label, same) => {
314
323
  const before = await readList();
315
- const body = bodyFor(type, rec, amount);
324
+ const body = same || bodyFor(type, rec, amount);
316
325
  used.add(rec.id);
317
326
  const res = await post(mode, body);
318
327
  const reply = authz.replyOf(res);
@@ -322,6 +331,7 @@ async function run(root, options) {
322
331
  const changed = !!(was && now && JSON.stringify(stableItem(was)) !== JSON.stringify(stableItem(now)));
323
332
  const took = reply.kind === 'accepted' || reply.kind === 'redirect';
324
333
  const t = { mode, label: label || mode, type, record: rec.id, status: res.status, took, changed, from: was && statusKey ? was[statusKey] : null, to: now && statusKey ? now[statusKey] : null };
334
+ Object.defineProperty(t, 'body', { value: body, enumerable: false }); // sent again as it was, never printed
325
335
  hook.tried.push(t);
326
336
  if (changed) out.left.push(name + ' ' + (label || mode) + ' ' + type + ' for ' + resource + ' ' + rec.id + (t.from !== null ? ': ' + statusKey + ' ' + t.from + ' -> ' + t.to : ' changed it'));
327
337
  else if (took) out.left.push(name + ' ' + (label || mode) + ' ' + type + ' for ' + resource + ' ' + rec.id + ' answered ' + res.status + ' (no change seen in ' + listRoute.path + '); the event id is in the app\'s webhook table');
@@ -338,6 +348,9 @@ async function run(root, options) {
338
348
  hook.notTried = 'the app refused even a correctly signed event (' + control.status + '), so either the signature is made differently from how the code reads, or the event needs more than ' + Object.keys(bodyFor(types[0], first)).join(', ') + ': nothing after it would mean anything';
339
349
  continue;
340
350
  }
351
+ // 2. the very same event again - same id, same signature - as a provider does when it is not sure it was taken
352
+ const replay = await attempt('signed', types[0], first, undefined, 'signed again (same event)', control.body);
353
+ void replay;
341
354
  // the tries, in the order that tells the most when records run out
342
355
  const cases = [];
343
356
  for (const type of types) cases.push({ mode: 'none', type, label: 'unsigned' });
@@ -377,7 +390,11 @@ async function run(root, options) {
377
390
  finding('webhook-amount', 'HIGH', 'confirmed',
378
391
  name + ' applied ' + t.type + ' for an amount that is not ' + resource + ' ' + t.record + '\'s: the status changed all the same.',
379
392
  ['Correctly signed, with the amount 100 higher than the record\'s: ' + lineOf(t)], 'Compare the amount (and currency) in the event with the amount the ' + resource + ' was created for, and ignore or flag the event when they differ.');
380
- } else if (t.mode === 'signed' && t.changed && REFUNDED.test(String(t.to)) && t.from !== null && !PAID.test(String(t.from))) {
393
+ } else if (t.label === 'signed again (same event)' && t.took && t.changed) {
394
+ finding('webhook-replay', 'HIGH', 'confirmed',
395
+ name + ' took the same ' + t.type + ' a second time - same event id, same signature - and changed ' + resource + ' ' + t.record + ' again.',
396
+ ['The correctly signed event, sent again unchanged: ' + lineOf(t)], 'Keep the id of every event you have handled, with a unique rule on it, and answer 200 without doing anything when the same id comes again. Providers resend events whenever they are not sure the first was taken.');
397
+ } else if (t.mode === 'signed' && t.label === 'signed' && t.changed && REFUNDED.test(String(t.to)) && t.from !== null && !PAID.test(String(t.from))) {
381
398
  finding('webhook-lifecycle', 'MEDIUM', 'verification required',
382
399
  name + ' marked ' + resource + ' ' + t.record + ' as ' + t.to + ' from ' + t.from + ': a refund event for something nobody had paid or approved.',
383
400
  ['Correctly signed: ' + lineOf(t)], 'Apply a refund event only to a record that was paid or approved first, in the same tenant, for no more than was paid; answer and log anything else without changing it.');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kryptheon",
3
- "version": "0.1.57",
3
+ "version": "0.1.58",
4
4
  "description": "Record what you do in your app and get plain-language reports when it breaks.",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "type": "commonjs",