kryptheon 0.1.55 → 0.1.57

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.
@@ -383,6 +383,7 @@ function makeContext(options) {
383
383
  const headers = { accept: 'application/json, text/html;q=0.8' };
384
384
  const cookie = cookieHeader(session.jar);
385
385
  if (cookie) headers.cookie = cookie;
386
+ for (const k of Object.keys(opts.headers || {})) if (!/^(cookie|host|content-type|content-length)$/i.test(k)) headers[k.toLowerCase()] = String(opts.headers[k]);
386
387
  let body;
387
388
  if (opts.json !== undefined) {
388
389
  headers['content-type'] = 'application/json';
package/kryptheon-code.js CHANGED
@@ -619,10 +619,14 @@ function judgedParts(node, scope, code) {
619
619
  /**
620
620
  * Which parts of a value make it unsafe, in the words of the code - so a fix
621
621
  * prompt can say "only ${log.caller_number}" instead of quoting sixty lines of
622
- * template and leaving the reader to find it. At most five, never repeated.
622
+ * template and leaving the reader to find it. Never repeated; past MAX_PARTS
623
+ * the list says there are more, so a fix prompt never reads as complete when
624
+ * it is not (round 24: a sixth part, a note's body, was dropped at five).
623
625
  */
626
+ const MAX_PARTS = 24;
624
627
  function riskyParts(node, scope, code, aliases, out, depth) {
625
- if (!node || out.length >= 5 || depth > MAX_DEPTH) return;
628
+ // one past the cap is kept, so whoever lists them can tell the list was cut
629
+ if (!node || out.length > MAX_PARTS || depth > MAX_DEPTH) return;
626
630
  const origin = classify(node, scope, code, 0, aliases);
627
631
  if (origin === 'safe') return;
628
632
  const go = (n, s, a) => riskyParts(n, s || scope, code, a || aliases, out, depth + 1);
@@ -2027,12 +2031,14 @@ function describe(f) {
2027
2031
  ];
2028
2032
  // A long template is mostly harmless markup. Naming the part that decides it
2029
2033
  // keeps the fix - and the AI tool doing it - to that part alone.
2030
- const parts = (f.parts || []).filter((p) => p.text && p.text !== f.expression);
2034
+ const more = (f.parts || []).length > MAX_PARTS;
2035
+ const parts = (f.parts || []).slice(0, MAX_PARTS).filter((p) => p.text && p.text !== f.expression);
2031
2036
  const only = parts.length
2032
2037
  ? [''].concat(
2033
2038
  [(parts.length === 1 ? 'Only this part' : 'Only these parts') + ' of it can carry text from outside the page, ' +
2034
2039
  'so only ' + (parts.length === 1 ? 'it needs' : 'they need') + ' changing:'],
2035
2040
  parts.map((p) => ' ${' + p.text + '}'),
2041
+ more ? ['There are more such parts in this template than are listed here: treat every ${} in it the same way.'] : [],
2036
2042
  )
2037
2043
  : [];
2038
2044
  const fixPrompt = [
@@ -2064,7 +2070,8 @@ function describe(f) {
2064
2070
  sink: f.sink,
2065
2071
  expression: f.expression,
2066
2072
  origin: f.origin,
2067
- parts: (f.parts || []).map((p) => ({ text: p.text, origin: p.origin })),
2073
+ parts: (f.parts || []).slice(0, MAX_PARTS).map((p) => ({ text: p.text, origin: p.origin })),
2074
+ moreParts: more,
2068
2075
  via: f.via || null,
2069
2076
  helper: f.helper || null,
2070
2077
  headline: 'Text ' + (f.origin === 'unknown' ? 'of unknown origin' : ORIGIN_WORDS[f.origin].split(',')[0]) +
@@ -0,0 +1,153 @@
1
+ // The export check: does a value someone typed come out of a CSV export as a
2
+ // formula?
3
+ //
4
+ // A cell that starts with = + - or @ is a formula to Excel and Google Sheets.
5
+ // An export that writes what customers typed as it was typed lets one customer
6
+ // put =HYPERLINK(...) or worse in front of whoever opens the file. Reading the
7
+ // export code cannot say whether a value is escaped further down; the file can.
8
+ //
9
+ // So: a record is made through the app's own create route with each plain text
10
+ // field set to a marked value that starts with "=", the export is downloaded
11
+ // before and after, and the cell is read. Written as sent = confirmed; written
12
+ // with a quote or a space in front = held; not in the file = said, not judged.
13
+ //
14
+ // Promises: only an app on this computer; the record it makes is listed as
15
+ // test data (nothing is deleted); the marked value is inert - it names no
16
+ // function and no address.
17
+
18
+ const crypto = require('crypto');
19
+
20
+ const EXPORT_PATH = /(\.csv$|(^|\/)(export|exports|download|downloads)(\/|$|\.))/i;
21
+ const NOT_TEXT = /(^|_)(id|email|mail|date|day|at|on|url|link|website|phone|mobile|role|status|state|currency|country|password|token|key|type|kind)$/i;
22
+ const MAX_ROUTES = 3;
23
+
24
+ /** The cells of a CSV text, rows of strings, quotes undone. */
25
+ function cellsOf(text) {
26
+ const rows = [];
27
+ let row = [];
28
+ let cell = '';
29
+ let quoted = false;
30
+ for (let i = 0; i < text.length; i++) {
31
+ const c = text[i];
32
+ if (quoted) {
33
+ if (c === '"' && text[i + 1] === '"') { cell += '"'; i++; }
34
+ else if (c === '"') quoted = false;
35
+ else cell += c;
36
+ } else if (c === '"' && cell === '') quoted = true;
37
+ else if (c === ',') { row.push(cell); cell = ''; }
38
+ else if (c === '\n' || c === '\r') {
39
+ if (c === '\r' && text[i + 1] === '\n') i++;
40
+ row.push(cell); rows.push(row); row = []; cell = '';
41
+ } else cell += c;
42
+ }
43
+ if (cell !== '' || row.length) { row.push(cell); rows.push(row); }
44
+ return rows;
45
+ }
46
+
47
+ /** How a marked value came out: 'raw' (a formula), 'escaped', or null when it is not in the file. */
48
+ function cellVerdict(rows, mark) {
49
+ for (const row of rows) {
50
+ for (const cell of row) {
51
+ if (cell === mark) return { verdict: 'raw', cell, row };
52
+ if (cell.endsWith(mark) && cell.length <= mark.length + 2) return { verdict: 'escaped', cell, row };
53
+ }
54
+ }
55
+ return null;
56
+ }
57
+
58
+ /** The text fields of a create action that could hold what a person typed. */
59
+ function textFields(action) {
60
+ return (action.fields || []).filter((f) => typeof f.sample === 'string' && !f.kinds.includes('enum') && !NOT_TEXT.test(f.name)).map((f) => f.name);
61
+ }
62
+
63
+ /**
64
+ * Exports tried with a marked value. { ran, why, tried: [...], findings, left }.
65
+ * `options`: origin, env, say, fetch, map, packageDir.
66
+ */
67
+ async function run(root, options) {
68
+ const api = require('./kryptheon-api.js');
69
+ const authz = require('./kryptheon-authz.js');
70
+ const opts = options || {};
71
+ const plan = api.planOf(root, opts);
72
+ if (!plan.ran) return { ran: false, why: plan.why };
73
+ const exports = (plan.map.routes || []).filter((r) => r.method === 'GET' && !r.unmounted && r.path && !/[:{]/.test(r.path) && EXPORT_PATH.test(r.path)).slice(0, MAX_ROUTES);
74
+ if (!exports.length) return { ran: false, why: 'the map found no export route (a GET ending in .csv, or under /export or /download)' };
75
+ let origin;
76
+ try {
77
+ origin = authz.assertLocalTarget(opts.origin);
78
+ } catch (err) {
79
+ return { ran: false, why: 'the app must be on this computer (localhost or 127.0.0.1): ' + (opts.origin || 'no address was given') };
80
+ }
81
+ const j = plan.journey;
82
+ if (!j.signIn) return { ran: false, why: j.signInWhy };
83
+ const creates = j.actions.filter((a) => a.method === 'POST' && !a.params.length && textFields(a).length);
84
+ if (!creates.length) return { ran: false, why: 'no route that creates something takes a text field to put a marked value in' };
85
+ const out = { ran: true, tried: [], findings: [], left: [] };
86
+ // The account that may export: the first one the export answers with a file.
87
+ let session = null;
88
+ const ctx = authz.makeContext({ baseUrl: origin, allowOrigins: [origin], fetch: opts.fetch, passwords: Object.fromEntries(j.accounts.usable.map((a) => [a.email, a.password])), maxRequests: 120 });
89
+ for (const a of j.accounts.usable) {
90
+ const s = authz.makeSession(ctx, 'account', a.email, a.password);
91
+ const fields = { [j.signIn.emailField]: a.email, [j.signIn.passwordField]: a.password };
92
+ const r = authz.replyOf(await s.send(j.signIn.method, j.signIn.path, j.signIn.encoding === 'form' ? { form: fields } : { json: fields }));
93
+ if (r.kind === 'refused' || r.kind === 'server-error' || r.kind === 'no-answer') continue;
94
+ const probe = await s.send('GET', exports[0].path, {});
95
+ if (probe.status === 200 && typeof probe.text === 'string') { session = s; break; }
96
+ }
97
+ if (!session) return { ran: false, why: 'no account with a password in .env could download ' + exports[0].path };
98
+
99
+ for (const exp of exports) {
100
+ const name = 'GET ' + exp.path;
101
+ const t = { what: name, from: exp.file + ':' + exp.line, verdict: 'not seen', field: null, through: null };
102
+ out.tried.push(t);
103
+ for (const a of creates) {
104
+ const fieldsHere = textFields(a);
105
+ const marks = {};
106
+ const body = Object.assign({}, a.fullData);
107
+ for (const f of fieldsHere) {
108
+ marks[f] = '=KX' + crypto.randomBytes(3).toString('hex').toUpperCase();
109
+ body[f] = marks[f];
110
+ }
111
+ let res = await session.send('POST', a.template, a.encoding === 'form' ? { form: body } : { json: body });
112
+ let reply = authz.replyOf(res);
113
+ // the app may refuse a value that starts with "=" in one field only: one field at a time, then
114
+ if (reply.kind !== 'accepted' && reply.kind !== 'redirect') {
115
+ for (const f of fieldsHere) {
116
+ const one = Object.assign({}, a.fullData, { [f]: marks[f] });
117
+ res = await session.send('POST', a.template, a.encoding === 'form' ? { form: one } : { json: one });
118
+ reply = authz.replyOf(res);
119
+ if (reply.kind === 'accepted' || reply.kind === 'redirect') {
120
+ for (const g of fieldsHere) if (g !== f) delete marks[g];
121
+ break;
122
+ }
123
+ }
124
+ }
125
+ if (reply.kind !== 'accepted' && reply.kind !== 'redirect') continue;
126
+ out.left.push(a.method + ' ' + a.template + ' with ' + Object.keys(marks).join(', ') + ' starting with "=" - made to look inside ' + exp.path);
127
+ const after = await session.send('GET', exp.path, {});
128
+ const rows = after.status === 200 ? cellsOf(after.text || '') : [];
129
+ for (const f of Object.keys(marks)) {
130
+ const v = cellVerdict(rows, marks[f]);
131
+ if (!v) continue;
132
+ t.field = f;
133
+ t.through = a.method + ' ' + a.template;
134
+ t.verdict = v.verdict;
135
+ if (v.verdict === 'raw') {
136
+ const finding = {
137
+ kind: 'csv-formula', where: name, field: f, from: t.from, status: 'confirmed', severity: 'MEDIUM',
138
+ headline: name + ' writes ' + f + ' as it was typed: a value starting with "=" comes out as a formula when the file is opened in Excel or Google Sheets.',
139
+ proof: ['As ' + session.email + ', made a record through ' + t.through + ' with ' + f + ' = ' + marks[f] + '.', 'Downloaded ' + exp.path + ': the cell reads ' + marks[f] + ', with nothing in front of the "=".'],
140
+ };
141
+ finding.fixPrompt = ['Kryptheon put a value starting with "=" into my app and downloaded its export:', '', finding.headline, '', 'Where: ' + finding.from, '',
142
+ 'First check this is real: open the export in a spreadsheet. If it is, in the code that writes the CSV, put a single quote in front of every cell that starts with = + - @ or a tab, and quote every cell. Do it for every column, not only ' + f + '. Change nothing else.', '', 'Afterwards I will run: npx kryptheon api'].join(String.fromCharCode(10));
143
+ out.findings.push(finding);
144
+ }
145
+ break;
146
+ }
147
+ if (t.verdict !== 'not seen') break;
148
+ }
149
+ }
150
+ return out;
151
+ }
152
+
153
+ module.exports = { run, cellsOf, cellVerdict, textFields, EXPORT_PATH };
@@ -302,7 +302,7 @@
302
302
  });
303
303
  }
304
304
 
305
- var CHECK_NAME = { regression: 'Recorded flows', frontend: 'Frontend code', data: 'Database', edge: 'Input and edge cases', reliability: 'Reliability', payments: 'Payments', roles: 'Roles and tenants' };
305
+ var CHECK_NAME = { regression: 'Recorded flows', frontend: 'Frontend code', backend: 'Backend map', data: 'Database', edge: 'Input and edge cases', reliability: 'Reliability', payments: 'Payments', roles: 'Roles and tenants' };
306
306
  var CHECK_TONE = { regression: 'sun', frontend: 'amber', data: 'dusk', edge: 'ember', reliability: 'ember', payments: 'ember', roles: 'ember' };
307
307
 
308
308
  function counts() {
@@ -56,8 +56,10 @@ const CHECKS = [
56
56
  what: 'Signs in as each account your recordings use and tries every recorded action as every one of them, against the permissions-policy.json in your app folder: a role that may not do it must be refused, one that may must still work, and one organization must not reach another organization records. Judged from what comes back and what your app stored before and after. It runs only against an app on this computer.' },
57
57
  { id: 'payments', label: 'Payments: a webhook sent twice', available: true,
58
58
  what: 'Real payments in Razorpay\'s or Stripe\'s TEST mode, through a proxy on this computer, each checked as it comes until you press Stop. Its signed webhook is sent to your app again, then once with a wrong signature, and your database is read before and after each - reporting the plan, credits or order given a second time, a repeat answered with an error, or a forged webhook accepted. Live mode is never sent again. It runs only when you press its button.' },
59
- { id: 'api', label: 'API', available: false },
60
- { id: 'backend', label: 'Backend', available: false },
59
+ { id: 'api', label: 'API: every route, tried on your running app', available: true,
60
+ what: 'Takes every route the backend map read and tries it on your app on this computer: each account aimed at another organization\'s records, every route with no sign-in, and bad values in fields the code never checks - judged from what comes back and from your own lists before and after, and said as a verdict on each thing the map pointed at. It adds test data and never deletes it, so it runs only in a terminal, on a test copy, with your yes (npx kryptheon api).' },
61
+ { id: 'backend', label: 'Backend map: routes, guards, tables', available: true,
62
+ what: 'Reads your server code - Express, Supabase Edge Functions, Next.js API routes - and lists every route with its sign-in, role and webhook-signature checks, the tables it reads and writes and whether those rows are limited to the signed-in organisation, and the checks on each field it is sent. Nothing is run or sent. What it points at is to check, never proven: npx kryptheon roles proves it on the running app (npx kryptheon map).' },
61
63
  { id: 'synthetic', label: 'Synthetic test users', available: false },
62
64
  ];
63
65
 
@@ -68,7 +70,7 @@ const CHECK_IDS = new Set(CHECKS.map((c) => c.id));
68
70
  // here (rounds 19 and 20: roles, payment rules, counts and searches).
69
71
  const NOT_CHECKED = [
70
72
  { what: 'Who can do what',
71
- why: 'Whether a lower role - a member, a customer - can open, change or delete what only a higher role should, or reach another account\'s record by changing an id in the address. Kryptheon replays each recording as the person who made it. The Roles and tenants check does try one account\'s actions as another, but only against the permissions-policy.json you write; without that file, or for anything it was not told, this is still not checked.',
73
+ why: 'Whether a lower role - a member, a customer - can open, change or delete what only a higher role should, or reach another account\'s record by changing an id in the address. Kryptheon replays each recording as the person who made it. The Roles and tenants check does try one account\'s actions as another, but only against the permissions-policy.json you write; without that file, or for anything it was not told, this is still not checked. The backend map points at routes whose code shows no role or organisation check, but a reading of code proves nothing.',
72
74
  yourself: 'Sign in as each role and open the higher role\'s pages and actions directly, by their address.' },
73
75
  { what: 'Payment rules beyond a repeat',
74
76
  why: 'Only the same webhook sent again, and a copy with a wrong signature, are tried. Not tried: a webhook with no signature at all, an amount or currency that does not match the order, buying again while a plan is active, or when a refund takes access away.',
@@ -553,6 +555,10 @@ function buildState(root, env) {
553
555
  const history = readHistory(paths.history, 20);
554
556
  const baselines = readJson(paths.baselines) || {};
555
557
  const code = readJson(paths.codeFindings);
558
+ // The last backend map (npx kryptheon map, or its button here).
559
+ const backendMap = readJson(path.join(paths.dir, 'map-last.json'));
560
+ // The last API run (npx kryptheon api --run): tries on the running app.
561
+ const apiLast = readJson(path.join(paths.dir, 'api-last.json'));
556
562
  const night = readJson(paths.nightLast);
557
563
  // What the nightly run inside the database last said, kept by
558
564
  // kryptheon-night whenever it connects. Not the same answer as night:
@@ -622,6 +628,41 @@ function buildState(root, env) {
622
628
  }
623
629
  }
624
630
 
631
+ // The backend map: what it points at, each one to check - a reading of the
632
+ // code, so never "confirmed".
633
+ if (backendMap && config.enabled.backend) {
634
+ for (const t of backendMap.toCheck || []) {
635
+ findings.push({
636
+ check: 'backend',
637
+ severity: t.severity,
638
+ status: 'verification required',
639
+ where: t.where,
640
+ headline: t.headline,
641
+ detail: (t.evidence || []).join('\n') + apiVerdictLine(apiLast, t),
642
+ fixPrompt: t.fixPrompt,
643
+ when: backendMap.checkedAt,
644
+ });
645
+ }
646
+ }
647
+
648
+ // The API run: what got through on the running app, each with its proof.
649
+ if (apiLast && config.enabled.api) {
650
+ for (const f of apiLast.findings || []) {
651
+ findings.push({
652
+ check: 'api',
653
+ severity: f.severity,
654
+ status: f.status,
655
+ evidence: f.status === 'confirmed' ? 'runtime confirmed' : 'seen once',
656
+ confidence: f.status === 'confirmed' ? 'high' : 'medium',
657
+ where: f.where,
658
+ headline: f.headline,
659
+ detail: (f.proof || []).join('\n'),
660
+ fixPrompt: f.fixPrompt,
661
+ when: apiLast.checkedAt,
662
+ });
663
+ }
664
+ }
665
+
625
666
  // Input and edge cases, and reliability: the last press of each one's
626
667
  // button. Each finding carries its proof, and what it was about - the
627
668
  // field and value, the flow, the step - so a re-check can try exactly that
@@ -792,6 +833,13 @@ function buildState(root, env) {
792
833
  : 'last run held';
793
834
  }
794
835
  else if (c.id === 'frontend') state = !code ? 'never run' : (code.findings || []).length ? 'things to check' : 'nothing found';
836
+ else if (c.id === 'backend') state = !backendMap ? 'never run' : !(backendMap.routes || []).length ? 'no routes read' : (backendMap.toCheck || []).length ? 'things to check' : 'nothing found';
837
+ else if (c.id === 'api') {
838
+ state = !apiLast ? 'never run' : !apiLast.ran ? 'nothing was tried'
839
+ : findings.some((f) => f.check === 'api' && f.status === 'confirmed') ? 'problems found'
840
+ : findings.some((f) => f.check === 'api') ? 'things to check'
841
+ : 'last run held';
842
+ }
795
843
  else if (c.id === 'payments') {
796
844
  state = !paymentsLast ? 'never run'
797
845
  : findings.some((f) => f.check === 'payments') ? 'problems found'
@@ -822,7 +870,7 @@ function buildState(root, env) {
822
870
  } : null,
823
871
  // Runnable from the page: what needs nothing the page cannot supply. The
824
872
  // database check needs a connection string, which stays in the terminal.
825
- runnable: c.available && (c.id === 'frontend' || (c.id === 'regression' && recordings > 0)),
873
+ runnable: c.available && (c.id === 'frontend' || c.id === 'backend' || (c.id === 'regression' && recordings > 0)),
826
874
  };
827
875
  });
828
876
 
@@ -892,11 +940,18 @@ function buildState(root, env) {
892
940
 
893
941
  // What each run is called on the page, and the command that does the same in
894
942
  // a terminal - shown under every button, for when the page cannot be used.
895
- const RUN_TITLE = { frontend: 'Reading your code', regression: 'Replaying your recordings', edge: 'Trying unusual inputs in your forms', reliability: 'Replaying your flows three times, and double-clicking',
943
+ const RUN_TITLE = { frontend: 'Reading your code', backend: 'Mapping your server code', regression: 'Replaying your recordings', edge: 'Trying unusual inputs in your forms', reliability: 'Replaying your flows three times, and double-clicking',
896
944
  roles: 'Trying every recorded action as each account',
897
945
  payments: 'Waiting for test payments, and sending each webhook again' };
898
- const RUN_COMMAND = { frontend: 'npx kryptheon code', regression: 'npx kryptheon check', edge: 'npx kryptheon edge', reliability: 'npx kryptheon reliability', roles: 'npx kryptheon roles',
946
+ const RUN_COMMAND = { frontend: 'npx kryptheon code', backend: 'npx kryptheon map', regression: 'npx kryptheon check', edge: 'npx kryptheon edge', reliability: 'npx kryptheon reliability', roles: 'npx kryptheon roles',
947
+ api: 'npx kryptheon api --run --origin http://localhost:3000 --yes',
899
948
  payments: 'npx kryptheon payments' };
949
+
950
+ /** The line a backend-map item gets once the API run has tried that route: "On the running app: held." */
951
+ function apiVerdictLine(apiLast, item) {
952
+ const v = apiLast && (apiLast.verdicts || []).find((x) => x.route === item.route && x.kind === item.kind);
953
+ return v ? '\nOn the running app (' + String(apiLast.checkedAt || '').slice(0, 10) + '): ' + v.verdict + (v.why ? ' - ' + v.why : '') + '.' : '';
954
+ }
900
955
  const DB_ACTIONS = {
901
956
  scan: { title: 'Checking your database', command: 'npx kryptheon-night' },
902
957
  install: { title: 'Setting up the nightly check', command: 'npx kryptheon-night install' },
package/kryptheon-look.js CHANGED
@@ -50,6 +50,7 @@ function owedReplay(paths) {
50
50
  /**
51
51
  * Looks once. `runners` does the work that needs the caller's tools:
52
52
  * runners.frontend() -> { ran, why } (the code read)
53
+ * runners.backend() -> { ran, why } (the backend map, read from the server code)
53
54
  * runners.regression() -> exit code, or null (replaying the recordings)
54
55
  * `options.replay` false keeps the recordings from being replayed - the
55
56
  * dashboard's automatic look does that by default, because a replay while the
@@ -96,6 +97,13 @@ function look(root, runners, options) {
96
97
  if (step.id === 'frontend') {
97
98
  const r = runners.frontend();
98
99
  step.state = r.ran ? 'ran' : 'could not run: ' + r.why;
100
+ } else if (step.id === 'backend') {
101
+ // a reading of the server code: cheap, and never run without a runner that can do it
102
+ if (typeof runners.backend !== 'function') step.state = 'not run here - npx kryptheon map';
103
+ else {
104
+ const r = runners.backend();
105
+ step.state = r.ran ? 'ran' : 'could not run: ' + r.why;
106
+ }
99
107
  } else if (step.id === 'regression') {
100
108
  regressionExit = runners.regression();
101
109
  step.state = regressionExit === 0 ? 'ran - every recording held'