kryptheon 0.1.69 → 0.1.71

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/LICENSE CHANGED
@@ -1,38 +1,38 @@
1
- Kryptheon Software License
2
-
3
- Copyright (c) 2026 Kryptheon (https://kryptheon.tech). All rights reserved.
4
-
5
- This software is proprietary. It is not open source.
6
-
7
- 1. Use. You may install and run this software, as published by Kryptheon,
8
- at no charge, to check software and databases that you own or are
9
- authorised to test. Features that kryptheon.tech offers on a paid plan
10
- are provided only under that plan and the Terms at
11
- https://kryptheon.tech/terms.
12
-
13
- 2. Restrictions. Without Kryptheon's written permission you may not:
14
- (a) copy, modify, translate or create derivative works of this software,
15
- except as needed to run it;
16
- (b) sell, sublicense, rent, publish or otherwise distribute it, or offer
17
- it, or anything derived from it, as a hosted or managed service;
18
- (c) remove, disable or work around any licence, plan or usage limit, or
19
- any part of the software that enforces them;
20
- (d) use it, or any part of it, to build a competing product.
21
-
22
- 3. No warranty. The software is provided "as is", without warranty of any
23
- kind, express or implied, including fitness for a particular purpose.
24
- It may miss problems; a check that finds nothing does not mean that
25
- nothing is wrong. You are responsible for what you test and for keeping
26
- your own backups.
27
-
28
- 4. Limitation of liability. To the extent the law allows, Kryptheon is not
29
- liable for any damages arising from the use of, or inability to use,
30
- this software.
31
-
32
- 5. Earlier versions. Versions published before this licence was added keep
33
- the licence they were published with.
34
-
35
- 6. Termination. This licence ends automatically if you break it. When it
36
- ends, you must stop using the software and delete your copies.
37
-
38
- Questions: company@kryptheon.tech
1
+ Kryptheon Software License
2
+
3
+ Copyright (c) 2026 Kryptheon (https://kryptheon.tech). All rights reserved.
4
+
5
+ This software is proprietary. It is not open source.
6
+
7
+ 1. Use. You may install and run this software, as published by Kryptheon,
8
+ at no charge, to check software and databases that you own or are
9
+ authorised to test. Features that kryptheon.tech offers on a paid plan
10
+ are provided only under that plan and the Terms at
11
+ https://kryptheon.tech/terms.
12
+
13
+ 2. Restrictions. Without Kryptheon's written permission you may not:
14
+ (a) copy, modify, translate or create derivative works of this software,
15
+ except as needed to run it;
16
+ (b) sell, sublicense, rent, publish or otherwise distribute it, or offer
17
+ it, or anything derived from it, as a hosted or managed service;
18
+ (c) remove, disable or work around any licence, plan or usage limit, or
19
+ any part of the software that enforces them;
20
+ (d) use it, or any part of it, to build a competing product.
21
+
22
+ 3. No warranty. The software is provided "as is", without warranty of any
23
+ kind, express or implied, including fitness for a particular purpose.
24
+ It may miss problems; a check that finds nothing does not mean that
25
+ nothing is wrong. You are responsible for what you test and for keeping
26
+ your own backups.
27
+
28
+ 4. Limitation of liability. To the extent the law allows, Kryptheon is not
29
+ liable for any damages arising from the use of, or inability to use,
30
+ this software.
31
+
32
+ 5. Earlier versions. Versions published before this licence was added keep
33
+ the licence they were published with.
34
+
35
+ 6. Termination. This licence ends automatically if you break it. When it
36
+ ends, you must stop using the software and delete your copies.
37
+
38
+ Questions: company@kryptheon.tech
package/kryptheon-api.js CHANGED
@@ -419,7 +419,7 @@ async function runWall(root, options) {
419
419
  say('Signing in as ' + j.accounts.usable.length + ' accounts, and trying ' + j.actions.length + ' routes across organizations and without signing in...');
420
420
  const result = await roles.checkAccess({
421
421
  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,
422
+ fetch: opts.fetch, say: say, stopped: opts.stopped, maxAttempts: opts.maxAttempts, anonymous: true, seed: true, dbTargets: dbTargetsFor(plan, opts.witness),
423
423
  only: (p) => p.kind !== 'role',
424
424
  });
425
425
  // Said without the word "policy": the wall check runs with no permissions file.
@@ -495,10 +495,12 @@ function findingsOf(attempts, journey) {
495
495
  const unguarded = action.mapAuth === map.STATUS.NOT;
496
496
  const f = {
497
497
  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',
498
+ severity: first.kind === 'tenant' && first.perUser ? 'HIGH' : first.kind === 'tenant' ? 'CRITICAL' : unguarded ? 'MEDIUM' : 'HIGH',
499
+ status: (first.kind === 'signin' && unguarded) || (first.kind === 'tenant' && first.perUser) ? 'verification required' : 'confirmed',
500
500
  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.'
501
+ ? (first.perUser
502
+ ? '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).'
503
+ : 'A user of one organization can reach another organization\'s record: ' + where + ' answered for a record of a different organization.')
502
504
  : unguarded
503
505
  ? '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
506
  : 'Anyone can use ' + where + ' without signing in, though the code looks like it checks for a signed-in user.',
@@ -686,7 +688,7 @@ async function runRoles(root, options) {
686
688
  const withInferred = inferPermissions(plan, plan.policy.policy);
687
689
  const result = await roles.checkAccess({
688
690
  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,
691
+ fetch: opts.fetch, say: say, stopped: opts.stopped, maxAttempts: opts.maxAttempts, anonymous: true, seed: true, tryUnruled: true, dbTargets: dbTargetsFor(plan, opts.witness),
690
692
  });
691
693
  roles.markHints(result.attempts);
692
694
  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 +2412,14 @@ function dbDoubleEffects(d, rec, listTemplate, stepTemplate) {
2410
2412
  }
2411
2413
  }
2412
2414
  for (const ch of t.changed) {
2415
+ // a stock or balance that was not below zero and is now (blind round 35: inventory consumed twice at once)
2416
+ for (const col of Object.keys(ch.row)) {
2417
+ const was = Number(ch.old[col]);
2418
+ const now = Number(ch.row[col]);
2419
+ if (typeof ch.row[col] === 'boolean' || ch.row[col] === null || ch.old[col] === null || !Number.isFinite(was) || !Number.isFinite(now)) continue;
2420
+ if (was < 0 || now >= 0 || ROW_IDENTITY.test(col) || /(^|_)(id|delta|change|diff|adjust\w*)$/i.test(col)) continue;
2421
+ 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' });
2422
+ }
2413
2423
  for (const [lim, c] of limitPairsIn(Object.keys(ch.row))) {
2414
2424
  const L = Number(ch.row[lim]);
2415
2425
  const C = Number(ch.row[c]);
@@ -2554,6 +2564,88 @@ async function oneLeft(s, c, j, recs, taken, out) {
2554
2564
  * taken with one effect, = held. Nothing is deleted; every record sent is listed as left. Round 33.
2555
2565
  * { ran, why, tried, findings, left, stoppedAt }.
2556
2566
  */
2567
+ /** The table a step on one record changes: the one named like its list (credits for /credits/{id}/redeem), else its first UPDATE. */
2568
+ function stepTableOf(route, listTemplate) {
2569
+ const last = String(listTemplate).split('/').filter(Boolean).pop() || '';
2570
+ const flat = (x) => String(x).toLowerCase().replace(/[^a-z0-9]/g, '');
2571
+ const names = new Set([flat(last), flat(last).replace(/s$/, ''), flat(last).replace(/ies$/, 'y')]);
2572
+ const db = route.db || [];
2573
+ const named = db.find((d) => d.table && names.has(flat(d.table)) && /^(UPDATE|UPSERT|SELECT)$/.test(d.op));
2574
+ const updated = db.find((d) => d.table && (d.op === 'UPDATE' || d.op === 'UPSERT'));
2575
+ return (named || updated || {}).table || null;
2576
+ }
2577
+
2578
+ /**
2579
+ * Records for a step that has no list, from the app's own rows (the read-only witness): the rows of its table, with
2580
+ * the signed-in person's organization first when a table of people names their email and an organization column.
2581
+ * Never a whole row is shown - only ids reach the report. [] when the table cannot be read.
2582
+ */
2583
+ function dbRecordsFor(snapshot, table, email) {
2584
+ const t = snapshot && snapshot.tables && snapshot.tables[table];
2585
+ if (!t || t.big || !t.rows || !t.pk || t.pk.length !== 1) return [];
2586
+ const pk = t.pk[0];
2587
+ const rows = [...t.rows.values()].map((r) => Object.assign({}, r, { id: r[pk] }));
2588
+ if (!rows.length) return [];
2589
+ const orgCol = Object.keys(rows[0]).find((k) => ORG_COLUMN.test(k));
2590
+ if (!orgCol) return rows;
2591
+ // whose organization is this account's: a row of any table that holds its email and the same organization column
2592
+ let mine;
2593
+ const wanted = String(email || '').toLowerCase();
2594
+ if (wanted.includes('@')) {
2595
+ for (const name of Object.keys(snapshot.tables)) {
2596
+ const other = snapshot.tables[name];
2597
+ if (!other || other.big || !other.rows) continue;
2598
+ for (const r of other.rows.values()) {
2599
+ const ek = Object.keys(r).find((k) => /^e?mail$|^email_?address$/i.test(k));
2600
+ if (ek && String(r[ek]).toLowerCase() === wanted && r[orgCol] !== undefined && r[orgCol] !== null) { mine = r[orgCol]; break; }
2601
+ }
2602
+ if (mine !== undefined) break;
2603
+ }
2604
+ }
2605
+ if (mine !== undefined) return rows.filter((r) => String(r[orgCol]) === String(mine));
2606
+ // not known: one record per organization, so one of them is this account's
2607
+ const seen = new Set();
2608
+ return rows.filter((r) => { const k = String(r[orgCol]); if (seen.has(k)) return false; seen.add(k); return true; });
2609
+ }
2610
+
2611
+ /**
2612
+ * For the wall between organizations: the records of one organization a route's address can be aimed at, read from
2613
+ * the app's own database (the read-only witness) when the app has no list of them. Rows of the route's table whose
2614
+ * organization column holds that organization; secret-looking columns are left out. Null when nothing fits.
2615
+ */
2616
+ // Columns that say which person owns a row, in an app with no organizations.
2617
+ const OWNER_COLUMN = /^(user|owner|author|creator|student|member|profile)_?id$|^(created|owned)_?by$/i;
2618
+
2619
+ function dbTargetsFor(plan, witness) {
2620
+ if (!witness || !witness.ok || !plan || !plan.map) return null;
2621
+ const cache = new Map();
2622
+ return async (action, listing, orgKey, perUser) => {
2623
+ if (orgKey === null || orgKey === undefined) return null;
2624
+ const route = (plan.map.routes || []).find((r) => !r.unmounted && r.method === action.method && normRoute(r.path || '') === action.template);
2625
+ const table = route && stepTableOf(route, listing);
2626
+ if (!table) return null;
2627
+ const k = table + '|' + orgKey + (perUser ? '|user' : '');
2628
+ if (cache.has(k)) return cache.get(k);
2629
+ const snap = await witness.snapshot();
2630
+ const t = snap && snap.tables && snap.tables[table];
2631
+ let out = null;
2632
+ if (t && !t.big && t.pk && t.pk.length === 1 && t.rows && t.rows.size) {
2633
+ const rows = [...t.rows.values()];
2634
+ const orgCol = Object.keys(rows[0]).find((c) => (perUser ? OWNER_COLUMN : ORG_COLUMN).test(c));
2635
+ if (orgCol) {
2636
+ out = rows.filter((r) => String(r[orgCol]) === String(orgKey)).slice(0, 5).map((r) => {
2637
+ const item = {};
2638
+ for (const c of Object.keys(r)) if (!witnessDb.SECRET_COLUMN.test(c)) item[c] = r[c];
2639
+ return { value: r[t.pk[0]], item: item, listing: listing, field: 'id', from: 'the ' + table + ' table' };
2640
+ });
2641
+ }
2642
+ }
2643
+ out = out && out.length ? out : null;
2644
+ cache.set(k, out);
2645
+ return out;
2646
+ };
2647
+ }
2648
+
2557
2649
  async function runStepsAtOnce(root, options) {
2558
2650
  const opts = options || {};
2559
2651
  const plan = planOf(root, opts);
@@ -2579,9 +2671,13 @@ async function runStepsAtOnce(root, options) {
2579
2671
  const route = (plan.map.routes || []).find((r) => !r.unmounted && r.method === a.method && normRoute(r.path || '') === a.template);
2580
2672
  const effect = route && stepEffectOf(root, route);
2581
2673
  const listTemplate = '/' + a.segs.slice(0, a.params[0].index).join('/');
2582
- if (effect && lists.includes(listTemplate)) candidates.push({ a, effect, listTemplate, route });
2674
+ if (!effect) continue;
2675
+ if (lists.includes(listTemplate)) candidates.push({ a, effect, listTemplate, route });
2676
+ // no list of these records: aimed from the app's own rows when its database can be read (blind round 35:
2677
+ // credits, payouts and inventory had no GET list, so redeem, release and consume were never sent twice)
2678
+ else if (opts.witness && opts.witness.ok && stepTableOf(route, listTemplate)) candidates.push({ a, effect, listTemplate, route, table: stepTableOf(route, listTemplate) });
2583
2679
  }
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' };
2680
+ 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
2681
  let origin;
2586
2682
  try {
2587
2683
  origin = authz.assertLocalTarget(opts.origin);
@@ -2608,6 +2704,10 @@ async function runStepsAtOnce(root, options) {
2608
2704
  };
2609
2705
  const taken = (res) => { const r = authz.replyOf(res); return r.kind === 'accepted' || r.kind === 'redirect'; };
2610
2706
  const witness = opts.witness && opts.witness.ok ? opts.witness : null;
2707
+ // Two reads at once just before a same-moment pair, so both of its requests go out on connections already open:
2708
+ // the first pair otherwise loses the race to its own connection set-up (blind rounds 33 and 34 - one taken, one
2709
+ // refused, every time). A read changes nothing.
2710
+ const warm = (sess) => Promise.all([sess.send('GET', lists[0] || '/', {}), sess.send('GET', lists[0] || '/', {})]).catch(() => null);
2611
2711
  // What one step costs here: per account a list and a read of every list, two sends and another read (up to three
2612
2712
  // records when one is refused); and the same again for two records. Sized to the app, never past 4000 (the limit
2613
2713
  // of the whole run is kept: ctx.limit only goes up).
@@ -2641,21 +2741,35 @@ async function runStepsAtOnce(root, options) {
2641
2741
  const shared = c.effect.moves.some((m) => m.sign === '-' || (m.col && isCounter(m.col)));
2642
2742
  const sets = (route.db || []).find((d) => d.setsState);
2643
2743
  const target = sets ? sets.setsState.value : null;
2644
- const bodyOf = () => (a.encoding === 'form' ? { form: a.fullData || {} } : { json: a.fullData || {} });
2744
+ // Aimed from the table, a step that takes a number down (on_hand = on_hand - $1) is sent for the row with the
2745
+ // least left, asking for all of it: each request alone fits, two at once do not.
2746
+ const down = c.table ? c.effect.moves.find((m) => m.sign === '-' && m.col) : null;
2747
+ const qtyKey = down ? Object.keys(a.fullData || {}).find((k) => CLAIM_QTY.test(k)) : null;
2748
+ let ask = null;
2749
+ const bodyOf = () => {
2750
+ const data = ask === null ? a.fullData || {} : Object.assign({}, a.fullData, { [qtyKey]: ask });
2751
+ return a.encoding === 'form' ? { form: data } : { json: data };
2752
+ };
2645
2753
  const atOf = (rec) => a.template.replace('{id}', encodeURIComponent(String(rec.id)));
2646
2754
  const open = (recs, statusKey) => recs.filter((x) => x && x.id !== undefined && !(statusKey && (ENDED_STATUS.test(String(x[statusKey])) || (target && String(x[statusKey]) === target))));
2647
2755
  for (const s of sessions) {
2648
- const recs = rowsOfReply(await s.send('GET', c.listTemplate, {}));
2756
+ const recs = c.table ? dbRecordsFor(await witness.snapshot(), c.table, s.email) : rowsOfReply(await s.send('GET', c.listTemplate, {}));
2649
2757
  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; }
2758
+ if (c.table) t.from = 'the ' + c.table + ' table (no list of these records)';
2759
+ 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
2760
  const statusKey = Object.keys(usable[0]).find((k) => /^(status|state|stage)$/i.test(k));
2652
2761
  // Records the seed made before ones Kryptheon's other checks made (blind round 34: the first promo in the
2653
2762
  // list was one made with a bad limit, both redeems were refused 409, and the step was called held), and
2654
2763
  // for a count that runs up to a limit, a record with exactly one use left - made, when there is none.
2655
2764
  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)));
2765
+ let fresh = open(usable, statusKey).sort((x, y) => Number(toolMade(x)) - Number(toolMade(y)));
2766
+ if (down && qtyKey) {
2767
+ const left = (x) => Number(x[down.col]);
2768
+ const withSome = fresh.filter((x) => Number.isFinite(left(x)) && left(x) > 0).sort((x, y) => left(x) - left(y));
2769
+ if (withSome.length) fresh = withSome;
2770
+ }
2657
2771
  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);
2772
+ const pool = (last ? [last] : []).concat(fresh.length ? fresh : usable).slice(0, c.table ? 6 : 3);
2659
2773
  let rec = pool[0];
2660
2774
  let at = atOf(rec);
2661
2775
  let before = null;
@@ -2665,8 +2779,10 @@ async function runStepsAtOnce(root, options) {
2665
2779
  for (const cand of pool) {
2666
2780
  rec = cand;
2667
2781
  at = atOf(rec);
2782
+ if (down && qtyKey && Number(rec[down.col]) > 0) ask = Number(rec[down.col]);
2668
2783
  before = await snap(s);
2669
2784
  dbBefore = witness ? await witness.snapshot() : null;
2785
+ await warm(s);
2670
2786
  [r1, r2] = await Promise.all([s.send(a.method, at, bodyOf()), s.send(a.method, at, bodyOf())]);
2671
2787
  if (taken(r1) || taken(r2)) break;
2672
2788
  // not this account's to change: the next account may be; any other refusal may be about this record
@@ -2700,6 +2816,7 @@ async function runStepsAtOnce(root, options) {
2700
2816
  const tc = { what, mode: 'compete', claimed: scarce.label, verdict: 'not tried' };
2701
2817
  out.tried.push(tc);
2702
2818
  const was = await snap(s);
2819
+ await warm(s);
2703
2820
  const [q1, q2] = await Promise.all(scarce.ids.map((id) => s.send(a.method, atOf({ id }), bodyOf())));
2704
2821
  tc.records = scarce.ids;
2705
2822
  tc.answers = [q1.status, q2.status];
@@ -2735,6 +2852,7 @@ async function runStepsAtOnce(root, options) {
2735
2852
  }
2736
2853
  }
2737
2854
  const was = await snap(s);
2855
+ await warm(s);
2738
2856
  const [q1, q2] = await Promise.all(pair.map((x) => s.send(a.method, atOf(x), bodyOf())));
2739
2857
  tc.records = pair.map((x) => x.id);
2740
2858
  tc.answers = [q1.status, q2.status];
@@ -3567,4 +3685,4 @@ function apiLines(run) {
3567
3685
  return out;
3568
3686
  }
3569
3687
 
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 };
3688
+ module.exports = { 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 };