kryptheon 0.1.70 → 0.1.72

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
@@ -27,6 +27,7 @@ const authz = require('./kryptheon-authz.js');
27
27
  const webhook = require('./kryptheon-webhook.js');
28
28
  const csv = require('./kryptheon-csv.js');
29
29
  const witnessDb = require('./kryptheon-witness.js');
30
+ const aiapp = require('./kryptheon-aiapp.js');
30
31
 
31
32
  const EMAIL = /[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/g;
32
33
  const SIGN_IN = /(^|\/)(log-?in|sign-?in|session|auth\/(login|signin|token))$/i;
@@ -419,7 +420,7 @@ async function runWall(root, options) {
419
420
  say('Signing in as ' + j.accounts.usable.length + ' accounts, and trying ' + j.actions.length + ' routes across organizations and without signing in...');
420
421
  const result = await roles.checkAccess({
421
422
  origin: origin, policy: wallPolicy(j), journey: { signIn: j.signIn, actions: j.actions }, accounts: j.accounts.usable,
422
- fetch: opts.fetch, say: say, stopped: opts.stopped, maxAttempts: opts.maxAttempts, anonymous: true, seed: true,
423
+ fetch: opts.fetch, say: say, stopped: opts.stopped, maxAttempts: opts.maxAttempts, anonymous: true, seed: true, dbTargets: dbTargetsFor(plan, opts.witness),
423
424
  only: (p) => p.kind !== 'role',
424
425
  });
425
426
  // Said without the word "policy": the wall check runs with no permissions file.
@@ -495,10 +496,12 @@ function findingsOf(attempts, journey) {
495
496
  const unguarded = action.mapAuth === map.STATUS.NOT;
496
497
  const f = {
497
498
  kind: first.kind, where: where, from: action.from ? action.from.file + ':' + action.from.line : null,
498
- severity: first.kind === 'tenant' ? 'CRITICAL' : unguarded ? 'MEDIUM' : 'HIGH',
499
- status: first.kind === 'signin' && unguarded ? 'verification required' : 'confirmed',
499
+ severity: first.kind === 'tenant' && first.perUser ? 'HIGH' : first.kind === 'tenant' ? 'CRITICAL' : unguarded ? 'MEDIUM' : 'HIGH',
500
+ status: (first.kind === 'signin' && unguarded) || (first.kind === 'tenant' && first.perUser) ? 'verification required' : 'confirmed',
500
501
  headline: first.kind === 'tenant'
501
- ? 'A user of one organization can reach another organization\'s record: ' + where + ' answered for a record of a different organization.'
502
+ ? (first.perUser
503
+ ? 'A signed-in user can open another user\'s record: ' + where + ' answered for a record that belongs to someone else - to check: fine only if it is meant to be seen by others (a public profile, a shared post).'
504
+ : 'A user of one organization can reach another organization\'s record: ' + where + ' answered for a record of a different organization.')
502
505
  : unguarded
503
506
  ? 'Anyone can use ' + where + ' without signing in, and the code has no sign-in check on it - to verify whether it is meant to be public.'
504
507
  : 'Anyone can use ' + where + ' without signing in, though the code looks like it checks for a signed-in user.',
@@ -686,7 +689,7 @@ async function runRoles(root, options) {
686
689
  const withInferred = inferPermissions(plan, plan.policy.policy);
687
690
  const result = await roles.checkAccess({
688
691
  origin: origin, policy: withInferred.policy, journey: { signIn: j.signIn, actions: j.actions }, accounts: j.accounts.usable,
689
- fetch: opts.fetch, say: say, stopped: opts.stopped, maxAttempts: opts.maxAttempts, anonymous: true, seed: true, tryUnruled: true,
692
+ fetch: opts.fetch, say: say, stopped: opts.stopped, maxAttempts: opts.maxAttempts, anonymous: true, seed: true, tryUnruled: true, dbTargets: dbTargetsFor(plan, opts.witness),
690
693
  });
691
694
  roles.markHints(result.attempts);
692
695
  const findings = roles.findingsOf(result.attempts.filter((a) => a.kind !== 'signin'), withInferred.policy).concat(findingsOf(result.attempts.filter((a) => a.kind === 'signin'), j));
@@ -2410,6 +2413,14 @@ function dbDoubleEffects(d, rec, listTemplate, stepTemplate) {
2410
2413
  }
2411
2414
  }
2412
2415
  for (const ch of t.changed) {
2416
+ // a stock or balance that was not below zero and is now (blind round 35: inventory consumed twice at once)
2417
+ for (const col of Object.keys(ch.row)) {
2418
+ const was = Number(ch.old[col]);
2419
+ const now = Number(ch.row[col]);
2420
+ if (typeof ch.row[col] === 'boolean' || ch.row[col] === null || ch.old[col] === null || !Number.isFinite(was) || !Number.isFinite(now)) continue;
2421
+ if (was < 0 || now >= 0 || ROW_IDENTITY.test(col) || /(^|_)(id|delta|change|diff|adjust\w*)$/i.test(col)) continue;
2422
+ if (!out.some((x) => x.kind === 'below-zero')) out.push({ kind: 'below-zero', sure: true, text: 'the database table ' + table + ' row ' + ch.key + ': ' + col + ' went ' + was + ' -> ' + now + ', below zero' });
2423
+ }
2413
2424
  for (const [lim, c] of limitPairsIn(Object.keys(ch.row))) {
2414
2425
  const L = Number(ch.row[lim]);
2415
2426
  const C = Number(ch.row[c]);
@@ -2554,6 +2565,88 @@ async function oneLeft(s, c, j, recs, taken, out) {
2554
2565
  * taken with one effect, = held. Nothing is deleted; every record sent is listed as left. Round 33.
2555
2566
  * { ran, why, tried, findings, left, stoppedAt }.
2556
2567
  */
2568
+ /** The table a step on one record changes: the one named like its list (credits for /credits/{id}/redeem), else its first UPDATE. */
2569
+ function stepTableOf(route, listTemplate) {
2570
+ const last = String(listTemplate).split('/').filter(Boolean).pop() || '';
2571
+ const flat = (x) => String(x).toLowerCase().replace(/[^a-z0-9]/g, '');
2572
+ const names = new Set([flat(last), flat(last).replace(/s$/, ''), flat(last).replace(/ies$/, 'y')]);
2573
+ const db = route.db || [];
2574
+ const named = db.find((d) => d.table && names.has(flat(d.table)) && /^(UPDATE|UPSERT|SELECT)$/.test(d.op));
2575
+ const updated = db.find((d) => d.table && (d.op === 'UPDATE' || d.op === 'UPSERT'));
2576
+ return (named || updated || {}).table || null;
2577
+ }
2578
+
2579
+ /**
2580
+ * Records for a step that has no list, from the app's own rows (the read-only witness): the rows of its table, with
2581
+ * the signed-in person's organization first when a table of people names their email and an organization column.
2582
+ * Never a whole row is shown - only ids reach the report. [] when the table cannot be read.
2583
+ */
2584
+ function dbRecordsFor(snapshot, table, email) {
2585
+ const t = snapshot && snapshot.tables && snapshot.tables[table];
2586
+ if (!t || t.big || !t.rows || !t.pk || t.pk.length !== 1) return [];
2587
+ const pk = t.pk[0];
2588
+ const rows = [...t.rows.values()].map((r) => Object.assign({}, r, { id: r[pk] }));
2589
+ if (!rows.length) return [];
2590
+ const orgCol = Object.keys(rows[0]).find((k) => ORG_COLUMN.test(k));
2591
+ if (!orgCol) return rows;
2592
+ // whose organization is this account's: a row of any table that holds its email and the same organization column
2593
+ let mine;
2594
+ const wanted = String(email || '').toLowerCase();
2595
+ if (wanted.includes('@')) {
2596
+ for (const name of Object.keys(snapshot.tables)) {
2597
+ const other = snapshot.tables[name];
2598
+ if (!other || other.big || !other.rows) continue;
2599
+ for (const r of other.rows.values()) {
2600
+ const ek = Object.keys(r).find((k) => /^e?mail$|^email_?address$/i.test(k));
2601
+ if (ek && String(r[ek]).toLowerCase() === wanted && r[orgCol] !== undefined && r[orgCol] !== null) { mine = r[orgCol]; break; }
2602
+ }
2603
+ if (mine !== undefined) break;
2604
+ }
2605
+ }
2606
+ if (mine !== undefined) return rows.filter((r) => String(r[orgCol]) === String(mine));
2607
+ // not known: one record per organization, so one of them is this account's
2608
+ const seen = new Set();
2609
+ return rows.filter((r) => { const k = String(r[orgCol]); if (seen.has(k)) return false; seen.add(k); return true; });
2610
+ }
2611
+
2612
+ /**
2613
+ * For the wall between organizations: the records of one organization a route's address can be aimed at, read from
2614
+ * the app's own database (the read-only witness) when the app has no list of them. Rows of the route's table whose
2615
+ * organization column holds that organization; secret-looking columns are left out. Null when nothing fits.
2616
+ */
2617
+ // Columns that say which person owns a row, in an app with no organizations.
2618
+ const OWNER_COLUMN = /^(user|owner|author|creator|student|member|profile)_?id$|^(created|owned)_?by$/i;
2619
+
2620
+ function dbTargetsFor(plan, witness) {
2621
+ if (!witness || !witness.ok || !plan || !plan.map) return null;
2622
+ const cache = new Map();
2623
+ return async (action, listing, orgKey, perUser) => {
2624
+ if (orgKey === null || orgKey === undefined) return null;
2625
+ const route = (plan.map.routes || []).find((r) => !r.unmounted && r.method === action.method && normRoute(r.path || '') === action.template);
2626
+ const table = route && stepTableOf(route, listing);
2627
+ if (!table) return null;
2628
+ const k = table + '|' + orgKey + (perUser ? '|user' : '');
2629
+ if (cache.has(k)) return cache.get(k);
2630
+ const snap = await witness.snapshot();
2631
+ const t = snap && snap.tables && snap.tables[table];
2632
+ let out = null;
2633
+ if (t && !t.big && t.pk && t.pk.length === 1 && t.rows && t.rows.size) {
2634
+ const rows = [...t.rows.values()];
2635
+ const orgCol = Object.keys(rows[0]).find((c) => (perUser ? OWNER_COLUMN : ORG_COLUMN).test(c));
2636
+ if (orgCol) {
2637
+ out = rows.filter((r) => String(r[orgCol]) === String(orgKey)).slice(0, 5).map((r) => {
2638
+ const item = {};
2639
+ for (const c of Object.keys(r)) if (!witnessDb.SECRET_COLUMN.test(c)) item[c] = r[c];
2640
+ return { value: r[t.pk[0]], item: item, listing: listing, field: 'id', from: 'the ' + table + ' table' };
2641
+ });
2642
+ }
2643
+ }
2644
+ out = out && out.length ? out : null;
2645
+ cache.set(k, out);
2646
+ return out;
2647
+ };
2648
+ }
2649
+
2557
2650
  async function runStepsAtOnce(root, options) {
2558
2651
  const opts = options || {};
2559
2652
  const plan = planOf(root, opts);
@@ -2579,9 +2672,13 @@ async function runStepsAtOnce(root, options) {
2579
2672
  const route = (plan.map.routes || []).find((r) => !r.unmounted && r.method === a.method && normRoute(r.path || '') === a.template);
2580
2673
  const effect = route && stepEffectOf(root, route);
2581
2674
  const listTemplate = '/' + a.segs.slice(0, a.params[0].index).join('/');
2582
- if (effect && lists.includes(listTemplate)) candidates.push({ a, effect, listTemplate, route });
2675
+ if (!effect) continue;
2676
+ if (lists.includes(listTemplate)) candidates.push({ a, effect, listTemplate, route });
2677
+ // no list of these records: aimed from the app's own rows when its database can be read (blind round 35:
2678
+ // credits, payouts and inventory had no GET list, so redeem, release and consume were never sent twice)
2679
+ else if (opts.witness && opts.witness.ok && stepTableOf(route, listTemplate)) candidates.push({ a, effect, listTemplate, route, table: stepTableOf(route, listTemplate) });
2583
2680
  }
2584
- if (!candidates.length) return { ran: false, why: 'the map found no step on one record (POST /things/{id}/verb) that changes a status, money or a count, with a list of those records to aim it at' };
2681
+ if (!candidates.length) return { ran: false, why: 'the map found no step on one record (POST /things/{id}/verb) that changes a status, money or a count, with a list of those records - or a readable database table - to aim it at' };
2585
2682
  let origin;
2586
2683
  try {
2587
2684
  origin = authz.assertLocalTarget(opts.origin);
@@ -2608,6 +2705,10 @@ async function runStepsAtOnce(root, options) {
2608
2705
  };
2609
2706
  const taken = (res) => { const r = authz.replyOf(res); return r.kind === 'accepted' || r.kind === 'redirect'; };
2610
2707
  const witness = opts.witness && opts.witness.ok ? opts.witness : null;
2708
+ // Two reads at once just before a same-moment pair, so both of its requests go out on connections already open:
2709
+ // the first pair otherwise loses the race to its own connection set-up (blind rounds 33 and 34 - one taken, one
2710
+ // refused, every time). A read changes nothing.
2711
+ const warm = (sess) => Promise.all([sess.send('GET', lists[0] || '/', {}), sess.send('GET', lists[0] || '/', {})]).catch(() => null);
2611
2712
  // What one step costs here: per account a list and a read of every list, two sends and another read (up to three
2612
2713
  // records when one is refused); and the same again for two records. Sized to the app, never past 4000 (the limit
2613
2714
  // of the whole run is kept: ctx.limit only goes up).
@@ -2641,21 +2742,35 @@ async function runStepsAtOnce(root, options) {
2641
2742
  const shared = c.effect.moves.some((m) => m.sign === '-' || (m.col && isCounter(m.col)));
2642
2743
  const sets = (route.db || []).find((d) => d.setsState);
2643
2744
  const target = sets ? sets.setsState.value : null;
2644
- const bodyOf = () => (a.encoding === 'form' ? { form: a.fullData || {} } : { json: a.fullData || {} });
2745
+ // Aimed from the table, a step that takes a number down (on_hand = on_hand - $1) is sent for the row with the
2746
+ // least left, asking for all of it: each request alone fits, two at once do not.
2747
+ const down = c.table ? c.effect.moves.find((m) => m.sign === '-' && m.col) : null;
2748
+ const qtyKey = down ? Object.keys(a.fullData || {}).find((k) => CLAIM_QTY.test(k)) : null;
2749
+ let ask = null;
2750
+ const bodyOf = () => {
2751
+ const data = ask === null ? a.fullData || {} : Object.assign({}, a.fullData, { [qtyKey]: ask });
2752
+ return a.encoding === 'form' ? { form: data } : { json: data };
2753
+ };
2645
2754
  const atOf = (rec) => a.template.replace('{id}', encodeURIComponent(String(rec.id)));
2646
2755
  const open = (recs, statusKey) => recs.filter((x) => x && x.id !== undefined && !(statusKey && (ENDED_STATUS.test(String(x[statusKey])) || (target && String(x[statusKey]) === target))));
2647
2756
  for (const s of sessions) {
2648
- const recs = rowsOfReply(await s.send('GET', c.listTemplate, {}));
2757
+ const recs = c.table ? dbRecordsFor(await witness.snapshot(), c.table, s.email) : rowsOfReply(await s.send('GET', c.listTemplate, {}));
2649
2758
  const usable = (recs || []).filter((x) => x && typeof x === 'object' && x.id !== undefined);
2650
- if (!usable.length) { t.verdict = 'no record at GET ' + c.listTemplate + ' to aim it at'; continue; }
2759
+ if (c.table) t.from = 'the ' + c.table + ' table (no list of these records)';
2760
+ if (!usable.length) { t.verdict = c.table ? 'no row in the ' + c.table + ' table to aim it at' : 'no record at GET ' + c.listTemplate + ' to aim it at'; continue; }
2651
2761
  const statusKey = Object.keys(usable[0]).find((k) => /^(status|state|stage)$/i.test(k));
2652
2762
  // Records the seed made before ones Kryptheon's other checks made (blind round 34: the first promo in the
2653
2763
  // list was one made with a bad limit, both redeems were refused 409, and the step was called held), and
2654
2764
  // for a count that runs up to a limit, a record with exactly one use left - made, when there is none.
2655
2765
  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)));
2766
+ let fresh = open(usable, statusKey).sort((x, y) => Number(toolMade(x)) - Number(toolMade(y)));
2767
+ if (down && qtyKey) {
2768
+ const left = (x) => Number(x[down.col]);
2769
+ const withSome = fresh.filter((x) => Number.isFinite(left(x)) && left(x) > 0).sort((x, y) => left(x) - left(y));
2770
+ if (withSome.length) fresh = withSome;
2771
+ }
2657
2772
  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);
2773
+ const pool = (last ? [last] : []).concat(fresh.length ? fresh : usable).slice(0, c.table ? 6 : 3);
2659
2774
  let rec = pool[0];
2660
2775
  let at = atOf(rec);
2661
2776
  let before = null;
@@ -2665,8 +2780,10 @@ async function runStepsAtOnce(root, options) {
2665
2780
  for (const cand of pool) {
2666
2781
  rec = cand;
2667
2782
  at = atOf(rec);
2783
+ if (down && qtyKey && Number(rec[down.col]) > 0) ask = Number(rec[down.col]);
2668
2784
  before = await snap(s);
2669
2785
  dbBefore = witness ? await witness.snapshot() : null;
2786
+ await warm(s);
2670
2787
  [r1, r2] = await Promise.all([s.send(a.method, at, bodyOf()), s.send(a.method, at, bodyOf())]);
2671
2788
  if (taken(r1) || taken(r2)) break;
2672
2789
  // not this account's to change: the next account may be; any other refusal may be about this record
@@ -2700,6 +2817,7 @@ async function runStepsAtOnce(root, options) {
2700
2817
  const tc = { what, mode: 'compete', claimed: scarce.label, verdict: 'not tried' };
2701
2818
  out.tried.push(tc);
2702
2819
  const was = await snap(s);
2820
+ await warm(s);
2703
2821
  const [q1, q2] = await Promise.all(scarce.ids.map((id) => s.send(a.method, atOf({ id }), bodyOf())));
2704
2822
  tc.records = scarce.ids;
2705
2823
  tc.answers = [q1.status, q2.status];
@@ -2735,6 +2853,7 @@ async function runStepsAtOnce(root, options) {
2735
2853
  }
2736
2854
  }
2737
2855
  const was = await snap(s);
2856
+ await warm(s);
2738
2857
  const [q1, q2] = await Promise.all(pair.map((x) => s.send(a.method, atOf(x), bodyOf())));
2739
2858
  tc.records = pair.map((x) => x.id);
2740
2859
  tc.answers = [q1.status, q2.status];
@@ -3043,6 +3162,10 @@ function verdictsFor(toCheck, wall, invalid, roleRun, forged, webhooks, steps, r
3043
3162
  v.verdict = 'not tried';
3044
3163
  v.why = (hook && hook.notTried ? hook.notTried : webhooks && !webhooks.ran ? webhooks.why : 'a webhook this run did not send to') + '; a provider\'s own webhook is the payments check\'s (npx kryptheon payments)';
3045
3164
  }
3165
+ } else if (/^ai-/.test(t.kind)) {
3166
+ // a route that asks an AI is never sent to: each answer costs the owner money, and some send real email
3167
+ v.verdict = 'not tried';
3168
+ v.why = 'read from the code only: Kryptheon never sends a request to a route that asks an AI - each answer costs you money, and some send real messages';
3046
3169
  } else if (kind === 'role') {
3047
3170
  // "held" only when the roles run really tried this route as a role the policy forbids
3048
3171
  // a try of a change no rule names (as the fewest permissions) is never "held" nor "confirmed": no rule says who may
@@ -3182,6 +3305,118 @@ async function runForged(root, options) {
3182
3305
  return out;
3183
3306
  }
3184
3307
 
3308
+ // --- what the app sends to the browser ------------------------------------------------
3309
+
3310
+ /**
3311
+ * Every page and list the app answers with no record in its address (and each one-record page, at a record the list
3312
+ * gave), read as each test account and with no session - GET only, nothing written - and looked through for:
3313
+ * a key or stored token that is only ever the server's (an AI provider's key, a Gmail refresh token), and a quiz sent
3314
+ * with its right answers. A key the same for two people, or sent to nobody signed in, is confirmed; one sent only to
3315
+ * the person it may belong to is a thing to check. No value is ever shown or saved: its kind and length only.
3316
+ * { ran, why, tried, findings }
3317
+ */
3318
+ async function runAnswers(root, options) {
3319
+ const opts = options || {};
3320
+ const plan = planOf(root, opts);
3321
+ if (!plan.ran) return { ran: false, why: plan.why };
3322
+ const j = plan.journey;
3323
+ let origin;
3324
+ try {
3325
+ origin = authz.assertLocalTarget(opts.origin, null, { readOnly: opts.readOnly === true });
3326
+ } catch (err) {
3327
+ return { ran: false, why: 'the app must be on this computer (localhost or 127.0.0.1): ' + (opts.origin || 'no address was given') };
3328
+ }
3329
+ const signInPath = j.signIn ? normRoute(j.signIn.path || '') : null;
3330
+ const gets = [];
3331
+ for (const r of plan.map.routes || []) {
3332
+ if (r.unmounted || !r.path || r.method !== 'GET' || r.kind === 'webhook') continue;
3333
+ const template = normRoute(r.path);
3334
+ const params = (template.match(/\{[^}]+\}/g) || []).length;
3335
+ if (params > 1 || /\*/.test(template) || template === signInPath || /log-?out|sign-?out/i.test(template) || PLUMBING.test(template) || gets.some((g) => g.template === template)) continue;
3336
+ gets.push({ template, params });
3337
+ }
3338
+ gets.splice(30);
3339
+ if (!gets.length) return { ran: false, why: 'the map found no page or list to read' };
3340
+ const accounts = j.signIn ? j.accounts.usable.slice(0, 2) : [];
3341
+ const ctx = authz.makeContext({ baseUrl: origin, allowOrigins: [origin], fetch: opts.fetch, passwords: Object.fromEntries(accounts.map((a) => [a.email, a.password])),
3342
+ maxRequests: 10 + (accounts.length + 1) * gets.length * 2, readOnly: opts.readOnly === true,
3343
+ allowWrites: j.signIn && j.signIn.path && !j.signIn.bearer ? [String(j.signIn.method || 'POST').toUpperCase() + ' ' + j.signIn.path] : [] });
3344
+ const readers = [{ who: 'nobody signed in', session: authz.makeSession(ctx, 'nobody', 'nobody', '') }];
3345
+ for (const a of accounts) {
3346
+ const s = authz.makeSession(ctx, 'account', a.email, a.password);
3347
+ const f = { [j.signIn.emailField]: a.email, [j.signIn.passwordField]: a.password };
3348
+ const r = authz.replyOf(await s.send(j.signIn.method, j.signIn.path, j.signIn.encoding === 'form' ? { form: f } : { json: f }));
3349
+ if (r.kind !== 'refused' && r.kind !== 'server-error' && r.kind !== 'no-answer') readers.push({ who: a.email, session: s, account: true });
3350
+ }
3351
+ const listIn = (json) => (Array.isArray(json) ? json : json && typeof json === 'object' ? Object.values(json).find((v) => Array.isArray(v)) || null : null);
3352
+ const out = { ran: true, tried: [], findings: [] };
3353
+ const seenKeys = new Map(); // value -> [{ who, at, field, path, label, account }]
3354
+ const quiz = new Map(); // route -> { field, who }
3355
+ for (const rd of readers) {
3356
+ const lists = new Map();
3357
+ for (const a of gets) {
3358
+ let at = a.template;
3359
+ if (a.params) {
3360
+ const listing = a.template.slice(0, a.template.indexOf('/{'));
3361
+ const items = lists.get(listing);
3362
+ const first = items && items.find((it) => it && typeof it === 'object' && (it.id !== undefined || it.uuid !== undefined || it.slug !== undefined));
3363
+ if (!first) continue;
3364
+ at = a.template.replace(/\{[^}]+\}/, encodeURIComponent(String(first.id !== undefined ? first.id : first.uuid !== undefined ? first.uuid : first.slug)));
3365
+ }
3366
+ let res;
3367
+ try { res = await rd.session.send('GET', at, {}); } catch (err) { if (authz.isRequestLimit(err)) break; throw err; }
3368
+ out.tried.push({ what: 'GET ' + a.template, as: rd.who, status: res.status });
3369
+ if (res.status !== 200 || res.json === undefined) continue;
3370
+ if (!a.params) { const l = listIn(res.json); if (l) lists.set(a.template, l); }
3371
+ const found = aiapp.scanAnswer(res.json);
3372
+ for (const s of found.secrets) {
3373
+ // a person's own token where signing in or connecting an account is the point is not a leak by itself
3374
+ if (!/^an? (Anthropic|OpenAI|Google API|Groq|Replicate|Hugging|OpenRouter|xAI|Stripe|Resend|SendGrid|Slack|GitHub)/.test(s.label) && aiapp.TOKEN_MEANT.test(a.template)) continue;
3375
+ if (!seenKeys.has(s.value)) seenKeys.set(s.value, []);
3376
+ seenKeys.get(s.value).push({ who: rd.who, account: !!rd.account, at: 'GET ' + a.template, field: s.field, path: s.path, label: s.label, length: s.value.length });
3377
+ }
3378
+ if (found.quiz.length && !aiapp.ANSWERS_MEANT.test(a.template) && !quiz.has('GET ' + a.template)) quiz.set('GET ' + a.template, { field: found.quiz[0].field, path: found.quiz[0].path, who: rd.who, count: found.quiz.length });
3379
+ }
3380
+ }
3381
+ const done = new Set();
3382
+ for (const sightings of seenKeys.values()) {
3383
+ const first = sightings[0];
3384
+ if (done.has(first.at + '|' + first.field)) continue;
3385
+ done.add(first.at + '|' + first.field);
3386
+ const people = Array.from(new Set(sightings.map((x) => x.who)));
3387
+ const shared = people.length > 1 || people.includes('nobody signed in');
3388
+ const f = {
3389
+ kind: 'secret-in-answer', where: first.at, field: first.field || null, status: shared ? 'confirmed' : 'verification required', severity: shared ? 'HIGH' : 'MEDIUM',
3390
+ headline: shared
3391
+ ? first.at + ' sends ' + first.label + ' to the browser of ' + (people.includes('nobody signed in') ? 'anyone, signed in or not' : 'more than one person') + ': whoever has it can use it as if they were your server'
3392
+ : first.at + ' sends ' + first.label + ' to the browser - to check: even shown only to its owner, anything running in that page can take it; the server should keep it and send at most its last 4 characters',
3393
+ proof: sightings.slice(0, 3).map((x) => 'As ' + x.who + ': ' + x.at + ' answered with ' + x.label + ' in ' + x.path + ' (' + x.length + ' characters, not shown).'),
3394
+ };
3395
+ f.fixPrompt = ['Kryptheon read what my running app sends to the browser:', '', f.headline + '.', '', f.proof.join('\n'), '',
3396
+ 'Keep this value on the server only: remove it from that answer (send at most its last 4 characters, to show which one is set). If it is an API key that has been sent like this, make a new one at the provider and delete the old one. Change nothing else.'].join('\n');
3397
+ out.findings.push(f);
3398
+ }
3399
+ for (const [where, q] of quiz) {
3400
+ const f = {
3401
+ kind: 'quiz-answers-sent', where, field: q.field, status: 'verification required', severity: 'MEDIUM',
3402
+ headline: where + ' sends each question\'s right answer (' + q.field + ') along with it - to check: a student can read the answers in the browser before answering; fine only after they answer, or for a teacher',
3403
+ proof: ['As ' + q.who + ': ' + where + ' answered with ' + q.count + ' question' + (q.count === 1 ? '' : 's') + ' that each carry ' + q.field + ' (values not shown).'],
3404
+ };
3405
+ f.fixPrompt = ['Kryptheon read what my running app sends to the browser:', '', f.headline + '.', '', f.proof[0], '',
3406
+ 'First check whether students see this answer. If they do, leave the right answers out of it, and check each answer on the server when it is sent; send which ones were right only in the result after answering. Change nothing else.'].join('\n');
3407
+ out.findings.push(f);
3408
+ }
3409
+ return out;
3410
+ }
3411
+
3412
+ /** Which answer finding a headline is, for a re-check that has only the headline: 'secret-in-answer', 'quiz-answers-sent' or null. */
3413
+ function answerKindOf(headline) {
3414
+ const h = String(headline || '');
3415
+ if (/^GET \S+ sends .+ to the browser/.test(h)) return 'secret-in-answer';
3416
+ if (/^GET \S+ sends each question's right answer/.test(h)) return 'quiz-answers-sent';
3417
+ return null;
3418
+ }
3419
+
3185
3420
  /**
3186
3421
  * The whole run on an app on this computer: across organizations and without
3187
3422
  * a session, then bad values, then (only with the owner's permissions file and
@@ -3211,6 +3446,8 @@ async function runApi(root, options) {
3211
3446
  }
3212
3447
  };
3213
3448
  const jobsRun = await contained('Jobs run twice', () => runJobs(root, shared));
3449
+ // read-only, and before anything below changes a record: what the app sends to the browser
3450
+ const answersRun = await contained('Keys and answers in what the app sends', () => runAnswers(root, shared));
3214
3451
  const wall = await contained('Across organizations and without a session', () => runWall(root, shared));
3215
3452
  const forged = await contained('Who is calling, as the request says', () => runForged(root, shared));
3216
3453
  const webhooks = await contained('Webhooks', () => webhook.run(root, shared));
@@ -3231,14 +3468,14 @@ async function runApi(root, options) {
3231
3468
  if (witness.ok && !opts.witness) await witness.close();
3232
3469
  const verdicts = verdictsFor(toCheck, wall, invalid, roleRun, forged, webhooks, stepsRun, redirectsRun);
3233
3470
  const seen = new Set();
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) => {
3471
+ const findings = (grantsRun.ran ? grantsRun.findings : []).concat(answersRun.ran ? answersRun.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) => {
3235
3472
  const k = f.kind + '|' + f.where + '|' + (f.field || '');
3236
3473
  return seen.has(k) ? false : (seen.add(k), true);
3237
3474
  });
3238
3475
  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) : []);
3239
3476
  return {
3240
3477
  ran: wall.ran || invalid.ran, why: wall.ran || invalid.ran ? null : wall.why, checkedAt: new Date().toISOString(),
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,
3478
+ 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, answers: answersRun, invalid: invalid, roles: roleRun, verdicts: verdicts, findings: findings, left: left,
3242
3479
  notTried: (wall.plan ? wall.plan.notTried : []).concat(invalid.ran ? invalid.notTried : []),
3243
3480
  writes: writesOf(m, wall),
3244
3481
  };
@@ -3347,6 +3584,13 @@ async function runLive(root, options) {
3347
3584
  }
3348
3585
  }
3349
3586
  const stub = { ran: false, why: 'not on a live app - it writes; run it on a test copy' };
3587
+ let answersRun;
3588
+ try {
3589
+ answersRun = await runAnswers(root, Object.assign({}, opts, { map: m, origin, readOnly: true }));
3590
+ } catch (err) {
3591
+ if (!(err && (err.code === authz.READ_ONLY || authz.isRequestLimit(err)))) throw err;
3592
+ answersRun = { ran: false, why: 'stopped: ' + err.message };
3593
+ }
3350
3594
  // Judged first, then every string kept from the tries masked - the answers, their excerpts, the lines printed from
3351
3595
  // them: a live app's records are real people's, and none of their values is shown or saved.
3352
3596
  const maskDeep = (v, depth) => {
@@ -3369,7 +3613,7 @@ async function runLive(root, options) {
3369
3613
  if (roleRun.ran) { roleRun.result = maskDeep(roleRun.result, 0); roleRun.findings = maskDeep(roleRun.findings, 0); }
3370
3614
  // the roles run keeps organizations apart too, so it finds the same reads across organizations again: said once
3371
3615
  const seenLive = new Set();
3372
- const findings = (wall.findings || []).concat(roleRun.ran ? roleRun.findings : []).filter((f) => {
3616
+ const findings = (wall.findings || []).concat(roleRun.ran ? roleRun.findings : []).concat(answersRun.ran ? answersRun.findings : []).filter((f) => {
3373
3617
  const k = f.kind + '|' + f.where + '|' + (f.field || '');
3374
3618
  return seenLive.has(k) ? false : (seenLive.add(k), true);
3375
3619
  }).map((f) => maskDeep(Object.assign({}, f, { live: true }), 0));
@@ -3377,7 +3621,7 @@ async function runLive(root, options) {
3377
3621
  return {
3378
3622
  ran: true, live: true, why: null, checkedAt: new Date().toISOString(), origin,
3379
3623
  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,
3624
+ roles: roleRun, invalid: stub, grants: stub, refs: stub, answers: answersRun,
3381
3625
  findings, left: [], leftOut: LIVE_LEFT_OUT,
3382
3626
  verdicts: verdictsFor(toCheck, wall, stub, roleRun.ran ? roleRun : null, stub, stub, stub, stub),
3383
3627
  notTried: wall.plan ? wall.plan.notTried : [],
@@ -3567,4 +3811,4 @@ function apiLines(run) {
3567
3811
  return out;
3568
3812
  }
3569
3813
 
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 };
3814
+ module.exports = { answerKindOf, runAnswers, dbTargetsFor, stepTableOf, dbRecordsFor, 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 };