kryptheon 0.1.67 → 0.1.68

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
@@ -2570,6 +2570,8 @@ async function apiCommand(args) {
2570
2570
  console.log('');
2571
2571
  console.log(' This signs in to ' + origin + ' as the accounts whose passwords are in your .env, and sends requests that must be refused');
2572
2572
  console.log(' (another organization\'s records, no session, values that should be turned away). It adds test data and never deletes it.');
2573
+ console.log(' If your .env holds a Postgres connection string for this computer (DATABASE_URL), it also READS that database,');
2574
+ console.log(' read-only, before and after its tries - to see what your pages do not show. It never reads a database on another computer.');
2573
2575
  console.log(' Run it on a test copy only. To go ahead, add --yes.');
2574
2576
  console.log('');
2575
2577
  return 2;
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;
@@ -161,6 +162,8 @@ function sampleValue(field, known, roleNames) {
161
162
  if (w.includes('role')) return null; // a role is never made up: the field is left out
162
163
  if (w.includes('email') || w.includes('mail')) return 'kx-test@kryptheon.invalid';
163
164
  if (/^(date|day)$/.test(last) || last === 'at' || last === 'on') return new Date().toISOString().slice(0, 10);
165
+ if (/^(start|starts|end|ends|until|expires|expiry|deadline|due)$/.test(last) && w.length > 1) return new Date().toISOString().slice(0, 10);
166
+ if (last === 'currency') return 'USD';
164
167
  if (/^(is|has)$/.test(w[0]) || /^(enabled|active|private|public)$/.test(last)) return true;
165
168
  if (last === 'id' && w.length > 1) return 1;
166
169
  // a list of ids (work_order_ids): one id in a list, never text
@@ -592,6 +595,42 @@ function writeDraft(root, text) {
592
595
  return { ok: true, file: file };
593
596
  }
594
597
 
598
+ /**
599
+ * A read the policy names no permission for, with no role check in the code, that reads a table the policy DOES
600
+ * keep for some roles only (GET /api/revenue sums the payments table; payments.read is for owner, admin and finance):
601
+ * it needs that table's read permission. Blind round 34: a viewer read the revenue of every event, and the route
602
+ * was "not judged: no permission is named like this address". The most restricted table decides. What is found
603
+ * this way is a thing to check, never proven - the owner did not write this rule, the code implies it.
604
+ * { policy, inferred: Map('GET /path' -> { permission, table }) }.
605
+ */
606
+ function inferPermissions(plan, policy) {
607
+ const inferred = new Map();
608
+ const all = Object.keys(policy.roles || {});
609
+ const holders = (perm) => all.filter((r) => (policy.roles[r] || []).includes(perm) || (policy.roles[r] || []).includes('*'));
610
+ const actions = [];
611
+ for (const a of plan.journey.actions) {
612
+ if (a.method !== 'GET' || a.roleChecked || a.guardRoles) continue;
613
+ const named = roles.permissionFor(a.method, a.template, policy);
614
+ if (named && (named.permission || named.ambiguous)) continue;
615
+ const route = (plan.map.routes || []).find((r) => !r.unmounted && r.method === a.method && normRoute(r.path || '') === a.template);
616
+ const tables = Array.from(new Set(((route && route.db) || []).filter((d) => d.op === 'SELECT' && d.table).map((d) => String(d.table))));
617
+ let best = null;
618
+ for (const t of tables) {
619
+ const p = roles.permissionFor('GET', '/' + t.replace(/_/g, '-'), policy);
620
+ if (!p || !p.permission) continue;
621
+ const h = holders(p.permission);
622
+ if (!h.length || h.length >= all.length) continue;
623
+ if (!best || h.length < best.holders) best = { permission: p.permission, table: t, holders: h.length };
624
+ }
625
+ if (!best) continue;
626
+ inferred.set(a.method + ' ' + a.template, best);
627
+ actions.push({ method: a.method, path: a.template, permission: best.permission });
628
+ }
629
+ if (!actions.length) return { policy, inferred };
630
+ const extra = Object.assign({}, policy.extra || {}, { actions: (((policy.extra || {}).actions) || []).concat(actions) });
631
+ return { policy: Object.assign({}, policy, { extra }), inferred };
632
+ }
633
+
595
634
  /**
596
635
  * Every mapped action tried as every account against the owner's permissions
597
636
  * file, across organizations, and with no session. Needs the policy file; only
@@ -616,19 +655,306 @@ async function runRoles(root, options) {
616
655
  if (!j.signIn) return no(j.signInWhy);
617
656
  if (j.accounts.usable.length < 2) return no('fewer than two accounts have a password in .env, so there is nothing to compare');
618
657
  say('Signing in as ' + j.accounts.usable.length + ' accounts, and trying ' + j.actions.length + ' routes as each, across organizations and without signing in...');
658
+ const withInferred = inferPermissions(plan, plan.policy.policy);
619
659
  const result = await roles.checkAccess({
620
- origin: origin, policy: plan.policy.policy, journey: { signIn: j.signIn, actions: j.actions }, accounts: j.accounts.usable,
660
+ origin: origin, policy: withInferred.policy, journey: { signIn: j.signIn, actions: j.actions }, accounts: j.accounts.usable,
621
661
  fetch: opts.fetch, say: say, stopped: opts.stopped, maxAttempts: opts.maxAttempts, anonymous: true, seed: true, tryUnruled: true,
622
662
  });
623
663
  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));
664
+ const findings = roles.findingsOf(result.attempts.filter((a) => a.kind !== 'signin'), withInferred.policy).concat(findingsOf(result.attempts.filter((a) => a.kind === 'signin'), j));
625
665
  for (const f of findings) {
626
666
  const a = j.actions.find((x) => x.method + ' ' + x.template === f.where);
627
667
  if (!f.from && a) f.from = a.from.file + ':' + a.from.line;
668
+ // a permission the code implies and the owner did not write: said, and never more than a thing to check
669
+ const inf = withInferred.inferred.get(f.where);
670
+ if (inf && f.kind === 'role') {
671
+ f.status = 'verification required';
672
+ f.inferred = inf;
673
+ 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?';
674
+ }
628
675
  }
629
676
  return { ran: true, plan: plan, result: result, findings: findings, policy: plan.policy.policy };
630
677
  }
631
678
 
679
+ // --- the records a request names, against the database's own rows -------------------
680
+
681
+ const ORG_COLUMN = /^(org|organi[sz]ation|tenant|workspace|team|account|company)_id$/i;
682
+ const PERSON_COLUMN = /^(user|owner|author|creator|assignee|actor|created_by|updated_by|assigned_to|beneficiary)(_id)?$/i;
683
+ const MAX_REF_TRIES = 14;
684
+
685
+ /**
686
+ * A write that names two or more records (the id in its address, ids in its body), tried two ways with rows read
687
+ * from the app's own database (kryptheon-witness.js - so only where its connection string is in .env, on this
688
+ * computer):
689
+ * another organization's record - one named record swapped for a row of ANOTHER organization, the rest the
690
+ * account's own (blind round 34: a Brightline event given a Fieldwork user as staff; a seat of another
691
+ * organization held);
692
+ * records that do not belong together - two named records of the SAME organization whose rows point at different
693
+ * 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).
694
+ * Judged from the database before and after: taken AND a row stored that carries the swapped id. The first is a
695
+ * proven leak across organizations; the second is a thing to check (the app may mean to allow it). Nothing is
696
+ * deleted; every stored row is listed as left. { ran, why, tried, findings, left }.
697
+ */
698
+ async function runRefs(root, options) {
699
+ const opts = options || {};
700
+ const witness = opts.witness && opts.witness.ok ? opts.witness : null;
701
+ 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') };
702
+ const plan = planOf(root, opts);
703
+ if (!plan.ran) return { ran: false, why: plan.why };
704
+ const j = plan.journey;
705
+ let origin;
706
+ try {
707
+ origin = authz.assertLocalTarget(opts.origin);
708
+ } catch (err) {
709
+ return { ran: false, why: 'the app must be on this computer (localhost or 127.0.0.1): ' + (opts.origin || 'no address was given') };
710
+ }
711
+ if (!j.signIn) return { ran: false, why: j.signInWhy };
712
+ const first = await witness.snapshot();
713
+ if (!first) return { ran: false, why: 'the database could not be read' };
714
+ const flat = (w) => String(w).toLowerCase().replace(/[^a-z0-9]/g, '');
715
+ const singular = (w) => w.replace(/ies$/, 'y').replace(/(ss|us)$/, '$1').replace(/([^s])s$/, '$1');
716
+ const tableNames = Object.keys(first.tables).filter((t) => !first.tables[t].big && first.tables[t].rows.size);
717
+ const tableFor = (name) => { const stem = flat(String(name).replace(/_?[iI][dD]$/, '')); return stem ? tableNames.find((t) => flat(singular(t)) === stem || flat(t) === stem) : undefined; };
718
+ const rowsOf = (snap, t) => Array.from(snap.tables[t].rows.values());
719
+ const orgColOf = (t) => { const r = rowsOf(first, t)[0]; return r ? Object.keys(r).find((k) => ORG_COLUMN.test(k)) : undefined; };
720
+ const users = tableNames.find((t) => /^(users|accounts|members|profiles)$/i.test(t) && rowsOf(first, t)[0] && rowsOf(first, t)[0].email !== undefined);
721
+ if (!users || !orgColOf(users)) return { ran: false, why: 'the database has no table of users with an organization column to tell the organizations apart' };
722
+
723
+ // the writes that name two records or more
724
+ const candidates = [];
725
+ for (const a of j.actions) {
726
+ 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;
727
+ const words = [].concat(...a.segs.filter((x) => x !== '{id}').map((x) => wordsOf(x)), ...Object.keys(a.fullData || {}).map((k) => wordsOf(k)));
728
+ // never a write about a role or a credential (the roles check tries those, and puts them back)
729
+ if (words.some((w) => /^(role|roles|password|passwd|secret|token|credential|credentials|apikey|otp|mfa)$/.test(w))) continue;
730
+ const refs = [];
731
+ if (a.params.length) {
732
+ const route = (plan.map.routes || []).find((r) => !r.unmounted && r.method === a.method && normRoute(r.path || '') === a.template);
733
+ const named = route && /:(\w+)/.exec(route.path || '');
734
+ const t = (named && !/^id$/i.test(named[1]) && tableFor(named[1])) || tableFor(singular(String(a.segs[a.params[0].index - 1] || '').replace(/-/g, '_')));
735
+ if (t) refs.push({ key: '{id}', table: t, path: true });
736
+ }
737
+ for (const k of Object.keys(a.fullData || {})) {
738
+ const v = a.fullData[k];
739
+ 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 }); }
740
+ }
741
+ if (refs.length >= 2 && refs.every((r) => orgColOf(r.table))) candidates.push({ a, refs });
742
+ }
743
+ if (!candidates.length) return { ran: false, why: 'no write names two records the database keeps an organization for' };
744
+
745
+ 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 });
746
+ const sessions = [];
747
+ for (const acc of j.accounts.usable) {
748
+ const sess = authz.makeSession(ctx, 'account', acc.email, acc.password);
749
+ const lf = { [j.signIn.emailField]: acc.email, [j.signIn.passwordField]: acc.password };
750
+ const r = authz.replyOf(await sess.send(j.signIn.method, j.signIn.path, j.signIn.encoding === 'form' ? { form: lf } : { json: lf }));
751
+ const row = rowsOf(first, users).find((u) => String(u.email).toLowerCase() === acc.email.toLowerCase());
752
+ if (r.kind !== 'refused' && r.kind !== 'server-error' && r.kind !== 'no-answer' && row) sessions.push({ sess, email: acc.email, org: row[orgColOf(users)] });
753
+ }
754
+ if (!sessions.length) return { ran: false, why: 'no account with a password in .env could sign in' };
755
+ const taken = (res) => { const r = authz.replyOf(res); return r.kind === 'accepted' || r.kind === 'redirect'; };
756
+ const out = { ran: true, tried: [], findings: [], left: [] };
757
+ const same = (x, y) => String(x) === String(y);
758
+ const pkOf = (t) => ((first.tables[t].pk || [])[0]) || 'id';
759
+ const idOf = (t, row) => row[pkOf(t)];
760
+ // 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
761
+ const sharedParents = (t1, t2) => {
762
+ const k1 = Object.keys(rowsOf(first, t1)[0] || {});
763
+ const k2 = new Set(Object.keys(rowsOf(first, t2)[0] || {}));
764
+ return k1.filter((k) => k2.has(k) && /_id$/.test(k) && !ORG_COLUMN.test(k) && !PERSON_COLUMN.test(k) && tableFor(k));
765
+ };
766
+ let tries = 0;
767
+ const send = async (c, picks, mode, swapped, why) => {
768
+ // as each account of the organization until one is let in; a refusal about who is asking moves on to the next
769
+ for (const who of sessions.filter((x) => same(x.org, picks.org))) {
770
+ if (tries >= MAX_REF_TRIES) return;
771
+ const body = Object.assign({}, c.a.fullData || {});
772
+ let at = c.a.template;
773
+ 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; }
774
+ const before = await witness.snapshot();
775
+ tries++;
776
+ const res = await who.sess.send(c.a.method, at, c.a.encoding === 'form' ? { form: body } : { json: body });
777
+ const t = { what: c.a.method + ' ' + c.a.template, mode, by: who.email, status: res.status, swapped: swapped.key, verdict: 'refused (' + res.status + ')' };
778
+ if ([401, 403].includes(res.status)) continue;
779
+ out.tried.push(t);
780
+ if (!taken(res)) return;
781
+ const d = witnessDb.diff(before, await witness.snapshot());
782
+ const sid = idOf(swapped.table, picks.rows[swapped.key]);
783
+ const col = (row) => Object.keys(row).find((k) => flat(k) === flat(singular(swapped.table)) + 'id' && same(row[k], sid));
784
+ let stored = null;
785
+ for (const table of Object.keys(d)) {
786
+ if (HISTORY_TABLE.test(table)) continue;
787
+ for (const row of d[table].added) { const k = col(row); if (k) stored = { table, k, row, how: 'gained a row' }; }
788
+ 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(', ') + ')' };
789
+ }
790
+ t.verdict = stored ? 'taken, and stored' : 'taken (' + res.status + '), nothing stored that names it';
791
+ if (!stored) return;
792
+ out.left.push(t.what + ' as ' + who.email + ': the database table ' + stored.table + ' ' + stored.how + ' - Kryptheon never deletes');
793
+ const orgCol = orgColOf(swapped.table);
794
+ const finding = mode === 'foreign' ? {
795
+ kind: 'tenant', where: t.what, from: c.a.from.file + ':' + c.a.from.line, status: 'confirmed', severity: 'HIGH', field: swapped.key,
796
+ 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.',
797
+ 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 + '.',
798
+ 'In the database, ' + swapped.table + ' row ' + sid + ' has ' + orgCol + ' ' + picks.rows[swapped.key][orgCol] + ' - not this account\'s organization.',
799
+ 'Read the database before and after: the table ' + stored.table + ' ' + stored.how + (stored.k && stored.how === 'gained a row' ? ' with ' + stored.k + ' ' + sid : '') + '.'],
800
+ 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.',
801
+ } : {
802
+ kind: 'parent-mismatch', where: t.what, from: c.a.from.file + ':' + c.a.from.line, status: 'verification required', severity: 'HIGH', field: swapped.key,
803
+ headline: t.what + ' took two records that do not belong together: ' + why + ' - and the database stored it. To check: should these have to match?',
804
+ 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 + '.',
805
+ 'Read the database before and after: the table ' + stored.table + ' ' + stored.how + '.'],
806
+ 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.',
807
+ };
808
+ 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));
809
+ delete finding.fix;
810
+ out.findings.push(finding);
811
+ return;
812
+ }
813
+ };
814
+ try {
815
+ for (const c of candidates) {
816
+ if (tries >= MAX_REF_TRIES) break;
817
+ const snap = await witness.snapshot();
818
+ if (!snap) break;
819
+ const orgs = Array.from(new Set(sessions.map((x) => String(x.org))));
820
+ const org = orgs.find((o) => c.refs.every((r) => rowsOf(snap, r.table).some((row) => same(row[orgColOf(r.table)], o))));
821
+ if (org === undefined) continue;
822
+ const ownRows = (r) => rowsOf(snap, r.table).filter((row) => same(row[orgColOf(r.table)], org));
823
+ const base = {};
824
+ for (const r of c.refs) base[r.key] = ownRows(r)[0];
825
+ // rows that already belong together, where the tables share a parent: the first row sets it, the others follow
826
+ for (const r of c.refs.slice(1)) {
827
+ const sp = sharedParents(c.refs[0].table, r.table);
828
+ 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;
829
+ if (fit) base[r.key] = fit;
830
+ }
831
+ // 1. another organization's record, one named record at a time (the body's first)
832
+ for (const r of c.refs.filter((x) => !x.path)) {
833
+ if (out.findings.some((x) => x.kind === 'tenant' && x.where === c.a.method + ' ' + c.a.template)) break;
834
+ const foreign = rowsOf(snap, r.table).find((row) => row[orgColOf(r.table)] !== null && !same(row[orgColOf(r.table)], org));
835
+ if (!foreign) continue;
836
+ await send(c, { org, rows: Object.assign({}, base, { [r.key]: foreign }) }, 'foreign', r, '');
837
+ }
838
+ // 2. two records of this organization whose rows name different parents
839
+ let done = false;
840
+ for (let x = 0; x < c.refs.length && !done; x++) {
841
+ for (let y = x + 1; y < c.refs.length && !done; y++) {
842
+ const [r1, r2] = [c.refs[x], c.refs[y]];
843
+ for (const k of sharedParents(r1.table, r2.table)) {
844
+ const a1 = base[r1.key];
845
+ const other = a1 && a1[k] !== null ? ownRows(r2).find((row) => row[k] !== null && !same(row[k], a1[k])) : null;
846
+ if (!other) continue;
847
+ 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];
848
+ await send(c, { org, rows: Object.assign({}, base, { [r2.key]: other }) }, 'mismatch', r2, why);
849
+ done = true;
850
+ break;
851
+ }
852
+ }
853
+ }
854
+ }
855
+ } catch (err) {
856
+ if (!authz.isRequestLimit(err)) throw err;
857
+ out.stoppedAt = err.limit;
858
+ }
859
+ return out;
860
+ }
861
+
862
+ // --- who may give which role -----------------------------------------------------
863
+
864
+ const GRANT_KEYS = ['roleGrantRules', 'grantRules', 'roleGrants', 'mayGrant', 'canGrant'];
865
+
866
+ /** The policy file's own rule of who may give which role: { role: [roles it may give] }, or null. */
867
+ function grantRulesOf(root) {
868
+ try {
869
+ const json = JSON.parse(fs.readFileSync(path.join(root, roles.POLICY_FILE), 'utf8').replace(/^\uFEFF/, ''));
870
+ for (const k of GRANT_KEYS) {
871
+ const v = json[k];
872
+ if (!v || typeof v !== 'object' || Array.isArray(v)) continue;
873
+ const out = {};
874
+ for (const r of Object.keys(v)) if (Array.isArray(v[r])) out[r] = v[r].map(String);
875
+ if (Object.keys(out).length) return { key: k, rules: out };
876
+ }
877
+ } catch (err) {
878
+ return null;
879
+ }
880
+ return null;
881
+ }
882
+
883
+ /**
884
+ * A create that carries a role (an invitation, a new member): sent by each account whose own role the policy lets
885
+ * give SOME roles, with the role it may NOT give that holds the most permissions. Taken, with that role in what the
886
+ * app stored = the finding (blind round 34: an admin invited an owner; roleGrantRules gives admin four lower roles).
887
+ * Only creates - a change of an existing person's role is the roles check's, which puts it back. The invitation made
888
+ * is listed as left; it is never accepted and never deleted. { ran, why, tried, findings, left }.
889
+ */
890
+ async function runGrants(root, options) {
891
+ const opts = options || {};
892
+ const plan = planOf(root, opts);
893
+ if (!plan.ran) return { ran: false, why: plan.why };
894
+ const found = grantRulesOf(root);
895
+ if (!found) return { ran: false, why: roles.POLICY_FILE + ' has no rule of who may give which role (roleGrantRules)' };
896
+ const j = plan.journey;
897
+ const roleFieldOf = (a) => (a.fields || []).map((x) => x.name).concat(Object.keys(a.fullData || {})).find((n) => wordsOf(n).includes('role'));
898
+ 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));
899
+ if (!routes.length) return { ran: false, why: 'no create route reads a role from the request' };
900
+ let origin;
901
+ try {
902
+ origin = authz.assertLocalTarget(opts.origin);
903
+ } catch (err) {
904
+ return { ran: false, why: 'the app must be on this computer (localhost or 127.0.0.1): ' + (opts.origin || 'no address was given') };
905
+ }
906
+ if (!j.signIn) return { ran: false, why: j.signInWhy };
907
+ const policy = plan.policy && plan.policy.ok ? plan.policy.policy : null;
908
+ const weight = (r) => (policy && policy.roles && policy.roles[r] ? policy.roles[r].length : 0);
909
+ const allRoles = Array.from(new Set(Object.keys(found.rules).concat(...Object.values(found.rules), policy ? Object.keys(policy.roles || {}) : [])));
910
+ 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 });
911
+ const meRoute = (plan.map.routes || []).find((r) => r.method === 'GET' && !r.unmounted && r.path && /(^|\/)(me|whoami|session|profile|account)$/i.test(r.path));
912
+ const seek = (o, key, depth) => {
913
+ if (!o || typeof o !== 'object' || depth > 3) return undefined;
914
+ if (typeof o[key] === 'string') return o[key];
915
+ for (const k of Object.keys(o)) { const v = seek(o[k], key, depth + 1); if (v !== undefined) return v; }
916
+ return undefined;
917
+ };
918
+ const taken = (res) => { const r = authz.replyOf(res); return r.kind === 'accepted' || r.kind === 'redirect'; };
919
+ const out = { ran: true, tried: [], findings: [], left: [], rule: found.key };
920
+ const done = new Set();
921
+ for (const acc of j.accounts.usable) {
922
+ const sess = authz.makeSession(ctx, 'account', acc.email, acc.password);
923
+ const lf = { [j.signIn.emailField]: acc.email, [j.signIn.passwordField]: acc.password };
924
+ const r0 = authz.replyOf(await sess.send(j.signIn.method, j.signIn.path, j.signIn.encoding === 'form' ? { form: lf } : { json: lf }));
925
+ if (r0.kind === 'refused' || r0.kind === 'server-error' || r0.kind === 'no-answer' || !meRoute) continue;
926
+ const me = await sess.send('GET', meRoute.path, {});
927
+ const own = me.status === 200 ? seek(me.json, 'role', 0) : undefined;
928
+ if (!own || !found.rules[own] || done.has(own)) continue;
929
+ // the role this one may not give that holds the most: never its own level, which the rule may simply not list
930
+ const forbidden = allRoles.filter((x) => x !== own && !found.rules[own].includes(x)).sort((x, y) => weight(y) - weight(x));
931
+ if (!forbidden.length || !found.rules[own].length) continue;
932
+ const target = forbidden[0];
933
+ done.add(own);
934
+ for (const a of routes) {
935
+ const field = roleFieldOf(a);
936
+ const body = Object.assign({}, a.fullData || {}, { [field]: target });
937
+ for (const k of Object.keys(body)) if (/mail/i.test(k)) body[k] = 'kx-grant-' + Date.now().toString(36) + '@kryptheon.invalid';
938
+ const res = await sess.send(a.method, a.template, a.encoding === 'form' ? { form: body } : { json: body });
939
+ const stored = res.json && typeof res.json === 'object' ? seek(res.json, field, 0) || seek(res.json, 'role', 0) : undefined;
940
+ const t = { what: a.method + ' ' + a.template, as: own, by: acc.email, role: target, status: res.status, took: taken(res), stored: stored };
941
+ out.tried.push(t);
942
+ if (!t.took) continue;
943
+ out.left.push(t.what + ' as ' + acc.email + ' with ' + field + ' "' + target + '" (answered ' + res.status + '): the record it made is still there - Kryptheon never deletes');
944
+ const sure = String(stored) === target;
945
+ const finding = {
946
+ kind: 'role-grant', where: t.what, from: a.from.file + ':' + a.from.line, status: sure ? 'confirmed' : 'verification required', severity: 'HIGH',
947
+ 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(', ') + ').',
948
+ 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.')],
949
+ };
950
+ 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, '',
951
+ '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));
952
+ out.findings.push(finding);
953
+ }
954
+ }
955
+ return out;
956
+ }
957
+
632
958
  // --- stage 4: what the app does with a value it should turn away ----------
633
959
 
634
960
  // A value no field of that name should hold, marked so the app's own list can show whether it was kept.
@@ -1368,7 +1694,7 @@ async function runJobs(root, options) {
1368
1694
 
1369
1695
  // Records a second copy of costs something: money, a delivery, someone's time - or lets one ticket in twice (round 28:
1370
1696
  // 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;
1697
+ 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
1698
 
1373
1699
  /**
1374
1700
  * Each create route for a costly record (POST /api/work-orders, /api/payments) that the app also lists at the same
@@ -1504,6 +1830,25 @@ async function runCreates(root, options) {
1504
1830
  // use_limit, seat_limit, limit_total: a limit too (round 32: a one-use credit, use_limit 1, redeemed twice at once)
1505
1831
  const LIMIT_FIELD = /^(capacity|max_\w+|limit|\w+_limit|limit_\w+|quota|stock|seats_total|total_seats)$/i;
1506
1832
  const COUNTER_FIELD = /^(sold|uses|used|used_count|booked|taken|reserved|issued|redeemed|count)$/i;
1833
+ // quantity_total and quantity_sold, seats_total and seats_booked: a limit and its count that share their first
1834
+ // word (blind round 34: tickets sold past quantity_total, and no pair was seen)
1835
+ const PAIR_LIMIT = /^(\w+?)_(total|limit|capacity|max)$/i;
1836
+ const PAIR_COUNT = /^(\w+?)_(sold|used|booked|taken|reserved|issued|redeemed|count)$/i;
1837
+ /** Every [limit, counter] pair among a row's field names. */
1838
+ function limitPairsIn(keys) {
1839
+ const out = [];
1840
+ 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]);
1841
+ for (const l of keys) {
1842
+ const m = PAIR_LIMIT.exec(l);
1843
+ if (!m) continue;
1844
+ for (const c of keys) {
1845
+ const n = PAIR_COUNT.exec(c);
1846
+ if (n && n[1].toLowerCase() === m[1].toLowerCase() && !out.some((p) => p[0] === l && p[1] === c)) out.push([l, c]);
1847
+ }
1848
+ }
1849
+ return out;
1850
+ }
1851
+ const isCounter = (col) => COUNTER_FIELD.test(col) || PAIR_COUNT.test(col);
1507
1852
  // a field of the create that says how many: what a capacity is used up by
1508
1853
  const AMOUNT_FIELD = /^(quantity|qty|seats|tickets|units|count)$/i;
1509
1854
  const MAX_LIMIT_SENDS = 25;
@@ -1511,13 +1856,10 @@ const MAX_LIMIT_SENDS = 25;
1511
1856
  /** A record's limit pair, or null: { limit, counter, left }. */
1512
1857
  function limitPairOf(rec) {
1513
1858
  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
- }
1859
+ for (const [l, c] of limitPairsIn(Object.keys(rec))) {
1860
+ const L = Number(rec[l]);
1861
+ const C = Number(rec[c]);
1862
+ 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
1863
  }
1522
1864
  return null;
1523
1865
  }
@@ -1846,7 +2188,13 @@ function rowsOfReply(res) {
1846
2188
  /** The code of a write the map found, from its first line to the end of the call (or twelve lines). */
1847
2189
  function sourceAround(root, d) {
1848
2190
  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');
2191
+ let lines = fs.readFileSync(path.join(root, d.file), 'utf8').split('\n').slice(Math.max(0, d.line - 1), d.line + 11).join('\n');
2192
+ // code written many statements to a line (blind round 34: a whole route on one line): from this statement's own
2193
+ // UPDATE or INSERT of its table, not from the start of the line - whose first ");" ends long before it
2194
+ if (d.table && /^(UPDATE|INSERT|UPSERT)$/.test(d.op || '')) {
2195
+ const at = lines.search(new RegExp('\\b' + (d.op === 'UPDATE' ? 'UPDATE\\s+' : 'INSERT\\s+INTO\\s+') + '"?' + String(d.table).replace(/[^\w]/g, '') + '\\b', 'i'));
2196
+ if (at > 0) lines = lines.slice(at);
2197
+ }
1850
2198
  const end = lines.search(/\)\s*;/);
1851
2199
  return end < 0 ? lines : lines.slice(0, end + 2);
1852
2200
  } catch (err) {
@@ -1990,16 +2338,49 @@ function pastLimit(before, after) {
1990
2338
  for (const l of Object.keys(after)) {
1991
2339
  for (const row of rowsAt(after, l)) {
1992
2340
  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
- }
2341
+ for (const [lim, c] of limitPairsIn(Object.keys(row))) {
2342
+ const L = Number(row[lim]);
2343
+ const C = Number(row[c]);
2344
+ if (!(Number.isFinite(L) && Number.isFinite(C) && L > 0 && C > L)) continue;
2345
+ const old = row.id === undefined ? null : rowsAt(before, l).find((x) => x && JSON.stringify(x.id) === JSON.stringify(row.id));
2346
+ if (old && Number(old[c]) > Number(old[lim])) continue;
2347
+ 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' });
2348
+ }
2349
+ }
2350
+ }
2351
+ return out;
2352
+ }
2353
+
2354
+ // Tables that take a line per request: two lines for two requests is the table doing its job.
2355
+ const HISTORY_TABLE = /audit|(^|_)logs?($|_)|activit|histor|outbox|notification|timeline|(^|_)jobs?(_runs?)?$|webhook_events?|(^|_)events?_log/i;
2356
+
2357
+ /**
2358
+ * The same double effects, read from the database's own rows (kryptheon-witness.js) - for what no list of the
2359
+ * app shows: two rows of a table no route reads, each tied to the record (blind round 34: two payout_transfers
2360
+ * for one payout, both requests answered 200, every list unchanged), a count the row itself holds past its limit.
2361
+ */
2362
+ function dbDoubleEffects(d, rec, listTemplate, stepTemplate) {
2363
+ const out = [];
2364
+ const flat = (w) => String(w).toLowerCase().replace(/[^a-z0-9]/g, '');
2365
+ const stem = flat((String(listTemplate).split('/').pop() || '').replace(/ies$/, 'y').replace(/s$/, ''));
2366
+ const last = stepTemplate ? String(stepTemplate).split('/').filter((x) => x && x !== '{id}').pop() || '' : '';
2367
+ const addsToList = /s$/i.test(last) && !STEP_VERB.test(last);
2368
+ for (const table of Object.keys(d || {})) {
2369
+ const t = d[table];
2370
+ if (!addsToList && stem && !HISTORY_TABLE.test(table)) {
2371
+ const tied = t.added.filter((row) => Object.keys(row).some((k) => flat(k) === stem + 'id' && String(row[k]) === String(rec.id)));
2372
+ const keys = tied.length ? Object.keys(tied[0]).filter((k) => !ROW_IDENTITY.test(k)) : [];
2373
+ if (tied.length >= 2 && tied.every((x) => keys.every((k) => JSON.stringify(x[k]) === JSON.stringify(tied[0][k])))) {
2374
+ 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 });
2375
+ }
2376
+ }
2377
+ for (const ch of t.changed) {
2378
+ for (const [lim, c] of limitPairsIn(Object.keys(ch.row))) {
2379
+ const L = Number(ch.row[lim]);
2380
+ const C = Number(ch.row[c]);
2381
+ if (!(Number.isFinite(L) && Number.isFinite(C) && L > 0 && C > L)) continue;
2382
+ if (Number(ch.old[c]) > Number(ch.old[lim])) continue;
2383
+ 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
2384
  }
2004
2385
  }
2005
2386
  }
@@ -2016,7 +2397,10 @@ const CLAIM_QTY = /^(quantity|qty|units|seats|count|amount_units)$/i;
2016
2397
  * both together do not. { ids, label } or null when there is no such create, list or field.
2017
2398
  */
2018
2399
  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);
2400
+ const down = c.effect.moves.filter((m) => m.sign === '-' && m.col).map((m) => m.col);
2401
+ // a count that goes up to a limit beside it (quantity_sold of quantity_total): what is left is the difference
2402
+ const up = c.effect.moves.filter((m) => m.sign === '+' && m.col && isCounter(m.col) && !down.includes(m.col)).map((m) => m.col);
2403
+ const cols = down.concat(up);
2020
2404
  if (!cols.length) return null;
2021
2405
  const create = j.actions.find((x) => x.method === 'POST' && x.template === c.listTemplate && !x.params.length);
2022
2406
  if (!create || !create.fullData) return null;
@@ -2038,7 +2422,9 @@ async function scarceClaim(s, c, j, lists, snap, taken) {
2038
2422
  }
2039
2423
  const flat = (k) => String(k).replace(/_/g, '').toLowerCase();
2040
2424
  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];
2425
+ const limitOf = (r) => { const p = limitPairsIn(Object.keys(r)).find((x) => x[1] === col); return p ? p[0] : null; };
2426
+ const leftOf = (r) => (up.includes(col) ? (limitOf(r) ? Number(r[limitOf(r)]) - Number(r[col]) : NaN) : Number(r[col]));
2427
+ const scarce = rows.filter((r) => leftOf(r) > 0).sort((x, y) => leftOf(x) - leftOf(y))[0];
2042
2428
  if (!scarce) continue;
2043
2429
  // What each pointer of the body is set to. Named like a field of the row (productId -> product_id, warehouseId ->
2044
2430
  // warehouse_id: the stock found by product and place), that field's value - every such field, so the new record
@@ -2050,12 +2436,18 @@ async function scarceClaim(s, c, j, lists, snap, taken) {
2050
2436
  const rk = Object.keys(scarce).find((x) => x !== 'id' && flat(x) === flat(l2.key));
2051
2437
  if (rk && scarce[rk] !== null && scarce[rk] !== undefined) sets.push({ path: l2.path, value: scarce[rk] });
2052
2438
  }
2439
+ // the pointer named for the list itself (ticketTypeId for /ticket-types, round 34): the scarce row's own id
2440
+ const listStem = String(l.split('/').pop() || '').toLowerCase().replace(/[^a-z]/g, '').replace(/ies$/, 'y').replace(/s$/, '');
2441
+ const own = leaves.find((l2) => isPointer(l2.key) && flat(l2.key.replace(/_?[iI]d$/, '')) === listStem);
2442
+ if (own && !sets.some((x) => x.path.join('.') === own.path.join('.'))) sets.push({ path: own.path, value: scarce.id });
2053
2443
  if (!sets.length) {
2054
2444
  const byId = leaves.find((l2) => isPointer(l2.key) && ids.has(String(l2.value)));
2055
2445
  if (byId) sets.push({ path: byId.path, value: scarce.id });
2056
2446
  }
2057
2447
  if (!sets.length) continue;
2058
- const units = Number(scarce[col]);
2448
+ const units = leftOf(scarce);
2449
+ // never a claim so large that making it is a load of its own (an app that writes one row per unit)
2450
+ if (!(units <= 2000)) continue;
2059
2451
  const qty = leaves.find((l2) => CLAIM_QTY.test(l2.key));
2060
2452
  if (!qty && units > 1) continue; // one unit per record and more than one left: two records cannot overdraw it
2061
2453
  if (qty) sets.push({ path: qty.path, value: units });
@@ -2079,11 +2471,45 @@ async function scarceClaim(s, c, j, lists, snap, taken) {
2079
2471
  if (id === undefined || id === null) return null;
2080
2472
  made.push(id);
2081
2473
  }
2082
- return { ids: made, label: l.split('/').pop() + ' ' + scarce.id + ' (' + col + ' ' + units + ')' };
2474
+ return { ids: made, label: l.split('/').pop() + ' ' + scarce.id + ' (' + (up.includes(col) ? units + ' left of ' + limitOf(scarce) : col + ' ' + units) + ')' };
2083
2475
  }
2084
2476
  return null;
2085
2477
  }
2086
2478
 
2479
+ /**
2480
+ * A record of the step's list with exactly one use left of its limit (use_limit 1, used_count 0): the one two
2481
+ * requests at the same moment must not both take. One the list already has, or one made through the list's own
2482
+ * create route with its limit field set to 1 (listed as left - never deleted). null when there is no limit pair,
2483
+ * no create route, or the create is refused. Blind round 34: a one-use promo redeemed twice at once.
2484
+ */
2485
+ async function oneLeft(s, c, j, recs, taken, out) {
2486
+ const sample = recs.find((x) => x && typeof x === 'object');
2487
+ if (!sample) return null;
2488
+ const counters = c.effect.moves.filter((m) => m.sign === '+' && m.col && isCounter(m.col)).map((m) => m.col);
2489
+ const pair = limitPairsIn(Object.keys(sample)).find((p) => counters.includes(p[1]));
2490
+ if (!pair) return null;
2491
+ const left = (x) => Number(x[pair[0]]) - Number(x[pair[1]]);
2492
+ const have = recs.find((x) => Number(x[pair[0]]) > 0 && left(x) === 1 && !/Kryptheon test|kx-|=KX/i.test(JSON.stringify(x)));
2493
+ if (have) return have;
2494
+ const create = j.actions.find((x) => x.method === 'POST' && x.template === c.listTemplate && !x.params.length);
2495
+ if (!create || !create.fullData) return null;
2496
+ const flat = (k) => String(k).replace(/_/g, '').toLowerCase();
2497
+ const limKey = Object.keys(create.fullData).find((k) => flat(k) === flat(pair[0]));
2498
+ if (!limKey) return null;
2499
+ const body = JSON.parse(JSON.stringify(create.fullData));
2500
+ body[limKey] = 1;
2501
+ // a code, slug or name of its own: the app may keep these unique
2502
+ const tag = 'KX' + Date.now().toString(36).toUpperCase();
2503
+ 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;
2504
+ const had = new Set((rowsOfReply(await s.send('GET', c.listTemplate, {})) || []).map((r) => r && String(r.id)));
2505
+ const res = await s.send('POST', c.listTemplate, create.encoding === 'form' ? { form: body } : { json: body });
2506
+ if (!taken(res)) return null;
2507
+ const made = (rowsOfReply(await s.send('GET', c.listTemplate, {})) || []).find((r) => r && r.id !== undefined && !had.has(String(r.id)));
2508
+ if (!made || left(made) !== 1) return null;
2509
+ out.left.push(c.listTemplate + ' record ' + made.id + ' made by Kryptheon with ' + pair[0] + ' 1, to send its step twice at once - never deleted');
2510
+ return made;
2511
+ }
2512
+
2087
2513
  /**
2088
2514
  * The map's steps on one record (POST|PUT|PATCH /things/{id}/verb that change a status, money or a count),
2089
2515
  * 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 +2572,11 @@ async function runStepsAtOnce(root, options) {
2146
2572
  return o;
2147
2573
  };
2148
2574
  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))));
2575
+ const witness = opts.witness && opts.witness.ok ? opts.witness : null;
2576
+ // What one step costs here: per account a list and a read of every list, two sends and another read (up to three
2577
+ // records when one is refused); and the same again for two records. Sized to the app, never past 4000 (the limit
2578
+ // of the whole run is kept: ctx.limit only goes up).
2579
+ 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
2580
  const out = { ran: true, tried: setAside.slice(), findings: [], left: [] };
2153
2581
  const headFor = (c) => c.a.method + ' ' + c.a.template;
2154
2582
  const finish = (c, what, s, atText, answers, effects, mode, body) => {
@@ -2175,7 +2603,7 @@ async function runStepsAtOnce(root, options) {
2175
2603
  const t = { what, verdict: 'no account was let in' };
2176
2604
  out.tried.push(t);
2177
2605
  const route = c.route;
2178
- const shared = c.effect.moves.some((m) => m.sign === '-' || (m.col && COUNTER_FIELD.test(m.col)));
2606
+ const shared = c.effect.moves.some((m) => m.sign === '-' || (m.col && isCounter(m.col)));
2179
2607
  const sets = (route.db || []).find((d) => d.setsState);
2180
2608
  const target = sets ? sets.setsState.value : null;
2181
2609
  const bodyOf = () => (a.encoding === 'form' ? { form: a.fullData || {} } : { json: a.fullData || {} });
@@ -2186,11 +2614,29 @@ async function runStepsAtOnce(root, options) {
2186
2614
  const usable = (recs || []).filter((x) => x && typeof x === 'object' && x.id !== undefined);
2187
2615
  if (!usable.length) { t.verdict = 'no record at GET ' + c.listTemplate + ' to aim it at'; continue; }
2188
2616
  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())]);
2617
+ // Records the seed made before ones Kryptheon's other checks made (blind round 34: the first promo in the
2618
+ // list was one made with a bad limit, both redeems were refused 409, and the step was called held), and
2619
+ // for a count that runs up to a limit, a record with exactly one use left - made, when there is none.
2620
+ const toolMade = (x) => /Kryptheon test|kx-|=KX/i.test(JSON.stringify(x));
2621
+ const fresh = open(usable, statusKey).sort((x, y) => Number(toolMade(x)) - Number(toolMade(y)));
2622
+ const last = c.effect.moves.some((m) => m.sign === '+' && m.col && isCounter(m.col)) ? await oneLeft(s, c, j, fresh, taken, out) : null;
2623
+ const pool = (last ? [last] : []).concat(fresh.length ? fresh : usable).slice(0, 3);
2624
+ let rec = pool[0];
2625
+ let at = atOf(rec);
2626
+ let before = null;
2627
+ let dbBefore = null;
2628
+ let r1 = null;
2629
+ let r2 = null;
2630
+ for (const cand of pool) {
2631
+ rec = cand;
2632
+ at = atOf(rec);
2633
+ before = await snap(s);
2634
+ dbBefore = witness ? await witness.snapshot() : null;
2635
+ [r1, r2] = await Promise.all([s.send(a.method, at, bodyOf()), s.send(a.method, at, bodyOf())]);
2636
+ if (taken(r1) || taken(r2)) break;
2637
+ // not this account's to change: the next account may be; any other refusal may be about this record
2638
+ if ([401, 403].includes(r1.status) && [401, 403].includes(r2.status)) break;
2639
+ }
2194
2640
  const answers = [r1.status, r2.status];
2195
2641
  const both = taken(r1) && taken(r2);
2196
2642
  t.by = s.email;
@@ -2203,7 +2649,9 @@ async function runStepsAtOnce(root, options) {
2203
2649
  break;
2204
2650
  }
2205
2651
  const after = await snap(s);
2652
+ const dbAfter = witness && dbBefore ? await witness.snapshot() : null;
2206
2653
  const effects = both ? doubleEffects(before, after, rec, c.listTemplate, a.fullData || {}, a.template) : [];
2654
+ 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
2655
  t.verdict = effects.length ? 'done twice' : both ? 'held: both taken, one effect' : 'held: one taken, one refused';
2208
2656
  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
2657
  if (effects.length) finish(c, what, s, a.method + ' ' + at, answers, effects, 'once', a.fullData);
@@ -2374,6 +2822,23 @@ function probesOf(a) {
2374
2822
  if (typeof f.sample === 'number' && /^(percent|pct|percentage)$/.test(w[w.length - 1] || '')) {
2375
2823
  out.push({ field: f, mode: 'above', value: 150, why: 'above 100 percent (150)' });
2376
2824
  }
2825
+ // a number inside a list of lines (items: [{ ticketTypeId, quantity }]): a negative one in the first line
2826
+ // (blind round 34: an order line with quantity -2 was stored, and only top-level fields were ever tried)
2827
+ if (Array.isArray(f.sample) && f.sample[0] && typeof f.sample[0] === 'object' && !Array.isArray(f.sample[0])) {
2828
+ for (const k of Object.keys(f.sample[0])) {
2829
+ const kw = wordsOf(k);
2830
+ if (typeof f.sample[0][k] !== 'number' || kw[kw.length - 1] === 'id' || !kw.some((x) => NUMBER_WORDS.has(x))) continue;
2831
+ out.push({ field: f, mode: 'below', value: [Object.assign({}, f.sample[0], { [k]: -2 })], why: 'a negative ' + k + ' (-2) in its first line' });
2832
+ }
2833
+ }
2834
+ // an end before its start (startsAt and endsAt, sales_start and sales_end): a day long before the start
2835
+ const m = /^(.*?)(_?)(end|ends|until|expires)(.*)$/i.exec(f.name);
2836
+ if (m && typeof f.sample === 'string' && /^\d{4}-\d\d-\d\d/.test(f.sample)) {
2837
+ const flat = (x) => String(x).replace(/_/g, '').toLowerCase();
2838
+ const start = (a.fields || []).find((g) => g !== f && typeof g.sample === 'string' && /^\d{4}-\d\d-\d\d/.test(g.sample) &&
2839
+ [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)));
2840
+ 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)' });
2841
+ }
2377
2842
  }
2378
2843
  return out;
2379
2844
  }
@@ -2693,7 +3158,10 @@ async function runApi(root, options) {
2693
3158
  const m = opts.map || map.mapProject(root, { packageDir: opts.packageDir || __dirname });
2694
3159
  if (!m.ran) return { ran: false, why: m.why };
2695
3160
  const toCheck = opts.toCheck || map.toCheck(m);
2696
- const shared = Object.assign({}, opts, { map: m, say: say });
3161
+ // the app's own database beside its lists, where its connection string is in .env and it is on this computer
3162
+ const witness = opts.witness || await witnessDb.open(root, { env: opts.env });
3163
+ 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 + '.');
3164
+ const shared = Object.assign({}, opts, { map: m, say: say, witness: witness });
2697
3165
  // jobs first, on the app as it was seeded: the tries after them cancel and refund the records a job works on
2698
3166
  // (round 28: the settlement job found no paid order left and made nothing either time)
2699
3167
  // A check that reaches its request limit stops there and says so; the others still run and report.
@@ -2721,18 +3189,21 @@ async function runApi(root, options) {
2721
3189
  const onceRun = await contained('Steps sent twice at once', () => runStepsAtOnce(root, shared));
2722
3190
  let roleRun = null;
2723
3191
  if (opts.roles) roleRun = await contained('By role', () => runRoles(root, shared));
3192
+ const refsRun = await contained('Records a request names', () => runRefs(root, shared));
3193
+ const grantsRun = opts.roles ? await contained('Who may give which role', () => runGrants(root, shared)) : { ran: false, why: 'not asked for' };
2724
3194
  // the bad values always last: one of them may stop the app (round 24 lost its whole roles run that way)
2725
3195
  const invalid = await contained('Bad values', () => runInvalid(root, shared));
3196
+ if (witness.ok && !opts.witness) await witness.close();
2726
3197
  const verdicts = verdictsFor(toCheck, wall, invalid, roleRun, forged, webhooks, stepsRun, redirectsRun);
2727
3198
  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) => {
3199
+ 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
3200
  const k = f.kind + '|' + f.where + '|' + (f.field || '');
2730
3201
  return seen.has(k) ? false : (seen.add(k), true);
2731
3202
  });
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) : []);
3203
+ 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
3204
  return {
2734
3205
  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,
3206
+ 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
3207
  notTried: (wall.plan ? wall.plan.notTried : []).concat(invalid.ran ? invalid.notTried : []),
2737
3208
  writes: writesOf(m, wall),
2738
3209
  };
@@ -2877,6 +3348,22 @@ function apiLines(run) {
2877
3348
  }
2878
3349
  out.push('');
2879
3350
  }
3351
+ if (run.refs && run.refs.ran) {
3352
+ 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);
3353
+ for (const f of run.refs.findings) {
3354
+ out.push(' ' + (f.status === 'confirmed' ? 'PROBLEM' : 'TO CHECK') + ' ' + f.severity + ' ' + f.headline + ' (' + f.from + ')');
3355
+ for (const l of f.proof) out.push(' ' + l);
3356
+ }
3357
+ out.push('');
3358
+ }
3359
+ if (run.grants && run.grants.ran) {
3360
+ 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 + ')'));
3361
+ for (const f of run.grants.findings) {
3362
+ out.push(' ' + (f.status === 'confirmed' ? 'PROBLEM' : 'TO CHECK') + ' ' + f.severity + ' ' + f.headline + ' (' + f.from + ')');
3363
+ for (const l of f.proof) out.push(' ' + l);
3364
+ }
3365
+ out.push('');
3366
+ }
2880
3367
  if (run.refunds && run.refunds.ran) {
2881
3368
  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
3369
  for (const f of run.refunds.findings) {
@@ -2940,4 +3427,4 @@ function apiLines(run) {
2940
3427
  return out;
2941
3428
  }
2942
3429
 
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 };
3430
+ module.exports = { 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 };
package/kryptheon-map.js CHANGED
@@ -3109,7 +3109,9 @@ function toCheck(map) {
3109
3109
  const writes = /^(POST|PUT|PATCH|DELETE|ANY|ALL)$/.test(r.method);
3110
3110
  const changing = (r.db || []).filter((d) => d.op !== 'SELECT');
3111
3111
  if (r.kind === 'webhook') {
3112
- if (r.signature.status === STATUS.NOT) {
3112
+ // a GET under /webhooks that writes nothing is where a provider sends the browser back, not where it posts events:
3113
+ // there is no body to sign (blind round 34: a return address that only checks its path was called an unsigned webhook)
3114
+ if (r.signature.status === STATUS.NOT && (writes || changing.length)) {
3113
3115
  add(r, 'webhook-signature', 'HIGH', name + ' is a webhook, and no check of its signature was found' + (r.signature.presenceOnly ? ': it only looks at whether a signature header is there' : ''),
3114
3116
  [r.file + ':' + r.line, r.signature.presenceOnly && ev(r.signature.presenceOnly)]);
3115
3117
  } else if (r.signature.status === STATUS.DETECTED && r.signature.skippable) {
@@ -816,7 +816,12 @@ async function run(root, options) {
816
816
  const captureType = shape.amount && amountKey ? (!REFUNDED.test(control.type) && CAPTURE.test(control.type) ? control.type : signing.types.find((t) => !REFUNDED.test(t) && CAPTURE.test(t)) || (actsOnAny ? control.type : null)) : null;
817
817
  const payControl = !!captureType;
818
818
  // a record a capture can still land on first, so what changed is the capture and not a record already settled
819
- const captureOrder = [...capturable, controlRec].concat(records).filter((r, i, all) => r && r.id !== null && all.indexOf(r) === i).slice(0, 3);
819
+ // ... and one no earlier event of this run has touched (blind round 34: the wrong amount and the wrong currency went
820
+ // to the order the control had just paid, nothing more could change on it, and neither was seen) - as long as one
821
+ // untouched record is left over for the unsigned event that follows
822
+ const untouched = () => capturable.filter((r) => r.id !== null && !used.has(r.id));
823
+ const orderNow = () => (untouched().length >= 2 ? untouched().slice(0, 1) : []).concat(capturable, [controlRec], records).filter((r, i, all) => r && r.id !== null && all.indexOf(r) === i).slice(0, 3);
824
+ let captureOrder = orderNow();
820
825
  if (captureType) {
821
826
  for (const rec of captureOrder) {
822
827
  const t = await attempt(S, captureType, rec, (Number(rec[amountKey]) || 0) + 100, 'signed, wrong amount');
@@ -825,6 +830,7 @@ async function run(root, options) {
825
830
  }
826
831
  // the same capture in another currency than the record's (blind round 32: EUR was taken for a USD payment)
827
832
  const currencyKey = records[0] && Object.keys(records[0]).find((k) => /^currency(_code)?$/i.test(k));
833
+ captureOrder = orderNow();
828
834
  if (captureType && shape.currency && currencyKey) {
829
835
  for (const rec of captureOrder) {
830
836
  const own = String(rec[currencyKey] || '').toUpperCase();
@@ -834,6 +840,39 @@ async function run(root, options) {
834
840
  if (t.took) break;
835
841
  }
836
842
  }
843
+ // A correctly signed event that names this organization and a record of ANOTHER one (blind round 34: a signed gate
844
+ // callback with orgId 1 checked in organization 2's ticket - the handler looked the ticket up by its id alone).
845
+ // Needs a field of the event that says whose it is, the list read as an account of another organization, and a
846
+ // record there this account's own list does not carry. Judged from that record, read by its own organization.
847
+ const ownerField = signing.fields.find((x) => OWNER_ID.test(x.name) && !/^(user|owner)_?id$/i.test(x.name));
848
+ if (ownerField && listRoute && control.body && control.body[ownerField.name] !== undefined && j.accounts.usable.length > 1) {
849
+ const mine = new Set(records.map((r) => String(r.id)));
850
+ for (const acc of j.accounts.usable.slice(1)) {
851
+ if (!ctx.secrets.includes(acc.password)) ctx.secrets.push(acc.password);
852
+ const other = authz.makeSession(ctx, 'account', acc.email, acc.password);
853
+ const lf = { [j.signIn.emailField]: acc.email, [j.signIn.passwordField]: acc.password };
854
+ const inOther = authz.replyOf(await other.send(j.signIn.method, j.signIn.path, j.signIn.encoding === 'form' ? { form: lf } : { json: lf }));
855
+ if (inOther.kind === 'refused' || inOther.kind === 'server-error' || inOther.kind === 'no-answer') continue;
856
+ const readTheirs = async () => { const r = await other.send('GET', listRoute.path, {}); return r.status === 200 ? listOf(r.json) : null; };
857
+ const theirs = ((await readTheirs()) || []).filter((r) => r && r.id !== undefined && !mine.has(String(r.id)));
858
+ if (!theirs.length) continue;
859
+ // one whose state the event can still move (a ticket not yet checked in), else the first
860
+ const openOne = statusKey ? theirs.find((r) => records.some((m) => m[statusKey] !== undefined && m[statusKey] !== r[statusKey])) : null;
861
+ const target = openOne || theirs[0];
862
+ const body = bodyFor(control.type, target, undefined);
863
+ body[ownerField.name] = control.body[ownerField.name];
864
+ const res = await post(S, body);
865
+ const reply = authz.replyOf(res);
866
+ const now = ((await readTheirs()) || []).find((x) => String(x.id) === String(target.id));
867
+ const [sw, sn] = [stableItem(target), stableItem(now)];
868
+ const moved = now ? Object.keys(Object.assign({}, sw, sn)).filter((k) => JSON.stringify(sw[k]) !== JSON.stringify(sn[k])) : [];
869
+ const t = { mode: S, label: 'signed, another organization\'s record', type: control.type, record: target.id, status: res.status, took: reply.kind === 'accepted' || reply.kind === 'redirect', changed: moved.length > 0, fields: moved,
870
+ from: statusKey ? target[statusKey] : null, to: now && statusKey ? now[statusKey] : null, foreign: { owner: ownerField.name, sentAs: control.body[ownerField.name], readAs: acc.email } };
871
+ hook.tried.push(t);
872
+ if (t.changed) out.left.push(name + ' signed ' + control.type + ' naming ' + ownerField.name + ' ' + control.body[ownerField.name] + ' for ' + resource + ' ' + target.id + ' of another organization: it changed (' + moved.join(', ') + ')');
873
+ break;
874
+ }
875
+ }
837
876
  // a refund on a record that was paid: sent twice as the same event, then for more than was paid
838
877
  const refundType = types.find((t) => REFUNDED.test(t)) || signing.types.find((t) => REFUNDED.test(t));
839
878
  const paidRec = refundType && statusKey && records.find((r) => PAID.test(String(r[statusKey])) && !REFUNDED.test(String(r[statusKey])));
@@ -893,6 +932,12 @@ async function run(root, options) {
893
932
  finding('webhook-forged', t.changed ? 'CRITICAL' : 'HIGH', t.changed ? 'confirmed' : 'verification required',
894
933
  name + ' took ' + t.type + ' with a wrong signature' + (t.changed ? ' and changed ' + resource + ' ' + t.record : '') + '.',
895
934
  ['A signature of the right length, one character wrong: ' + lineOf(t)], 'Compare the signature with a timing-safe compare (crypto.timingSafeEqual) over the exact raw body, and answer 401 when it does not match, before anything is saved.');
935
+ } else if (t.foreign && t.took && t.changed) {
936
+ finding('webhook-tenant', 'CRITICAL', 'confirmed',
937
+ name + ' took a correctly signed ' + shown(t) + ' that names ' + t.foreign.owner + ' ' + t.foreign.sentAs + ' and changed ' + resource + ' ' + t.record + ' of another organization (' + (t.from !== t.to && t.from !== null ? statusKey + ' ' + t.from + ' -> ' + t.to : t.fields.join(', ')) + '): the record is found by its id alone.',
938
+ ['Correctly signed, ' + t.foreign.owner + ' = ' + t.foreign.sentAs + ', ' + resource + ' ' + t.record + ' (which GET ' + (listRoute ? listRoute.path : '') + ' shows only to ' + t.foreign.readAs + '\'s organization): answered ' + t.status + '.',
939
+ 'Read as ' + t.foreign.readAs + ' before and after: ' + (t.from !== t.to && t.from !== null ? statusKey + ' went ' + t.from + ' -> ' + t.to : t.fields.join(', ') + ' changed') + '.'],
940
+ 'Look the ' + resource + ' up by its id AND the organization the event is for (WHERE id = $1 AND ' + t.foreign.owner.replace(/Id$/, '_id').toLowerCase() + ' = $2), and tie each provider integration to the one organization it belongs to, so a signed event can only ever touch that organization\'s records. Answer 404 when no row matches.');
896
941
  } else if (t.label === 'signed, wrong currency' && t.took && t.changed) {
897
942
  finding('webhook-currency', 'HIGH', 'confirmed',
898
943
  name + ' applied ' + shown(t) + ' in another currency than ' + resource + ' ' + t.record + '\'s, and it changed all the same (' + (t.from !== t.to ? statusKey + ' ' + t.from + ' -> ' + t.to : t.fields.join(', ')) + ').',
@@ -0,0 +1,150 @@
1
+ // The database witness: what the app's own database held before and after a try.
2
+ //
3
+ // The API check judges a try from what the app shows in its lists. Some effects are in no list: a payout paid
4
+ // out twice writes two rows to a transfers table that no route reads (blind round 34: both requests answered
5
+ // 200, the lists showed one payout, and the check said "held"), a seat hold naming another organization's seat
6
+ // is stored though the page shows nothing new. Where the app runs on this computer and its connection string
7
+ // is in its .env (the same file the app reads), the rows themselves are read before and after.
8
+ //
9
+ // Rules, kept by the code and by kryptheon-witness.check.js:
10
+ // - Postgres on this computer only (localhost, 127.0.0.1, ::1): any other host is refused before connecting;
11
+ // - read-only: the session is set read-only and every read runs in a READ ONLY transaction;
12
+ // - nothing of it is saved or printed but table and column names, ids, counts and the plain values of a
13
+ // changed column; a column named like a password, hash, secret, token or key is never shown;
14
+ // - a table of sessions is not read at all; a table over MAX_ROWS rows is counted, not read;
15
+ // - no connection string, or one that fails: the check goes on without a witness and says so.
16
+
17
+ const secrets = require('./kryptheon-secrets.js');
18
+
19
+ const NAMES = ['KRYPTHEON_DATABASE_URL', 'DATABASE_URL', 'POSTGRES_URL', 'POSTGRES_PRISMA_URL', 'PG_URL', 'PGURL'];
20
+ const LOCAL_HOSTS = new Set(['localhost', '127.0.0.1', '::1', '[::1]']);
21
+ const MAX_ROWS = 5000;
22
+ const MAX_TABLES = 200;
23
+ const SKIP_TABLE = /session/i;
24
+ const SECRET_COLUMN = /pass(word|wd|code|phrase)?|hash|secret|token|(^|_)key($|_)|credential|salt|otp/i;
25
+
26
+ /** The connection string to witness with: { url, from } or { none: true, why }. Never printed. */
27
+ function urlFor(root, env) {
28
+ const here = env || process.env;
29
+ let file = {};
30
+ try { file = secrets.readEnvFile(secrets.envFileFor(root)); } catch (err) { file = {}; }
31
+ for (const from of [here, file]) {
32
+ for (const name of NAMES) {
33
+ const value = from[name];
34
+ if (typeof value !== 'string' || !value.trim()) continue;
35
+ let u;
36
+ try { u = new URL(value.trim()); } catch (err) { continue; }
37
+ if (!/^postgres(ql)?:$/.test(u.protocol)) continue;
38
+ if (!LOCAL_HOSTS.has(u.hostname.toLowerCase())) return { none: true, why: name + ' names a database that is not on this computer, so it is not read' };
39
+ return { url: value.trim(), from: name };
40
+ }
41
+ }
42
+ return { none: true, why: 'no Postgres connection string for this computer in .env (DATABASE_URL)' };
43
+ }
44
+
45
+ const plain = (v) => (v instanceof Date ? v.toISOString() : Buffer.isBuffer(v) ? '<bytes>' : v);
46
+
47
+ /**
48
+ * Opens the witness. { ok: true, snapshot(), close(), from } or { ok: false, why }. `connect` is for the checks:
49
+ * (url) -> a client with query() and end().
50
+ */
51
+ async function open(root, options) {
52
+ const opts = options || {};
53
+ const found = urlFor(root, opts.env);
54
+ if (found.none) return { ok: false, why: found.why };
55
+ let client;
56
+ try {
57
+ if (opts.connect) client = await opts.connect(found.url);
58
+ else {
59
+ const { Client } = require('pg');
60
+ client = new Client({ connectionString: found.url, connectionTimeoutMillis: 4000, statement_timeout: 8000, application_name: 'kryptheon-witness' });
61
+ await client.connect();
62
+ }
63
+ await client.query('SET default_transaction_read_only = on');
64
+ } catch (err) {
65
+ try { if (client) await client.end(); } catch (e) { /* closed already */ }
66
+ return { ok: false, why: 'the database in ' + found.from + ' could not be read (' + String(err && err.code ? err.code : 'no connection') + ')' };
67
+ }
68
+ let shape = null;
69
+ const shapeOf = async () => {
70
+ if (shape) return shape;
71
+ const t = await client.query("SELECT c.oid::int AS oid, c.relname AS name FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace WHERE n.nspname = 'public' AND c.relkind = 'r' ORDER BY c.relname LIMIT " + (MAX_TABLES + 1));
72
+ const pk = await client.query("SELECT i.indrelid::int AS oid, a.attname AS col FROM pg_index i JOIN pg_attribute a ON a.attrelid = i.indrelid AND a.attnum = ANY(i.indkey) WHERE i.indisprimary");
73
+ const keys = new Map();
74
+ for (const r of pk.rows) keys.set(r.oid, (keys.get(r.oid) || []).concat(r.col));
75
+ shape = t.rows.slice(0, MAX_TABLES).filter((r) => !SKIP_TABLE.test(r.name)).map((r) => ({ name: r.name, pk: keys.get(r.oid) || [] }));
76
+ return shape;
77
+ };
78
+ const quote = (name) => '"' + String(name).replace(/"/g, '""') + '"';
79
+ return {
80
+ ok: true,
81
+ from: found.from,
82
+ /** Every table's rows by primary key: { tables: { name: { count, big, rows: Map(key -> row) } } } or null when a read failed. */
83
+ async snapshot() {
84
+ try {
85
+ const out = { at: Date.now(), tables: {} };
86
+ await client.query('BEGIN TRANSACTION READ ONLY');
87
+ try {
88
+ for (const t of await shapeOf()) {
89
+ const r = await client.query('SELECT * FROM public.' + quote(t.name) + ' LIMIT ' + (MAX_ROWS + 1));
90
+ if (r.rows.length > MAX_ROWS) {
91
+ const n = await client.query('SELECT count(*)::int AS n FROM public.' + quote(t.name));
92
+ out.tables[t.name] = { count: n.rows[0].n, big: true, rows: new Map() };
93
+ continue;
94
+ }
95
+ const rows = new Map();
96
+ for (const row of r.rows) {
97
+ const o = {};
98
+ for (const k of Object.keys(row)) o[k] = plain(row[k]);
99
+ rows.set(t.pk.length ? t.pk.map((c) => String(o[c])).join('|') : JSON.stringify(o), o);
100
+ }
101
+ out.tables[t.name] = { count: rows.size, big: false, pk: t.pk, rows };
102
+ }
103
+ } finally {
104
+ await client.query('COMMIT');
105
+ }
106
+ return out;
107
+ } catch (err) {
108
+ return null;
109
+ }
110
+ },
111
+ async close() { try { await client.end(); } catch (err) { /* closed already */ } },
112
+ };
113
+ }
114
+
115
+ /**
116
+ * What changed between two snapshots: { table: { added: [row], removed: n, changed: [{ key, row, cols: [{ col, from, to }] }] } }
117
+ * - only tables that changed; a table too big to read gives { grew: n }. Empty when either snapshot is missing.
118
+ */
119
+ function diff(before, after) {
120
+ const out = {};
121
+ if (!before || !after) return out;
122
+ for (const name of Object.keys(after.tables)) {
123
+ const a = before.tables[name];
124
+ const b = after.tables[name];
125
+ if (!a) continue;
126
+ if (a.big || b.big) { if (b.count !== a.count) out[name] = { added: [], removed: 0, changed: [], grew: b.count - a.count }; continue; }
127
+ const added = [];
128
+ const changed = [];
129
+ for (const [key, row] of b.rows) {
130
+ const old = a.rows.get(key);
131
+ if (!old) { added.push(row); continue; }
132
+ const cols = [];
133
+ for (const k of Object.keys(row)) if (JSON.stringify(row[k]) !== JSON.stringify(old[k])) cols.push({ col: k, from: old[k], to: row[k] });
134
+ if (cols.length) changed.push({ key, row, old, cols });
135
+ }
136
+ let removed = 0;
137
+ for (const key of a.rows.keys()) if (!b.rows.has(key)) removed++;
138
+ if (added.length || changed.length || removed) out[name] = { added, removed, changed };
139
+ }
140
+ return out;
141
+ }
142
+
143
+ /** A value as it may be shown: a secret column's is never shown, a long one is clipped. */
144
+ function show(col, value) {
145
+ if (SECRET_COLUMN.test(col)) return '(not shown)';
146
+ const s = typeof value === 'string' ? value : JSON.stringify(value);
147
+ return s === undefined ? 'null' : s.length > 60 ? s.slice(0, 57) + '...' : s;
148
+ }
149
+
150
+ module.exports = { open, urlFor, diff, show, NAMES, MAX_ROWS, SECRET_COLUMN, SKIP_TABLE };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kryptheon",
3
- "version": "0.1.67",
3
+ "version": "0.1.68",
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",
@@ -60,6 +60,7 @@
60
60
  "kryptheon-api.js",
61
61
  "kryptheon-webhook.js",
62
62
  "kryptheon-csv.js",
63
+ "kryptheon-witness.js",
63
64
  "kryptheon-newer.js"
64
65
  ],
65
66
  "engines": {
@@ -70,7 +71,7 @@
70
71
  "pg": "^8.23.0"
71
72
  },
72
73
  "scripts": {
73
- "check": "node kryptheon-store.check.js && node kryptheon-code.check.js && node kryptheon-constdata.check.js && node kryptheon-listitems.check.js && node kryptheon-accept.check.js && node kryptheon-keys.check.js && node kryptheon-dashboard.check.js && node kryptheon-edge.check.js && node kryptheon-reliability.check.js && node kryptheon-fresh.check.js && node kryptheon-payments-dash.check.js && node kryptheon-embedded.check.js && node kryptheon-autostart.check.js && node kryptheon-changes.check.js && node kryptheon-look.check.js && node kryptheon-verify.check.js && node kryptheon-recheck.check.js && node kryptheon-reporter.check.js && node kryptheon-signature.check.js && node kryptheon-signature.stream.check.js && node kryptheon-signature.baseline.check.js && node kryptheon-signature.settle.check.js && node kryptheon-signature.number.check.js && node kryptheon-signature.rows.check.js && node kryptheon-env.check.js && node kryptheon-watchdog.check.js && node kryptheon-phone.check.js && node kryptheon-homedash.check.js && node kryptheon-record.check.js && node kryptheon-selectors.check.js && node kryptheon-recordings.check.js && node kryptheon-testfiles.check.js && node kryptheon-cleanup.check.js && node kryptheon-orphan.check.js && node kryptheon-project.check.js && node kryptheon-setup.check.js && node kryptheon-browser.check.js && node kryptheon-heal.check.js && node kryptheon-load.check.js && node kryptheon-authz.check.js && node kryptheon-roles.check.js && node kryptheon-map.check.js && node kryptheon-api.check.js && node kryptheon-webhook.check.js && node kryptheon-csv.check.js && node kryptheon-package.check.js && node kryptheon-newer.check.js",
74
+ "check": "node kryptheon-store.check.js && node kryptheon-code.check.js && node kryptheon-constdata.check.js && node kryptheon-listitems.check.js && node kryptheon-accept.check.js && node kryptheon-keys.check.js && node kryptheon-dashboard.check.js && node kryptheon-edge.check.js && node kryptheon-reliability.check.js && node kryptheon-fresh.check.js && node kryptheon-payments-dash.check.js && node kryptheon-embedded.check.js && node kryptheon-autostart.check.js && node kryptheon-changes.check.js && node kryptheon-look.check.js && node kryptheon-verify.check.js && node kryptheon-recheck.check.js && node kryptheon-reporter.check.js && node kryptheon-signature.check.js && node kryptheon-signature.stream.check.js && node kryptheon-signature.baseline.check.js && node kryptheon-signature.settle.check.js && node kryptheon-signature.number.check.js && node kryptheon-signature.rows.check.js && node kryptheon-env.check.js && node kryptheon-watchdog.check.js && node kryptheon-phone.check.js && node kryptheon-homedash.check.js && node kryptheon-record.check.js && node kryptheon-selectors.check.js && node kryptheon-recordings.check.js && node kryptheon-testfiles.check.js && node kryptheon-cleanup.check.js && node kryptheon-orphan.check.js && node kryptheon-project.check.js && node kryptheon-setup.check.js && node kryptheon-browser.check.js && node kryptheon-heal.check.js && node kryptheon-load.check.js && node kryptheon-authz.check.js && node kryptheon-roles.check.js && node kryptheon-map.check.js && node kryptheon-api.check.js && node kryptheon-webhook.check.js && node kryptheon-csv.check.js && node kryptheon-witness.check.js && node kryptheon-round34.check.js && node kryptheon-package.check.js && node kryptheon-newer.check.js",
74
75
  "check:store": "node kryptheon-store.check.js",
75
76
  "check:code": "node kryptheon-code.check.js",
76
77
  "check:constdata": "node kryptheon-constdata.check.js",
@@ -116,6 +117,8 @@
116
117
  "check:api": "node kryptheon-api.check.js",
117
118
  "check:webhook": "node kryptheon-webhook.check.js",
118
119
  "check:csv": "node kryptheon-csv.check.js",
120
+ "check:witness": "node kryptheon-witness.check.js",
121
+ "check:round34": "node kryptheon-round34.check.js",
119
122
  "check:newer": "node kryptheon-newer.check.js",
120
123
  "check:account": "node kryptheon-account.check.js"
121
124
  },