kryptheon 0.1.61 → 0.1.63

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/bin/kryptheon.js CHANGED
@@ -3080,6 +3080,106 @@ function scanSummary(code, printed) {
3080
3080
  ' not tested, so that is not a green light. See what it printed.';
3081
3081
  }
3082
3082
 
3083
+ /** Whether something answers at this address on this computer, within a few seconds. */
3084
+ function appAnswers(origin) {
3085
+ return new Promise((resolve) => {
3086
+ let req;
3087
+ try {
3088
+ req = require(/^https:/.test(origin) ? 'https' : 'http').get(origin, { timeout: 5000, rejectUnauthorized: false }, (res) => {
3089
+ res.resume();
3090
+ resolve(true);
3091
+ });
3092
+ } catch (err) {
3093
+ resolve(false);
3094
+ return;
3095
+ }
3096
+ req.on('timeout', () => req.destroy());
3097
+ req.on('error', () => resolve(false));
3098
+ });
3099
+ }
3100
+
3101
+ /**
3102
+ * The API check on the app at `origin`, from the page: the same command a
3103
+ * person types, with --roles, as a child. It listens for no stop message, and
3104
+ * sends only HTTP requests - no browser to close - so Stop ends it outright.
3105
+ */
3106
+ async function apiFromDashboard(origin, say, control) {
3107
+ if (!(await appAnswers(origin))) {
3108
+ return { ok: false, why: 'your app at ' + origin + ' is not answering - start it (for example npm run dev), then press Check my app again' };
3109
+ }
3110
+ let stopped = false;
3111
+ const { code } = await kryptheonChild(['api', '--run', '--origin', origin, '--roles', '--yes'], say, (child) => control.onStop(() => {
3112
+ stopped = true;
3113
+ try { child.kill(); } catch (err) { /* already gone */ }
3114
+ }));
3115
+ if (stopped) return { ok: false, why: 'stopped - what it found before Stop was not kept' };
3116
+ return code === 0 ? { ok: true, summary: 'nothing got through' }
3117
+ : code === 1 ? { ok: true, summary: 'problems found - see Findings' }
3118
+ : { ok: false, why: 'it could not run - see what it printed' };
3119
+ }
3120
+
3121
+ /**
3122
+ * "Check my app" on the page: each step the server planned, one after another,
3123
+ * in this one job. A step that does not finish is said and the next one runs;
3124
+ * Stop ends the step running (where it can be ended) and runs no more.
3125
+ */
3126
+ async function everythingFromDashboard(steps, opened, say, control, info) {
3127
+ let stopped = false;
3128
+ let stopStep = null;
3129
+ control.onStop(() => {
3130
+ stopped = true;
3131
+ if (stopStep) stopStep();
3132
+ });
3133
+ const step = { onStop: (fn) => (stopStep = fn) };
3134
+ const done = [];
3135
+ for (let i = 0; i < steps.length; i++) {
3136
+ const s = steps[i];
3137
+ if (stopped) {
3138
+ done.push({ id: s.id, label: s.label, ok: false, said: 'not run: stopped' });
3139
+ continue;
3140
+ }
3141
+ say('');
3142
+ say('Step ' + (i + 1) + ' of ' + steps.length + ': ' + s.label + ' - ' + s.title);
3143
+ stopStep = null;
3144
+ let r;
3145
+ try {
3146
+ if (s.id === 'api') r = await apiFromDashboard(s.origin, say, step);
3147
+ else if (PROBE_CHECKS[s.id]) r = await probeFromDashboard(s.id, say, step, info.confirmed);
3148
+ else r = await runFromDashboard(s.id, opened, say);
3149
+ } catch (err) {
3150
+ r = { ok: false, why: err && err.message ? err.message : String(err) };
3151
+ }
3152
+ const ok = Boolean(r && r.ok);
3153
+ done.push({ id: s.id, label: s.label, ok: ok, said: ok ? r.summary : 'did not finish: ' + ((r && r.why) || 'it gave no answer') });
3154
+ say((ok ? 'Done: ' : 'Did not finish: ') + (ok ? r.summary : (r && r.why) || 'it gave no answer'));
3155
+ }
3156
+ say('');
3157
+ say('What ran:');
3158
+ for (const d of done) say(' ' + d.label + ': ' + d.said);
3159
+ if ((info.skipped || []).length) {
3160
+ say('Not in this run:');
3161
+ for (const s of info.skipped) say(' ' + s.label + ': ' + s.why);
3162
+ }
3163
+ const finished = done.filter((d) => d.ok).length;
3164
+ let open = [];
3165
+ try {
3166
+ open = require(path.join(PACKAGE_DIR, 'kryptheon-dashboard.js')).buildState(USER_DIR).findings;
3167
+ } catch (err) {
3168
+ open = [];
3169
+ }
3170
+ const proven = open.filter((f) => f.status === 'confirmed').length;
3171
+ const found = proven ? proven + (proven === 1 ? ' problem proven' : ' problems proven') + (open.length > proven ? ', ' + (open.length - proven) + ' more to look at' : '') + ' - see Findings'
3172
+ : open.length ? open.length + (open.length === 1 ? ' thing' : ' things') + ' to look at - see Findings' : 'no open findings';
3173
+ return {
3174
+ ok: finished > 0,
3175
+ why: finished ? undefined : 'no step finished - see what it printed',
3176
+ summary: finished + ' of ' + steps.length + ' checks finished' + (stopped ? ' (stopped part way)' : '') + '. ' + found + '.',
3177
+ steps: done,
3178
+ skipped: info.skipped || [],
3179
+ stopped: stopped,
3180
+ };
3181
+ }
3182
+
3083
3183
  function runFromDashboard(id, opened, say) {
3084
3184
  if (id === 'backend') {
3085
3185
  say('Reading the server code in ' + USER_DIR + ' ...');
@@ -3549,6 +3649,7 @@ async function dashboardCommand() {
3549
3649
  const dash = require(path.join(PACKAGE_DIR, 'kryptheon-dashboard.js'));
3550
3650
  const app = dash.createServer(USER_DIR, {
3551
3651
  run: (id, say) => runFromDashboard(id, opened, say),
3652
+ everything: (steps, say, control, info) => everythingFromDashboard(steps, opened, say, control, info),
3552
3653
  recheck: (id, say) => recheckFromDashboard(id, opened, say),
3553
3654
  probe: (id, say, control, confirmed) => probeFromDashboard(id, say, control, confirmed),
3554
3655
  probeRecheck: (id, say, control, confirmed) => probeRecheckFromDashboard(id, say, control, confirmed),
package/kryptheon-api.js CHANGED
@@ -659,6 +659,8 @@ async function runInvalid(root, options) {
659
659
  say('Signed in as ' + who.email + '. Sending bad values to routes that create something...');
660
660
 
661
661
  const out = { ran: true, account: who.email, tried: [], findings: [], notTried: [], left: [] };
662
+ // this run's own mark on values that must be unique: a code made by an earlier run is never what is refused
663
+ const runTag = Date.now().toString(36).slice(-4);
662
664
  const read = async (template) => {
663
665
  const res = await session.send('GET', template, {});
664
666
  return res.status === 200 ? JSON.stringify(res.json !== undefined ? res.json : res.text) : null;
@@ -719,11 +721,20 @@ async function runInvalid(root, options) {
719
721
  const send = (b) => session.send(a.method, a.template, a.encoding === 'form' ? { form: b } : { json: b });
720
722
  const minimal = {};
721
723
  for (const f of a.fields) if (f.kinds.length || wordsOf(f.name).some((x) => REQUIRED_NAME.test(x)) || f.kinds.includes('enum')) minimal[f.name] = f.sample;
724
+ let tries = 0;
725
+ // a value that must be unique (a promo code, a sku) is new on every send, the first one too: the run's earlier
726
+ // steps may already have made one with the same code (round 28)
727
+ const fresh = (b, skip) => {
728
+ const c = Object.assign({}, b);
729
+ tries++;
730
+ for (const k of Object.keys(c)) if (k !== skip && typeof c[k] === 'string' && UNIQUE_NAME.test(wordsOf(k).slice(-1)[0] || '')) c[k] = c[k] + '-' + runTag + tries;
731
+ return c;
732
+ };
722
733
  let base = a.fullData;
723
- let first = await send(base);
734
+ let first = await send(fresh(base));
724
735
  if (!isTaken(first) && Object.keys(minimal).length && JSON.stringify(minimal) !== JSON.stringify(base)) {
725
736
  base = minimal;
726
- first = await send(base);
737
+ first = await send(fresh(base));
727
738
  }
728
739
  if (!isTaken(first)) {
729
740
  out.notTried.push({ what: what, why: 'the app refused the request (' + first.status + ') before any bad value was added, so no bad value could be judged' });
@@ -733,7 +744,7 @@ async function runInvalid(root, options) {
733
744
  for (const pr of probes) {
734
745
  if (opts.stopped && opts.stopped()) return out;
735
746
  const f = pr.field;
736
- const body = Object.assign({}, base, { [f.name]: pr.value });
747
+ const body = fresh(Object.assign({}, base, { [f.name]: pr.value }), f.name);
737
748
  if (pr.mode === 'overdraw') {
738
749
  await overdraw(a, f, pr, base, body, send);
739
750
  continue;
@@ -1038,10 +1049,26 @@ async function runJobs(root, options) {
1038
1049
  const send = (s) => s.send(a.method, a.template, a.encoding === 'form' ? { form: a.fullData || {} } : { json: a.fullData || {} });
1039
1050
  let first = null;
1040
1051
  let by = null;
1052
+ let crash = null;
1041
1053
  for (const s of sessions) {
1042
1054
  const res = await send(s);
1043
1055
  const reply = authz.replyOf(res);
1044
1056
  if (reply.kind === 'accepted' || reply.kind === 'redirect') { first = res; by = s; break; }
1057
+ if (res.status >= 500 && !crash) crash = { res, s };
1058
+ }
1059
+ // a job that fails when it runs is not a refusal (round 28: the settlement job answered 500 whenever there was a
1060
+ // paid order to settle - its own SQL named a column the table does not have)
1061
+ if (!first && crash) {
1062
+ out.tried.push({ what, by: crash.s.email, verdict: 'crashed (' + crash.res.status + ')' });
1063
+ const finding = {
1064
+ kind: 'job-crash', where: what, from: a.from.file + ':' + a.from.line, status: 'confirmed', severity: 'LOW',
1065
+ headline: what + ' answered ' + crash.res.status + ' when ' + crash.s.email + ' ran it, with nothing wrong in the request: the job fails when it has work to do.',
1066
+ proof: ['As ' + crash.s.email + ', sent ' + what + '.', 'The answer: ' + crash.res.status + ' ' + authz.clip(crash.res.text, 120)],
1067
+ };
1068
+ finding.fixPrompt = ['Kryptheon ran a job of my app and it failed:', '', finding.headline, '', 'Where: ' + finding.from, '',
1069
+ 'Look at the server log for this request: the job\'s query or code fails on the data it reads. Fix that one cause, then run the job twice in a row to see it does its work once. Change nothing else.', '', 'Afterwards I will run: npx kryptheon api'].join(String.fromCharCode(10));
1070
+ out.findings.push(finding);
1071
+ continue;
1045
1072
  }
1046
1073
  if (!first) { out.tried.push({ what, verdict: 'no account was let in' }); continue; }
1047
1074
  // The rows a job's answer gives, each marked with its organization (round 27 R27-22: /maintenance/run made every
@@ -1069,7 +1096,7 @@ async function runJobs(root, options) {
1069
1096
  const c1 = countsIn(first.json);
1070
1097
  const c2 = countsIn(second.json);
1071
1098
  const again = Object.keys(c1).filter((k) => c1[k] > 0 && c2[k] === c1[k]);
1072
- 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' });
1099
+ out.tried.push({ what, by: by.email, first: c1, second: c2, verdict: again.length ? 'made the same again' : !Object.keys(c1).length ? 'no count to compare' : Object.values(c1).every((v) => !v) && Object.values(c2).every((v) => !v) ? 'nothing to do either time' : 'held' });
1073
1100
  out.left.push(what + ' run twice as ' + by.email + (Object.keys(c1).length ? ' (' + JSON.stringify(c1) + ', then ' + JSON.stringify(c2) + ')' : ''));
1074
1101
  if (!again.length) continue;
1075
1102
  const k = again[0];
@@ -1087,8 +1114,9 @@ async function runJobs(root, options) {
1087
1114
 
1088
1115
  // --- a create sent twice at once ------------------------------------------------
1089
1116
 
1090
- // Records a second copy of costs something: money, a delivery, someone's time.
1091
- const COSTLY_RECORD = /(^|[\/_-])(orders?|payments?|invoices?|deliver(y|ies)|shipments?|bookings?|reservations?|transfers?|refunds?|payouts?|charges?|subscriptions?|appointments?)$/i;
1117
+ // Records a second copy of costs something: money, a delivery, someone's time - or lets one ticket in twice (round 28:
1118
+ // POST /api/tickets/{id}/scan recorded a second accepted check-in).
1119
+ const COSTLY_RECORD = /(^|[\/_-])(orders?|payments?|invoices?|deliver(y|ies)|shipments?|bookings?|reservations?|transfers?|refunds?|payouts?|charges?|subscriptions?|appointments?|scans?|check-?ins?|redeem|redemptions?|claims?)$/i;
1092
1120
 
1093
1121
  /**
1094
1122
  * Each create route for a costly record (POST /api/work-orders, /api/payments) that the app also lists at the same
@@ -1102,7 +1130,19 @@ async function runCreates(root, options) {
1102
1130
  if (!plan.ran) return { ran: false, why: plan.why };
1103
1131
  const j = plan.journey;
1104
1132
  const lists = new Set(j.actions.filter((x) => x.method === 'GET' && !x.params.length).map((x) => x.template));
1105
- const routes = j.actions.filter((a) => a.method === 'POST' && !a.params.length && lists.has(a.template) && COSTLY_RECORD.test(a.template) && !JOB_PATH.test(a.template));
1133
+ // a create with its parent's id in the address (POST /api/invoices/{orderId}/payment-order, round 28): aimed at a
1134
+ // record of the list its id is named for (orderId: /api/orders), or of the list its address starts with
1135
+ const singular = (w) => w.replace(/ies$/, 'y').replace(/s$/, '');
1136
+ const parentListOf = (a) => {
1137
+ const r = (plan.map.routes || []).find((x) => x.method === a.method && normRoute(x.path || '') === a.template);
1138
+ const m = r && /:(\w+)/.exec(r.path);
1139
+ const stem = m ? singular(m[1].replace(/_?[iI]d$/, '').toLowerCase()) : '';
1140
+ const named = stem && Array.from(lists).find((l) => singular(l.split('/').pop().toLowerCase().replace(/[^a-z]/g, '')) === stem);
1141
+ const head = '/' + a.segs.slice(0, a.params[0].index).join('/');
1142
+ return named || (lists.has(head) ? head : null);
1143
+ };
1144
+ const routes = j.actions.filter((a) => a.method === 'POST' && COSTLY_RECORD.test(a.template) && !JOB_PATH.test(a.template) &&
1145
+ (a.params.length ? a.params.length === 1 && parentListOf(a) : lists.has(a.template)));
1106
1146
  if (!routes.length) return { ran: false, why: 'no create route of an order, payment or delivery is also listed at its address' };
1107
1147
  let origin;
1108
1148
  try {
@@ -1111,7 +1151,7 @@ async function runCreates(root, options) {
1111
1151
  return { ran: false, why: 'the app must be on this computer (localhost or 127.0.0.1): ' + (opts.origin || 'no address was given') };
1112
1152
  }
1113
1153
  if (!j.signIn) return { ran: false, why: j.signInWhy };
1114
- const ctx = authz.makeContext({ baseUrl: origin, allowOrigins: [origin], fetch: opts.fetch, passwords: Object.fromEntries(j.accounts.usable.map((a) => [a.email, a.password])), maxRequests: 120 });
1154
+ const ctx = authz.makeContext({ baseUrl: origin, allowOrigins: [origin], fetch: opts.fetch, passwords: Object.fromEntries(j.accounts.usable.map((a) => [a.email, a.password])), maxRequests: opts.maxRequests || 400 });
1115
1155
  const sessions = [];
1116
1156
  for (const a of j.accounts.usable) {
1117
1157
  const s = authz.makeSession(ctx, 'account', a.email, a.password);
@@ -1123,56 +1163,233 @@ async function runCreates(root, options) {
1123
1163
  const recordsIn = (r) => (r.status === 200 ? (Array.isArray(r.json) ? r.json : r.json && typeof r.json === 'object' ? Object.values(r.json).find(Array.isArray) : null) : null);
1124
1164
  const taken = (res) => { const r = authz.replyOf(res); return r.kind === 'accepted' || r.kind === 'redirect'; };
1125
1165
  const out = { ran: true, tried: [], findings: [], left: [] };
1126
- for (const a of routes) {
1127
- const what = a.method + ' ' + a.template;
1128
- const t = { what, verdict: 'no account was let in' };
1129
- out.tried.push(t);
1130
- const minimal = {};
1131
- for (const f of a.fields || []) if (f.kinds.length) minimal[f.name] = f.sample;
1132
- const bodies = [a.fullData || {}].concat(Object.keys(minimal).length && JSON.stringify(minimal) !== JSON.stringify(a.fullData || {}) ? [minimal] : []);
1133
- for (const s of sessions) {
1134
- const before = recordsIn(await s.send('GET', a.template, {}));
1135
- if (!before) continue;
1136
- const had = new Set(before.map((x) => x && JSON.stringify(x.id)));
1137
- let done = null;
1138
- let answered = [];
1139
- for (const body of bodies) {
1140
- const send = () => s.send(a.method, a.template, a.encoding === 'form' ? { form: body } : { json: body });
1141
- const [r1, r2] = await Promise.all([send(), send()]);
1142
- answered = [r1.status, r2.status];
1143
- if (!taken(r1) && !taken(r2)) continue;
1144
- done = { body, r1, r2 };
1166
+ // the cap on requests ends this step only: what it did not reach is said, and the rest of the run goes on
1167
+ // (round 28: reading every list around each create reached the cap and stopped the whole API run)
1168
+ try {
1169
+ for (const a of routes) {
1170
+ const what = a.method + ' ' + a.template;
1171
+ const t = { what, verdict: 'no account was let in' };
1172
+ out.tried.push(t);
1173
+ const minimal = {};
1174
+ for (const f of a.fields || []) if (f.kinds.length) minimal[f.name] = f.sample;
1175
+ const bodies = [a.fullData || {}].concat(Object.keys(minimal).length && JSON.stringify(minimal) !== JSON.stringify(a.fullData || {}) ? [minimal] : []);
1176
+ for (const s of sessions) {
1177
+ let at = a.template;
1178
+ let parentId = null;
1179
+ if (a.params.length) {
1180
+ const parents = recordsIn(await s.send('GET', parentListOf(a), {}));
1181
+ const parent = parents && parents.find((x) => x && x.id !== undefined);
1182
+ if (!parent) { t.verdict = 'no record at GET ' + parentListOf(a) + ' to aim it at'; continue; }
1183
+ parentId = parent.id;
1184
+ at = a.template.replace('{id}', encodeURIComponent(String(parent.id)));
1185
+ }
1186
+ // its own list, or - when the record it makes lives elsewhere - every list the account reads
1187
+ const watch = a.params.length ? Array.from(lists) : [a.template];
1188
+ const readAll = async () => {
1189
+ const o = {};
1190
+ for (const l of watch) { const r = recordsIn(await s.send('GET', l, {})); if (r) o[l] = r; }
1191
+ return o;
1192
+ };
1193
+ const beforeAll = await readAll();
1194
+ if (!Object.keys(beforeAll).length) continue;
1195
+ let done = null;
1196
+ let answered = [];
1197
+ for (const body of bodies) {
1198
+ const send = () => s.send(a.method, at, a.encoding === 'form' ? { form: body } : { json: body });
1199
+ const [r1, r2] = await Promise.all([send(), send()]);
1200
+ answered = [r1.status, r2.status];
1201
+ if (!taken(r1) && !taken(r2)) continue;
1202
+ done = { body, r1, r2 };
1203
+ break;
1204
+ }
1205
+ // refused both times: the next account may be let in
1206
+ if (!done) { t.verdict = 'refused (' + answered.join(' and ') + ')'; continue; }
1207
+ // the list that gained the most records is where the creates went
1208
+ const afterAll = await readAll();
1209
+ let made = [];
1210
+ let madeIn = watch[0];
1211
+ for (const l of Object.keys(afterAll)) {
1212
+ const had = new Set((beforeAll[l] || []).map((x) => x && JSON.stringify(x.id)));
1213
+ const fresh = afterAll[l].filter((x) => x && x.id !== undefined && !had.has(JSON.stringify(x.id)));
1214
+ if (fresh.length > made.length) { made = fresh; madeIn = l; }
1215
+ }
1216
+ const before = beforeAll[madeIn] || [];
1217
+ const after = afterAll[madeIn] || [];
1218
+ t.by = s.email;
1219
+ t.answers = [done.r1.status, done.r2.status];
1220
+ t.made = made.map((x) => x.id);
1221
+ if (made.length) out.left.push(what + ' sent twice at once as ' + s.email + ': record(s) ' + made.map((x) => x.id).join(', ') + ' (Kryptheon never deletes)');
1222
+ // the same fields the body set, on both records
1223
+ const keys = Object.keys(done.body).filter((k) => made.length && made.every((x) => valueIn(x, k) !== undefined))
1224
+ // and the field that points at the parent the address named (order_id = 1 on both payment orders)
1225
+ .concat(parentId === null || !made.length ? [] : Object.keys(made[0]).filter((k) => k !== 'id' && String(made[0][k]) === String(parentId)));
1226
+ const alike = made.length >= 2 && keys.every((k) => JSON.stringify(valueIn(made[0], k)) === JSON.stringify(valueIn(made[1], k)));
1227
+ t.verdict = alike ? 'made twice' : made.length === 1 ? 'held: one record' : made.length ? 'made ' + made.length + ' different records' : 'nothing new in the list';
1228
+ if (t.verdict === 'made twice') {
1229
+ const finding = {
1230
+ kind: 'create-twice', where: what, from: a.from.file + ':' + a.from.line, status: 'verification required', severity: 'MEDIUM',
1231
+ headline: 'Sent twice at once, as a double click sends it, ' + what + ' made ' + made.length + ' records with the same ' + (keys.slice(0, 3).join(', ') || 'fields') + ' (' + made.map((x) => x.id).join(', ') + '): nothing stops a repeated submit.',
1232
+ proof: ['As ' + s.email + ', sent ' + a.method + ' ' + at + ' twice at the same moment with the same body: ' + authz.clip(JSON.stringify(done.body), 120), 'Answered ' + done.r1.status + ' and ' + done.r2.status + '.', 'GET ' + madeIn + ' before: ' + before.length + ' records; after: ' + after.length + ', new ' + made.map((x) => x.id).join(', ') + '.'],
1233
+ };
1234
+ finding.fixPrompt = ['Kryptheon sent the same create to my running app twice at once, as a double click does:', '', finding.headline, '', 'Where: ' + finding.from, '',
1235
+ 'First check this is real: is a second, identical record ever wanted here? If not, accept an idempotency key from the client (or a fingerprint of the request) and return the first record for a repeat, with a unique index behind it; and disable the submit button while the request is pending. Change nothing else.', '', 'Afterwards I will run: npx kryptheon api'].join(String.fromCharCode(10));
1236
+ out.findings.push(finding);
1237
+ }
1145
1238
  break;
1146
1239
  }
1147
- // refused both times: the next account may be let in
1148
- if (!done) { t.verdict = 'refused (' + answered.join(' and ') + ')'; continue; }
1149
- const after = recordsIn(await s.send('GET', a.template, {})) || [];
1150
- const made = after.filter((x) => x && x.id !== undefined && !had.has(JSON.stringify(x.id)));
1151
- t.by = s.email;
1152
- t.answers = [done.r1.status, done.r2.status];
1153
- t.made = made.map((x) => x.id);
1154
- if (made.length) out.left.push(what + ' sent twice at once as ' + s.email + ': record(s) ' + made.map((x) => x.id).join(', ') + ' (Kryptheon never deletes)');
1155
- // the same fields the body set, on both records
1156
- const keys = Object.keys(done.body).filter((k) => made.length && made.every((x) => valueIn(x, k) !== undefined));
1157
- const alike = made.length >= 2 && keys.every((k) => JSON.stringify(valueIn(made[0], k)) === JSON.stringify(valueIn(made[1], k)));
1158
- t.verdict = alike ? 'made twice' : made.length === 1 ? 'held: one record' : made.length ? 'made ' + made.length + ' different records' : 'nothing new in the list';
1159
- if (t.verdict === 'made twice') {
1160
- const finding = {
1161
- kind: 'create-twice', where: what, from: a.from.file + ':' + a.from.line, status: 'verification required', severity: 'MEDIUM',
1162
- headline: 'Sent twice at once, as a double click sends it, ' + what + ' made ' + made.length + ' records with the same ' + (keys.slice(0, 3).join(', ') || 'fields') + ' (' + made.map((x) => x.id).join(', ') + '): nothing stops a repeated submit.',
1163
- proof: ['As ' + s.email + ', sent ' + what + ' twice at the same moment with the same body: ' + authz.clip(JSON.stringify(done.body), 120), 'Answered ' + done.r1.status + ' and ' + done.r2.status + '.', 'GET ' + a.template + ' before: ' + before.length + ' records; after: ' + after.length + ', new ' + made.map((x) => x.id).join(', ') + '.'],
1240
+ }
1241
+ } catch (err) {
1242
+ if (!/authz: stopped/.test(String(err && err.message))) throw err;
1243
+ for (const a of routes) if (!out.tried.some((x) => x.what === a.method + ' ' + a.template)) out.tried.push({ what: a.method + ' ' + a.template, verdict: 'not tried: the cap on requests was reached' });
1244
+ }
1245
+ return out;
1246
+ }
1247
+
1248
+
1249
+ // --- a limit used up -------------------------------------------------------------
1250
+
1251
+ // A record's limit and the count that runs up to it: capacity and sold, max_uses and uses.
1252
+ const LIMIT_FIELD = /^(capacity|max_\w+|limit|quota|stock|seats_total|total_seats)$/i;
1253
+ const COUNTER_FIELD = /^(sold|uses|used|used_count|booked|taken|reserved|issued|redeemed|count)$/i;
1254
+ // a field of the create that says how many: what a capacity is used up by
1255
+ const AMOUNT_FIELD = /^(quantity|qty|seats|tickets|units|count)$/i;
1256
+ const MAX_LIMIT_SENDS = 25;
1257
+
1258
+ /** A record's limit pair, or null: { limit, counter, left }. */
1259
+ function limitPairOf(rec) {
1260
+ if (!rec || typeof rec !== 'object') return null;
1261
+ const keys = Object.keys(rec);
1262
+ for (const l of keys.filter((k) => LIMIT_FIELD.test(k))) {
1263
+ for (const c of keys.filter((k) => COUNTER_FIELD.test(k))) {
1264
+ const L = Number(rec[l]);
1265
+ const C = Number(rec[c]);
1266
+ if (Number.isFinite(L) && Number.isFinite(C) && L > 0 && C >= 0 && C < L && L - C <= 1000) return { limit: l, counter: c, left: L - C };
1267
+ }
1268
+ }
1269
+ return null;
1270
+ }
1271
+
1272
+ /**
1273
+ * Each create of a costly record (POST /api/orders) whose body names a record that carries a limit and its count
1274
+ * (ticket_type_id: capacity and sold; promo_code: max_uses and uses), found through the route named for it
1275
+ * (/api/ticket-types/{eventId}, /api/promos). One send asks for one more than is left; if the count moves by one
1276
+ * per request instead, one request more than is left is sent (at most 25 in all). The record read before and
1277
+ * after decides: the count above the limit = confirmed. Round 28 EF28-20 (capacity) and EF28-21 (promo max_uses).
1278
+ * Nothing is deleted; what was made is listed as left. { ran, why, tried, findings, left }.
1279
+ */
1280
+ async function runLimits(root, options) {
1281
+ const opts = options || {};
1282
+ const plan = planOf(root, opts);
1283
+ if (!plan.ran) return { ran: false, why: plan.why };
1284
+ const j = plan.journey;
1285
+ const gets = j.actions.filter((x) => x.method === 'GET' && x.params.length <= 1);
1286
+ const singular = (w) => w.replace(/ies$/, 'y').replace(/s$/, '');
1287
+ const flat = (w) => singular(String(w).toLowerCase().replace(/[^a-z]/g, ''));
1288
+ // the routes a body key names: ticket_type_id -> /api/ticket-types/{id}, promo_code -> /api/promos
1289
+ const routesFor = (key) => {
1290
+ const words = key.replace(/_?(id|code)$/i, '');
1291
+ return words ? gets.filter((g) => flat(g.segs.filter((s) => s !== '{id}').pop() || '') === flat(words)) : [];
1292
+ };
1293
+ const routes = j.actions.filter((a) => a.method === 'POST' && !a.params.length && COSTLY_RECORD.test(a.template) && Object.keys(a.fullData || {}).some((k) => /_(id|code)$/i.test(k) && routesFor(k).length));
1294
+ if (!routes.length) return { ran: false, why: 'no create of an order, payment or booking names a record with a limit' };
1295
+ let origin;
1296
+ try {
1297
+ origin = authz.assertLocalTarget(opts.origin);
1298
+ } catch (err) {
1299
+ return { ran: false, why: 'the app must be on this computer (localhost or 127.0.0.1): ' + (opts.origin || 'no address was given') };
1300
+ }
1301
+ if (!j.signIn) return { ran: false, why: j.signInWhy };
1302
+ const ctx = authz.makeContext({ baseUrl: origin, allowOrigins: [origin], fetch: opts.fetch, passwords: Object.fromEntries(j.accounts.usable.map((a) => [a.email, a.password])), maxRequests: opts.maxRequests || 300 });
1303
+ const sessions = [];
1304
+ for (const a of j.accounts.usable) {
1305
+ const s = authz.makeSession(ctx, 'account', a.email, a.password);
1306
+ const f = { [j.signIn.emailField]: a.email, [j.signIn.passwordField]: a.password };
1307
+ const r = authz.replyOf(await s.send(j.signIn.method, j.signIn.path, j.signIn.encoding === 'form' ? { form: f } : { json: f }));
1308
+ if (r.kind !== 'refused' && r.kind !== 'server-error' && r.kind !== 'no-answer') sessions.push(s);
1309
+ }
1310
+ if (!sessions.length) return { ran: false, why: 'no account with a password in .env could sign in' };
1311
+ const recordsIn = (r) => (r.status === 200 ? (Array.isArray(r.json) ? r.json : r.json && typeof r.json === 'object' ? Object.values(r.json).find(Array.isArray) : null) : null);
1312
+ const taken = (res) => { const r = authz.replyOf(res); return r.kind === 'accepted' || r.kind === 'redirect'; };
1313
+ const out = { ran: true, tried: [], findings: [], left: [] };
1314
+ try {
1315
+ for (const a of routes) {
1316
+ const what = a.method + ' ' + a.template;
1317
+ const base = Object.assign({}, a.fullData || {});
1318
+ for (const key of Object.keys(base).filter((k) => /_(id|code)$/i.test(k))) {
1319
+ const t = { what, key, verdict: 'no record with a limit was found' };
1320
+ const s = sessions[0];
1321
+ // the records the key names: a list, or one filled with another id of the body (event_id for ticket types)
1322
+ const fills = Object.keys(base).filter((k) => k !== key && /_id$/i.test(k)).map((k) => base[k]);
1323
+ let recs = [];
1324
+ let readAt = null;
1325
+ for (const g of routesFor(key)) {
1326
+ const tries = g.params.length ? fills.map((v) => g.template.replace('{id}', encodeURIComponent(String(v)))) : [g.template];
1327
+ for (const at of tries) {
1328
+ const found = recordsIn(await s.send('GET', at, {})) || [];
1329
+ if (found.some(limitPairOf)) { recs = found; readAt = at; break; }
1330
+ }
1331
+ if (readAt) break;
1332
+ }
1333
+ const rec = recs.find(limitPairOf);
1334
+ if (!rec) continue;
1335
+ out.tried.push(t);
1336
+ const pair = limitPairOf(rec);
1337
+ const field = /_code$/i.test(key) ? Object.keys(rec).find((k) => /code$/i.test(k)) : 'id';
1338
+ if (!field || rec[field] === undefined) { t.verdict = 'the record has no ' + (field || 'code') + ' to name it by'; continue; }
1339
+ const body = Object.assign({}, base, { [key]: rec[field] });
1340
+ // the body's other fields the record also carries (event_id of the ticket type) are the record's own
1341
+ for (const k of Object.keys(body)) if (k !== key && rec[k] !== undefined && /_id$/i.test(k)) body[k] = rec[k];
1342
+ const amount = Object.keys(body).find((k) => AMOUNT_FIELD.test(k));
1343
+ const readCount = async () => {
1344
+ const again = recordsIn(await s.send('GET', readAt, {})) || [];
1345
+ const now = again.find((x) => x && String(x[field]) === String(rec[field]));
1346
+ return now ? Number(now[pair.counter]) : null;
1164
1347
  };
1165
- finding.fixPrompt = ['Kryptheon sent the same create to my running app twice at once, as a double click does:', '', finding.headline, '', 'Where: ' + finding.from, '',
1166
- 'First check this is real: is a second, identical record ever wanted here? If not, accept an idempotency key from the client (or a fingerprint of the request) and return the first record for a repeat, with a unique index behind it; and disable the submit button while the request is pending. Change nothing else.', '', 'Afterwards I will run: npx kryptheon api'].join(String.fromCharCode(10));
1167
- out.findings.push(finding);
1348
+ const send = (b) => s.send(a.method, a.template, a.encoding === 'form' ? { form: b } : { json: b });
1349
+ const limit = Number(rec[pair.limit]);
1350
+ const before = Number(rec[pair.counter]);
1351
+ // one send for one more than is left
1352
+ const first = await send(amount ? Object.assign({}, body, { [amount]: pair.left + 1 }) : body);
1353
+ let now = await readCount();
1354
+ let sends = 1;
1355
+ // the count moved by one: it counts requests, so one request more than is left
1356
+ if (taken(first) && now !== null && now === before + 1 && now <= limit && limit - now + 1 + sends <= MAX_LIMIT_SENDS) {
1357
+ const one = amount ? Object.assign({}, body, { [amount]: 1 }) : body;
1358
+ while (now !== null && now <= limit && sends < MAX_LIMIT_SENDS) {
1359
+ const r = await send(one);
1360
+ sends++;
1361
+ if (!taken(r)) break;
1362
+ now = await readCount();
1363
+ }
1364
+ }
1365
+ t.by = s.email;
1366
+ t.sends = sends;
1367
+ t.record = rec[field];
1368
+ out.left.push(what + ' sent ' + sends + ' time(s) as ' + s.email + ' against ' + key + ' ' + rec[field] + ' (Kryptheon never deletes)');
1369
+ if (now !== null && now > limit) {
1370
+ t.verdict = 'over the limit';
1371
+ const finding = {
1372
+ kind: 'over-limit', where: what, field: pair.limit, from: a.from.file + ':' + a.from.line, status: 'confirmed', severity: 'HIGH',
1373
+ headline: what + ' went past ' + key + ' ' + rec[field] + '\'s ' + pair.limit + ': ' + pair.counter + ' is now ' + now + ' of ' + limit + ' - nothing checks the limit before it is used.',
1374
+ proof: ['As ' + s.email + ', read GET ' + readAt + ': ' + pair.counter + ' ' + before + ' of ' + pair.limit + ' ' + limit + '.',
1375
+ 'Sent ' + what + ' ' + sends + ' time(s)' + (amount ? ', the first asking for ' + amount + ' ' + (pair.left + 1) : '') + '.',
1376
+ 'Read it again: ' + pair.counter + ' ' + now + ' of ' + limit + '.'],
1377
+ };
1378
+ finding.fixPrompt = ['Kryptheon used up a limit in my running app, and went past it:', '', finding.headline, '', 'Where: ' + finding.from, '',
1379
+ 'First check this is real. If it is, in the same transaction as the write, check ' + pair.counter + ' + what is asked <= ' + pair.limit + ' (with a row lock, or an UPDATE ... WHERE ' + pair.counter + ' + $n <= ' + pair.limit + ' that refuses when no row changes), and answer 409 when it would go past. Change nothing else.', '', 'Afterwards I will run: npx kryptheon api'].join(String.fromCharCode(10));
1380
+ out.findings.push(finding);
1381
+ } else {
1382
+ t.verdict = now === null ? 'the record could not be read again' : now === before ? 'held: refused, the count did not move' : 'held: stopped at the limit (' + now + ' of ' + limit + ')';
1383
+ }
1168
1384
  }
1169
- break;
1170
1385
  }
1386
+ } catch (err) {
1387
+ if (!/authz: stopped/.test(String(err && err.message))) throw err;
1388
+ out.tried.push({ what: '(the rest)', verdict: 'not tried: the cap on requests was reached' });
1171
1389
  }
1172
1390
  return out;
1173
1391
  }
1174
1392
 
1175
-
1176
1393
  /**
1177
1394
  * The map's "a status set with no look at the status now" items, tried: the
1178
1395
  * step sent for one record, then sent again for the same record. Judged by
@@ -1293,6 +1510,8 @@ function downFinding(email, what, f, pr, res) {
1293
1510
 
1294
1511
  // Names of fields that are plainly required, so a blank one is worth trying even where the code checks nothing.
1295
1512
  const REQUIRED_NAME = /^(name|title|subject|address|city|street|postcode|postal|zip|recipient|reason|label)$/;
1513
+ // The last word of a field whose value must be unique: a code, a sku, a slug, a reference.
1514
+ const UNIQUE_NAME = /^(code|slug|sku|ref|reference|handle|username)$/;
1296
1515
  const NOT_PLAIN_TEXT = /^(email|mail|date|day|at|on|url|link|website|phone|mobile|role|status|state|currency|country|id|key|token)$/;
1297
1516
 
1298
1517
  /**
@@ -1346,6 +1565,10 @@ function probesOf(a) {
1346
1565
  } else if (typeof f.sample === 'number' && w[w.length - 1] !== 'id' && (w.some((x) => NUMBER_WORDS.has(x)) || f.kinds.includes('range'))) {
1347
1566
  out.push({ field: f, mode: 'below', value: -1, why: 'a negative number (-1)' });
1348
1567
  }
1568
+ // a share of 100: one above it (round 28: discount_percent 150 makes an order total negative)
1569
+ if (typeof f.sample === 'number' && /^(percent|pct|percentage)$/.test(w[w.length - 1] || '')) {
1570
+ out.push({ field: f, mode: 'above', value: 150, why: 'above 100 percent (150)' });
1571
+ }
1349
1572
  }
1350
1573
  return out;
1351
1574
  }
@@ -1582,13 +1805,16 @@ async function runApi(root, options) {
1582
1805
  if (!m.ran) return { ran: false, why: m.why };
1583
1806
  const toCheck = opts.toCheck || map.toCheck(m);
1584
1807
  const shared = Object.assign({}, opts, { map: m, say: say });
1808
+ // jobs first, on the app as it was seeded: the tries after them cancel and refund the records a job works on
1809
+ // (round 28: the settlement job found no paid order left and made nothing either time)
1810
+ const jobsRun = await runJobs(root, shared);
1585
1811
  const wall = await runWall(root, shared);
1586
1812
  const forged = await runForged(root, shared);
1587
1813
  const webhooks = await webhook.run(root, shared);
1588
1814
  const exportsRun = await csv.run(root, shared);
1589
- const jobsRun = await runJobs(root, shared);
1590
1815
  const keptRun = await runKept(root, shared);
1591
1816
  const createsRun = await runCreates(root, shared);
1817
+ const limitsRun = await runLimits(root, shared);
1592
1818
  const stepsRun = await runSteps(root, Object.assign({}, shared, { toCheck: toCheck }));
1593
1819
  let roleRun = null;
1594
1820
  if (opts.roles) roleRun = await runRoles(root, shared);
@@ -1596,14 +1822,14 @@ async function runApi(root, options) {
1596
1822
  const invalid = await runInvalid(root, shared);
1597
1823
  const verdicts = verdictsFor(toCheck, wall, invalid, roleRun, forged, webhooks, stepsRun);
1598
1824
  const seen = new Set();
1599
- const findings = (wall.ran ? wall.findings : []).concat(forged.ran ? forged.findings : []).concat(webhooks.ran ? webhooks.findings : []).concat(exportsRun.ran ? exportsRun.findings : []).concat(jobsRun.ran ? jobsRun.findings : []).concat(keptRun.ran ? keptRun.findings : []).concat(createsRun.ran ? createsRun.findings : []).concat(stepsRun.ran ? stepsRun.findings : []).concat(roleRun && roleRun.ran ? roleRun.findings : []).concat(invalid.ran ? invalid.findings : []).filter((f) => {
1825
+ const findings = (wall.ran ? wall.findings : []).concat(forged.ran ? forged.findings : []).concat(webhooks.ran ? webhooks.findings : []).concat(exportsRun.ran ? exportsRun.findings : []).concat(jobsRun.ran ? jobsRun.findings : []).concat(keptRun.ran ? keptRun.findings : []).concat(createsRun.ran ? createsRun.findings : []).concat(limitsRun.ran ? limitsRun.findings : []).concat(stepsRun.ran ? stepsRun.findings : []).concat(roleRun && roleRun.ran ? roleRun.findings : []).concat(invalid.ran ? invalid.findings : []).filter((f) => {
1600
1826
  const k = f.kind + '|' + f.where + '|' + (f.field || '');
1601
1827
  return seen.has(k) ? false : (seen.add(k), true);
1602
1828
  });
1603
- const left = (webhooks.ran ? webhooks.left : []).concat(exportsRun.ran ? exportsRun.left : []).concat(jobsRun.ran ? jobsRun.left : []).concat(keptRun.ran ? keptRun.left : []).concat(createsRun.ran ? createsRun.left : []).concat(stepsRun.ran ? stepsRun.left : []).concat(wall.ran ? wall.result.seeded || [] : []).concat(roleRun && roleRun.ran ? roleRun.result.seeded || [] : []).concat(wall.ran ? leftBehind(wall.result.attempts) : []).concat(invalid.ran ? invalid.left : []).concat(roleRun && roleRun.ran ? leftBehind(roleRun.result.attempts) : []);
1829
+ const left = (webhooks.ran ? webhooks.left : []).concat(exportsRun.ran ? exportsRun.left : []).concat(jobsRun.ran ? jobsRun.left : []).concat(keptRun.ran ? keptRun.left : []).concat(createsRun.ran ? createsRun.left : []).concat(limitsRun.ran ? limitsRun.left : []).concat(stepsRun.ran ? stepsRun.left : []).concat(wall.ran ? wall.result.seeded || [] : []).concat(roleRun && roleRun.ran ? roleRun.result.seeded || [] : []).concat(wall.ran ? leftBehind(wall.result.attempts) : []).concat(invalid.ran ? invalid.left : []).concat(roleRun && roleRun.ran ? leftBehind(roleRun.result.attempts) : []);
1604
1830
  return {
1605
1831
  ran: wall.ran || invalid.ran, why: wall.ran || invalid.ran ? null : wall.why, checkedAt: new Date().toISOString(),
1606
- origin: opts.origin, wall: wall, forged: forged, webhooks: webhooks, exports: exportsRun, jobs: jobsRun, kept: keptRun, creates: createsRun, steps: stepsRun, invalid: invalid, roles: roleRun, verdicts: verdicts, findings: findings, left: left,
1832
+ origin: opts.origin, wall: wall, forged: forged, webhooks: webhooks, exports: exportsRun, jobs: jobsRun, kept: keptRun, creates: createsRun, limits: limitsRun, steps: stepsRun, invalid: invalid, roles: roleRun, verdicts: verdicts, findings: findings, left: left,
1607
1833
  notTried: (wall.plan ? wall.plan.notTried : []).concat(invalid.ran ? invalid.notTried : []),
1608
1834
  };
1609
1835
  }
@@ -1708,6 +1934,14 @@ function apiLines(run) {
1708
1934
  }
1709
1935
  out.push('');
1710
1936
  }
1937
+ if (run.limits && run.limits.ran) {
1938
+ for (const t of run.limits.tried) out.push(' Limit ' + t.what + (t.key ? ' (' + t.key + (t.record !== undefined ? ' ' + t.record : '') + ')' : '') + ': ' + t.verdict + (t.sends ? ' after ' + t.sends + ' send(s)' : ''));
1939
+ for (const f of run.limits.findings) {
1940
+ out.push(' PROBLEM ' + f.severity + ' ' + f.headline + ' (' + f.from + ')');
1941
+ for (const l of f.proof) out.push(' ' + l);
1942
+ }
1943
+ out.push('');
1944
+ }
1711
1945
  if (run.steps && run.steps.ran) {
1712
1946
  for (const t of run.steps.tried) out.push(' Step ' + t.what + ' sent twice: ' + t.verdict + (t.record !== undefined ? ' (record ' + t.record + ', answered ' + t.first + ', then ' + t.second + ')' : ''));
1713
1947
  for (const f of run.steps.findings) {
@@ -1747,4 +1981,4 @@ function apiLines(run) {
1747
1981
  return out;
1748
1982
  }
1749
1983
 
1750
- module.exports = { reconcile, heldStrength, runSteps, numbersMoved, runKept, runCreates, COSTLY_RECORD, valueIn, plainFieldOf, runJobs, countsIn, JOB_PATH, runForged, personIn, REPORT_FILE, verdictsFor, runApi, reportOf, saveReport, apiLines, badValueFor, runInvalid, signInOf, emailsIn, accountsOf, wordsOf, sampleValue, allowedByName, actionOf, journeyFromMap, planOf, planLines, wallPolicy, runWall, findingsOf, fixPrompt, leftBehind, wallLines, DRAFT_FILE, permissionName, draftPolicy, writeDraft, runRoles };
1984
+ module.exports = { reconcile, heldStrength, runSteps, numbersMoved, runKept, runCreates, COSTLY_RECORD, runLimits, limitPairOf, valueIn, plainFieldOf, runJobs, countsIn, JOB_PATH, runForged, personIn, REPORT_FILE, verdictsFor, runApi, reportOf, saveReport, apiLines, badValueFor, runInvalid, signInOf, emailsIn, accountsOf, wordsOf, sampleValue, allowedByName, actionOf, journeyFromMap, planOf, planLines, wallPolicy, runWall, findingsOf, fixPrompt, leftBehind, wallLines, DRAFT_FILE, permissionName, draftPolicy, writeDraft, runRoles };
@@ -1152,7 +1152,7 @@
1152
1152
  var box = byId('runList');
1153
1153
  clear(box);
1154
1154
  byId('runBadge').textContent = String(state.runs.length);
1155
- if (!state.runs.length) { box.appendChild(el('p', 'empty', 'No recorded flows have been replayed yet. Record one with npx kryptheon record, then press Run checks.')); return; }
1155
+ if (!state.runs.length) { box.appendChild(el('p', 'empty', 'No recorded flows have been replayed yet. Record one with npx kryptheon record, then press Check my app.')); return; }
1156
1156
  state.runs.slice(0, 30).forEach(function (r) {
1157
1157
  var row = el('div', 'line-row');
1158
1158
  var skipped = r.skipped || [];
@@ -1281,18 +1281,82 @@
1281
1281
  }).catch(function (err) { input.checked = !on; toast('Not saved: ' + err.message); });
1282
1282
  }
1283
1283
 
1284
+ // "Check my app": one press, every check that can run from here. What it
1285
+ // runs, what it leaves out and why, and - when it adds test data - one yes
1286
+ // for all of it, shown before anything starts. The tick is kept across the
1287
+ // page's own reloads while the box is open.
1288
+ var everyOpen = false;
1289
+ var everyTicked = false;
1284
1290
  byId('runAll').addEventListener('click', function () {
1285
- if (!state) return;
1286
- var ids = state.checks.filter(function (c) { return c.runnable && c.enabled; }).map(function (c) { return c.id; });
1287
- if (!ids.length) { toast('Nothing can run from here yet - switch a check on, or record a flow.'); return; }
1288
- runChecks(ids, byId('runAll'));
1291
+ if (!state || !state.everything) return;
1292
+ var plan = state.everything;
1293
+ if (!plan.steps.length) { toast('Nothing can run from here yet' + (plan.skipped[0] ? ' - ' + plan.skipped[0].label + ': ' + plan.skipped[0].why : '')); return; }
1294
+ everyOpen = true;
1295
+ everyTicked = false;
1296
+ renderEvery();
1297
+ byId('everyBox').scrollIntoView({ block: 'nearest' });
1289
1298
  });
1299
+ function startEvery() {
1300
+ var plan = state.everything;
1301
+ everyOpen = false;
1302
+ renderEvery();
1303
+ runJob('/api/everything', { consent: true, liveConfirmed: everyTicked }).then(function (done) {
1304
+ var r = (done && done.result) || {};
1305
+ toast(r.ok ? r.summary : 'It did not finish' + (r.why ? ' - ' + r.why : ''));
1306
+ if (r.ok && plan) location.hash = '#findings';
1307
+ }, function () {});
1308
+ }
1309
+ function renderEvery() {
1310
+ var box = byId('everyBox');
1311
+ if (!box) return;
1312
+ clear(box);
1313
+ var plan = state && state.everything;
1314
+ box.hidden = !everyOpen || !plan || !plan.steps.length;
1315
+ if (box.hidden) return;
1316
+ var wrap = el('div', 'consent');
1317
+ wrap.appendChild(el('div', 'd-step', plan.addsData ? 'CHECK MY APP - THIS ADDS TEST DATA TO YOUR APP' : 'CHECK MY APP'));
1318
+ var list = el('ol', 'every-steps');
1319
+ plan.steps.forEach(function (s) {
1320
+ var li = el('li', null, s.label);
1321
+ if (s.live) li.appendChild(el('span', 'every-why', ' - only with the tick below'));
1322
+ list.appendChild(li);
1323
+ });
1324
+ wrap.appendChild(list);
1325
+ wrap.appendChild(linesBox(plan.live.length ? 'warn' : 'help', plan.lines));
1326
+ if (plan.skipped.length) {
1327
+ var out = el('div', 'every-out');
1328
+ out.appendChild(el('b', null, 'Not in this run'));
1329
+ var ul = el('ul');
1330
+ plan.skipped.forEach(function (s) { ul.appendChild(el('li', null, s.label + ': ' + s.why)); });
1331
+ out.appendChild(ul);
1332
+ wrap.appendChild(out);
1333
+ }
1334
+ if (plan.live.length) {
1335
+ var label = el('label', 'tick');
1336
+ var tick = el('input');
1337
+ tick.type = 'checkbox';
1338
+ tick.checked = everyTicked;
1339
+ tick.addEventListener('change', function () { everyTicked = tick.checked; });
1340
+ label.appendChild(tick);
1341
+ label.appendChild(document.createTextNode(' ' + plan.live.join(', ') + ' is my own test copy, not the live app real people use (leave it unticked and the steps that open it are left out)'));
1342
+ wrap.appendChild(label);
1343
+ }
1344
+ var row = el('div', 'd-actions');
1345
+ var yes = button('sun sm', 'check', plan.addsData ? 'Yes, check my app' : 'Check my app', startEvery);
1346
+ yes.setAttribute('data-action', 'everything');
1347
+ yes.disabled = busy;
1348
+ row.appendChild(yes);
1349
+ row.appendChild(button('sm', null, 'Cancel', function () { everyOpen = false; renderEvery(); }));
1350
+ wrap.appendChild(row);
1351
+ box.appendChild(wrap);
1352
+ }
1290
1353
 
1291
1354
  /* ----------------------------------- load ----------------------------------- */
1292
1355
 
1293
1356
  function renderTop() {
1294
1357
  var run = byId('runAll');
1295
- if (!busy) { clear(run); run.appendChild(icon('play', 15)); run.appendChild(el('span', null, 'Run checks')); }
1358
+ if (!busy) { clear(run); run.appendChild(icon('play', 15)); run.appendChild(el('span', null, 'Check my app')); }
1359
+ renderEvery();
1296
1360
  }
1297
1361
 
1298
1362
  /* ------------------------------ your app ------------------------------ */
@@ -313,6 +313,11 @@
313
313
  .consent pre.warn { color: var(--orange-deep); background: rgba(255, 77, 0, 0.06); border-left: 2px solid var(--orange); }
314
314
  .consent .tick { display: flex; gap: 8px; align-items: flex-start; font-size: 14px; line-height: 1.45; }
315
315
  .consent .tick input { margin-top: 3px; }
316
+ .every { margin-top: 20px; padding: 18px 20px; background: var(--white); border-left: 3px solid var(--orange); }
317
+ .every-steps { margin: 0; padding-left: 22px; display: grid; gap: 4px; font-size: 14px; }
318
+ .every-why, .every-out li { color: var(--muted); }
319
+ .every-out { font-size: 13px; line-height: 1.5; }
320
+ .every-out ul { margin: 6px 0 0; padding-left: 18px; display: grid; gap: 3px; }
316
321
  .act { display: inline-flex; flex-direction: column; align-items: flex-start; gap: 4px; }
317
322
  .act .btn, .step .act .btn { margin-top: 0; }
318
323
  .step .act { margin-top: 14px; }
@@ -368,6 +373,8 @@
368
373
  <button class="btn sun" id="runAll" type="button" data-action="run"></button>
369
374
  </div>
370
375
 
376
+ <section class="every" id="everyBox" hidden></section>
377
+
371
378
  <section class="job" id="job" hidden aria-live="polite"></section>
372
379
 
373
380
  <section class="view" id="view-dashboard">
@@ -57,7 +57,7 @@ const CHECKS = [
57
57
  { id: 'payments', label: 'Payments: a webhook sent twice', available: true,
58
58
  what: 'Real payments in Razorpay\'s or Stripe\'s TEST mode, through a proxy on this computer, each checked as it comes until you press Stop. Its signed webhook is sent to your app again, then once with a wrong signature, and your database is read before and after each - reporting the plan, credits or order given a second time, a repeat answered with an error, or a forged webhook accepted. Live mode is never sent again. It runs only when you press its button.' },
59
59
  { id: 'api', label: 'API: every route, tried on your running app', available: true,
60
- what: 'Takes every route the backend map read and tries it on your app on this computer: each account aimed at another organization\'s records, every route with no sign-in, and bad values in fields the code never checks - judged from what comes back and from your own lists before and after, and said as a verdict on each thing the map pointed at. It adds test data and never deletes it, so it runs only in a terminal, on a test copy, with your yes (npx kryptheon api).' },
60
+ what: 'Takes every route the backend map read and tries it on your app on this computer: each account aimed at another organization\'s records, every route with no sign-in, and bad values in fields the code never checks - judged from what comes back and from your own lists before and after, and said as a verdict on each thing the map pointed at. It adds test data and never deletes it, so it runs only on a test copy, with your yes: from Check my app at the top of this page, or in a terminal (npx kryptheon api).' },
61
61
  { id: 'backend', label: 'Backend map: routes, guards, tables', available: true,
62
62
  what: 'Reads your server code - Express, Supabase Edge Functions, Next.js API routes - and lists every route with its sign-in, role and webhook-signature checks, the tables it reads and writes and whether those rows are limited to the signed-in organisation, and the checks on each field it is sent. Nothing is run or sent. What it points at is to check, never proven: npx kryptheon roles proves it on the running app (npx kryptheon map).' },
63
63
  { id: 'synthetic', label: 'Synthetic test users', available: false },
@@ -277,6 +277,71 @@ const PROBE_CHECKS = ['edge', 'reliability', 'roles'];
277
277
  // The roles check is loaded when it is first needed.
278
278
  const rolesModule = () => require('./kryptheon-roles.js');
279
279
 
280
+ // "Check my app": one press on the page runs every check that can run from
281
+ // here, one after another - asked for by the owner: a person who builds with
282
+ // an AI will not copy a command for the API check into a terminal. In this
283
+ // order: the code is read first, so the API step tries the routes that read found.
284
+ const EVERYTHING_ORDER = ['frontend', 'backend', 'regression', 'api', 'edge', 'reliability', 'roles'];
285
+ const ADDS_DATA = new Set(['api', 'edge', 'reliability', 'roles']);
286
+ const EVERYTHING_NAME = { frontend: 'Frontend code', backend: 'Backend map', regression: 'Recorded flows', api: 'API on your running app', edge: 'Input and edge cases',
287
+ reliability: 'Reliability', roles: 'Roles and tenants', payments: 'Payments', data: 'Database' };
288
+
289
+ /** The address's origin when it is this computer (localhost, 127.0.0.1, [::1]); else null. */
290
+ function localOrigin(address) {
291
+ try {
292
+ const u = new URL(String(address || ''));
293
+ return /^https?:$/.test(u.protocol) && /^(localhost|127\.0\.0\.1|\[::1\])$/i.test(u.hostname) ? u.origin : null;
294
+ } catch (err) {
295
+ return null;
296
+ }
297
+ }
298
+
299
+ /**
300
+ * What one press of "Check my app" runs, and what it leaves out with the
301
+ * reason, from the same checks the page shows. The API step goes to the saved
302
+ * address when it is this computer, else to the first address on this
303
+ * computer the recordings open. A step marked live opens an address that is
304
+ * not this computer: it runs only with the person's tick.
305
+ */
306
+ function everythingPlan(checks, app, addresses) {
307
+ const by = Object.fromEntries(checks.map((c) => [c.id, c]));
308
+ const steps = [];
309
+ const skipped = [];
310
+ const skip = (id, why) => skipped.push({ id: id, label: EVERYTHING_NAME[id], why: why });
311
+ const origin = localOrigin(app.address) || (addresses || []).map(localOrigin).find(Boolean) || null;
312
+ let live = [];
313
+ for (const id of EVERYTHING_ORDER) {
314
+ const c = by[id];
315
+ if (!c || !c.available) continue;
316
+ if (!c.enabled) { skip(id, 'switched off in Checks'); continue; }
317
+ const step = { id: id, label: EVERYTHING_NAME[id], title: RUN_TITLE[id] || c.label, addsData: ADDS_DATA.has(id), live: false };
318
+ if ((id === 'frontend' || id === 'backend' || id === 'api') && app.webOnly) { skip(id, 'your app\'s code is not on this computer - open the dashboard from your app\'s folder'); continue; }
319
+ if (id === 'regression' && !c.runnable) { skip(id, 'no recordings yet - record one of your flows first'); continue; }
320
+ if (id === 'api') {
321
+ if (!origin) { skip(id, 'it runs only on your app on this computer - start it (for example npm run dev) and save its address, like http://localhost:3000, in Your app'); continue; }
322
+ step.origin = origin;
323
+ }
324
+ if (id === 'edge' || id === 'reliability' || id === 'roles') {
325
+ if (!c.asks) { skip(id, 'no recordings yet - record one of your flows first'); continue; }
326
+ if (id === 'roles' && c.policy && !c.policy.ok) { skip(id, c.policy.lines[0]); continue; }
327
+ if (id === 'roles' && c.asks.live.length) { skip(id, 'it runs only against your app on this computer - ' + c.asks.live.join(', ') + ' is not'); continue; }
328
+ if (c.asks.live.length) { step.live = true; live = c.asks.live; }
329
+ }
330
+ steps.push(step);
331
+ }
332
+ if (by.payments && by.payments.available) skip('payments', 'it waits for you to make test payments, so it has its own button in Checks');
333
+ if (by.data && by.data.available) skip('data', 'it needs your database connection string, so it has its own box in Start here');
334
+ const addsData = steps.some((s) => s.addsData);
335
+ const where = Array.from(new Set([origin].concat(addresses || []).filter(Boolean)));
336
+ const lines = ['One press runs these, one after another: ' + steps.map((s) => s.label).join(', ') + '.'];
337
+ if (addsData) {
338
+ lines.push(steps.filter((s) => s.addsData).map((s) => s.label).join(', ') + ' add test data to your app' + (origin && steps.some((s) => s.id === 'api') ? ' (the API step signs in as the accounts whose passwords are in your .env)' : '') + '. Nothing is ever deleted - the test data stays.');
339
+ lines.push('Where: ' + (where.length ? where.join(', ') : 'your app on this computer') + '. Run it on your own test copy, never on the live app real people use.');
340
+ }
341
+ lines.push('It takes a few minutes. Stop ends it part way; what finished is kept.');
342
+ return { steps: steps, skipped: skipped, addsData: addsData, live: live, origin: origin, lines: lines };
343
+ }
344
+
280
345
  // Loaded when it is first needed, so a page that never shows payments never
281
346
  // reads it (and pg is only ever loaded by a run).
282
347
  function paymentsRun() {
@@ -930,6 +995,8 @@ function buildState(root, env) {
930
995
  canChooseFolder: process.platform === 'win32' || process.platform === 'darwin',
931
996
  },
932
997
  next: nextAction(findings, steps),
998
+ // What "Check my app" runs with one press, and what it leaves out.
999
+ everything: everythingPlan(checks, { address: readAppAddress(paths), webOnly: webOnly }, recordings ? probeAsk(root, '', env).addresses : []),
933
1000
  baselines: Object.keys(baselines).length,
934
1001
  changes: recentChanges(root),
935
1002
  // Part 3 is where a version gets verified. Until then this says so,
@@ -948,9 +1015,10 @@ function buildState(root, env) {
948
1015
  // a terminal - shown under every button, for when the page cannot be used.
949
1016
  const RUN_TITLE = { frontend: 'Reading your code', backend: 'Mapping your server code', regression: 'Replaying your recordings', edge: 'Trying unusual inputs in your forms', reliability: 'Replaying your flows three times, and double-clicking',
950
1017
  roles: 'Trying every recorded action as each account',
1018
+ api: 'Trying every route on your running app',
951
1019
  payments: 'Waiting for test payments, and sending each webhook again' };
952
1020
  const RUN_COMMAND = { frontend: 'npx kryptheon code', backend: 'npx kryptheon map', regression: 'npx kryptheon check', edge: 'npx kryptheon edge', reliability: 'npx kryptheon reliability', roles: 'npx kryptheon roles',
953
- api: 'npx kryptheon api --run --origin http://localhost:3000 --yes',
1021
+ api: 'npx kryptheon api --run --origin http://localhost:3000 --roles --yes',
954
1022
  payments: 'npx kryptheon payments' };
955
1023
 
956
1024
  /** The line a backend-map item gets once the API run has tried that route: "On the running app: held." */
@@ -1311,6 +1379,23 @@ function createServer(root, options) {
1311
1379
  }
1312
1380
  return begin('recheck', 'Re-checking: ' + open.headline, 'npx kryptheon recheck ' + id, (say) => opts.recheck(id, say));
1313
1381
  }
1382
+ // "Check my app": every step the plan holds, one job. The plan is read
1383
+ // again here, never taken from the page; a step that opens an address
1384
+ // that is not this computer runs only with the person's tick.
1385
+ if (url.pathname === '/api/everything' && typeof opts.everything === 'function') {
1386
+ const plan = buildState(root, env).everything;
1387
+ if (!plan.steps.length) return json(res, 400, { ok: false, why: 'nothing can run from here yet' + (plan.skipped[0] ? ' - ' + plan.skipped[0].label + ': ' + plan.skipped[0].why : '') });
1388
+ if (plan.addsData && input.consent !== true) return json(res, 400, { ok: false, why: 'it adds test data to your app, so it runs only after you say yes' });
1389
+ const ticked = input.liveConfirmed === true && plan.live.length > 0;
1390
+ const steps = plan.steps.filter((s) => !s.live || ticked);
1391
+ const skipped = plan.steps.filter((s) => s.live && !ticked)
1392
+ .map((s) => ({ id: s.id, label: s.label, why: plan.live.join(', ') + ' is not this computer - tick that it is your own test copy to include this' }))
1393
+ .concat(plan.skipped);
1394
+ if (!steps.length) return json(res, 400, { ok: false, why: skipped[0].why });
1395
+ const command = steps.map((s) => (s.id === 'api' ? RUN_COMMAND.api.replace('http://localhost:3000', s.origin) : RUN_COMMAND[s.id])).join(', then ');
1396
+ return begin('everything', 'Checking your app', command,
1397
+ (say, control) => opts.everything(steps, say, control, { origin: plan.origin, confirmed: ticked ? plan.live : [], skipped: skipped }));
1398
+ }
1314
1399
  if (url.pathname === '/api/run' && typeof opts.run === 'function') {
1315
1400
  const id = String(input.id || '');
1316
1401
  const check = buildState(root, env).checks.find((c) => c.id === id);
@@ -1451,6 +1536,8 @@ function createServer(root, options) {
1451
1536
 
1452
1537
  module.exports = {
1453
1538
  CHECKS: CHECKS,
1539
+ everythingPlan: everythingPlan,
1540
+ localOrigin: localOrigin,
1454
1541
  buildState: buildState,
1455
1542
  clearedBy: clearedBy,
1456
1543
  localTime: localTime,
package/kryptheon-map.js CHANGED
@@ -209,6 +209,16 @@ function makeContext(root, parser) {
209
209
  }
210
210
 
211
211
  /** ['req', 'session', 'user'] for req.session.user; null when any part is computed and not a fixed text. */
212
+ /** The function a name of this scope is bound to (function name() {} or const name = () => {}), or null. */
213
+ function localFunction(scope, name) {
214
+ const b = scope.getBinding(name);
215
+ const n = b && b.path && b.path.node;
216
+ if (!n) return null;
217
+ if (n.type === 'FunctionDeclaration') return n;
218
+ if (n.type === 'VariableDeclarator' && n.init && /Function/.test(n.init.type)) return n.init;
219
+ return null;
220
+ }
221
+
212
222
  function memberChain(node) {
213
223
  const out = [];
214
224
  let n = node;
@@ -216,6 +226,8 @@ function memberChain(node) {
216
226
  if (n.type === 'MemberExpression' || n.type === 'OptionalMemberExpression') {
217
227
  if (!n.computed && n.property.type === 'Identifier') out.unshift(n.property.name);
218
228
  else if (n.computed && n.property.type === 'StringLiteral') out.unshift(n.property.value);
229
+ // rows[0]: one item of the list, read by the same names (round 28: p.rows[0].id is the id of p's row)
230
+ else if (n.computed && n.property.type === 'NumericLiteral') { /* skipped */ }
219
231
  else return null;
220
232
  n = n.object;
221
233
  } else if (n.type === 'Identifier') {
@@ -1551,6 +1563,17 @@ function sourceReader(ctx, fnRef, env) {
1551
1563
  }
1552
1564
  case 'CallExpression': case 'OptionalCallExpression': {
1553
1565
  const callee = memberChain(n.callee) || [];
1566
+ // hashToken(token): a one-argument helper of the file that runs no query hands its argument on - the hash of the
1567
+ // caller's own session cookie is still the caller's (round 28 logout)
1568
+ if (n.callee.type === 'Identifier' && n.arguments.length === 1) {
1569
+ const helper = localFunction(fn.scope, n.callee.name);
1570
+ const text = helper ? mod.source.slice(helper.start, helper.end) : '';
1571
+ // only when the argument tells something: scopedOrg(req) is decided by what it returns, below
1572
+ // a database call, not a hash's .update(token)
1573
+ const readsDb = /\bquery\s*\(|\.from\s*\(|\bfind\w*\s*\(|\bfetch\s*\(|\bsql`|\$queryRaw/.test(text);
1574
+ const passed = helper && helper.params.length === 1 && !readsDb ? of(n.arguments[0], depth + 1) : 'unknown';
1575
+ if (passed !== 'unknown') return passed;
1576
+ }
1554
1577
  // a Fetch-style request: await req.json(), req.formData(), req.headers.get(...)
1555
1578
  if (reqName && callee[0] === reqName && /^(json|formData|text|arrayBuffer|blob)$/.test(callee[callee.length - 1] || '')) return 'client';
1556
1579
  // body.get('x'), url.searchParams.get('x'): a method of something the request sent
@@ -1601,6 +1624,11 @@ function sourceReader(ctx, fnRef, env) {
1601
1624
  const awaited = init.type === 'AwaitExpression' ? init.argument : init;
1602
1625
  if (s === 'unknown' && isSessionLookup(awaited)) s = 'identity';
1603
1626
  if (s === 'unknown' && /query|rows|select|find|maybeSingle|single\(/i.test(mod.source.slice(init.start, init.end))) s = 'record';
1627
+ // const ev = await getOrgEvent(id, org): a helper of the file that runs a query returns a row (round 28)
1628
+ if (s === 'unknown' && awaited && awaited.type === 'CallExpression' && awaited.callee.type === 'Identifier') {
1629
+ const fnNode = localFunction(q.scope, awaited.callee.name);
1630
+ if (fnNode && /\b(query|select|find\w*)\s*\(/i.test(mod.source.slice(fnNode.start, fnNode.end))) s = 'record';
1631
+ }
1604
1632
  for (const name of patternNames(id)) if (!locals.has(name)) locals.set(name, s);
1605
1633
  },
1606
1634
  // for (const s of pending): each item is a row of the list it walks
@@ -1732,6 +1760,10 @@ function readDb(ctx, fnRef, schema, opts = {}) {
1732
1760
  });
1733
1761
  const inner = readDb(ctx, r, schema, { depth: depth + 1, paramSources, reqName: null });
1734
1762
  for (const op of inner) out.push(Object.assign({}, op, { via: callee.node.name + '() at ' + mod.rel + ':' + line }));
1763
+ // const ev = await getOrgEvent(id, org): the row the helper returns was found the way its query found it
1764
+ const holder = q.findParent((x) => x.isVariableDeclarator() || x.isFunction());
1765
+ const found = inner.find((x) => x.op === 'SELECT' && !x.joinedTo);
1766
+ if (found && holder && holder.isVariableDeclarator() && t.isIdentifier(holder.node.id)) recordScopes.set(holder.node.id.name, { table: found.table, status: found.scoping.status, line, file: mod.rel });
1735
1767
  }
1736
1768
  };
1737
1769
  fnRef.path.traverse({
@@ -779,7 +779,9 @@ async function checkAccess(input) {
779
779
  }
780
780
  const items = await itemsOf(orgKey, listing);
781
781
  if (!items) return null;
782
- const field = await fieldFor(action, listing);
782
+ // a record named by its code: the field of that name, or "code"; else the field the recorded value is found in
783
+ const has = (f) => items.some((it) => it && typeof it === 'object' && it[f] !== undefined && it[f] !== null);
784
+ const field = (action.field && [action.field, 'code'].find(has)) || await fieldFor(action, listing);
783
785
  const out2 = items.filter((it) => it && it[field] !== undefined).map((it) => ({ value: it[field], item: it, listing: listing, field: field }));
784
786
  return out2.length ? out2 : null;
785
787
  };
@@ -912,8 +914,10 @@ function bodyRecords(action, body, getTemplates) {
912
914
  for (const key of Object.keys(body || {})) {
913
915
  // a list of ids names records too: work_order_ids = [7] (round 27: a dispatch run took another organization's)
914
916
  const many = Array.isArray(body[key]) && body[key].length > 0 && body[key].every(one);
915
- if (!/^[A-Za-z]+(?:_[A-Za-z]+)*_?[iI]ds?$/.test(key) || /^ids?$/i.test(key) || !(one(body[key]) || many)) continue;
916
- const words = key.replace(/_?[iI]ds?$/, '').replace(/([a-z])([A-Z])/g, '$1_$2').toLowerCase().split('_').filter(Boolean);
917
+ // and a code names one by its code: ticket_code (round 28: a door scan let in another organization's ticket)
918
+ const coded = /^[A-Za-z]+(?:_[A-Za-z]+)*_?[cC]ode$/.test(key) && one(body[key]);
919
+ if (!coded && (!/^[A-Za-z]+(?:_[A-Za-z]+)*_?[iI]ds?$/.test(key) || /^ids?$/i.test(key) || !(one(body[key]) || many))) continue;
920
+ const words = key.replace(/_?([iI]ds?|[cC]ode)$/, '').replace(/([a-z])([A-Z])/g, '$1_$2').toLowerCase().split('_').filter(Boolean);
917
921
  // the words together first (work_order: /work-orders), then the last one (to_location: /locations)
918
922
  const stems = [singular(words.join('')), singular(words[words.length - 1] || '')];
919
923
  const named = (t, stem) => !t.includes('{id}') && singular(t.split('/').pop().toLowerCase().replace(/[^a-z]/g, '')) === stem;
@@ -921,7 +925,7 @@ function bodyRecords(action, body, getTemplates) {
921
925
  const listing = stem ? Array.from(getTemplates || []).find((t) => named(t, stem)) : null;
922
926
  if (!listing) continue;
923
927
  const segs = listing.split('/').filter(Boolean).concat('{id}');
924
- out.push({ key: key, many: many, like: { template: listing + '/{id}', segs: segs, params: [{ index: segs.length - 1, value: many ? body[key][0] : body[key] }], by: action.by } });
928
+ out.push({ key: key, many: many, like: { template: listing + '/{id}', segs: segs, params: [{ index: segs.length - 1, value: many ? body[key][0] : body[key] }], by: action.by, field: coded ? key : undefined } });
925
929
  }
926
930
  return out;
927
931
  }
@@ -1069,7 +1073,14 @@ async function tryOnce(x) {
1069
1073
  // a write that names another organization's record in its body: our own lists, before and after
1070
1074
  const pointing = p.kind === 'tenant' && !isGet && bodyItem && actor.org && markers.length > 0;
1071
1075
  const ownBefore = pointing ? JSON.stringify(await x.snapshot(actor.org.key)) : '';
1072
- const res = await actor.send(a.method, pathNow, isGet ? {} : (a.encoding === 'form' ? { form: body } : { json: body }));
1076
+ let res = await actor.send(a.method, pathNow, isGet ? {} : (a.encoding === 'form' ? { form: body } : { json: body }));
1077
+ // A change cut to one field that the app cannot take alone: answered 5xx, it is sent once more with every field the
1078
+ // code reads (round 28: PATCH /api/settings saves a key with its value; {key} alone was a 500 for every role, so who
1079
+ // may change the settings was never judged).
1080
+ if (!isGet && res.status >= 500 && a.fullData && Object.keys(a.fullData).some((k) => body[k] === undefined)) {
1081
+ for (const k of Object.keys(a.fullData)) if (body[k] === undefined) body[k] = a.fullData[k];
1082
+ res = await actor.send(a.method, pathNow, a.encoding === 'form' ? { form: body } : { json: body });
1083
+ }
1073
1084
  const reply = authz.replyOf(res);
1074
1085
  const foreign = actor.org && isGet ? foreignRecords(res.json, actor.org.key) : [];
1075
1086
  const ownMarked = actor.org && isGet ? ownRecords(res.json, actor.org.key) : 0;
@@ -1090,7 +1101,11 @@ async function tryOnce(x) {
1090
1101
  const sameAfter = await readSame();
1091
1102
  if (sameAfter !== undefined) after[sameGet] = sameAfter;
1092
1103
  }
1093
- const changes = x.compare(before, after);
1104
+ // Aimed across by one key, the body's other records are the caller's own (event_id = its own event). A list that
1105
+ // shows every organization's rows shows those change when the caller's own order is made: the caller's doing, not
1106
+ // the other organization's (round 28: a revenue report of every event moved for the caller's own event).
1107
+ const ownIds = p.kind === 'tenant' && bodyKey ? Object.keys(body).filter((k) => k !== bodyKey && /_?[iI]d$/.test(k) && ['string', 'number'].includes(typeof body[k])).map((k) => String(body[k])) : [];
1108
+ const changes = x.compare(before, after).filter((l) => !ownIds.some((v) => l.includes(': record ' + v + ' changed - ')));
1094
1109
  const changed = Object.keys(before).length ? changes.length > 0 : null;
1095
1110
 
1096
1111
  const att = Object.assign(base, {
@@ -164,6 +164,9 @@ function readSigning(parser, root, route, fields) {
164
164
  const kindsOf = () => Array.from(literals).concat(Array.from(listed).filter((t) => !literals.has(t)));
165
165
  const destructured = [];
166
166
  const helpers = [];
167
+ // the helper call being read now: a call found inside it is that call's child, so a secret can be followed back
168
+ // through each of them to the handler (round 28: hmac(secret, raw) -> signBody(secret, body) -> createHmac)
169
+ let reading = null;
167
170
  const readFrom = (fnPath) => fnPath.traverse({
168
171
  CallExpression(p) {
169
172
  const n = p.node;
@@ -183,7 +186,7 @@ function readSigning(parser, root, route, fields) {
183
186
  if (n.callee && n.callee.type === 'Identifier') {
184
187
  // handed the body, or the body's bytes: sign(raw, key) where raw = JSON.stringify(req.body || {})
185
188
  const at = n.arguments.findIndex((a) => isBody(a) || isPayload(resolve(a, 3, p)));
186
- if (at >= 0) helpers.push({ name: n.callee.name, at, scope: p.scope, callPath: p });
189
+ if (at >= 0) helpers.push({ name: n.callee.name, at, scope: p.scope, callPath: p, parent: reading });
187
190
  }
188
191
  // ['payment.captured', 'refund.processed'].includes(payload.type), or TYPES.includes(event.type): every kind in
189
192
  // the list (round 27)
@@ -239,12 +242,15 @@ function readSigning(parser, root, route, fields) {
239
242
  if (!param || param.type !== 'Identifier') continue;
240
243
  bodyNames.add(param.name);
241
244
  scopePath = fnPath;
245
+ call.fnPath = fnPath;
246
+ reading = call;
242
247
  readFrom(fnPath);
248
+ reading = null;
243
249
  if (hmac) {
244
250
  via = call.name;
245
251
  // the helper's own parameters are this call's arguments: hmac(secret, body) called as
246
252
  // hmac(process.env.PAYMENT_WEBHOOK_SECRET || '', raw) signs with that secret (round 27)
247
- hmac.via = { fnPath, callPath: call.callPath };
253
+ hmac.via = call;
248
254
  call.read = true;
249
255
  break;
250
256
  }
@@ -282,10 +288,12 @@ function readSigning(parser, root, route, fields) {
282
288
  if (!isPayload(pay)) return { ok: false, why: 'what is signed is more than the body (a timestamp or a prefix in front, say), which this check does not make' };
283
289
  // the secret
284
290
  let key = resolve(hmac.key, 3, hmac.keyScope);
285
- if (hmac.via && key && key.type === 'Identifier') {
286
- const at = hmac.via.fnPath.node.params.findIndex((q) => q.type === 'Identifier' && q.name === key.name);
287
- const arg = at >= 0 ? hmac.via.callPath.node.arguments[at] : null;
288
- if (arg) key = resolve(arg, 3, hmac.via.callPath);
291
+ // a parameter of the helper is the argument of its call, and that may be a parameter of the helper around it
292
+ for (let step = hmac.via; step && key && key.type === 'Identifier'; step = step.parent) {
293
+ const at = step.fnPath.node.params.findIndex((q) => q.type === 'Identifier' && q.name === key.name);
294
+ const arg = at >= 0 ? step.callPath.node.arguments[at] : null;
295
+ if (!arg) break;
296
+ key = resolve(arg, 3, step.callPath);
289
297
  }
290
298
  let secret = null;
291
299
  const envName = (n) => { const c = n && chainOf(n); return c && c[0] === 'process' && c[1] === 'env' && c[2] ? c[2] : null; };
@@ -526,7 +534,13 @@ async function run(root, options) {
526
534
  // 1. a correctly signed event first: if the app refuses its own, nothing after it means anything.
527
535
  // Open (no signature checked at all): the first event goes with none, and is itself the finding if it is taken.
528
536
  const S = open ? 'none' : 'signed';
529
- const ctypes = types.length ? types : [NEUTRAL];
537
+ // With an amount to try and at most one record left that a capture could still land on, the control is a kind that
538
+ // neither captures nor refunds, so the wrong amount has somewhere to go (round 28: one payment per organization,
539
+ // and a captured control left none). With more records the order stays (round 26 refuses payment.created).
540
+ const CAPTURE = /captur|charge|paid|success/i;
541
+ const capturable = records.filter((r) => r.id !== null && (!statusKey || !(CAPTURE.test(String(r[statusKey])) || REFUNDED.test(String(r[statusKey])))));
542
+ const plain = shape.amount && amountKey && capturable.length <= 1 ? types.find((t) => !REFUNDED.test(t) && !CAPTURE.test(t)) : null;
543
+ const ctypes = types.length ? (plain ? [plain].concat(types.filter((t) => t !== plain)) : types) : [NEUTRAL];
530
544
  say('Sending ' + (open ? 'an unsigned' : 'a correctly signed') + ' ' + ctypes[0] + ' to ' + name + ' first...');
531
545
  const first = nextRecord(unpaidFor(ctypes[0])) || nextRecord();
532
546
  if (!first) { hook.notTried = 'no record left to aim an event at'; continue; }
@@ -534,9 +548,13 @@ async function run(root, options) {
534
548
  // a refusal can be the record's state, not the signature (round 26: "captured" sent to a failed payment is 409):
535
549
  // the same type at the other records, then the next type, three sends at most - a refusal changes nothing
536
550
  const refusals = [control];
537
- for (const type of ctypes) {
551
+ // a plain control refused (round 26 refuses payment.created for the record's state): the usual order then, with the
552
+ // usual three sends besides it
553
+ const order = plain && !control.took ? types : ctypes;
554
+ const budget = plain && !control.took ? 4 : 3;
555
+ for (const type of order) {
538
556
  for (const rec of records) {
539
- if (control.took || refusals.length >= 3) break;
557
+ if (control.took || refusals.length >= budget) break;
540
558
  if (refusals.some((t) => t.type === type && t.record === rec.id) || (statusKey && REFUNDED.test(String(rec[statusKey])))) continue;
541
559
  control = await attempt(S, type, rec, undefined, open ? 'unsigned' : 'signed');
542
560
  refusals.push(control);
@@ -569,7 +587,6 @@ async function run(root, options) {
569
587
  // (round 26: the next record was a failed payment, which refuses any capture for its state, not its amount)
570
588
  // a capture-kind event, even when the control was another kind (round 27: payment.created took the control); at the
571
589
  // control's record first, then the others, until one takes a capture at all - a refusal for its state says nothing
572
- const CAPTURE = /captur|charge|paid|success/i;
573
590
  const captureType = shape.amount && amountKey ? (!REFUNDED.test(control.type) && CAPTURE.test(control.type) ? control.type : signing.types.find((t) => !REFUNDED.test(t) && CAPTURE.test(t))) : null;
574
591
  const payControl = !!captureType;
575
592
  if (captureType) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kryptheon",
3
- "version": "0.1.61",
3
+ "version": "0.1.63",
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",