kryptheon 0.1.56 → 0.1.58

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
@@ -112,7 +112,9 @@ function taglessColumns(root) {
112
112
  if (!entry.name.startsWith('.') && !SKIP_DIRS.has(entry.name)) walk(full, depth + 1);
113
113
  continue;
114
114
  }
115
- if (!/\.sql$/i.test(entry.name)) continue;
115
+ // and a server file that makes its own tables: CREATE TABLE in a string of server.js (round 25)
116
+ const script = /\.(c|m)?(js|ts)$/i.test(entry.name);
117
+ if (!/\.sql$/i.test(entry.name) && !script) continue;
116
118
  let sql;
117
119
  try {
118
120
  if (fs.statSync(full).size > 4 * MAX_BYTES) continue;
@@ -120,6 +122,7 @@ function taglessColumns(root) {
120
122
  } catch (err) {
121
123
  continue;
122
124
  }
125
+ if (script && !/create\s+table/i.test(sql)) continue;
123
126
  read++;
124
127
  sql = sql.replace(/--[^\n]*/g, ' ').replace(/\/\*[\s\S]*?\*\//g, ' ');
125
128
  const add = (name, type) => {
@@ -349,6 +352,13 @@ function classify(node, scope, code, depth, aliases) {
349
352
  const inner = argumentAliases(local.node, node.arguments, scope, local.scope, code, depth, aliases);
350
353
  if (returns.length) return worst(returns.map((r) => classify(r.node, r.scope, code, depth + 1, inner)));
351
354
  }
355
+ // ({ inventory: renderInventory, activity: renderActivity })[view](): as
356
+ // dangerous as the worst of the functions it can pick (blind round 25).
357
+ const picked = pickedFunctions(node, scope);
358
+ if (picked) {
359
+ const returns = picked.flatMap((fn) => returnsOf(fn).map((r) => ({ r: r, fn: fn })));
360
+ if (returns.length) return worst(returns.map((x) => classify(x.r.node, x.r.scope, code, depth + 1, argumentAliases(x.fn.node, node.arguments, scope, x.fn.scope, code, depth, aliases))));
361
+ }
352
362
  // String methods pass through whatever they were called on, and so do
353
363
  // the array methods that only pick or reorder items: notes.filter(...)
354
364
  // holds the same notes.
@@ -443,6 +453,23 @@ function localFunction(name, scope) {
443
453
  return null;
444
454
  }
445
455
 
456
+ /**
457
+ * The functions of this file's own that a call picks by name from an object
458
+ * written out right there - ({ a: renderA, b: renderB })[view]() - or null.
459
+ * Null too when any of them is not a function this file declares.
460
+ */
461
+ function pickedFunctions(call, scope) {
462
+ const callee = call.callee;
463
+ if (!callee || !/MemberExpression$/.test(callee.type) || !callee.computed || callee.object.type !== 'ObjectExpression') return null;
464
+ const fns = [];
465
+ for (const prop of callee.object.properties) {
466
+ const fn = prop.type === 'ObjectProperty' && prop.value.type === 'Identifier' ? localFunction(prop.value.name, scope) : null;
467
+ if (!fn) return null;
468
+ fns.push(fn);
469
+ }
470
+ return fns.length ? fns : null;
471
+ }
472
+
446
473
  /** Every value a function returns, with the scope to judge it in; nested functions excluded. */
447
474
  function returnsOf(fnPath) {
448
475
  if (fnPath.node.body.type !== 'BlockStatement') return [{ node: fnPath.node.body, scope: fnPath.scope }];
@@ -619,10 +646,14 @@ function judgedParts(node, scope, code) {
619
646
  /**
620
647
  * Which parts of a value make it unsafe, in the words of the code - so a fix
621
648
  * 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.
649
+ * template and leaving the reader to find it. Never repeated; past MAX_PARTS
650
+ * the list says there are more, so a fix prompt never reads as complete when
651
+ * it is not (round 24: a sixth part, a note's body, was dropped at five).
623
652
  */
653
+ const MAX_PARTS = 24;
624
654
  function riskyParts(node, scope, code, aliases, out, depth) {
625
- if (!node || out.length >= 5 || depth > MAX_DEPTH) return;
655
+ // one past the cap is kept, so whoever lists them can tell the list was cut
656
+ if (!node || out.length > MAX_PARTS || depth > MAX_DEPTH) return;
626
657
  const origin = classify(node, scope, code, 0, aliases);
627
658
  if (origin === 'safe') return;
628
659
  const go = (n, s, a) => riskyParts(n, s || scope, code, a || aliases, out, depth + 1);
@@ -632,12 +663,33 @@ function riskyParts(node, scope, code, aliases, out, depth) {
632
663
  }
633
664
  if (node.type === 'ConditionalExpression') { go(node.consequent); go(node.alternate); return; }
634
665
  if (node.type === 'LogicalExpression') { go(node.left); go(node.right); return; }
666
+ if (node.type === 'AwaitExpression') { go(node.argument); return; }
635
667
  if (/CallExpression$/.test(node.type)) {
636
668
  const mapped = mapCallback(node, scope, code, depth, aliases);
637
669
  if (mapped && mapped.returns.length) {
638
670
  mapped.returns.forEach((r) => go(r.node, r.scope, mapped.inner));
639
671
  return;
640
672
  }
673
+ // a function of this file's own, or one picked from an object of them: the
674
+ // parts are inside what it returns (round 25 named only "html")
675
+ const own = node.callee.type === 'Identifier' ? localFunction(node.callee.name, scope) : null;
676
+ const fns = own ? [own] : pickedFunctions(node, scope);
677
+ const returns = (fns || []).flatMap((fn) => returnsOf(fn).map((r) => ({ r: r, fn: fn })));
678
+ if (returns.length) {
679
+ returns.forEach((x) => go(x.r.node, x.r.scope, argumentAliases(x.fn.node, node.arguments, scope, x.fn.scope, code, depth, aliases)));
680
+ return;
681
+ }
682
+ }
683
+ // const html = await render(): the parts are in what it was set to
684
+ if (node.type === 'Identifier' && !(aliases && Object.prototype.hasOwnProperty.call(aliases, node.name))) {
685
+ const binding = scope && scope.getBinding(node.name);
686
+ const decl = binding && binding.path && binding.path.node;
687
+ if (decl && decl.type === 'VariableDeclarator' && decl.id.type === 'Identifier' && decl.init && !(binding.constantViolations && binding.constantViolations.length) &&
688
+ (/CallExpression$/.test(decl.init.type) || decl.init.type === 'AwaitExpression')) {
689
+ const before = out.length;
690
+ go(decl.init, binding.path.scope);
691
+ if (out.length > before) return;
692
+ }
641
693
  }
642
694
  const text = code.slice(node.start, node.end).replace(/\s+/g, ' ').slice(0, 80);
643
695
  if (!out.some((p) => p.text === text)) out.push({ text: text, origin: origin, line: node.loc ? node.loc.start.line : null });
@@ -2027,12 +2079,14 @@ function describe(f) {
2027
2079
  ];
2028
2080
  // A long template is mostly harmless markup. Naming the part that decides it
2029
2081
  // 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);
2082
+ const more = (f.parts || []).length > MAX_PARTS;
2083
+ const parts = (f.parts || []).slice(0, MAX_PARTS).filter((p) => p.text && p.text !== f.expression);
2031
2084
  const only = parts.length
2032
2085
  ? [''].concat(
2033
2086
  [(parts.length === 1 ? 'Only this part' : 'Only these parts') + ' of it can carry text from outside the page, ' +
2034
2087
  'so only ' + (parts.length === 1 ? 'it needs' : 'they need') + ' changing:'],
2035
2088
  parts.map((p) => ' ${' + p.text + '}'),
2089
+ more ? ['There are more such parts in this template than are listed here: treat every ${} in it the same way.'] : [],
2036
2090
  )
2037
2091
  : [];
2038
2092
  const fixPrompt = [
@@ -2064,7 +2118,8 @@ function describe(f) {
2064
2118
  sink: f.sink,
2065
2119
  expression: f.expression,
2066
2120
  origin: f.origin,
2067
- parts: (f.parts || []).map((p) => ({ text: p.text, origin: p.origin })),
2121
+ parts: (f.parts || []).slice(0, MAX_PARTS).map((p) => ({ text: p.text, origin: p.origin })),
2122
+ moreParts: more,
2068
2123
  via: f.via || null,
2069
2124
  helper: f.helper || null,
2070
2125
  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 };
package/kryptheon-map.js CHANGED
@@ -60,6 +60,11 @@ const OTHER_FRAMEWORKS = /^(fastify|koa|@koa\/router|koa-router|hono|@hono\/hono
60
60
  // Where the signed-in person lives on a request, in the apps this was built on.
61
61
  const IDENTITY_SEGS = /^(user|users|session|auth|actor|account|member|currentUser|identity|principal|claims|userId|user_id|uid|sub|me|profile|viewer|isAuthenticated|isLoggedIn|token|kauth|apiKey|api_key|tenant|org|workspace)$/;
62
62
  const IDENTITY_NAMES = /^(user|currentUser|current_user|me|actor|member|account|profile|viewer|identity|principal|session|sessionUser|authUser|auth|claims)$/i;
63
+ // A cookie or header that says who is calling: a session token (sid, token) or, worse, the user id itself.
64
+ const COOKIE_IDENTITY = /^(uid|user|userId|user_id|userid|id|sid|session|sessionId|session_id|token|auth|jwt|access_token|accessToken|connect\.sid)$/i;
65
+ const HEADER_IDENTITY = /^x-(user|uid|user-id|userid|account|account-id|member|member-id|actor|actor-id)$/i;
66
+ // Of those, the ones that hold the bare id - nothing a server signed, so anyone can write another person's.
67
+ const BARE_ID = /^(uid|user|userId|user_id|userid|id)$/i;
63
68
  const ROLE_SEGS = /^(role|roles|isAdmin|is_admin|isOwner|is_owner|admin|permissions|perms|scopes|scope|capabilities|access|accessLevel|access_level|level)$/;
64
69
  const SIGNATURE_CALLS = /^(createHmac|timingSafeEqual|validateWebhookSignature|constructEvent|constructEventAsync|verifyWebhook|verifySignature|validateSignature|verify)$/;
65
70
  const SIGNATURE_NAMES = /sign|hmac|webhook|valid/i;
@@ -780,11 +785,16 @@ function readGuardFn(ctx, ref, opts = {}) {
780
785
  const chain = memberChain(n);
781
786
  if (!chain) return false;
782
787
  if (reqName && chain[0] === reqName && chain.length > 1 && IDENTITY_SEGS.test(chain[1])) return true;
788
+ // req.cookies.uid / req.signedCookies.sid: who is calling, as the browser sent it
789
+ if (reqName && chain[0] === reqName && /^(cookies|signedCookies)$/.test(chain[1] || '') && chain[2] && COOKIE_IDENTITY.test(chain[2])) return true;
783
790
  if (chain[0] === 'res' && chain[1] === 'locals' && chain[2] && IDENTITY_SEGS.test(chain[2])) return true;
784
791
  if (identityLocals.has(chain[0])) return true;
785
792
  return false;
786
793
  };
787
794
  const isIdentityNode = (n) => {
795
+ // req.headers['x-user-id'] / req.get('x-user-id'): who is calling, as a header says
796
+ const h = headerName(n);
797
+ if (h && HEADER_IDENTITY.test(h)) return true;
788
798
  if (n.type === 'MemberExpression' || n.type === 'OptionalMemberExpression') return identityChain(n);
789
799
  if (n.type === 'Identifier') return identityLocals.has(n.name);
790
800
  if (n.type === 'CallExpression' || n.type === 'OptionalCallExpression') {
@@ -1139,6 +1149,25 @@ function readGuardFn(ctx, ref, opts = {}) {
1139
1149
  for (const m of g.methods) out.methods.add(m);
1140
1150
  }
1141
1151
  }
1152
+ // who is calling, taken from a cookie or header that holds the bare user id: the caller can write anyone's
1153
+ if (out.auth && !opts.asHelper && reqName) {
1154
+ let bare = null;
1155
+ fn.traverse({
1156
+ enter(q) {
1157
+ if (bare) return;
1158
+ const n = q.node;
1159
+ const h = headerName(n);
1160
+ if (h && HEADER_IDENTITY.test(h)) bare = { kind: 'header', name: h.toLowerCase(), node: n };
1161
+ const c = (n.type === 'MemberExpression' || n.type === 'OptionalMemberExpression') && memberChain(n);
1162
+ if (c && c[0] === reqName && c[1] === 'cookies' && c[2] && BARE_ID.test(c[2])) bare = { kind: 'cookie', name: c[2], node: n };
1163
+ },
1164
+ });
1165
+ if (bare) {
1166
+ const line = bare.node.loc.start.line;
1167
+ out.identityFromRequest = { file: mod.rel, line, code: snippet(mod, line), kind: bare.kind, name: bare.name,
1168
+ how: bare.kind === 'cookie' ? 'takes who is calling from the cookie "' + bare.name + '", which holds the user id itself and is not signed' : 'takes who is calling from the request header "' + bare.name + '", which anyone can send' };
1169
+ }
1170
+ }
1142
1171
  // a role check implies a signed-in one: nobody signed in has no role to pass
1143
1172
  if (out.role && !out.auth) out.auth = Object.assign({}, out.role, { how: 'turns away anyone without an allowed role, which nobody signed out has' });
1144
1173
  if (out.apiKey && !out.auth) out.auth = Object.assign({}, out.apiKey, { how: 'turns away a request without a valid API key', kind: 'api-key' });
@@ -1241,6 +1270,8 @@ function judge(route, elements) {
1241
1270
  const out = { auth: verdict('auth'), role: verdict('role'), signature: verdict('signature') };
1242
1271
  const authPartial = elements.find((e) => e.guards && e.guards.authPartial);
1243
1272
  if (authPartial && out.auth.status === STATUS.NOT) out.auth = { status: STATUS.UNKNOWN, why: [authPartial.text + ': ' + authPartial.guards.authPartial.how + ' (' + authPartial.guards.authPartial.file + ':' + authPartial.guards.authPartial.line + ')'] };
1273
+ const fromRequest = elements.find((e) => e.guards && e.guards.identityFromRequest);
1274
+ if (fromRequest && out.auth.status === STATUS.DETECTED) out.auth.fromRequest = fromRequest.guards.identityFromRequest;
1244
1275
  const skippable = elements.find((e) => e.guards && e.guards.signatureSkippable);
1245
1276
  if (skippable && out.signature.status === STATUS.DETECTED) out.signature.skippable = true;
1246
1277
  const presence = elements.find((e) => e.guards && e.guards.sigPresenceOnly);
@@ -1271,6 +1302,7 @@ const OTHER_DB = /^(knex|sequelize|typeorm|drizzle-orm|mongoose|mongodb|kysely|o
1271
1302
  function readSchema(ctx, root) {
1272
1303
  const tables = new Map();
1273
1304
  const rlsTables = new Set();
1305
+ const types = new Map(); // column name -> the types it is declared with, in any table
1274
1306
  const texts = [];
1275
1307
  (function walk(dir, depth) {
1276
1308
  if (depth > 6) return;
@@ -1320,8 +1352,17 @@ function readSchema(ctx, root) {
1320
1352
  }
1321
1353
  parts.push(cur);
1322
1354
  for (const p of parts) {
1323
- const w = p.trim().split(/\s+/)[0];
1324
- if (w && !/^(constraint|primary|foreign|unique|check|exclude|like|index|key)$/i.test(w)) cols.add(w.replace(/["`]/g, '').toLowerCase());
1355
+ const words = p.trim().split(/\s+/);
1356
+ const w = words[0];
1357
+ if (w && !/^(constraint|primary|foreign|unique|check|exclude|like|index|key)$/i.test(w)) {
1358
+ const col = w.replace(/["`]/g, '').toLowerCase();
1359
+ cols.add(col);
1360
+ // the column's type, so a request can send a value it can hold (round 25: "true kx-..." into a boolean)
1361
+ if (words[1]) {
1362
+ if (!types.has(col)) types.set(col, new Set());
1363
+ types.get(col).add(words[1].replace(/\(.*$/, '').toLowerCase());
1364
+ }
1365
+ }
1325
1366
  }
1326
1367
  tables.set(bare(m[1]), cols);
1327
1368
  }
@@ -1336,6 +1377,7 @@ function readSchema(ctx, root) {
1336
1377
  while ((m = rls.exec(text))) rlsTables.add(bare(m[1]));
1337
1378
  }
1338
1379
  tables.rls = rlsTables;
1380
+ tables.types = types;
1339
1381
  return tables;
1340
1382
  }
1341
1383
 
@@ -1403,7 +1445,9 @@ function parseSql(text) {
1403
1445
  }
1404
1446
  const upd = new RegExp('update\\s+(?:only\\s+)?' + name + '(?:\\s+(?:as\\s+)?(\\w+))?\\s+set\\b', 'gi');
1405
1447
  while ((m = upd.exec(sql))) {
1406
- out.push({ op: 'UPDATE', table: clean(m[1]), alias: m[2] && m[2].toLowerCase(), where: whereAfter(sql, upd.lastIndex) });
1448
+ const rest = sql.slice(upd.lastIndex);
1449
+ const w = rest.search(/\b(where|returning|from)\b/i);
1450
+ out.push({ op: 'UPDATE', table: clean(m[1]), alias: m[2] && m[2].toLowerCase(), where: whereAfter(sql, upd.lastIndex), set: w < 0 ? rest : rest.slice(0, w) });
1407
1451
  writes.add(clean(m[1]));
1408
1452
  }
1409
1453
  const del = new RegExp('delete\\s+from\\s+(?:only\\s+)?' + name + '(?:\\s+(?:as\\s+)?(?!where\\b|using\\b|returning\\b)(\\w+))?', 'gi');
@@ -1593,6 +1637,7 @@ function readDb(ctx, fnRef, schema, opts = {}) {
1593
1637
  const mod = fnRef.mod;
1594
1638
  const seenCalls = new Set();
1595
1639
  const recordScopes = new Map();
1640
+ const scopedLookups = []; // const po = await query('SELECT ... WHERE id=$1 AND organization_id=$2', [req.params.id, ...])
1596
1641
  const visitCall = (q) => {
1597
1642
  if (seenCalls.has(q.node)) return;
1598
1643
  seenCalls.add(q.node);
@@ -1628,8 +1673,32 @@ function readDb(ctx, fnRef, schema, opts = {}) {
1628
1673
  return;
1629
1674
  }
1630
1675
  const here = [];
1631
- for (const tx of texts) for (const st of parseSql(tx.text)) here.push(judgeSql(ctx, mod, line, st, tx, paramsArg, src, schema, recordScopes));
1676
+ const idOf = [];
1677
+ for (const tx of texts) {
1678
+ for (const st of parseSql(tx.text)) {
1679
+ const v = judgeSql(ctx, mod, line, st, tx, paramsArg, src, schema, recordScopes);
1680
+ // SET status = 'received': a step of the record's life; WHERE status = ...: a look at where it is now
1681
+ const stateSet = st.set && /(?:^|[\s,])"?(status|state|stage)"?\s*=\s*'([^']+)'/i.exec(st.set);
1682
+ if (stateSet) v.setsState = { col: stateSet[1].toLowerCase(), value: stateSet[2] };
1683
+ if (st.where && /\b(status|state|stage)\b/i.test(st.where)) v.readsState = true;
1684
+ here.push(v);
1685
+ idOf.push(idValueText(st, paramsArg, mod));
1686
+ }
1687
+ }
1632
1688
  joinWith(here);
1689
+ // UPDATE ... WHERE id=$1 after SELECT ... WHERE id=$1 AND organization_id=$2 of the same row,
1690
+ // which stops the route when nothing was found: limited by that lookup (round 25's receive)
1691
+ here.forEach((x, i) => {
1692
+ if (x.op === 'SELECT' || x.scoping.status !== STATUS.NOT || x.scoping.clientChosen || x.scoping.applies === false || !idOf[i]) return;
1693
+ const before = scopedLookups.find((l) => l.table === x.table && l.idText === idOf[i] && l.line < line && stopsWhenMissing(t, fnRef.path, l.name, l.line, line));
1694
+ if (before) x.scoping = { status: STATUS.DETECTED, evidence: { file: mod.rel, line, code: snippet(mod, line), how: 'the same ' + x.table + ' row was looked up with the organisation limit at ' + before.file + ':' + before.line + ', and the route stops when it is not found' } };
1695
+ });
1696
+ const lookupDecl = q.findParent((p) => p.isVariableDeclarator() || p.isFunction());
1697
+ here.forEach((x, i) => {
1698
+ if (x.op === 'SELECT' && !x.joinedTo && x.scoping.status === STATUS.DETECTED && idOf[i] && lookupDecl && lookupDecl.isVariableDeclarator() && t.isIdentifier(lookupDecl.node.id)) {
1699
+ scopedLookups.push({ table: x.table, idText: idOf[i], name: lookupDecl.node.id.name, line, file: mod.rel });
1700
+ }
1701
+ });
1633
1702
  out.push(...here);
1634
1703
  // const shipment = (await query('SELECT ... WHERE id=$1 AND org_id=$2')).rows[0]: remember how its row was found
1635
1704
  const decl = q.findParent((x) => x.isVariableDeclarator() || x.isFunction());
@@ -1670,9 +1739,22 @@ function readDb(ctx, fnRef, schema, opts = {}) {
1670
1739
  for (const st of parseSql(text)) out.push(judgeSql(ctx, mod, line, st, { text, dynamic: false }, { tagged: values }, src, schema));
1671
1740
  },
1672
1741
  });
1742
+ // if (po.status !== 'approved') ... / ['draft', 'sent'].includes(order.status): the code looks at where the record is now
1743
+ if (out.some((d) => d.setsState)) {
1744
+ const body = mod.source.slice(fnRef.path.node.start, fnRef.path.node.end);
1745
+ if (STATE_CHECK.some((re) => re.test(body))) for (const d of out) if (d.setsState) d.stateChecked = true;
1746
+ }
1673
1747
  return out;
1674
1748
  }
1675
1749
 
1750
+ // a status read and compared - never res.status(404), which is a call
1751
+ const STATE_CHECK = [
1752
+ /\.(status|state|stage)\b(?!\s*\()\s*(!==|===|!=|==)/,
1753
+ /(!==|===|!=|==)\s*[\w$.[\]]+\.(status|state|stage)\b(?!\s*\()/,
1754
+ /\.includes\(\s*[\w$.[\]]+\.(status|state|stage)\s*\)/,
1755
+ /switch\s*\(\s*[\w$.[\]]+\.(status|state|stage)\s*\)/,
1756
+ ];
1757
+
1676
1758
  /** The outermost call of a supabase chain that has .from('x') in it. */
1677
1759
  function fromCall(t, q) {
1678
1760
  let n = q.node.callee.object;
@@ -1804,6 +1886,40 @@ function isQueryWrapper(ctx, mod, calleePath) {
1804
1886
  return yes;
1805
1887
  }
1806
1888
 
1889
+ /** The code of the value a statement finds its row by - `id = $1` with [req.params.id] -> "req.params.id" - or null. */
1890
+ function idValueText(st, paramsArg, mod) {
1891
+ const m = st.where && /(?:^|[^\w.])(?:\w+\.)?"?id"?\s*=\s*\$(\d+)/i.exec(st.where);
1892
+ if (!m || !paramsArg || paramsArg.tagged) return null;
1893
+ const pa = unwrapTs(paramsArg);
1894
+ if (!pa.isArrayExpression()) return null;
1895
+ const el = pa.node.elements[Number(m[1]) - 1];
1896
+ if (!el || !memberChain(el)) return null;
1897
+ return mod.source.slice(el.start, el.end).replace(/\s+/g, '');
1898
+ }
1899
+
1900
+ /**
1901
+ * Whether, between the lookup at `from` and the write at `to`, the route
1902
+ * stops when the lookup named `name` found nothing: if (!po.rowCount) return ...
1903
+ */
1904
+ function stopsWhenMissing(t, fnPath, name, from, to) {
1905
+ let yes = false;
1906
+ fnPath.traverse({
1907
+ IfStatement(p) {
1908
+ const l = p.node.loc.start.line;
1909
+ if (yes || l < from || l > to) return;
1910
+ const test = p.node.test;
1911
+ const subject = test.type === 'UnaryExpression' && test.operator === '!' ? test.argument
1912
+ : test.type === 'BinaryExpression' && /^(===|==|<|<=)$/.test(test.operator) && test.right.type === 'NumericLiteral' && test.right.value <= 0 ? test.left : null;
1913
+ const chain = subject && memberChain(subject);
1914
+ if (!chain || chain[0] !== name || !(chain.length === 1 || /^(rowCount|length|rows)$/.test(chain[1]))) return;
1915
+ let stops = t.isReturnStatement(p.node.consequent) || t.isThrowStatement(p.node.consequent);
1916
+ p.get('consequent').traverse({ Function(f) { f.skip(); }, ReturnStatement() { stops = true; }, ThrowStatement() { stops = true; } });
1917
+ if (stops) yes = true;
1918
+ },
1919
+ });
1920
+ return yes;
1921
+ }
1922
+
1807
1923
  /** One SQL statement's touch on one table, judged for scoping. */
1808
1924
  function judgeSql(ctx, mod, line, st, tx, paramsArg, src, schema, recordScopes) {
1809
1925
  // the values behind $1, $2..., and the row each one was read from, if any
@@ -2005,7 +2121,9 @@ function readValidation(ctx, fnRef, elements, db) {
2005
2121
  const id = q.node.id;
2006
2122
  const init = q.node.init;
2007
2123
  if (!init) return;
2008
- let chain = memberChain(init.type === 'TSAsExpression' ? init.expression : init);
2124
+ // req.body || {} / req.body ?? {}: still the body (round 25 lost its sign-in, and so its whole API run, to this)
2125
+ const bare = init.type === 'TSAsExpression' ? init.expression : init;
2126
+ let chain = memberChain(bare.type === 'LogicalExpression' && (bare.operator === '||' || bare.operator === '??') && bare.right.type === 'ObjectExpression' ? bare.left : bare);
2009
2127
  // await req.json() / await req.formData() in a Fetch-style handler is the body
2010
2128
  const awaited = init.type === 'AwaitExpression' ? init.argument : init;
2011
2129
  if (!chain && awaited.type === 'CallExpression' && reqName) {
@@ -2179,9 +2297,40 @@ function readValidation(ctx, fnRef, elements, db) {
2179
2297
  if (el.kind === 'package' && /express-validator|celebrate|joi/.test(el.source || '')) schemas.push({ from: 'body', file: el.usedFile, line: el.usedLine, code: el.name, fields: null, why: 'checked by ' + el.source + ' middleware (its rules are not read)' });
2180
2298
  }
2181
2299
  const unread = (elements || []).some((e) => e.kind === 'package' && /express-validator|celebrate|joi/.test(e.source || ''));
2300
+ // a list of items in the body (lines = [{ product_id, quantity }]): the names read off each item, in
2301
+ // for (const line of lines) and lines.some((x) => ...) - so a request can send one item (round 25's transfer)
2302
+ const itemNames = new Map();
2303
+ const itemsOfLocal = (local, fnOrBody, itemName) => {
2304
+ const f = localField.get(local);
2305
+ if (!f || typeof f !== 'object') return;
2306
+ const k = key(f.from, f.name);
2307
+ if (!itemNames.has(k)) itemNames.set(k, new Set());
2308
+ fnOrBody.traverse({
2309
+ MemberExpression(m) {
2310
+ if (t.isIdentifier(m.node.object, { name: itemName }) && !m.node.computed && t.isIdentifier(m.node.property)) itemNames.get(k).add(m.node.property.name);
2311
+ },
2312
+ });
2313
+ };
2314
+ fn.traverse({
2315
+ ForOfStatement(q) {
2316
+ const right = q.node.right;
2317
+ const left = q.node.left;
2318
+ const v = left.type === 'VariableDeclaration' && left.declarations[0] && left.declarations[0].id;
2319
+ if (t.isIdentifier(right) && t.isIdentifier(v)) itemsOfLocal(right.name, q.get('body'), v.name);
2320
+ },
2321
+ CallExpression(q) {
2322
+ const c = q.node.callee;
2323
+ const cb = q.node.arguments[0];
2324
+ if (!t.isMemberExpression(c) || !t.isIdentifier(c.object) || !t.isIdentifier(c.property) || !/^(some|every|map|forEach|filter|find)$/.test(c.property.name)) return;
2325
+ if (!cb || !/Function/.test(cb.type) || !cb.params[0] || !t.isIdentifier(cb.params[0])) return;
2326
+ itemsOfLocal(c.object.name, q.get('arguments.0'), cb.params[0].name);
2327
+ },
2328
+ });
2182
2329
  const out = Array.from(fields.values()).map((e) => {
2183
2330
  let status = e.checks.length ? STATUS.DETECTED : (e.schemaUnread || unread ? STATUS.UNKNOWN : STATUS.NOT);
2184
- return { name: e.name, from: e.from, line: e.line, file: mod.rel, status, checks: e.checks, reachesWrite: e.reachesWrite || [], why: status === STATUS.UNKNOWN ? 'checked by a schema or middleware whose rules are not read' : null };
2331
+ const items = itemNames.get(key(e.from, e.name));
2332
+ return Object.assign({ name: e.name, from: e.from, line: e.line, file: mod.rel, status, checks: e.checks, reachesWrite: e.reachesWrite || [], why: status === STATUS.UNKNOWN ? 'checked by a schema or middleware whose rules are not read' : null },
2333
+ items && items.size ? { items: Array.from(items).sort() } : {});
2185
2334
  });
2186
2335
  return { fields: out.sort((a, b) => a.line - b.line), schemas };
2187
2336
  }
@@ -2615,7 +2764,10 @@ function nextRoutes(ctx, schema) {
2615
2764
 
2616
2765
  /** What kind of route this is, beside an ordinary one: a webhook, or a way to start a job. */
2617
2766
  function routeKind(r) {
2618
- if (/(^|\/)(webhooks?|hooks?|callbacks?|ipn|notify)(\/|$)/i.test(r.path || '') || r.signature.status === STATUS.DETECTED || r.signature.presenceOnly) return 'webhook';
2767
+ if (/(^|\/)(webhooks?|hooks?|callbacks?|ipn|notify)(\/|$)/i.test(r.path || '')) return 'webhook';
2768
+ // a sign-in compares a password hash with timingSafeEqual too: that is not a signed body (round 25)
2769
+ const signIn = /(^|\/)(log-?in|sign-?in|session|auth)(\/|$)/i.test(r.path || '');
2770
+ if (!signIn && (r.signature.status === STATUS.DETECTED || r.signature.presenceOnly)) return 'webhook';
2619
2771
  if (/(^|\/)(jobs?|cron|tasks?|workers?|queues?|schedules?)(\/|$)/i.test(r.path || '')) return 'job';
2620
2772
  return 'route';
2621
2773
  }
@@ -2727,7 +2879,9 @@ function mapProject(root, options) {
2727
2879
  seen.add(k);
2728
2880
  return true;
2729
2881
  });
2730
- return { ran: true, filesRead: files.length, routes, jobs, notRead, unreadable: ctx.unreadable, tablesKnown: schema.size };
2882
+ const columnTypes = {};
2883
+ for (const [col, set] of schema.types || []) columnTypes[col] = Array.from(set);
2884
+ return { ran: true, filesRead: files.length, routes, jobs, notRead, unreadable: ctx.unreadable, tablesKnown: schema.size, columnTypes };
2731
2885
  }
2732
2886
 
2733
2887
  // --- what is worth a look: read from code, never proven ---------------------------------
@@ -2744,6 +2898,8 @@ const ADVICE = {
2744
2898
  'tenant-from-request': 'take the organisation from the signed-in person\'s session, never from the request body or query string; for an id the request sends, look the record up with the organisation condition before using it.',
2745
2899
  'tenant-unscoped': 'add the signed-in person\'s organisation (or owner) to the statement - WHERE org_id = <the session\'s org> - or, for a row found by its id, check that the row belongs to that organisation before using it.',
2746
2900
  'no-role': 'allow only the roles that should be able to do this, and answer 403 for the others.',
2901
+ 'no-state-check': 'look at the record\'s status first and refuse the step when the record is not in the status it must come from (WHERE status = <the one before> in the same statement, or a check that answers 409). A second call must change nothing - no stock, money or log entry added again.',
2902
+ 'identity-from-request': 'take who is calling only from something the server itself issued and can check - a signed session cookie (express-session, a signed JWT) - never from a bare user id in a cookie or a header the browser can set. Look the session up on the server and set req.user from that.',
2747
2903
  };
2748
2904
 
2749
2905
  /**
@@ -2801,11 +2957,35 @@ function toCheck(map) {
2801
2957
  const byOrg = open.some((d) => d.level !== 'owner');
2802
2958
  add(r, 'tenant-unscoped', open.some((d) => d.op !== 'SELECT') ? 'HIGH' : 'MEDIUM', name + ' ' + tables(open) + (byOrg ? ' with no limit to the signed-in person\'s organisation' : ' with no check that the rows belong to the signed-in person') + (fromRow ? ' (rows found by an id that was itself looked up without one)' : ''), open.map(at));
2803
2959
  }
2960
+ // SET status = 'received' with no look at what the status is now: sent twice, the step is done twice (round 25's receive)
2961
+ const steps = writes ? (r.db || []).filter((d) => d.setsState && !d.stateChecked && !(r.db || []).some((x) => x.readsState && x.table === d.table)) : [];
2962
+ if (steps.length) {
2963
+ const s = steps[0];
2964
+ add(r, 'no-state-check', 'MEDIUM', name + ' sets ' + s.table + '.' + s.setsState.col + " to '" + s.setsState.value + "' without looking at what it is now: sent again, or out of order, it does the step again", steps.map((d) => d.file + ':' + d.line + ' ' + d.sql));
2965
+ }
2804
2966
  if (checksRoles && writes && r.role.status === STATUS.NOT && SENSITIVE_PATH.test(r.path || '')) {
2805
2967
  add(r, 'no-role', 'MEDIUM', name + ' is a sensitive change, and no role check was found in front of it, while other routes of this app check roles', [r.file + ':' + r.line]);
2806
2968
  }
2807
2969
  }
2808
2970
  }
2971
+ // who is calling, written by the caller: said once for the guard, with how many routes stand behind it
2972
+ const byGuard = new Map();
2973
+ for (const r of routes) {
2974
+ const f = r.auth && r.auth.fromRequest;
2975
+ if (!f) continue;
2976
+ const k = f.file + ':' + f.line;
2977
+ if (!byGuard.has(k)) byGuard.set(k, { f, routes: [] });
2978
+ byGuard.get(k).routes.push(r);
2979
+ }
2980
+ for (const { f, routes: behind } of byGuard.values()) {
2981
+ const first = behind[0];
2982
+ items.push({
2983
+ kind: 'identity-from-request', severity: 'HIGH', route: first.method + ' ' + (first.path || '(path not read)'), where: f.file + ':' + f.line,
2984
+ headline: 'The sign-in check ' + f.how + ': anyone can send another person\'s id and be them, on the ' + behind.length + ' route' + (behind.length === 1 ? '' : 's') + ' behind it',
2985
+ evidence: [f.file + ':' + f.line + ' ' + (f.code || '')],
2986
+ identity: { kind: f.kind, name: f.name },
2987
+ });
2988
+ }
2809
2989
  const rank = { HIGH: 0, MEDIUM: 1 };
2810
2990
  items.sort((a, b) => rank[a.severity] - rank[b.severity] || a.where.localeCompare(b.where, undefined, { numeric: true }));
2811
2991
  for (const i of items) i.fixPrompt = fixPromptFor(i);
@@ -2907,4 +3087,4 @@ function reportLines(map, opts = {}) {
2907
3087
  return out;
2908
3088
  }
2909
3089
 
2910
- module.exports = { mapProject, listFiles, joinPath, covers, STATUS, snippet, toCheck, summary, reportLines };
3090
+ module.exports = { mapProject, listFiles, joinPath, covers, STATUS, snippet, toCheck, summary, reportLines, ADVICE };