kryptheon 0.1.67 → 0.1.69

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/kryptheon-api.js CHANGED
@@ -26,6 +26,7 @@ const secrets = require('./kryptheon-secrets.js');
26
26
  const authz = require('./kryptheon-authz.js');
27
27
  const webhook = require('./kryptheon-webhook.js');
28
28
  const csv = require('./kryptheon-csv.js');
29
+ const witnessDb = require('./kryptheon-witness.js');
29
30
 
30
31
  const EMAIL = /[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/g;
31
32
  const SIGN_IN = /(^|\/)(log-?in|sign-?in|session|auth\/(login|signin|token))$/i;
@@ -121,6 +122,27 @@ function accountsOf(root, env) {
121
122
  return { usable, left };
122
123
  }
123
124
 
125
+ const TOKEN_PREFIX = 'KRYPTHEON_TOKEN_';
126
+
127
+ /**
128
+ * The accounts of an app that signs people in with a token, not a password: each KRYPTHEON_TOKEN_<NAME> in .env or
129
+ * the environment is one account, named <name> (KRYPTHEON_TOKEN_NORTHSTAR_OWNER -> northstar-owner). A token is
130
+ * never read from a README or guessed, as a password never is. [{ email: name, password: token, envName, token }].
131
+ */
132
+ function tokenAccountsOf(root, env) {
133
+ const fromFile = secrets.readEnvFile(secrets.envFileFor(root));
134
+ const here = env || process.env;
135
+ const out = new Map();
136
+ for (const from of [fromFile, here]) {
137
+ for (const k of Object.keys(from || {})) {
138
+ if (!k.startsWith(TOKEN_PREFIX) || !secrets.isSet(from[k])) continue;
139
+ const name = k.slice(TOKEN_PREFIX.length).toLowerCase().replace(/_+/g, '-').replace(/^-|-$/g, '');
140
+ if (name) out.set(k, { email: name, password: String(from[k]).trim(), envName: k, from: from === fromFile ? '.env' : 'the environment', token: true });
141
+ }
142
+ }
143
+ return Array.from(out.values());
144
+ }
145
+
124
146
  /** The words of a field name: recipientEmail, weight_kg -> ['recipient', 'email'], ['weight', 'kg']. */
125
147
  function wordsOf(name) {
126
148
  return String(name).replace(/([a-z0-9])([A-Z])/g, '$1 $2').toLowerCase().split(/[^a-z0-9]+/).filter(Boolean);
@@ -161,6 +183,8 @@ function sampleValue(field, known, roleNames) {
161
183
  if (w.includes('role')) return null; // a role is never made up: the field is left out
162
184
  if (w.includes('email') || w.includes('mail')) return 'kx-test@kryptheon.invalid';
163
185
  if (/^(date|day)$/.test(last) || last === 'at' || last === 'on') return new Date().toISOString().slice(0, 10);
186
+ if (/^(start|starts|end|ends|until|expires|expiry|deadline|due)$/.test(last) && w.length > 1) return new Date().toISOString().slice(0, 10);
187
+ if (last === 'currency') return 'USD';
164
188
  if (/^(is|has)$/.test(w[0]) || /^(enabled|active|private|public)$/.test(last)) return true;
165
189
  if (last === 'id' && w.length > 1) return 1;
166
190
  // a list of ids (work_order_ids): one id in a list, never text
@@ -287,7 +311,14 @@ function journeyFromMap(m, root, env, roleNames) {
287
311
  }
288
312
  actions.push(action);
289
313
  }
290
- return { signIn: s.none ? null : s, signInWhy: s.none ? s.why : null, accounts, actions, notTried };
314
+ // No sign-in route, and tokens in .env: the app signs people in with a token (blind round 35)
315
+ const tokens = s.none ? tokenAccountsOf(root, env) : [];
316
+ if (tokens.length) {
317
+ return { signIn: { method: authz.BEARER_SIGN_IN, path: '(a token in each request)', emailField: 'email', passwordField: 'password', extra: {}, encoding: 'json', bearer: true },
318
+ signInWhy: null, accounts: { usable: tokens, left: [] }, actions, notTried };
319
+ }
320
+ const why = s.none ? s.why + '. If your app signs people in with a token instead (Authorization: Bearer), put each account\'s token in .env as ' + TOKEN_PREFIX + '<NAME>' : null;
321
+ return { signIn: s.none ? null : s, signInWhy: why, accounts, actions, notTried };
291
322
  }
292
323
 
293
324
  /**
@@ -331,7 +362,7 @@ function planLines(plan) {
331
362
  if (!plan.ran) return out.concat([' Could not read the code: ' + plan.why, '']);
332
363
  out.push(' A dry run: nothing was sent. This is what the API check would try.');
333
364
  out.push('');
334
- out.push(plan.signIn ? ' Sign in: ' + plan.signIn.method + ' ' + plan.signIn.path + ' (' + plan.signIn.emailField + ', ' + plan.signIn.passwordField + ' - ' + plan.signIn.file + ':' + plan.signIn.line + ')' : ' Sign in: not found - ' + plan.signInWhy);
365
+ out.push(plan.signIn && plan.signIn.bearer ? ' Sign in: with a token in each request (Authorization: Bearer), one per ' + TOKEN_PREFIX + '<NAME> in .env' : plan.signIn ? ' Sign in: ' + plan.signIn.method + ' ' + plan.signIn.path + ' (' + plan.signIn.emailField + ', ' + plan.signIn.passwordField + ' - ' + plan.signIn.file + ':' + plan.signIn.line + ')' : ' Sign in: not found - ' + plan.signInWhy);
335
366
  out.push(' Accounts that can sign in: ' + (plan.accounts.length ? plan.accounts.map((a) => a.email).join(', ') : 'none'));
336
367
  for (const a of plan.accountsLeft) out.push(' not usable: ' + a.email + ' (' + a.from + ') - ' + a.why);
337
368
  out.push(' Permissions: ' + (plan.policy.ok ? roles.POLICY_FILE + ', ' + Object.keys(plan.policy.policy.roles).length + ' roles' + (plan.policy.policy.tenants ? ', organizations kept apart' : '') : (plan.policy.problems || []).join(' ')));
@@ -592,6 +623,42 @@ function writeDraft(root, text) {
592
623
  return { ok: true, file: file };
593
624
  }
594
625
 
626
+ /**
627
+ * A read the policy names no permission for, with no role check in the code, that reads a table the policy DOES
628
+ * keep for some roles only (GET /api/revenue sums the payments table; payments.read is for owner, admin and finance):
629
+ * it needs that table's read permission. Blind round 34: a viewer read the revenue of every event, and the route
630
+ * was "not judged: no permission is named like this address". The most restricted table decides. What is found
631
+ * this way is a thing to check, never proven - the owner did not write this rule, the code implies it.
632
+ * { policy, inferred: Map('GET /path' -> { permission, table }) }.
633
+ */
634
+ function inferPermissions(plan, policy) {
635
+ const inferred = new Map();
636
+ const all = Object.keys(policy.roles || {});
637
+ const holders = (perm) => all.filter((r) => (policy.roles[r] || []).includes(perm) || (policy.roles[r] || []).includes('*'));
638
+ const actions = [];
639
+ for (const a of plan.journey.actions) {
640
+ if (a.method !== 'GET' || a.roleChecked || a.guardRoles) continue;
641
+ const named = roles.permissionFor(a.method, a.template, policy);
642
+ if (named && (named.permission || named.ambiguous)) continue;
643
+ const route = (plan.map.routes || []).find((r) => !r.unmounted && r.method === a.method && normRoute(r.path || '') === a.template);
644
+ const tables = Array.from(new Set(((route && route.db) || []).filter((d) => d.op === 'SELECT' && d.table).map((d) => String(d.table))));
645
+ let best = null;
646
+ for (const t of tables) {
647
+ const p = roles.permissionFor('GET', '/' + t.replace(/_/g, '-'), policy);
648
+ if (!p || !p.permission) continue;
649
+ const h = holders(p.permission);
650
+ if (!h.length || h.length >= all.length) continue;
651
+ if (!best || h.length < best.holders) best = { permission: p.permission, table: t, holders: h.length };
652
+ }
653
+ if (!best) continue;
654
+ inferred.set(a.method + ' ' + a.template, best);
655
+ actions.push({ method: a.method, path: a.template, permission: best.permission });
656
+ }
657
+ if (!actions.length) return { policy, inferred };
658
+ const extra = Object.assign({}, policy.extra || {}, { actions: (((policy.extra || {}).actions) || []).concat(actions) });
659
+ return { policy: Object.assign({}, policy, { extra }), inferred };
660
+ }
661
+
595
662
  /**
596
663
  * Every mapped action tried as every account against the owner's permissions
597
664
  * file, across organizations, and with no session. Needs the policy file; only
@@ -616,19 +683,313 @@ async function runRoles(root, options) {
616
683
  if (!j.signIn) return no(j.signInWhy);
617
684
  if (j.accounts.usable.length < 2) return no('fewer than two accounts have a password in .env, so there is nothing to compare');
618
685
  say('Signing in as ' + j.accounts.usable.length + ' accounts, and trying ' + j.actions.length + ' routes as each, across organizations and without signing in...');
686
+ const withInferred = inferPermissions(plan, plan.policy.policy);
619
687
  const result = await roles.checkAccess({
620
- origin: origin, policy: plan.policy.policy, journey: { signIn: j.signIn, actions: j.actions }, accounts: j.accounts.usable,
688
+ origin: origin, policy: withInferred.policy, journey: { signIn: j.signIn, actions: j.actions }, accounts: j.accounts.usable,
621
689
  fetch: opts.fetch, say: say, stopped: opts.stopped, maxAttempts: opts.maxAttempts, anonymous: true, seed: true, tryUnruled: true,
622
690
  });
623
691
  roles.markHints(result.attempts);
624
- const findings = roles.findingsOf(result.attempts.filter((a) => a.kind !== 'signin'), plan.policy.policy).concat(findingsOf(result.attempts.filter((a) => a.kind === 'signin'), j));
692
+ const findings = roles.findingsOf(result.attempts.filter((a) => a.kind !== 'signin'), withInferred.policy).concat(findingsOf(result.attempts.filter((a) => a.kind === 'signin'), j));
625
693
  for (const f of findings) {
626
694
  const a = j.actions.find((x) => x.method + ' ' + x.template === f.where);
627
695
  if (!f.from && a) f.from = a.from.file + ':' + a.from.line;
696
+ // a permission the code implies and the owner did not write: said, and never more than a thing to check
697
+ const inf = withInferred.inferred.get(f.where);
698
+ if (inf && f.kind === 'role') {
699
+ f.status = 'verification required';
700
+ f.inferred = inf;
701
+ f.headline = String(f.headline).replace(/\.?\s*$/, '') + ' - no permission is named for this route; ' + inf.permission + ' is read from the code, which reads the ' + inf.table + ' table here. To check: should every signed-in role see this?';
702
+ }
628
703
  }
629
704
  return { ran: true, plan: plan, result: result, findings: findings, policy: plan.policy.policy };
630
705
  }
631
706
 
707
+ // --- the records a request names, against the database's own rows -------------------
708
+
709
+ const ORG_COLUMN = /^(org|organi[sz]ation|tenant|workspace|team|account|company)_id$/i;
710
+ const PERSON_COLUMN = /^(user|owner|author|creator|assignee|actor|created_by|updated_by|assigned_to|beneficiary)(_id)?$/i;
711
+ const MAX_REF_TRIES = 14;
712
+
713
+ /**
714
+ * A write that names two or more records (the id in its address, ids in its body), tried two ways with rows read
715
+ * from the app's own database (kryptheon-witness.js - so only where its connection string is in .env, on this
716
+ * computer):
717
+ * another organization's record - one named record swapped for a row of ANOTHER organization, the rest the
718
+ * account's own (blind round 34: a Brightline event given a Fieldwork user as staff; a seat of another
719
+ * organization held);
720
+ * records that do not belong together - two named records of the SAME organization whose rows point at different
721
+ * parents (a promo of event 2 redeemed on an order of event 1; a seat of venue 2 held for an event at venue 1).
722
+ * Judged from the database before and after: taken AND a row stored that carries the swapped id. The first is a
723
+ * proven leak across organizations; the second is a thing to check (the app may mean to allow it). Nothing is
724
+ * deleted; every stored row is listed as left. { ran, why, tried, findings, left }.
725
+ */
726
+ async function runRefs(root, options) {
727
+ const opts = options || {};
728
+ const witness = opts.witness && opts.witness.ok ? opts.witness : null;
729
+ if (!witness) return { ran: false, why: 'it reads the rows from the app\'s database, and ' + ((opts.witness && opts.witness.why) || 'no database could be read') };
730
+ const plan = planOf(root, opts);
731
+ if (!plan.ran) return { ran: false, why: plan.why };
732
+ const j = plan.journey;
733
+ let origin;
734
+ try {
735
+ origin = authz.assertLocalTarget(opts.origin);
736
+ } catch (err) {
737
+ return { ran: false, why: 'the app must be on this computer (localhost or 127.0.0.1): ' + (opts.origin || 'no address was given') };
738
+ }
739
+ if (!j.signIn) return { ran: false, why: j.signInWhy };
740
+ const first = await witness.snapshot();
741
+ if (!first) return { ran: false, why: 'the database could not be read' };
742
+ const flat = (w) => String(w).toLowerCase().replace(/[^a-z0-9]/g, '');
743
+ const singular = (w) => w.replace(/ies$/, 'y').replace(/(ss|us)$/, '$1').replace(/([^s])s$/, '$1');
744
+ const tableNames = Object.keys(first.tables).filter((t) => !first.tables[t].big && first.tables[t].rows.size);
745
+ const tableFor = (name) => { const stem = flat(String(name).replace(/_?[iI][dD]$/, '')); return stem ? tableNames.find((t) => flat(singular(t)) === stem || flat(t) === stem) : undefined; };
746
+ const rowsOf = (snap, t) => Array.from(snap.tables[t].rows.values());
747
+ const orgColOf = (t) => { const r = rowsOf(first, t)[0]; return r ? Object.keys(r).find((k) => ORG_COLUMN.test(k)) : undefined; };
748
+ const users = tableNames.find((t) => /^(users|accounts|members|profiles)$/i.test(t) && rowsOf(first, t)[0] && rowsOf(first, t)[0].email !== undefined);
749
+ if (!users || !orgColOf(users)) return { ran: false, why: 'the database has no table of users with an organization column to tell the organizations apart' };
750
+
751
+ // the writes that name two records or more
752
+ const candidates = [];
753
+ for (const a of j.actions) {
754
+ if (!/^(POST|PUT|PATCH)$/.test(a.method) || a.params.length > 1 || JOB_PATH.test(a.template) || SIGN_IN.test(a.template) || SIGN_OUT.test(a.template) || PLUMBING.test(a.template)) continue;
755
+ const words = [].concat(...a.segs.filter((x) => x !== '{id}').map((x) => wordsOf(x)), ...Object.keys(a.fullData || {}).map((k) => wordsOf(k)));
756
+ // never a write about a role or a credential (the roles check tries those, and puts them back)
757
+ if (words.some((w) => /^(role|roles|password|passwd|secret|token|credential|credentials|apikey|otp|mfa)$/.test(w))) continue;
758
+ const refs = [];
759
+ if (a.params.length) {
760
+ const route = (plan.map.routes || []).find((r) => !r.unmounted && r.method === a.method && normRoute(r.path || '') === a.template);
761
+ const named = route && /:(\w+)/.exec(route.path || '');
762
+ const t = (named && !/^id$/i.test(named[1]) && tableFor(named[1])) || tableFor(singular(String(a.segs[a.params[0].index - 1] || '').replace(/-/g, '_')));
763
+ if (t) refs.push({ key: '{id}', table: t, path: true });
764
+ }
765
+ for (const k of Object.keys(a.fullData || {})) {
766
+ const v = a.fullData[k];
767
+ if ((v === null || typeof v !== 'object') && /(_id|Id)$/.test(k) && !ORG_COLUMN.test(k.replace(/Id$/, '_id'))) { const t = tableFor(k); if (t) refs.push({ key: k, table: t }); }
768
+ }
769
+ if (refs.length >= 2 && refs.every((r) => orgColOf(r.table))) candidates.push({ a, refs });
770
+ }
771
+ if (!candidates.length) return { ran: false, why: 'no write names two records the database keeps an organization for' };
772
+
773
+ const ctx = authz.makeContext({ baseUrl: origin, allowOrigins: [origin], fetch: opts.fetch, passwords: Object.fromEntries(j.accounts.usable.map((x) => [x.email, x.password])), maxRequests: opts.maxRequests || 160 });
774
+ const sessions = [];
775
+ for (const acc of j.accounts.usable) {
776
+ const sess = authz.makeSession(ctx, 'account', acc.email, acc.password);
777
+ const lf = { [j.signIn.emailField]: acc.email, [j.signIn.passwordField]: acc.password };
778
+ const r = authz.replyOf(await sess.send(j.signIn.method, j.signIn.path, j.signIn.encoding === 'form' ? { form: lf } : { json: lf }));
779
+ let email = acc.email;
780
+ if (acc.token) {
781
+ const meRoute = (plan.map.routes || []).find((r) => r.method === 'GET' && !r.unmounted && r.path && IDENTITY_ROUTE.test(r.path));
782
+ const me = meRoute ? await sess.send('GET', meRoute.path, {}) : null;
783
+ const found = me && me.status === 200 && JSON.stringify(me.json || {}).match(/"email"\s*:\s*"([^"]+)"/);
784
+ if (found) email = found[1];
785
+ }
786
+ const row = rowsOf(first, users).find((u) => String(u.email).toLowerCase() === String(email).toLowerCase());
787
+ if (r.kind !== 'refused' && r.kind !== 'server-error' && r.kind !== 'no-answer' && row) sessions.push({ sess, email: acc.email, org: row[orgColOf(users)] });
788
+ }
789
+ if (!sessions.length) return { ran: false, why: 'no account with a password in .env could sign in' };
790
+ const taken = (res) => { const r = authz.replyOf(res); return r.kind === 'accepted' || r.kind === 'redirect'; };
791
+ const out = { ran: true, tried: [], findings: [], left: [] };
792
+ const same = (x, y) => String(x) === String(y);
793
+ const pkOf = (t) => ((first.tables[t].pk || [])[0]) || 'id';
794
+ const idOf = (t, row) => row[pkOf(t)];
795
+ // the parents two tables share: a column of both that names a table (venue_id of events and of seats), never the organization or a person
796
+ const sharedParents = (t1, t2) => {
797
+ const k1 = Object.keys(rowsOf(first, t1)[0] || {});
798
+ const k2 = new Set(Object.keys(rowsOf(first, t2)[0] || {}));
799
+ return k1.filter((k) => k2.has(k) && /_id$/.test(k) && !ORG_COLUMN.test(k) && !PERSON_COLUMN.test(k) && tableFor(k));
800
+ };
801
+ let tries = 0;
802
+ const send = async (c, picks, mode, swapped, why) => {
803
+ // as each account of the organization until one is let in; a refusal about who is asking moves on to the next
804
+ for (const who of sessions.filter((x) => same(x.org, picks.org))) {
805
+ if (tries >= MAX_REF_TRIES) return;
806
+ const body = Object.assign({}, c.a.fullData || {});
807
+ let at = c.a.template;
808
+ for (const r of c.refs) { const id = idOf(r.table, picks.rows[r.key]); if (r.path) at = at.replace('{id}', encodeURIComponent(String(id))); else body[r.key] = id; }
809
+ const before = await witness.snapshot();
810
+ tries++;
811
+ const res = await who.sess.send(c.a.method, at, c.a.encoding === 'form' ? { form: body } : { json: body });
812
+ const t = { what: c.a.method + ' ' + c.a.template, mode, by: who.email, status: res.status, swapped: swapped.key, verdict: 'refused (' + res.status + ')' };
813
+ if ([401, 403].includes(res.status)) continue;
814
+ out.tried.push(t);
815
+ if (!taken(res)) return;
816
+ const d = witnessDb.diff(before, await witness.snapshot());
817
+ const sid = idOf(swapped.table, picks.rows[swapped.key]);
818
+ const col = (row) => Object.keys(row).find((k) => flat(k) === flat(singular(swapped.table)) + 'id' && same(row[k], sid));
819
+ let stored = null;
820
+ for (const table of Object.keys(d)) {
821
+ if (HISTORY_TABLE.test(table)) continue;
822
+ for (const row of d[table].added) { const k = col(row); if (k) stored = { table, k, row, how: 'gained a row' }; }
823
+ for (const ch of d[table].changed) if (table === swapped.table && same(ch.key, sid)) stored = { table, k: pkOf(table), row: ch.row, how: 'had row ' + ch.key + ' changed (' + ch.cols.map((x) => x.col).filter((x) => !witnessDb.SECRET_COLUMN.test(x)).slice(0, 4).join(', ') + ')' };
824
+ }
825
+ t.verdict = stored ? 'taken, and stored' : 'taken (' + res.status + '), nothing stored that names it';
826
+ if (!stored) return;
827
+ out.left.push(t.what + ' as ' + who.email + ': the database table ' + stored.table + ' ' + stored.how + ' - Kryptheon never deletes');
828
+ const orgCol = orgColOf(swapped.table);
829
+ const finding = mode === 'foreign' ? {
830
+ kind: 'tenant', where: t.what, from: c.a.from.file + ':' + c.a.from.line, status: 'confirmed', severity: 'HIGH', field: swapped.key,
831
+ headline: 'A user of one organization can attach another organization\'s record: ' + t.what + ' took ' + (swapped.path ? 'the id in its address' : swapped.key) + ' naming a ' + singular(swapped.table).replace(/_/g, ' ') + ' of organization ' + picks.rows[swapped.key][orgCol] + ', and the database stored it.',
832
+ proof: ['As ' + who.email + ' (organization ' + picks.org + '), sent ' + c.a.method + ' ' + at + (Object.keys(body).length ? ' with ' + authz.clip(JSON.stringify(body), 120) : '') + ': answered ' + res.status + '.',
833
+ 'In the database, ' + swapped.table + ' row ' + sid + ' has ' + orgCol + ' ' + picks.rows[swapped.key][orgCol] + ' - not this account\'s organization.',
834
+ 'Read the database before and after: the table ' + stored.table + ' ' + stored.how + (stored.k && stored.how === 'gained a row' ? ' with ' + stored.k + ' ' + sid : '') + '.'],
835
+ fix: 'Look every record the request names up WITH the signed-in person\'s organization (WHERE id = $1 AND ' + orgCol + ' = $2) before anything is saved, and answer 404 when it is not found. Then look for the same on every other route that takes an id from the request.',
836
+ } : {
837
+ kind: 'parent-mismatch', where: t.what, from: c.a.from.file + ':' + c.a.from.line, status: 'verification required', severity: 'HIGH', field: swapped.key,
838
+ headline: t.what + ' took two records that do not belong together: ' + why + ' - and the database stored it. To check: should these have to match?',
839
+ proof: ['As ' + who.email + ', sent ' + c.a.method + ' ' + at + (Object.keys(body).length ? ' with ' + authz.clip(JSON.stringify(body), 120) : '') + ': answered ' + res.status + '.', 'In the database: ' + why + '.',
840
+ 'Read the database before and after: the table ' + stored.table + ' ' + stored.how + '.'],
841
+ fix: 'First decide whether this is allowed. If not: after looking both records up, compare the parent they each point at and answer 400 when they differ - and make the database refuse it too, with a foreign key on both columns together.',
842
+ };
843
+ finding.fixPrompt = ['Kryptheon sent my running app a request naming records that should not go together, and it was stored:', '', finding.headline, '', 'Where: ' + finding.from, '', finding.fix, '', 'Afterwards I will run: npx kryptheon api'].join(String.fromCharCode(10));
844
+ delete finding.fix;
845
+ out.findings.push(finding);
846
+ return;
847
+ }
848
+ };
849
+ try {
850
+ for (const c of candidates) {
851
+ if (tries >= MAX_REF_TRIES) break;
852
+ const snap = await witness.snapshot();
853
+ if (!snap) break;
854
+ const orgs = Array.from(new Set(sessions.map((x) => String(x.org))));
855
+ const org = orgs.find((o) => c.refs.every((r) => rowsOf(snap, r.table).some((row) => same(row[orgColOf(r.table)], o))));
856
+ if (org === undefined) continue;
857
+ const ownRows = (r) => rowsOf(snap, r.table).filter((row) => same(row[orgColOf(r.table)], org));
858
+ const base = {};
859
+ for (const r of c.refs) base[r.key] = ownRows(r)[0];
860
+ // rows that already belong together, where the tables share a parent: the first row sets it, the others follow
861
+ for (const r of c.refs.slice(1)) {
862
+ const sp = sharedParents(c.refs[0].table, r.table);
863
+ const fit = sp.length ? ownRows(r).find((row) => sp.every((k) => row[k] === null || base[c.refs[0].key][k] === null || same(row[k], base[c.refs[0].key][k]))) : null;
864
+ if (fit) base[r.key] = fit;
865
+ }
866
+ // 1. another organization's record, one named record at a time (the body's first)
867
+ for (const r of c.refs.filter((x) => !x.path)) {
868
+ if (out.findings.some((x) => x.kind === 'tenant' && x.where === c.a.method + ' ' + c.a.template)) break;
869
+ const foreign = rowsOf(snap, r.table).find((row) => row[orgColOf(r.table)] !== null && !same(row[orgColOf(r.table)], org));
870
+ if (!foreign) continue;
871
+ await send(c, { org, rows: Object.assign({}, base, { [r.key]: foreign }) }, 'foreign', r, '');
872
+ }
873
+ // 2. two records of this organization whose rows name different parents
874
+ let done = false;
875
+ for (let x = 0; x < c.refs.length && !done; x++) {
876
+ for (let y = x + 1; y < c.refs.length && !done; y++) {
877
+ const [r1, r2] = [c.refs[x], c.refs[y]];
878
+ for (const k of sharedParents(r1.table, r2.table)) {
879
+ const a1 = base[r1.key];
880
+ const other = a1 && a1[k] !== null ? ownRows(r2).find((row) => row[k] !== null && !same(row[k], a1[k])) : null;
881
+ if (!other) continue;
882
+ const why = singular(r1.table).replace(/_/g, ' ') + ' ' + idOf(r1.table, a1) + ' has ' + k + ' ' + a1[k] + ', ' + singular(r2.table).replace(/_/g, ' ') + ' ' + idOf(r2.table, other) + ' has ' + k + ' ' + other[k];
883
+ await send(c, { org, rows: Object.assign({}, base, { [r2.key]: other }) }, 'mismatch', r2, why);
884
+ done = true;
885
+ break;
886
+ }
887
+ }
888
+ }
889
+ }
890
+ } catch (err) {
891
+ if (!authz.isRequestLimit(err)) throw err;
892
+ out.stoppedAt = err.limit;
893
+ }
894
+ return out;
895
+ }
896
+
897
+ // --- who may give which role -----------------------------------------------------
898
+
899
+ const GRANT_KEYS = ['roleGrantRules', 'grantRules', 'roleGrants', 'mayGrant', 'canGrant'];
900
+
901
+ /** The policy file's own rule of who may give which role: { role: [roles it may give] }, or null. */
902
+ function grantRulesOf(root) {
903
+ try {
904
+ const json = JSON.parse(fs.readFileSync(path.join(root, roles.POLICY_FILE), 'utf8').replace(/^\uFEFF/, ''));
905
+ for (const k of GRANT_KEYS) {
906
+ const v = json[k];
907
+ if (!v || typeof v !== 'object' || Array.isArray(v)) continue;
908
+ const out = {};
909
+ for (const r of Object.keys(v)) if (Array.isArray(v[r])) out[r] = v[r].map(String);
910
+ if (Object.keys(out).length) return { key: k, rules: out };
911
+ }
912
+ } catch (err) {
913
+ return null;
914
+ }
915
+ return null;
916
+ }
917
+
918
+ /**
919
+ * A create that carries a role (an invitation, a new member): sent by each account whose own role the policy lets
920
+ * give SOME roles, with the role it may NOT give that holds the most permissions. Taken, with that role in what the
921
+ * app stored = the finding (blind round 34: an admin invited an owner; roleGrantRules gives admin four lower roles).
922
+ * Only creates - a change of an existing person's role is the roles check's, which puts it back. The invitation made
923
+ * is listed as left; it is never accepted and never deleted. { ran, why, tried, findings, left }.
924
+ */
925
+ async function runGrants(root, options) {
926
+ const opts = options || {};
927
+ const plan = planOf(root, opts);
928
+ if (!plan.ran) return { ran: false, why: plan.why };
929
+ const found = grantRulesOf(root);
930
+ if (!found) return { ran: false, why: roles.POLICY_FILE + ' has no rule of who may give which role (roleGrantRules)' };
931
+ const j = plan.journey;
932
+ const roleFieldOf = (a) => (a.fields || []).map((x) => x.name).concat(Object.keys(a.fullData || {})).find((n) => wordsOf(n).includes('role'));
933
+ const routes = j.actions.filter((a) => a.method === 'POST' && !a.params.length && roleFieldOf(a) && !SIGN_IN.test(a.template) && !/(^|\/)(sign-?up|register)$/i.test(a.template));
934
+ if (!routes.length) return { ran: false, why: 'no create route reads a role from the request' };
935
+ let origin;
936
+ try {
937
+ origin = authz.assertLocalTarget(opts.origin);
938
+ } catch (err) {
939
+ return { ran: false, why: 'the app must be on this computer (localhost or 127.0.0.1): ' + (opts.origin || 'no address was given') };
940
+ }
941
+ if (!j.signIn) return { ran: false, why: j.signInWhy };
942
+ const policy = plan.policy && plan.policy.ok ? plan.policy.policy : null;
943
+ const weight = (r) => (policy && policy.roles && policy.roles[r] ? policy.roles[r].length : 0);
944
+ const allRoles = Array.from(new Set(Object.keys(found.rules).concat(...Object.values(found.rules), policy ? Object.keys(policy.roles || {}) : [])));
945
+ 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 || 120 });
946
+ const meRoute = (plan.map.routes || []).find((r) => r.method === 'GET' && !r.unmounted && r.path && /(^|\/)(me|whoami|session|profile|account)$/i.test(r.path));
947
+ const seek = (o, key, depth) => {
948
+ if (!o || typeof o !== 'object' || depth > 3) return undefined;
949
+ if (typeof o[key] === 'string') return o[key];
950
+ for (const k of Object.keys(o)) { const v = seek(o[k], key, depth + 1); if (v !== undefined) return v; }
951
+ return undefined;
952
+ };
953
+ const taken = (res) => { const r = authz.replyOf(res); return r.kind === 'accepted' || r.kind === 'redirect'; };
954
+ const out = { ran: true, tried: [], findings: [], left: [], rule: found.key };
955
+ const done = new Set();
956
+ for (const acc of j.accounts.usable) {
957
+ const sess = authz.makeSession(ctx, 'account', acc.email, acc.password);
958
+ const lf = { [j.signIn.emailField]: acc.email, [j.signIn.passwordField]: acc.password };
959
+ const r0 = authz.replyOf(await sess.send(j.signIn.method, j.signIn.path, j.signIn.encoding === 'form' ? { form: lf } : { json: lf }));
960
+ if (r0.kind === 'refused' || r0.kind === 'server-error' || r0.kind === 'no-answer' || !meRoute) continue;
961
+ const me = await sess.send('GET', meRoute.path, {});
962
+ const own = me.status === 200 ? seek(me.json, 'role', 0) : undefined;
963
+ if (!own || !found.rules[own] || done.has(own)) continue;
964
+ // the role this one may not give that holds the most: never its own level, which the rule may simply not list
965
+ const forbidden = allRoles.filter((x) => x !== own && !found.rules[own].includes(x)).sort((x, y) => weight(y) - weight(x));
966
+ if (!forbidden.length || !found.rules[own].length) continue;
967
+ const target = forbidden[0];
968
+ done.add(own);
969
+ for (const a of routes) {
970
+ const field = roleFieldOf(a);
971
+ const body = Object.assign({}, a.fullData || {}, { [field]: target });
972
+ for (const k of Object.keys(body)) if (/mail/i.test(k)) body[k] = 'kx-grant-' + Date.now().toString(36) + '@kryptheon.invalid';
973
+ const res = await sess.send(a.method, a.template, a.encoding === 'form' ? { form: body } : { json: body });
974
+ const stored = res.json && typeof res.json === 'object' ? seek(res.json, field, 0) || seek(res.json, 'role', 0) : undefined;
975
+ const t = { what: a.method + ' ' + a.template, as: own, by: acc.email, role: target, status: res.status, took: taken(res), stored: stored };
976
+ out.tried.push(t);
977
+ if (!t.took) continue;
978
+ out.left.push(t.what + ' as ' + acc.email + ' with ' + field + ' "' + target + '" (answered ' + res.status + '): the record it made is still there - Kryptheon never deletes');
979
+ const sure = String(stored) === target;
980
+ const finding = {
981
+ kind: 'role-grant', where: t.what, from: a.from.file + ':' + a.from.line, status: sure ? 'confirmed' : 'verification required', severity: 'HIGH',
982
+ headline: 'A' + (/^[aeiou]/i.test(own) ? 'n ' : ' ') + own + ' can give the role ' + target + ' through ' + t.what + ', which ' + found.key + ' in ' + roles.POLICY_FILE + ' does not let ' + own + ' give (only ' + found.rules[own].join(', ') + ').',
983
+ proof: ['As ' + acc.email + ' (' + own + '), sent ' + t.what + ' with ' + field + ' = "' + target + '".', 'The app answered ' + res.status + (sure ? ', and what it stored says ' + field + ' "' + target + '".' : '; the answer does not show the role it stored.')],
984
+ };
985
+ finding.fixPrompt = ['Kryptheon sent my running app a create that carries a role, as an account that may not give that role:', '', finding.headline, '', 'Where: ' + finding.from, '',
986
+ 'Before saving, look up the roles the signed-in person may give (from the same rule the app uses elsewhere, not from the request) and answer 403 when the requested role is not one of them. Do the same on every route that sets a role: invitations, role changes, sign-up with a role.', '', 'Afterwards I will run: npx kryptheon api'].join(String.fromCharCode(10));
987
+ out.findings.push(finding);
988
+ }
989
+ }
990
+ return out;
991
+ }
992
+
632
993
  // --- stage 4: what the app does with a value it should turn away ----------
633
994
 
634
995
  // A value no field of that name should hold, marked so the app's own list can show whether it was kept.
@@ -1368,7 +1729,7 @@ async function runJobs(root, options) {
1368
1729
 
1369
1730
  // Records a second copy of costs something: money, a delivery, someone's time - or lets one ticket in twice (round 28:
1370
1731
  // POST /api/tickets/{id}/scan recorded a second accepted check-in).
1371
- 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;
1732
+ const COSTLY_RECORD = /(^|[\/_-])(orders?|payments?|invoices?|deliver(y|ies)|shipments?|bookings?|reservations?|transfers?|refunds?|payouts?|charges?|subscriptions?|appointments?|scans?|check-?ins?|redeem|redemptions?|claims?|holds?)$/i;
1372
1733
 
1373
1734
  /**
1374
1735
  * Each create route for a costly record (POST /api/work-orders, /api/payments) that the app also lists at the same
@@ -1504,6 +1865,25 @@ async function runCreates(root, options) {
1504
1865
  // use_limit, seat_limit, limit_total: a limit too (round 32: a one-use credit, use_limit 1, redeemed twice at once)
1505
1866
  const LIMIT_FIELD = /^(capacity|max_\w+|limit|\w+_limit|limit_\w+|quota|stock|seats_total|total_seats)$/i;
1506
1867
  const COUNTER_FIELD = /^(sold|uses|used|used_count|booked|taken|reserved|issued|redeemed|count)$/i;
1868
+ // quantity_total and quantity_sold, seats_total and seats_booked: a limit and its count that share their first
1869
+ // word (blind round 34: tickets sold past quantity_total, and no pair was seen)
1870
+ const PAIR_LIMIT = /^(\w+?)_(total|limit|capacity|max)$/i;
1871
+ const PAIR_COUNT = /^(\w+?)_(sold|used|booked|taken|reserved|issued|redeemed|count)$/i;
1872
+ /** Every [limit, counter] pair among a row's field names. */
1873
+ function limitPairsIn(keys) {
1874
+ const out = [];
1875
+ for (const l of keys.filter((k) => LIMIT_FIELD.test(k))) for (const c of keys.filter((k) => COUNTER_FIELD.test(k))) out.push([l, c]);
1876
+ for (const l of keys) {
1877
+ const m = PAIR_LIMIT.exec(l);
1878
+ if (!m) continue;
1879
+ for (const c of keys) {
1880
+ const n = PAIR_COUNT.exec(c);
1881
+ if (n && n[1].toLowerCase() === m[1].toLowerCase() && !out.some((p) => p[0] === l && p[1] === c)) out.push([l, c]);
1882
+ }
1883
+ }
1884
+ return out;
1885
+ }
1886
+ const isCounter = (col) => COUNTER_FIELD.test(col) || PAIR_COUNT.test(col);
1507
1887
  // a field of the create that says how many: what a capacity is used up by
1508
1888
  const AMOUNT_FIELD = /^(quantity|qty|seats|tickets|units|count)$/i;
1509
1889
  const MAX_LIMIT_SENDS = 25;
@@ -1511,13 +1891,10 @@ const MAX_LIMIT_SENDS = 25;
1511
1891
  /** A record's limit pair, or null: { limit, counter, left }. */
1512
1892
  function limitPairOf(rec) {
1513
1893
  if (!rec || typeof rec !== 'object') return null;
1514
- const keys = Object.keys(rec);
1515
- for (const l of keys.filter((k) => LIMIT_FIELD.test(k))) {
1516
- for (const c of keys.filter((k) => COUNTER_FIELD.test(k))) {
1517
- const L = Number(rec[l]);
1518
- const C = Number(rec[c]);
1519
- if (Number.isFinite(L) && Number.isFinite(C) && L > 0 && C >= 0 && C < L && L - C <= 1000) return { limit: l, counter: c, left: L - C };
1520
- }
1894
+ for (const [l, c] of limitPairsIn(Object.keys(rec))) {
1895
+ const L = Number(rec[l]);
1896
+ const C = Number(rec[c]);
1897
+ if (Number.isFinite(L) && Number.isFinite(C) && L > 0 && C >= 0 && C < L && L - C <= 1000) return { limit: l, counter: c, left: L - C };
1521
1898
  }
1522
1899
  return null;
1523
1900
  }
@@ -1846,7 +2223,13 @@ function rowsOfReply(res) {
1846
2223
  /** The code of a write the map found, from its first line to the end of the call (or twelve lines). */
1847
2224
  function sourceAround(root, d) {
1848
2225
  try {
1849
- const lines = fs.readFileSync(path.join(root, d.file), 'utf8').split('\n').slice(Math.max(0, d.line - 1), d.line + 11).join('\n');
2226
+ let lines = fs.readFileSync(path.join(root, d.file), 'utf8').split('\n').slice(Math.max(0, d.line - 1), d.line + 11).join('\n');
2227
+ // code written many statements to a line (blind round 34: a whole route on one line): from this statement's own
2228
+ // UPDATE or INSERT of its table, not from the start of the line - whose first ");" ends long before it
2229
+ if (d.table && /^(UPDATE|INSERT|UPSERT)$/.test(d.op || '')) {
2230
+ const at = lines.search(new RegExp('\\b' + (d.op === 'UPDATE' ? 'UPDATE\\s+' : 'INSERT\\s+INTO\\s+') + '"?' + String(d.table).replace(/[^\w]/g, '') + '\\b', 'i'));
2231
+ if (at > 0) lines = lines.slice(at);
2232
+ }
1850
2233
  const end = lines.search(/\)\s*;/);
1851
2234
  return end < 0 ? lines : lines.slice(0, end + 2);
1852
2235
  } catch (err) {
@@ -1990,16 +2373,49 @@ function pastLimit(before, after) {
1990
2373
  for (const l of Object.keys(after)) {
1991
2374
  for (const row of rowsAt(after, l)) {
1992
2375
  if (!row || typeof row !== 'object') continue;
1993
- const keys = Object.keys(row);
1994
- for (const lim of keys.filter((k) => LIMIT_FIELD.test(k))) {
1995
- for (const c of keys.filter((k) => COUNTER_FIELD.test(k))) {
1996
- const L = Number(row[lim]);
1997
- const C = Number(row[c]);
1998
- if (!(Number.isFinite(L) && Number.isFinite(C) && L > 0 && C > L)) continue;
1999
- const old = row.id === undefined ? null : rowsAt(before, l).find((x) => x && JSON.stringify(x.id) === JSON.stringify(row.id));
2000
- if (old && Number(old[c]) > Number(old[lim])) continue;
2001
- out.push({ kind: 'past-limit', sure: true, text: 'GET ' + l + (row.id !== undefined ? ' record ' + row.id : '') + ': ' + c + ' ' + (old ? old[c] + ' -> ' : '') + C + ' of ' + lim + ' ' + L + ', past its limit' });
2002
- }
2376
+ for (const [lim, c] of limitPairsIn(Object.keys(row))) {
2377
+ const L = Number(row[lim]);
2378
+ const C = Number(row[c]);
2379
+ if (!(Number.isFinite(L) && Number.isFinite(C) && L > 0 && C > L)) continue;
2380
+ const old = row.id === undefined ? null : rowsAt(before, l).find((x) => x && JSON.stringify(x.id) === JSON.stringify(row.id));
2381
+ if (old && Number(old[c]) > Number(old[lim])) continue;
2382
+ out.push({ kind: 'past-limit', sure: true, text: 'GET ' + l + (row.id !== undefined ? ' record ' + row.id : '') + ': ' + c + ' ' + (old ? old[c] + ' -> ' : '') + C + ' of ' + lim + ' ' + L + ', past its limit' });
2383
+ }
2384
+ }
2385
+ }
2386
+ return out;
2387
+ }
2388
+
2389
+ // Tables that take a line per request: two lines for two requests is the table doing its job.
2390
+ const HISTORY_TABLE = /audit|(^|_)logs?($|_)|activit|histor|outbox|notification|timeline|(^|_)jobs?(_runs?)?$|webhook_events?|(^|_)events?_log/i;
2391
+
2392
+ /**
2393
+ * The same double effects, read from the database's own rows (kryptheon-witness.js) - for what no list of the
2394
+ * app shows: two rows of a table no route reads, each tied to the record (blind round 34: two payout_transfers
2395
+ * for one payout, both requests answered 200, every list unchanged), a count the row itself holds past its limit.
2396
+ */
2397
+ function dbDoubleEffects(d, rec, listTemplate, stepTemplate) {
2398
+ const out = [];
2399
+ const flat = (w) => String(w).toLowerCase().replace(/[^a-z0-9]/g, '');
2400
+ const stem = flat((String(listTemplate).split('/').pop() || '').replace(/ies$/, 'y').replace(/s$/, ''));
2401
+ const last = stepTemplate ? String(stepTemplate).split('/').filter((x) => x && x !== '{id}').pop() || '' : '';
2402
+ const addsToList = /s$/i.test(last) && !STEP_VERB.test(last);
2403
+ for (const table of Object.keys(d || {})) {
2404
+ const t = d[table];
2405
+ if (!addsToList && stem && !HISTORY_TABLE.test(table)) {
2406
+ const tied = t.added.filter((row) => Object.keys(row).some((k) => flat(k) === stem + 'id' && String(row[k]) === String(rec.id)));
2407
+ const keys = tied.length ? Object.keys(tied[0]).filter((k) => !ROW_IDENTITY.test(k)) : [];
2408
+ if (tied.length >= 2 && tied.every((x) => keys.every((k) => JSON.stringify(x[k]) === JSON.stringify(tied[0][k])))) {
2409
+ out.push({ kind: 'rows', sure: true, text: 'the database table ' + table + ' gained ' + tied.length + ' rows with the same ' + (keys.filter((k) => !witnessDb.SECRET_COLUMN.test(k)).slice(0, 4).join(', ') || 'fields') + ', each tied to record ' + rec.id });
2410
+ }
2411
+ }
2412
+ for (const ch of t.changed) {
2413
+ for (const [lim, c] of limitPairsIn(Object.keys(ch.row))) {
2414
+ const L = Number(ch.row[lim]);
2415
+ const C = Number(ch.row[c]);
2416
+ if (!(Number.isFinite(L) && Number.isFinite(C) && L > 0 && C > L)) continue;
2417
+ if (Number(ch.old[c]) > Number(ch.old[lim])) continue;
2418
+ out.push({ kind: 'past-limit', sure: true, text: 'the database table ' + table + ' row ' + ch.key + ': ' + c + ' ' + ch.old[c] + ' -> ' + C + ' of ' + lim + ' ' + L + ', past its limit' });
2003
2419
  }
2004
2420
  }
2005
2421
  }
@@ -2016,7 +2432,10 @@ const CLAIM_QTY = /^(quantity|qty|units|seats|count|amount_units)$/i;
2016
2432
  * both together do not. { ids, label } or null when there is no such create, list or field.
2017
2433
  */
2018
2434
  async function scarceClaim(s, c, j, lists, snap, taken) {
2019
- const cols = c.effect.moves.filter((m) => m.sign === '-' && m.col).map((m) => m.col);
2435
+ const down = c.effect.moves.filter((m) => m.sign === '-' && m.col).map((m) => m.col);
2436
+ // a count that goes up to a limit beside it (quantity_sold of quantity_total): what is left is the difference
2437
+ const up = c.effect.moves.filter((m) => m.sign === '+' && m.col && isCounter(m.col) && !down.includes(m.col)).map((m) => m.col);
2438
+ const cols = down.concat(up);
2020
2439
  if (!cols.length) return null;
2021
2440
  const create = j.actions.find((x) => x.method === 'POST' && x.template === c.listTemplate && !x.params.length);
2022
2441
  if (!create || !create.fullData) return null;
@@ -2038,7 +2457,9 @@ async function scarceClaim(s, c, j, lists, snap, taken) {
2038
2457
  }
2039
2458
  const flat = (k) => String(k).replace(/_/g, '').toLowerCase();
2040
2459
  const isPointer = (k) => /(^|_)id$|Id$/.test(k);
2041
- const scarce = rows.filter((r) => Number(r[col]) > 0).sort((x, y) => Number(x[col]) - Number(y[col]))[0];
2460
+ const limitOf = (r) => { const p = limitPairsIn(Object.keys(r)).find((x) => x[1] === col); return p ? p[0] : null; };
2461
+ const leftOf = (r) => (up.includes(col) ? (limitOf(r) ? Number(r[limitOf(r)]) - Number(r[col]) : NaN) : Number(r[col]));
2462
+ const scarce = rows.filter((r) => leftOf(r) > 0).sort((x, y) => leftOf(x) - leftOf(y))[0];
2042
2463
  if (!scarce) continue;
2043
2464
  // What each pointer of the body is set to. Named like a field of the row (productId -> product_id, warehouseId ->
2044
2465
  // warehouse_id: the stock found by product and place), that field's value - every such field, so the new record
@@ -2050,12 +2471,18 @@ async function scarceClaim(s, c, j, lists, snap, taken) {
2050
2471
  const rk = Object.keys(scarce).find((x) => x !== 'id' && flat(x) === flat(l2.key));
2051
2472
  if (rk && scarce[rk] !== null && scarce[rk] !== undefined) sets.push({ path: l2.path, value: scarce[rk] });
2052
2473
  }
2474
+ // the pointer named for the list itself (ticketTypeId for /ticket-types, round 34): the scarce row's own id
2475
+ const listStem = String(l.split('/').pop() || '').toLowerCase().replace(/[^a-z]/g, '').replace(/ies$/, 'y').replace(/s$/, '');
2476
+ const own = leaves.find((l2) => isPointer(l2.key) && flat(l2.key.replace(/_?[iI]d$/, '')) === listStem);
2477
+ if (own && !sets.some((x) => x.path.join('.') === own.path.join('.'))) sets.push({ path: own.path, value: scarce.id });
2053
2478
  if (!sets.length) {
2054
2479
  const byId = leaves.find((l2) => isPointer(l2.key) && ids.has(String(l2.value)));
2055
2480
  if (byId) sets.push({ path: byId.path, value: scarce.id });
2056
2481
  }
2057
2482
  if (!sets.length) continue;
2058
- const units = Number(scarce[col]);
2483
+ const units = leftOf(scarce);
2484
+ // never a claim so large that making it is a load of its own (an app that writes one row per unit)
2485
+ if (!(units <= 2000)) continue;
2059
2486
  const qty = leaves.find((l2) => CLAIM_QTY.test(l2.key));
2060
2487
  if (!qty && units > 1) continue; // one unit per record and more than one left: two records cannot overdraw it
2061
2488
  if (qty) sets.push({ path: qty.path, value: units });
@@ -2079,11 +2506,45 @@ async function scarceClaim(s, c, j, lists, snap, taken) {
2079
2506
  if (id === undefined || id === null) return null;
2080
2507
  made.push(id);
2081
2508
  }
2082
- return { ids: made, label: l.split('/').pop() + ' ' + scarce.id + ' (' + col + ' ' + units + ')' };
2509
+ return { ids: made, label: l.split('/').pop() + ' ' + scarce.id + ' (' + (up.includes(col) ? units + ' left of ' + limitOf(scarce) : col + ' ' + units) + ')' };
2083
2510
  }
2084
2511
  return null;
2085
2512
  }
2086
2513
 
2514
+ /**
2515
+ * A record of the step's list with exactly one use left of its limit (use_limit 1, used_count 0): the one two
2516
+ * requests at the same moment must not both take. One the list already has, or one made through the list's own
2517
+ * create route with its limit field set to 1 (listed as left - never deleted). null when there is no limit pair,
2518
+ * no create route, or the create is refused. Blind round 34: a one-use promo redeemed twice at once.
2519
+ */
2520
+ async function oneLeft(s, c, j, recs, taken, out) {
2521
+ const sample = recs.find((x) => x && typeof x === 'object');
2522
+ if (!sample) return null;
2523
+ const counters = c.effect.moves.filter((m) => m.sign === '+' && m.col && isCounter(m.col)).map((m) => m.col);
2524
+ const pair = limitPairsIn(Object.keys(sample)).find((p) => counters.includes(p[1]));
2525
+ if (!pair) return null;
2526
+ const left = (x) => Number(x[pair[0]]) - Number(x[pair[1]]);
2527
+ const have = recs.find((x) => Number(x[pair[0]]) > 0 && left(x) === 1 && !/Kryptheon test|kx-|=KX/i.test(JSON.stringify(x)));
2528
+ if (have) return have;
2529
+ const create = j.actions.find((x) => x.method === 'POST' && x.template === c.listTemplate && !x.params.length);
2530
+ if (!create || !create.fullData) return null;
2531
+ const flat = (k) => String(k).replace(/_/g, '').toLowerCase();
2532
+ const limKey = Object.keys(create.fullData).find((k) => flat(k) === flat(pair[0]));
2533
+ if (!limKey) return null;
2534
+ const body = JSON.parse(JSON.stringify(create.fullData));
2535
+ body[limKey] = 1;
2536
+ // a code, slug or name of its own: the app may keep these unique
2537
+ const tag = 'KX' + Date.now().toString(36).toUpperCase();
2538
+ for (const k of Object.keys(body)) if (typeof body[k] === 'string' && /^(code|slug|sku|name|title|key)$/i.test(k)) body[k] = /^(name|title)$/i.test(k) ? 'Kryptheon test ' + tag : tag;
2539
+ const had = new Set((rowsOfReply(await s.send('GET', c.listTemplate, {})) || []).map((r) => r && String(r.id)));
2540
+ const res = await s.send('POST', c.listTemplate, create.encoding === 'form' ? { form: body } : { json: body });
2541
+ if (!taken(res)) return null;
2542
+ const made = (rowsOfReply(await s.send('GET', c.listTemplate, {})) || []).find((r) => r && r.id !== undefined && !had.has(String(r.id)));
2543
+ if (!made || left(made) !== 1) return null;
2544
+ out.left.push(c.listTemplate + ' record ' + made.id + ' made by Kryptheon with ' + pair[0] + ' 1, to send its step twice at once - never deleted');
2545
+ return made;
2546
+ }
2547
+
2087
2548
  /**
2088
2549
  * The map's steps on one record (POST|PUT|PATCH /things/{id}/verb that change a status, money or a count),
2089
2550
  * sent twice at the same moment for ONE record, as a double click or two tabs do; and, where the step takes a
@@ -2146,9 +2607,11 @@ async function runStepsAtOnce(root, options) {
2146
2607
  return o;
2147
2608
  };
2148
2609
  const taken = (res) => { const r = authz.replyOf(res); return r.kind === 'accepted' || r.kind === 'redirect'; };
2149
- // What one step costs here: per account a list and a read of every list, two sends and another read; and the same
2150
- // again for two records. Sized to the app, never past 3000 (the limit of the whole run is kept: ctx.limit only goes up).
2151
- if (opts.maxRequests === undefined) ctx.limit = Math.min(3000, Math.max(ctx.limit, ctx.requests + 20 + candidates.length * (sessions.length * (lists.length + 3) + 2 * (2 * lists.length + 4))));
2610
+ const witness = opts.witness && opts.witness.ok ? opts.witness : null;
2611
+ // What one step costs here: per account a list and a read of every list, two sends and another read (up to three
2612
+ // records when one is refused); and the same again for two records. Sized to the app, never past 4000 (the limit
2613
+ // of the whole run is kept: ctx.limit only goes up).
2614
+ if (opts.maxRequests === undefined) ctx.limit = Math.min(4000, Math.max(ctx.limit, ctx.requests + 20 + candidates.length * (sessions.length * (lists.length + 3) + 4 * (2 * lists.length + 6))));
2152
2615
  const out = { ran: true, tried: setAside.slice(), findings: [], left: [] };
2153
2616
  const headFor = (c) => c.a.method + ' ' + c.a.template;
2154
2617
  const finish = (c, what, s, atText, answers, effects, mode, body) => {
@@ -2175,7 +2638,7 @@ async function runStepsAtOnce(root, options) {
2175
2638
  const t = { what, verdict: 'no account was let in' };
2176
2639
  out.tried.push(t);
2177
2640
  const route = c.route;
2178
- const shared = c.effect.moves.some((m) => m.sign === '-' || (m.col && COUNTER_FIELD.test(m.col)));
2641
+ const shared = c.effect.moves.some((m) => m.sign === '-' || (m.col && isCounter(m.col)));
2179
2642
  const sets = (route.db || []).find((d) => d.setsState);
2180
2643
  const target = sets ? sets.setsState.value : null;
2181
2644
  const bodyOf = () => (a.encoding === 'form' ? { form: a.fullData || {} } : { json: a.fullData || {} });
@@ -2186,11 +2649,29 @@ async function runStepsAtOnce(root, options) {
2186
2649
  const usable = (recs || []).filter((x) => x && typeof x === 'object' && x.id !== undefined);
2187
2650
  if (!usable.length) { t.verdict = 'no record at GET ' + c.listTemplate + ' to aim it at'; continue; }
2188
2651
  const statusKey = Object.keys(usable[0]).find((k) => /^(status|state|stage)$/i.test(k));
2189
- const fresh = open(usable, statusKey);
2190
- const rec = fresh[0] || usable[0];
2191
- const at = atOf(rec);
2192
- const before = await snap(s);
2193
- const [r1, r2] = await Promise.all([s.send(a.method, at, bodyOf()), s.send(a.method, at, bodyOf())]);
2652
+ // Records the seed made before ones Kryptheon's other checks made (blind round 34: the first promo in the
2653
+ // list was one made with a bad limit, both redeems were refused 409, and the step was called held), and
2654
+ // for a count that runs up to a limit, a record with exactly one use left - made, when there is none.
2655
+ const toolMade = (x) => /Kryptheon test|kx-|=KX/i.test(JSON.stringify(x));
2656
+ const fresh = open(usable, statusKey).sort((x, y) => Number(toolMade(x)) - Number(toolMade(y)));
2657
+ const last = c.effect.moves.some((m) => m.sign === '+' && m.col && isCounter(m.col)) ? await oneLeft(s, c, j, fresh, taken, out) : null;
2658
+ const pool = (last ? [last] : []).concat(fresh.length ? fresh : usable).slice(0, 3);
2659
+ let rec = pool[0];
2660
+ let at = atOf(rec);
2661
+ let before = null;
2662
+ let dbBefore = null;
2663
+ let r1 = null;
2664
+ let r2 = null;
2665
+ for (const cand of pool) {
2666
+ rec = cand;
2667
+ at = atOf(rec);
2668
+ before = await snap(s);
2669
+ dbBefore = witness ? await witness.snapshot() : null;
2670
+ [r1, r2] = await Promise.all([s.send(a.method, at, bodyOf()), s.send(a.method, at, bodyOf())]);
2671
+ if (taken(r1) || taken(r2)) break;
2672
+ // not this account's to change: the next account may be; any other refusal may be about this record
2673
+ if ([401, 403].includes(r1.status) && [401, 403].includes(r2.status)) break;
2674
+ }
2194
2675
  const answers = [r1.status, r2.status];
2195
2676
  const both = taken(r1) && taken(r2);
2196
2677
  t.by = s.email;
@@ -2203,7 +2684,9 @@ async function runStepsAtOnce(root, options) {
2203
2684
  break;
2204
2685
  }
2205
2686
  const after = await snap(s);
2687
+ const dbAfter = witness && dbBefore ? await witness.snapshot() : null;
2206
2688
  const effects = both ? doubleEffects(before, after, rec, c.listTemplate, a.fullData || {}, a.template) : [];
2689
+ if (both && dbAfter) for (const e of dbDoubleEffects(witnessDb.diff(dbBefore, dbAfter), rec, c.listTemplate, a.template)) if (!effects.some((x) => x.kind === e.kind)) effects.push(e);
2207
2690
  t.verdict = effects.length ? 'done twice' : both ? 'held: both taken, one effect' : 'held: one taken, one refused';
2208
2691
  out.left.push(what.replace('{id}', rec.id) + ' sent twice at the same moment as ' + s.email + ' (answered ' + answers.join(' and ') + ')' + (effects.length ? ': its effect, twice, is still there - Kryptheon does not undo it' : ': ' + (both ? 'taken twice' : 'taken once') + ', Kryptheon does not undo it'));
2209
2692
  if (effects.length) finish(c, what, s, a.method + ' ' + at, answers, effects, 'once', a.fullData);
@@ -2374,6 +2857,23 @@ function probesOf(a) {
2374
2857
  if (typeof f.sample === 'number' && /^(percent|pct|percentage)$/.test(w[w.length - 1] || '')) {
2375
2858
  out.push({ field: f, mode: 'above', value: 150, why: 'above 100 percent (150)' });
2376
2859
  }
2860
+ // a number inside a list of lines (items: [{ ticketTypeId, quantity }]): a negative one in the first line
2861
+ // (blind round 34: an order line with quantity -2 was stored, and only top-level fields were ever tried)
2862
+ if (Array.isArray(f.sample) && f.sample[0] && typeof f.sample[0] === 'object' && !Array.isArray(f.sample[0])) {
2863
+ for (const k of Object.keys(f.sample[0])) {
2864
+ const kw = wordsOf(k);
2865
+ if (typeof f.sample[0][k] !== 'number' || kw[kw.length - 1] === 'id' || !kw.some((x) => NUMBER_WORDS.has(x))) continue;
2866
+ out.push({ field: f, mode: 'below', value: [Object.assign({}, f.sample[0], { [k]: -2 })], why: 'a negative ' + k + ' (-2) in its first line' });
2867
+ }
2868
+ }
2869
+ // an end before its start (startsAt and endsAt, sales_start and sales_end): a day long before the start
2870
+ const m = /^(.*?)(_?)(end|ends|until|expires)(.*)$/i.exec(f.name);
2871
+ if (m && typeof f.sample === 'string' && /^\d{4}-\d\d-\d\d/.test(f.sample)) {
2872
+ const flat = (x) => String(x).replace(/_/g, '').toLowerCase();
2873
+ const start = (a.fields || []).find((g) => g !== f && typeof g.sample === 'string' && /^\d{4}-\d\d-\d\d/.test(g.sample) &&
2874
+ [m[1] + m[2] + 'start' + m[4], m[1] + m[2] + 'starts' + m[4], m[1] + m[2] + 'from' + m[4]].some((n) => flat(n) === flat(g.name)));
2875
+ if (start) out.push({ field: f, mode: 'date-order', value: '2001-01-01', why: 'a day long before ' + start.name + ' (it ends before it starts)' });
2876
+ }
2377
2877
  }
2378
2878
  return out;
2379
2879
  }
@@ -2693,7 +3193,10 @@ async function runApi(root, options) {
2693
3193
  const m = opts.map || map.mapProject(root, { packageDir: opts.packageDir || __dirname });
2694
3194
  if (!m.ran) return { ran: false, why: m.why };
2695
3195
  const toCheck = opts.toCheck || map.toCheck(m);
2696
- const shared = Object.assign({}, opts, { map: m, say: say });
3196
+ // the app's own database beside its lists, where its connection string is in .env and it is on this computer
3197
+ const witness = opts.witness || await witnessDb.open(root, { env: opts.env });
3198
+ say(witness.ok ? ' Reading the database beside the lists (' + witness.from + ' in .env, this computer, read-only).' : ' Judging from the app\'s lists only: ' + witness.why + '.');
3199
+ const shared = Object.assign({}, opts, { map: m, say: say, witness: witness });
2697
3200
  // jobs first, on the app as it was seeded: the tries after them cancel and refund the records a job works on
2698
3201
  // (round 28: the settlement job found no paid order left and made nothing either time)
2699
3202
  // A check that reaches its request limit stops there and says so; the others still run and report.
@@ -2721,18 +3224,21 @@ async function runApi(root, options) {
2721
3224
  const onceRun = await contained('Steps sent twice at once', () => runStepsAtOnce(root, shared));
2722
3225
  let roleRun = null;
2723
3226
  if (opts.roles) roleRun = await contained('By role', () => runRoles(root, shared));
3227
+ const refsRun = await contained('Records a request names', () => runRefs(root, shared));
3228
+ const grantsRun = opts.roles ? await contained('Who may give which role', () => runGrants(root, shared)) : { ran: false, why: 'not asked for' };
2724
3229
  // the bad values always last: one of them may stop the app (round 24 lost its whole roles run that way)
2725
3230
  const invalid = await contained('Bad values', () => runInvalid(root, shared));
3231
+ if (witness.ok && !opts.witness) await witness.close();
2726
3232
  const verdicts = verdictsFor(toCheck, wall, invalid, roleRun, forged, webhooks, stepsRun, redirectsRun);
2727
3233
  const seen = new Set();
2728
- const findings = (wall.ran ? wall.findings : []).concat(redirectsRun.ran ? redirectsRun.findings : []).concat(refundsRun.ran ? refundsRun.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(onceRun.ran ? onceRun.findings : []).concat(roleRun && roleRun.ran ? roleRun.findings : []).concat(invalid.ran ? invalid.findings : []).filter((f) => {
3234
+ const findings = (grantsRun.ran ? grantsRun.findings : []).concat(refsRun.ran ? refsRun.findings : []).concat(wall.ran ? wall.findings : []).concat(redirectsRun.ran ? redirectsRun.findings : []).concat(refundsRun.ran ? refundsRun.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(onceRun.ran ? onceRun.findings : []).concat(roleRun && roleRun.ran ? roleRun.findings : []).concat(invalid.ran ? invalid.findings : []).filter((f) => {
2729
3235
  const k = f.kind + '|' + f.where + '|' + (f.field || '');
2730
3236
  return seen.has(k) ? false : (seen.add(k), true);
2731
3237
  });
2732
- const left = (webhooks.ran ? webhooks.left : []).concat(refundsRun.ran ? refundsRun.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(onceRun.ran ? onceRun.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) : []);
3238
+ const left = (grantsRun.ran ? grantsRun.left : []).concat(refsRun.ran ? refsRun.left : []).concat(webhooks.ran ? webhooks.left : []).concat(refundsRun.ran ? refundsRun.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(onceRun.ran ? onceRun.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) : []);
2733
3239
  return {
2734
3240
  ran: wall.ran || invalid.ran, why: wall.ran || invalid.ran ? null : wall.why, checkedAt: new Date().toISOString(),
2735
- origin: opts.origin, wall: wall, forged: forged, webhooks: webhooks, exports: exportsRun, jobs: jobsRun, kept: keptRun, creates: createsRun, limits: limitsRun, steps: stepsRun, redirects: redirectsRun, refunds: refundsRun, stepsAtOnce: onceRun, invalid: invalid, roles: roleRun, verdicts: verdicts, findings: findings, left: left,
3241
+ origin: opts.origin, grants: grantsRun, refs: refsRun, wall: wall, forged: forged, webhooks: webhooks, exports: exportsRun, jobs: jobsRun, kept: keptRun, creates: createsRun, limits: limitsRun, steps: stepsRun, redirects: redirectsRun, refunds: refundsRun, stepsAtOnce: onceRun, invalid: invalid, roles: roleRun, verdicts: verdicts, findings: findings, left: left,
2736
3242
  notTried: (wall.plan ? wall.plan.notTried : []).concat(invalid.ran ? invalid.notTried : []),
2737
3243
  writes: writesOf(m, wall),
2738
3244
  };
@@ -2766,6 +3272,7 @@ function reportOf(run) {
2766
3272
  const wallTries = run.wall && run.wall.ran ? run.wall.result.attempts.filter((a) => !a.notTried).length : 0;
2767
3273
  return {
2768
3274
  ran: run.ran, why: run.why || null, checkedAt: run.checkedAt, origin: run.origin || null,
3275
+ live: run.live === true, leftOut: run.live === true ? (run.leftOut || []).slice() : [],
2769
3276
  accounts: run.wall && run.wall.ran ? run.wall.result.accounts.map((a) => a.email) : run.invalid && run.invalid.ran ? [run.invalid.account] : [],
2770
3277
  tries: { acrossOrganizationsAndNoSession: wallTries, badValues: run.invalid && run.invalid.ran ? run.invalid.tried.length : 0 },
2771
3278
  findings: run.findings.map((f) => ({ kind: f.kind, severity: f.severity, status: f.status, where: f.where, from: f.from, headline: f.headline, proof: f.proof, fixPrompt: f.fixPrompt })),
@@ -2774,6 +3281,110 @@ function reportOf(run) {
2774
3281
  };
2775
3282
  }
2776
3283
 
3284
+ // --- a live app: read-only ----------------------------------------------------------
3285
+
3286
+ /** Every value in a JSON excerpt replaced: "email":"a@b.c" -> "email":"…", "total":120 -> "total":#. Field names stay. */
3287
+ function maskValues(text) {
3288
+ return String(text == null ? '' : text)
3289
+ .replace(/("[^"\\]{1,80}"\s*:\s*)"(?:[^"\\]|\\.)*"/g, '$1"…"')
3290
+ .replace(/("[^"\\]{1,80}"\s*:\s*)-?\d+(?:\.\d+)?/g, '$1#');
3291
+ }
3292
+
3293
+ // The checks a live run never makes, each with why: all of them write.
3294
+ const LIVE_LEFT_OUT = [
3295
+ 'creates and changes across organizations, and bad values - they write to your app',
3296
+ 'roles for anything that changes data - it would change data',
3297
+ 'webhooks, payments and refunds - they write',
3298
+ 'two requests at the same moment, repeats, jobs and limits - they write',
3299
+ 'exports with a marked value - it is made by a write first',
3300
+ ];
3301
+
3302
+ /**
3303
+ * The API check on a LIVE app (an address that is not this computer): reads only. As each account and with no
3304
+ * session: every GET the map read, aimed across organizations, and - with permissions-policy.json - as the roles
3305
+ * that may not read it. Nothing is created, changed or deleted: the connection itself refuses any request but GET,
3306
+ * HEAD and the one sign-in. The answers' values are never shown or kept, only field names and counts. Run on a
3307
+ * test copy for everything else. Same shape as runApi, so the report and the page read it the same way.
3308
+ */
3309
+ async function runLive(root, options) {
3310
+ const opts = options || {};
3311
+ const say = typeof opts.say === 'function' ? opts.say : () => {};
3312
+ const m = opts.map || map.mapProject(root, { packageDir: opts.packageDir || __dirname });
3313
+ if (!m.ran) return { ran: false, why: m.why };
3314
+ const plan = planOf(root, Object.assign({}, opts, { map: m }));
3315
+ if (!plan.ran) return { ran: false, why: plan.why };
3316
+ const j = plan.journey;
3317
+ let origin;
3318
+ try {
3319
+ origin = authz.assertLocalTarget(opts.origin, null, { readOnly: true });
3320
+ } catch (err) {
3321
+ return { ran: false, why: 'not an address: ' + (opts.origin || 'none was given') };
3322
+ }
3323
+ if (!j.signIn) return { ran: false, why: j.signInWhy };
3324
+ if (j.accounts.usable.length < 2) return { ran: false, why: 'fewer than two test accounts can sign in, so there is nothing to compare - add them in "Who Kryptheon signs in as"' };
3325
+ const gets = j.actions.filter((a) => a.method === 'GET');
3326
+ say('Reading only: ' + gets.length + ' pages and lists of ' + origin + ', as ' + j.accounts.usable.length + ' test accounts and with no session. Nothing is created, changed or deleted.');
3327
+ const base = { origin, fetch: opts.fetch, say, stopped: opts.stopped, maxAttempts: opts.maxAttempts, readOnly: true, seed: false, anonymous: true };
3328
+ let wallResult;
3329
+ try {
3330
+ wallResult = await roles.checkAccess(Object.assign({}, base, { policy: wallPolicy(j), journey: { signIn: j.signIn, actions: gets }, accounts: j.accounts.usable, only: (p) => p.kind !== 'role' }));
3331
+ } catch (err) {
3332
+ if (err && err.code === authz.READ_ONLY) return { ran: false, why: 'stopped: ' + err.message + ' - nothing was written' };
3333
+ throw err;
3334
+ }
3335
+ const wall = { ran: true, plan, result: wallResult, findings: findingsOf(wallResult.attempts, j) };
3336
+ let roleRun = { ran: false, why: 'no ' + roles.POLICY_FILE };
3337
+ if (plan.policy.ok) {
3338
+ const withInferred = inferPermissions(plan, plan.policy.policy);
3339
+ try {
3340
+ const result = await roles.checkAccess(Object.assign({}, base, { policy: withInferred.policy, journey: { signIn: j.signIn, actions: gets }, accounts: j.accounts.usable, tryUnruled: false }));
3341
+ const findings = roles.findingsOf(result.attempts.filter((a) => a.kind !== 'signin'), withInferred.policy);
3342
+ for (const f of findings) { const inf = withInferred.inferred.get(f.where); if (inf && f.kind === 'role') f.status = 'verification required'; }
3343
+ roleRun = { ran: true, plan, result, findings, policy: withInferred.policy };
3344
+ } catch (err) {
3345
+ if (!(err && err.code === authz.READ_ONLY)) throw err;
3346
+ roleRun = { ran: false, why: 'stopped: ' + err.message + ' - nothing was written' };
3347
+ }
3348
+ }
3349
+ const stub = { ran: false, why: 'not on a live app - it writes; run it on a test copy' };
3350
+ // Judged first, then every string kept from the tries masked - the answers, their excerpts, the lines printed from
3351
+ // them: a live app's records are real people's, and none of their values is shown or saved.
3352
+ const maskDeep = (v, depth) => {
3353
+ if (depth > 8 || v === null || v === undefined) return v;
3354
+ if (typeof v === 'string') return maskValues(v);
3355
+ if (Array.isArray(v)) return v.map((x) => maskDeep(x, depth + 1));
3356
+ if (typeof v === 'object') { const o = {}; for (const k of Object.keys(v)) o[k] = /^(json|item|items|body|data|record|records|response|value|values|before|after|answer)$/i.test(k) && v[k] && typeof v[k] === 'object' ? maskObject(v[k], depth + 1) : maskDeep(v[k], depth + 1); return o; }
3357
+ return v;
3358
+ };
3359
+ // a whole record or answer as an object: every leaf value goes, the keys stay
3360
+ const maskObject = (v, depth) => {
3361
+ if (depth > 8 || v === null || typeof v !== 'object') return typeof v === 'string' ? '…' : typeof v === 'number' ? '#' : v;
3362
+ if (Array.isArray(v)) return v.map((x) => maskObject(x, depth + 1));
3363
+ const o = {};
3364
+ for (const k of Object.keys(v)) o[k] = maskObject(v[k], depth + 1);
3365
+ return o;
3366
+ };
3367
+ wall.result = maskDeep(wall.result, 0);
3368
+ wall.findings = maskDeep(wall.findings, 0);
3369
+ if (roleRun.ran) { roleRun.result = maskDeep(roleRun.result, 0); roleRun.findings = maskDeep(roleRun.findings, 0); }
3370
+ // the roles run keeps organizations apart too, so it finds the same reads across organizations again: said once
3371
+ const seenLive = new Set();
3372
+ const findings = (wall.findings || []).concat(roleRun.ran ? roleRun.findings : []).filter((f) => {
3373
+ const k = f.kind + '|' + f.where + '|' + (f.field || '');
3374
+ return seenLive.has(k) ? false : (seenLive.add(k), true);
3375
+ }).map((f) => maskDeep(Object.assign({}, f, { live: true }), 0));
3376
+ const toCheck = opts.toCheck || map.toCheck(m);
3377
+ return {
3378
+ ran: true, live: true, why: null, checkedAt: new Date().toISOString(), origin,
3379
+ wall, forged: stub, webhooks: stub, exports: stub, jobs: stub, kept: stub, creates: stub, limits: stub, steps: stub, redirects: stub, refunds: stub, stepsAtOnce: stub,
3380
+ roles: roleRun, invalid: stub, grants: stub, refs: stub,
3381
+ findings, left: [], leftOut: LIVE_LEFT_OUT,
3382
+ verdicts: verdictsFor(toCheck, wall, stub, roleRun.ran ? roleRun : null, stub, stub, stub, stub),
3383
+ notTried: wall.plan ? wall.plan.notTried : [],
3384
+ writes: writesOf(m, wall),
3385
+ };
3386
+ }
3387
+
2777
3388
  /** Writes api-last.json in the store folder. Never throws. */
2778
3389
  function saveReport(dir, run) {
2779
3390
  try {
@@ -2877,6 +3488,22 @@ function apiLines(run) {
2877
3488
  }
2878
3489
  out.push('');
2879
3490
  }
3491
+ if (run.refs && run.refs.ran) {
3492
+ for (const t of run.refs.tried) out.push(' Records named ' + t.what + ' (' + (t.mode === 'foreign' ? 'another organization\'s ' : 'a mismatched ') + t.swapped + ', as ' + t.by + '): ' + t.verdict);
3493
+ for (const f of run.refs.findings) {
3494
+ out.push(' ' + (f.status === 'confirmed' ? 'PROBLEM' : 'TO CHECK') + ' ' + f.severity + ' ' + f.headline + ' (' + f.from + ')');
3495
+ for (const l of f.proof) out.push(' ' + l);
3496
+ }
3497
+ out.push('');
3498
+ }
3499
+ if (run.grants && run.grants.ran) {
3500
+ for (const t of run.grants.tried) out.push(' Role given ' + t.what + ' as ' + t.as + ' with the role ' + t.role + ': ' + (t.took ? 'taken (' + t.status + ')' : 'refused (' + t.status + ')'));
3501
+ for (const f of run.grants.findings) {
3502
+ out.push(' ' + (f.status === 'confirmed' ? 'PROBLEM' : 'TO CHECK') + ' ' + f.severity + ' ' + f.headline + ' (' + f.from + ')');
3503
+ for (const l of f.proof) out.push(' ' + l);
3504
+ }
3505
+ out.push('');
3506
+ }
2880
3507
  if (run.refunds && run.refunds.ran) {
2881
3508
  for (const t of run.refunds.tried) out.push(' Refund ' + t.what + (t.record !== undefined ? ' (record ' + t.record + ', paid ' + t.paid + ', a refund of ' + t.over + ')' : '') + ': ' + (t.took === undefined ? t.verdict + (t.why ? ' - ' + t.why : '') : t.took ? 'taken (' + t.status + ')' : 'refused (' + t.status + ')'));
2882
3509
  for (const f of run.refunds.findings) {
@@ -2940,4 +3567,4 @@ function apiLines(run) {
2940
3567
  return out;
2941
3568
  }
2942
3569
 
2943
- module.exports = { reconcile, isUpdateRoute, storedAs, updateProbesOf, heldStrength, runSteps, runRedirects, OUTSIDE, runRefundCap, PLUMBING, runStepsAtOnce, doubleEffects, 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 };
3570
+ module.exports = { runLive, maskValues, LIVE_LEFT_OUT, tokenAccountsOf, TOKEN_PREFIX, limitPairsIn, dbDoubleEffects, oneLeft, inferPermissions, grantRulesOf, runGrants, runRefs, probesOf, stepEffectOf, reconcile, isUpdateRoute, storedAs, updateProbesOf, heldStrength, runSteps, runRedirects, OUTSIDE, runRefundCap, PLUMBING, runStepsAtOnce, doubleEffects, 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 };