kryptheon 0.1.62 → 0.1.64

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;
@@ -1103,8 +1114,9 @@ async function runJobs(root, options) {
1103
1114
 
1104
1115
  // --- a create sent twice at once ------------------------------------------------
1105
1116
 
1106
- // Records a second copy of costs something: money, a delivery, someone's time.
1107
- 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;
1108
1120
 
1109
1121
  /**
1110
1122
  * Each create route for a costly record (POST /api/work-orders, /api/payments) that the app also lists at the same
@@ -1139,7 +1151,7 @@ async function runCreates(root, options) {
1139
1151
  return { ran: false, why: 'the app must be on this computer (localhost or 127.0.0.1): ' + (opts.origin || 'no address was given') };
1140
1152
  }
1141
1153
  if (!j.signIn) return { ran: false, why: j.signInWhy };
1142
- 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 });
1143
1155
  const sessions = [];
1144
1156
  for (const a of j.accounts.usable) {
1145
1157
  const s = authz.makeSession(ctx, 'account', a.email, a.password);
@@ -1151,82 +1163,233 @@ async function runCreates(root, options) {
1151
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);
1152
1164
  const taken = (res) => { const r = authz.replyOf(res); return r.kind === 'accepted' || r.kind === 'redirect'; };
1153
1165
  const out = { ran: true, tried: [], findings: [], left: [] };
1154
- for (const a of routes) {
1155
- const what = a.method + ' ' + a.template;
1156
- const t = { what, verdict: 'no account was let in' };
1157
- out.tried.push(t);
1158
- const minimal = {};
1159
- for (const f of a.fields || []) if (f.kinds.length) minimal[f.name] = f.sample;
1160
- const bodies = [a.fullData || {}].concat(Object.keys(minimal).length && JSON.stringify(minimal) !== JSON.stringify(a.fullData || {}) ? [minimal] : []);
1161
- for (const s of sessions) {
1162
- let at = a.template;
1163
- let parentId = null;
1164
- if (a.params.length) {
1165
- const parents = recordsIn(await s.send('GET', parentListOf(a), {}));
1166
- const parent = parents && parents.find((x) => x && x.id !== undefined);
1167
- if (!parent) { t.verdict = 'no record at GET ' + parentListOf(a) + ' to aim it at'; continue; }
1168
- parentId = parent.id;
1169
- at = a.template.replace('{id}', encodeURIComponent(String(parent.id)));
1170
- }
1171
- // its own list, or - when the record it makes lives elsewhere - every list the account reads
1172
- const watch = a.params.length ? Array.from(lists) : [a.template];
1173
- const readAll = async () => {
1174
- const o = {};
1175
- for (const l of watch) { const r = recordsIn(await s.send('GET', l, {})); if (r) o[l] = r; }
1176
- return o;
1177
- };
1178
- const beforeAll = await readAll();
1179
- if (!Object.keys(beforeAll).length) continue;
1180
- let done = null;
1181
- let answered = [];
1182
- for (const body of bodies) {
1183
- const send = () => s.send(a.method, at, a.encoding === 'form' ? { form: body } : { json: body });
1184
- const [r1, r2] = await Promise.all([send(), send()]);
1185
- answered = [r1.status, r2.status];
1186
- if (!taken(r1) && !taken(r2)) continue;
1187
- 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
+ }
1188
1238
  break;
1189
1239
  }
1190
- // refused both times: the next account may be let in
1191
- if (!done) { t.verdict = 'refused (' + answered.join(' and ') + ')'; continue; }
1192
- // the list that gained the most records is where the creates went
1193
- const afterAll = await readAll();
1194
- let made = [];
1195
- let madeIn = watch[0];
1196
- for (const l of Object.keys(afterAll)) {
1197
- const had = new Set((beforeAll[l] || []).map((x) => x && JSON.stringify(x.id)));
1198
- const fresh = afterAll[l].filter((x) => x && x.id !== undefined && !had.has(JSON.stringify(x.id)));
1199
- if (fresh.length > made.length) { made = fresh; madeIn = l; }
1200
- }
1201
- const before = beforeAll[madeIn] || [];
1202
- const after = afterAll[madeIn] || [];
1203
- t.by = s.email;
1204
- t.answers = [done.r1.status, done.r2.status];
1205
- t.made = made.map((x) => x.id);
1206
- 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)');
1207
- // the same fields the body set, on both records
1208
- const keys = Object.keys(done.body).filter((k) => made.length && made.every((x) => valueIn(x, k) !== undefined))
1209
- // and the field that points at the parent the address named (order_id = 1 on both payment orders)
1210
- .concat(parentId === null || !made.length ? [] : Object.keys(made[0]).filter((k) => k !== 'id' && String(made[0][k]) === String(parentId)));
1211
- const alike = made.length >= 2 && keys.every((k) => JSON.stringify(valueIn(made[0], k)) === JSON.stringify(valueIn(made[1], k)));
1212
- t.verdict = alike ? 'made twice' : made.length === 1 ? 'held: one record' : made.length ? 'made ' + made.length + ' different records' : 'nothing new in the list';
1213
- if (t.verdict === 'made twice') {
1214
- const finding = {
1215
- kind: 'create-twice', where: what, from: a.from.file + ':' + a.from.line, status: 'verification required', severity: 'MEDIUM',
1216
- 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.',
1217
- 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(', ') + '.'],
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;
1218
1347
  };
1219
- finding.fixPrompt = ['Kryptheon sent the same create to my running app twice at once, as a double click does:', '', finding.headline, '', 'Where: ' + finding.from, '',
1220
- '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));
1221
- 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
+ }
1222
1384
  }
1223
- break;
1224
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' });
1225
1389
  }
1226
1390
  return out;
1227
1391
  }
1228
1392
 
1229
-
1230
1393
  /**
1231
1394
  * The map's "a status set with no look at the status now" items, tried: the
1232
1395
  * step sent for one record, then sent again for the same record. Judged by
@@ -1347,6 +1510,8 @@ function downFinding(email, what, f, pr, res) {
1347
1510
 
1348
1511
  // Names of fields that are plainly required, so a blank one is worth trying even where the code checks nothing.
1349
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)$/;
1350
1515
  const NOT_PLAIN_TEXT = /^(email|mail|date|day|at|on|url|link|website|phone|mobile|role|status|state|currency|country|id|key|token)$/;
1351
1516
 
1352
1517
  /**
@@ -1400,6 +1565,10 @@ function probesOf(a) {
1400
1565
  } else if (typeof f.sample === 'number' && w[w.length - 1] !== 'id' && (w.some((x) => NUMBER_WORDS.has(x)) || f.kinds.includes('range'))) {
1401
1566
  out.push({ field: f, mode: 'below', value: -1, why: 'a negative number (-1)' });
1402
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
+ }
1403
1572
  }
1404
1573
  return out;
1405
1574
  }
@@ -1645,6 +1814,7 @@ async function runApi(root, options) {
1645
1814
  const exportsRun = await csv.run(root, shared);
1646
1815
  const keptRun = await runKept(root, shared);
1647
1816
  const createsRun = await runCreates(root, shared);
1817
+ const limitsRun = await runLimits(root, shared);
1648
1818
  const stepsRun = await runSteps(root, Object.assign({}, shared, { toCheck: toCheck }));
1649
1819
  let roleRun = null;
1650
1820
  if (opts.roles) roleRun = await runRoles(root, shared);
@@ -1652,14 +1822,14 @@ async function runApi(root, options) {
1652
1822
  const invalid = await runInvalid(root, shared);
1653
1823
  const verdicts = verdictsFor(toCheck, wall, invalid, roleRun, forged, webhooks, stepsRun);
1654
1824
  const seen = new Set();
1655
- 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) => {
1656
1826
  const k = f.kind + '|' + f.where + '|' + (f.field || '');
1657
1827
  return seen.has(k) ? false : (seen.add(k), true);
1658
1828
  });
1659
- 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) : []);
1660
1830
  return {
1661
1831
  ran: wall.ran || invalid.ran, why: wall.ran || invalid.ran ? null : wall.why, checkedAt: new Date().toISOString(),
1662
- 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,
1663
1833
  notTried: (wall.plan ? wall.plan.notTried : []).concat(invalid.ran ? invalid.notTried : []),
1664
1834
  };
1665
1835
  }
@@ -1764,6 +1934,14 @@ function apiLines(run) {
1764
1934
  }
1765
1935
  out.push('');
1766
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
+ }
1767
1945
  if (run.steps && run.steps.ran) {
1768
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 + ')' : ''));
1769
1947
  for (const f of run.steps.findings) {
@@ -1803,4 +1981,4 @@ function apiLines(run) {
1803
1981
  return out;
1804
1982
  }
1805
1983
 
1806
- 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 };
@@ -103,6 +103,7 @@
103
103
  function setBusy(on) {
104
104
  busy = on;
105
105
  Array.prototype.forEach.call(document.querySelectorAll('[data-action]'), function (b) { b.disabled = on; });
106
+ if (state) renderLaunch();
106
107
  }
107
108
  // The clipboard, and when a browser refuses it, the older way: the text
108
109
  // put in a box off the screen, selected and copied.
@@ -1152,7 +1153,7 @@
1152
1153
  var box = byId('runList');
1153
1154
  clear(box);
1154
1155
  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; }
1156
+ 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
1157
  state.runs.slice(0, 30).forEach(function (r) {
1157
1158
  var row = el('div', 'line-row');
1158
1159
  var skipped = r.skipped || [];
@@ -1281,18 +1282,133 @@
1281
1282
  }).catch(function (err) { input.checked = !on; toast('Not saved: ' + err.message); });
1282
1283
  }
1283
1284
 
1284
- 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'));
1289
- });
1285
+ // "Check my app": one press, every check that can run from here. What it
1286
+ // runs, what it leaves out and why, and - when it adds test data - one yes
1287
+ // for all of it, shown before anything starts. The tick is kept across the
1288
+ // page's own reloads while the box is open.
1289
+ var everyOpen = false;
1290
+ var everyTicked = false;
1291
+ function openEvery() {
1292
+ if (!state || !state.everything) return;
1293
+ var plan = state.everything;
1294
+ if (!plan.steps.length) { toast('Nothing can run from here yet' + (plan.skipped[0] ? ' - ' + plan.skipped[0].label + ': ' + plan.skipped[0].why : '')); return; }
1295
+ everyOpen = true;
1296
+ everyTicked = false;
1297
+ renderEvery();
1298
+ byId('everyBox').scrollIntoView({ block: 'start', behavior: 'smooth' });
1299
+ }
1300
+ byId('runAll').addEventListener('click', openEvery);
1301
+
1302
+ // The same press, made to be seen first on the dashboard: what one press
1303
+ // runs, as chips, and what it leaves out, dimmed.
1304
+ function renderLaunch() {
1305
+ var box = byId('launch');
1306
+ if (!box || !state || !state.everything) return;
1307
+ var plan = state.everything;
1308
+ clear(box);
1309
+ var text = el('div');
1310
+ var eyebrow = el('div', 'eyebrow');
1311
+ eyebrow.appendChild(el('span', 'live-dot'));
1312
+ eyebrow.appendChild(document.createTextNode('One press - every check'));
1313
+ text.appendChild(eyebrow);
1314
+ var h = el('h2');
1315
+ h.appendChild(document.createTextNode('Check my '));
1316
+ h.appendChild(el('em', null, 'whole app'));
1317
+ text.appendChild(h);
1318
+ text.appendChild(el('p', null, plan.steps.length
1319
+ ? plural(plan.steps.length, 'check') + ' run one after another while you wait. You say yes once; Kryptheon never deletes anything, and every problem comes with its proof and a prompt to fix it.'
1320
+ : 'Nothing can run from here yet' + (plan.skipped[0] ? ': ' + plan.skipped[0].why : '') + '.'));
1321
+ var chips = el('ul', 'launch-steps');
1322
+ plan.steps.forEach(function (s, i) {
1323
+ var li = el('li');
1324
+ li.appendChild(el('b', null, (i < 9 ? '0' : '') + (i + 1)));
1325
+ li.appendChild(document.createTextNode(s.label));
1326
+ chips.appendChild(li);
1327
+ });
1328
+ plan.skipped.forEach(function (s) {
1329
+ var li = el('li', 'out', s.label);
1330
+ li.title = s.why;
1331
+ chips.appendChild(li);
1332
+ });
1333
+ text.appendChild(chips);
1334
+ box.appendChild(text);
1335
+ var go = el('div', 'launch-go');
1336
+ var b = el('button', 'btn go');
1337
+ b.type = 'button';
1338
+ b.setAttribute('data-action', 'everything');
1339
+ b.appendChild(icon('play', 18));
1340
+ b.appendChild(el('span', null, busy ? 'Checking...' : 'Check my app'));
1341
+ var arrow = icon('arrow', 18);
1342
+ arrow.setAttribute('class', 'go-arrow');
1343
+ b.appendChild(arrow);
1344
+ b.disabled = busy || !plan.steps.length;
1345
+ b.addEventListener('click', openEvery);
1346
+ go.appendChild(b);
1347
+ go.appendChild(el('div', 'launch-note', busy ? 'Running - follow it below' : plan.addsData ? 'Asks before adding test data' : 'Reads only'));
1348
+ box.appendChild(go);
1349
+ }
1350
+ function startEvery() {
1351
+ var plan = state.everything;
1352
+ everyOpen = false;
1353
+ renderEvery();
1354
+ runJob('/api/everything', { consent: true, liveConfirmed: everyTicked }).then(function (done) {
1355
+ var r = (done && done.result) || {};
1356
+ toast(r.ok ? r.summary : 'It did not finish' + (r.why ? ' - ' + r.why : ''));
1357
+ if (r.ok && plan) location.hash = '#findings';
1358
+ }, function () {});
1359
+ }
1360
+ function renderEvery() {
1361
+ var box = byId('everyBox');
1362
+ if (!box) return;
1363
+ clear(box);
1364
+ var plan = state && state.everything;
1365
+ box.hidden = !everyOpen || !plan || !plan.steps.length;
1366
+ if (box.hidden) return;
1367
+ var wrap = el('div', 'consent');
1368
+ wrap.appendChild(el('div', 'd-step', plan.addsData ? 'CHECK MY APP - THIS ADDS TEST DATA TO YOUR APP' : 'CHECK MY APP'));
1369
+ var list = el('ol', 'every-steps');
1370
+ plan.steps.forEach(function (s) {
1371
+ var li = el('li', null, s.label);
1372
+ if (s.live) li.appendChild(el('span', 'every-why', ' - only with the tick below'));
1373
+ list.appendChild(li);
1374
+ });
1375
+ wrap.appendChild(list);
1376
+ wrap.appendChild(linesBox(plan.live.length ? 'warn' : 'help', plan.lines));
1377
+ if (plan.skipped.length) {
1378
+ var out = el('div', 'every-out');
1379
+ out.appendChild(el('b', null, 'Not in this run'));
1380
+ var ul = el('ul');
1381
+ plan.skipped.forEach(function (s) { ul.appendChild(el('li', null, s.label + ': ' + s.why)); });
1382
+ out.appendChild(ul);
1383
+ wrap.appendChild(out);
1384
+ }
1385
+ if (plan.live.length) {
1386
+ var label = el('label', 'tick');
1387
+ var tick = el('input');
1388
+ tick.type = 'checkbox';
1389
+ tick.checked = everyTicked;
1390
+ tick.addEventListener('change', function () { everyTicked = tick.checked; });
1391
+ label.appendChild(tick);
1392
+ 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)'));
1393
+ wrap.appendChild(label);
1394
+ }
1395
+ var row = el('div', 'd-actions');
1396
+ var yes = button('sun sm', 'check', plan.addsData ? 'Yes, check my app' : 'Check my app', startEvery);
1397
+ yes.setAttribute('data-action', 'everything');
1398
+ yes.disabled = busy;
1399
+ row.appendChild(yes);
1400
+ row.appendChild(button('sm', null, 'Cancel', function () { everyOpen = false; renderEvery(); }));
1401
+ wrap.appendChild(row);
1402
+ box.appendChild(wrap);
1403
+ }
1290
1404
 
1291
1405
  /* ----------------------------------- load ----------------------------------- */
1292
1406
 
1293
1407
  function renderTop() {
1294
1408
  var run = byId('runAll');
1295
- if (!busy) { clear(run); run.appendChild(icon('play', 15)); run.appendChild(el('span', null, 'Run checks')); }
1409
+ if (!busy) { clear(run); run.appendChild(icon('play', 15)); run.appendChild(el('span', null, 'Check my app')); }
1410
+ renderEvery();
1411
+ renderLaunch();
1296
1412
  }
1297
1413
 
1298
1414
  /* ------------------------------ your app ------------------------------ */
@@ -313,6 +313,33 @@
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; }
321
+ /* --- Check my app: the one press, made to be seen -------------------- */
322
+ .launch { position: relative; overflow: hidden; display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: center; gap: 28px 40px; margin-bottom: 28px; padding: 38px 36px 40px; color: var(--on-dark); background: radial-gradient(90% 160% at 100% 0%, rgba(255, 77, 0, 0.34), rgba(255, 77, 0, 0) 60%), var(--ink); border-left: 3px solid var(--orange); }
323
+ .launch::after { content: ""; position: absolute; left: 0; top: 0; width: 34%; height: 2px; background: linear-gradient(90deg, rgba(255, 77, 0, 0), var(--orange), rgba(255, 77, 0, 0)); animation: sweep 3.6s var(--ease) infinite; }
324
+ @keyframes sweep { from { transform: translateX(-100%); } to { transform: translateX(300%); } }
325
+ .launch .eyebrow { display: flex; align-items: center; gap: 10px; font-size: 10px; color: var(--orange); }
326
+ .launch h2 { margin-top: 12px; color: var(--white); font-size: clamp(2.4rem, 5.4vw, 4.2rem); }
327
+ .launch h2 em { font-style: normal; color: var(--orange); }
328
+ .launch p { max-width: 60ch; margin: 14px 0 0; font-size: 15px; line-height: 1.6; color: rgba(233, 237, 242, 0.72); }
329
+ .launch-steps { display: flex; flex-wrap: wrap; gap: 6px; margin: 18px 0 0; padding: 0; list-style: none; }
330
+ .launch-steps li { padding: 6px 10px; font: 10px/1.2 var(--mono); letter-spacing: 0.06em; text-transform: uppercase; color: var(--on-dark); border: 1px solid var(--line-dark); background: rgba(233, 237, 242, 0.04); }
331
+ .launch-steps li b { margin-right: 6px; color: var(--orange); font-weight: 400; }
332
+ .launch-steps li.out { color: rgba(233, 237, 242, 0.4); border-style: dashed; }
333
+ .launch-go { display: grid; justify-items: center; gap: 10px; }
334
+ .btn.go { height: 68px; padding: 0 34px; gap: 12px; font-size: 13px; letter-spacing: 0.1em; color: var(--white); background: var(--orange); border-color: var(--orange); animation: ring 2.4s var(--ease) infinite; }
335
+ .btn.go:hover:not(:disabled) { color: var(--white); background: var(--orange-deep); border-color: var(--orange-deep); }
336
+ .btn.go .go-arrow { transition: transform 0.3s var(--ease); }
337
+ .btn.go:hover:not(:disabled) .go-arrow { transform: translateX(5px); }
338
+ .btn.go:disabled, #runAll:disabled { animation: none; }
339
+ .launch-note { font: 10px var(--mono); letter-spacing: 0.06em; color: rgba(233, 237, 242, 0.5); text-transform: uppercase; }
340
+ #runAll { animation: ring 2.4s var(--ease) infinite; }
341
+ @keyframes ring { 0% { box-shadow: 0 0 0 0 rgba(255, 77, 0, 0.55); } 70% { box-shadow: 0 0 0 16px rgba(255, 77, 0, 0); } 100% { box-shadow: 0 0 0 0 rgba(255, 77, 0, 0); } }
342
+ @media (max-width: 760px) { .launch { grid-template-columns: 1fr; padding: 28px 20px 30px; } .launch-go { justify-items: stretch; } .btn.go { width: 100%; } }
316
343
  .act { display: inline-flex; flex-direction: column; align-items: flex-start; gap: 4px; }
317
344
  .act .btn, .step .act .btn { margin-top: 0; }
318
345
  .step .act { margin-top: 14px; }
@@ -368,9 +395,12 @@
368
395
  <button class="btn sun" id="runAll" type="button" data-action="run"></button>
369
396
  </div>
370
397
 
398
+ <section class="every" id="everyBox" hidden></section>
399
+
371
400
  <section class="job" id="job" hidden aria-live="polite"></section>
372
401
 
373
402
  <section class="view" id="view-dashboard">
403
+ <div class="launch" id="launch"></div>
374
404
  <div class="card app-card" id="appCard">
375
405
  <div class="card-head"><div><h3>Your app</h3><div class="c-sub">What this dashboard checks</div></div></div>
376
406
  <div id="appBox"></div>
@@ -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,
@@ -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, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kryptheon",
3
- "version": "0.1.62",
3
+ "version": "0.1.64",
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",