kryptheon 0.1.55 → 0.1.56

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.
@@ -85,6 +85,8 @@ function loadPolicy(root) {
85
85
  function checkPolicy(json) {
86
86
  const problems = [];
87
87
  if (!json || typeof json !== 'object' || Array.isArray(json)) return { ok: false, problems: ['it must be a JSON object with a "roles" section.'] };
88
+ // A draft Kryptheon wrote from the code says what the code does today, not what the owner wants.
89
+ if (json.draft !== undefined) return { ok: false, problems: ['it is still marked "draft": it lists what your code does today, not what you want. Edit each role to what you want, then delete the "draft" line.'] };
88
90
  const roles = {};
89
91
  if (!json.roles || typeof json.roles !== 'object' || Array.isArray(json.roles)) problems.push('"roles" must say, for each role, the list of permissions it has.');
90
92
  else {
@@ -106,6 +108,8 @@ function checkPolicy(json) {
106
108
 
107
109
  /** Whether `role` has `permission` ("*" and "resource.*" are allowed in the file). */
108
110
  function holds(policy, role, permission) {
111
+ // The API check's organization-only run: who may do what is not judged, every signed-in account may.
112
+ if (policy.anyRole) return true;
109
113
  const key = Object.keys(policy.roles).find((r) => r.toLowerCase() === String(role || '').toLowerCase());
110
114
  if (!key) return false;
111
115
  return policy.roles[key].some((p) => p === permission || p === '*' || (p.endsWith('.*') && permission.startsWith(p.slice(0, -1))));
@@ -437,6 +441,13 @@ function findUser(json) {
437
441
  return pools.find(looks) || null;
438
442
  }
439
443
 
444
+ /** Who the app says is signed in when it names no role: an object with an id and an email. */
445
+ function findAnyUser(json) {
446
+ const looks = (o) => o && typeof o === 'object' && !Array.isArray(o) && o.id !== undefined && typeof o.email === 'string';
447
+ const pools = [json, json && json.user, json && json.data, json && json.data && json.data.user, json && json.me, json && json.account, json && json.profile];
448
+ return pools.find(looks) || null;
449
+ }
450
+
440
451
  function orgOf(user, extra) {
441
452
  const idPath = extra && extra.identity && extra.identity.orgPath;
442
453
  const namePath = extra && extra.identity && extra.identity.orgNamePath;
@@ -509,7 +520,7 @@ async function checkAccess(input) {
509
520
  for (const p of candidates) {
510
521
  const me = await s.send('GET', p);
511
522
  if (me.status === 200 && me.json) {
512
- user = findUser(me.json);
523
+ user = findUser(me.json) || (policy.anyRole ? findAnyUser(me.json) : null);
513
524
  if (user) break;
514
525
  }
515
526
  }
@@ -517,8 +528,8 @@ async function checkAccess(input) {
517
528
  skipped.push({ what: a.email, why: 'signed in, but the app did not say who it is (looked at ' + candidates.slice(0, 3).join(', ') + '...). Name the address in ' + POLICY_FILE + ' under kryptheon.identity.path' });
518
529
  continue;
519
530
  }
520
- const roleName = typeof user.role === 'string' ? user.role : user.role.name;
521
- const known = Object.keys(policy.roles).find((r) => r.toLowerCase() === roleName.toLowerCase());
531
+ const roleName = typeof user.role === 'string' ? user.role : user.role ? user.role.name : 'signed in';
532
+ const known = policy.anyRole ? roleName : Object.keys(policy.roles).find((r) => r.toLowerCase() === roleName.toLowerCase());
522
533
  if (!known) {
523
534
  skipped.push({ what: a.email, why: 'the app says its role is "' + roleName + '", which ' + POLICY_FILE + ' does not list' });
524
535
  continue;
@@ -601,7 +612,7 @@ async function checkAccess(input) {
601
612
  for (const t of Object.keys(after)) if (before[t] !== undefined) lines.push.apply(lines, diffLines(t, before[t], after[t]));
602
613
  return lines;
603
614
  };
604
- for (const key of orgs.keys()) {
615
+ for (const key of orgs.size ? Array.from(orgs.keys()) : [null]) {
605
616
  const one = await snapshot(key);
606
617
  const two = await snapshot(key);
607
618
  for (const t of Object.keys(one)) if (JSON.stringify(one[t]) !== JSON.stringify(two[t])) {
@@ -641,12 +652,53 @@ async function checkAccess(input) {
641
652
  fieldCache.set(k, field);
642
653
  return field;
643
654
  };
655
+ // A route whose address names a record, with no list of those records but a POST at the same address: the POST is sent once
656
+ // per organization (by an account that may) and the id in its answer is the record to aim at. Test data, listed.
657
+ const seeded = new Map();
658
+ out.seeded = [];
659
+ const idIn = (json) => {
660
+ const find = (v, depth) => {
661
+ if (v == null || typeof v !== 'object' || depth > 2) return undefined;
662
+ if (!Array.isArray(v) && ['string', 'number'].includes(typeof v.id)) return v.id;
663
+ for (const k of Object.keys(v)) {
664
+ const f = find(v[k], depth + 1);
665
+ if (f !== undefined) return f;
666
+ }
667
+ return undefined;
668
+ };
669
+ return find(json, 0);
670
+ };
671
+ if (input.seed) {
672
+ for (const action of journey.actions) {
673
+ if (action.params.length !== 1) continue;
674
+ const listing = '/' + action.segs.slice(0, action.params[0].index).join('/');
675
+ if (getTemplates.has(listing) || seeded.has(listing)) continue;
676
+ const maker = journey.actions.find((a) => a.method === 'POST' && a.template === listing && !a.params.length);
677
+ if (!maker) continue;
678
+ const perm = (mapped.find((m) => m.method === 'POST' && m.template === listing) || {}).permission || null;
679
+ const made = [];
680
+ for (const org of orgs.size ? Array.from(orgs.keys()) : [null]) {
681
+ const who = accountsOf(org).find((ss) => !perm || holds(policy, ss.role, perm)) || accountsOf(org)[0];
682
+ if (!who) continue;
683
+ const body = sampleBody(maker);
684
+ const res = await who.send('POST', listing, maker.encoding === 'form' ? { form: body } : { json: body });
685
+ const id = res.status >= 200 && res.status < 300 ? idIn(res.json) : undefined;
686
+ if (id === undefined) continue;
687
+ made.push({ org: org, value: id, item: res.json, listing: listing, field: 'id' });
688
+ out.seeded.push('POST ' + listing + ' -> ' + id + (org !== null ? ' (' + (orgs.get(org) ? orgs.get(org).name : org) + ')' : '') + ': made to have a record to aim at');
689
+ }
690
+ seeded.set(listing, made);
691
+ }
692
+ }
644
693
  /** The records of one organization an action's address can be aimed at: [{ value, item }]. */
645
694
  const targetsFor = async (action, orgKey) => {
646
695
  if (!action.params.length) return [{ value: null, item: null }];
647
696
  if (action.params.length > 1) return null;
648
697
  const listing = '/' + action.segs.slice(0, action.params[0].index).join('/');
649
- if (!getTemplates.has(listing)) return null;
698
+ if (!getTemplates.has(listing)) {
699
+ const mine = (seeded.get(listing) || []).filter((r) => r.org === orgKey);
700
+ return mine.length ? mine : null;
701
+ }
650
702
  const items = await itemsOf(orgKey, listing);
651
703
  if (!items) return null;
652
704
  const field = await fieldFor(action, listing);
@@ -693,6 +745,15 @@ async function checkAccess(input) {
693
745
  }
694
746
  }
695
747
  }
748
+ // Asked for (the API check): every action once with no session at all, aimed at
749
+ // the first organization's records and judged by its lists before and after.
750
+ if (input.anonymous) {
751
+ const anon = authz.makeSession(ctx, null, '(not signed in)', null);
752
+ anon.role = 'not signed in';
753
+ anon.org = null;
754
+ const first = sessions.find((s) => s.org);
755
+ for (const action of mapped) plan.push({ action: action, actor: anon, kind: 'signin', expected: 'deny', org: first ? first.org.key : null });
756
+ }
696
757
  // Refusals first, so a control that changes things (an archive, a job) cannot make a later refusal look kept.
697
758
  const rank = (p) => (p.expected === 'deny' ? 0 : 1);
698
759
  plan.sort((a, b) => rank(a) - rank(b));
@@ -736,7 +797,8 @@ async function checkAccess(input) {
736
797
  groups.get(k).push(a);
737
798
  }
738
799
  for (const [k, list] of groups) {
739
- if (list.every((a) => !a.notTried && a.response.status === 401)) {
800
+ const signedIn = list.filter((a) => a.kind !== 'signin');
801
+ if (signedIn.length && signedIn.every((a) => !a.notTried && a.response.status === 401)) {
740
802
  out.attempts = out.attempts.filter((a) => !list.includes(a));
741
803
  skipped.push({ what: k, why: 'every account was answered 401 (not signed in): it does not take the session, probably its own key, so it was not judged' });
742
804
  }
@@ -832,7 +894,14 @@ async function tryOnce(x) {
832
894
  }
833
895
  if (/^(PATCH|PUT)$/.test(a.method) && !Object.keys(body).some((k) => nameOf(body[k]))) {
834
896
  const field = Object.keys(body).find((k) => typeof body[k] === 'string' && body[k].length > 2 && !/status|state|type|role|kind|currency|country|id$/i.test(k));
835
- if (field) body[field] = body[field] + ' kx-roles';
897
+ if (field) {
898
+ // the same value as the record already holds (a try of an earlier run, kept) would change nothing and look like a refusal
899
+ let v = body[field] + ' kx-roles';
900
+ const flat = (k) => String(k).split('_').join('').toLowerCase();
901
+ const held = target.item && typeof target.item === 'object' ? Object.keys(target.item).find((k) => flat(k) === flat(field)) : undefined;
902
+ if (held !== undefined && String(target.item[held]) === v) v = v + ' ' + Math.random().toString(36).slice(2, 5);
903
+ body[field] = v;
904
+ }
836
905
  }
837
906
  }
838
907
  const request = { method: a.method, path: pathNow };
@@ -840,7 +909,7 @@ async function tryOnce(x) {
840
909
  const requestText = pathNow + ' ' + JSON.stringify(body || {});
841
910
 
842
911
  // what the app holds now
843
- const reader = targetOrg !== null;
912
+ const reader = targetOrg !== null || p.kind === 'signin' || x.orgs.size === 0;
844
913
  const before = reader ? await x.snapshot(targetOrg) : {};
845
914
  const known = await x.knownTo(actor);
846
915
 
@@ -893,9 +962,15 @@ async function tryOnce(x) {
893
962
  const original = target.item && /^(PATCH|PUT)$/.test(a.method) ? target.item : null;
894
963
  if (original) {
895
964
  const back = {};
896
- for (const k of Object.keys(body || {})) if (original[k] !== undefined) back[k] = original[k];
965
+ // The body says recipientName where the record says recipient_name: the same field.
966
+ const plain = (k) => String(k).replace(/[^A-Za-z0-9]/g, '').toLowerCase();
967
+ const held = new Map(Object.keys(original).map((k) => [plain(k), k]));
968
+ for (const k of Object.keys(body || {})) {
969
+ const at = original[k] !== undefined ? k : held.get(plain(k));
970
+ if (at !== undefined && original[at] !== undefined) back[k] = original[at];
971
+ }
897
972
  if (Object.keys(back).length) {
898
- const writers = x.orgs.get(targetOrg).sessions.filter((s) => holds(policy, s.role, a.permission)).concat([actor]);
973
+ const writers = (x.orgs.get(targetOrg) ? x.orgs.get(targetOrg).sessions : x.sessions).filter((s) => holds(policy, s.role, a.permission)).concat([actor]);
899
974
  for (const who of writers) {
900
975
  await who.send(a.method, pathNow, a.encoding === 'form' ? { form: back } : { json: back });
901
976
  const again = await x.snapshot(targetOrg);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kryptheon",
3
- "version": "0.1.55",
3
+ "version": "0.1.56",
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",
@@ -54,7 +54,9 @@
54
54
  "kryptheon-payments.js",
55
55
  "kryptheon-payments-run.js",
56
56
  "kryptheon-authz.js",
57
- "kryptheon-roles.js"
57
+ "kryptheon-roles.js",
58
+ "kryptheon-map.js",
59
+ "kryptheon-api.js"
58
60
  ],
59
61
  "engines": {
60
62
  "node": ">=20.6.0"
@@ -64,7 +66,7 @@
64
66
  "pg": "^8.23.0"
65
67
  },
66
68
  "scripts": {
67
- "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-package.check.js",
69
+ "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-package.check.js",
68
70
  "check:store": "node kryptheon-store.check.js",
69
71
  "check:code": "node kryptheon-code.check.js",
70
72
  "check:constdata": "node kryptheon-constdata.check.js",
@@ -105,7 +107,9 @@
105
107
  "check:fresh": "node kryptheon-fresh.check.js",
106
108
  "check:authz": "node kryptheon-authz.check.js",
107
109
  "check:roles": "node kryptheon-roles.check.js",
108
- "check:roles-mutants": "node kryptheon-roles.mutants.js"
110
+ "check:roles-mutants": "node kryptheon-roles.mutants.js",
111
+ "check:map": "node kryptheon-map.check.js",
112
+ "check:api": "node kryptheon-api.check.js"
109
113
  },
110
114
  "keywords": [
111
115
  "playwright",