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.
@@ -0,0 +1,1156 @@
1
+ // The API check: every route the backend map read, tried on the running app
2
+ // by every account and across organizations - no recording needed.
3
+ //
4
+ // The backend map (kryptheon-map.js) says what the server answers; this file
5
+ // turns that into the "journey" the roles check already knows how to run:
6
+ // - how to sign in: the map's sign-in route and the two fields it reads;
7
+ // - who can sign in: every email the project names (README, tests, the
8
+ // permissions file) whose password is set as KRYPTHEON_PASSWORD_<name> -
9
+ // a password is never guessed, read from a README or written anywhere;
10
+ // - what to try: each mapped route, its record ids taken from the app's own
11
+ // lists at run time, its body built from the fields the map saw it read
12
+ // (an allowed value for a field the code checks against a list).
13
+ //
14
+ // A dry run (planOf) sends nothing: it says what would be tried, as whom, and
15
+ // what would not be tried and why. The run itself is the roles check's
16
+ // checkAccess, so it is judged the same way - from what comes back and what
17
+ // the app holds before and after, never from a status code alone - and keeps
18
+ // its rules: this computer only, never a delete, a cap on tries.
19
+
20
+ const fs = require('fs');
21
+ const path = require('path');
22
+ const map = require('./kryptheon-map.js');
23
+ const roles = require('./kryptheon-roles.js');
24
+ const replay = require('./kryptheon-replay.js');
25
+ const secrets = require('./kryptheon-secrets.js');
26
+ const authz = require('./kryptheon-authz.js');
27
+ const webhook = require('./kryptheon-webhook.js');
28
+ const csv = require('./kryptheon-csv.js');
29
+
30
+ const EMAIL = /[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/g;
31
+ const SIGN_IN = /(^|\/)(log-?in|sign-?in|session|auth\/(login|signin|token))$/i;
32
+ const SIGN_OUT = /(^|\/)(log-?out|sign-?out)$/i;
33
+ // Addresses that are sign-up, password and account plumbing: not actions to try as another account.
34
+ const PLUMBING = /(^|\/)(sign-?up|register|password|forgot|reset|verify|confirm|callback|oauth|health|healthz|ready|live|favicon)(\/|$)/i;
35
+ const MAX_FILE = 400 * 1024;
36
+
37
+ /** The sign-in route of the map: { method, path, emailField, passwordField, encoding } or null with why. */
38
+ function signInOf(m) {
39
+ const candidates = (m.routes || []).filter((r) => !r.unmounted && /^(POST|ANY)$/.test(r.method) && SIGN_IN.test(r.path || ''));
40
+ for (const r of candidates) {
41
+ const body = (r.validation.fields || []).filter((f) => f.from === 'body').map((f) => f.name);
42
+ const emailField = body.find((n) => /mail|user(name)?$|login|account|phone/i.test(n));
43
+ const passwordField = body.find((n) => /pass/i.test(n));
44
+ if (emailField && passwordField) {
45
+ return { method: 'POST', path: r.path, emailField, passwordField, extra: {}, encoding: encodingOf(r), file: r.file, line: r.line };
46
+ }
47
+ }
48
+ return { none: true, why: candidates.length ? 'the sign-in route (' + candidates.map((r) => r.method + ' ' + r.path).join(', ') + ') does not read an email and a password field the map could see' : 'the map found no sign-in route (POST /login, /signin, /session ...)' };
49
+ }
50
+
51
+ /** json, or form when the only body parser in front of the route reads forms. */
52
+ function encodingOf(r) {
53
+ const names = (r.middleware || []).map((m) => m.name || '');
54
+ const json = names.some((n) => /json/i.test(n));
55
+ const form = names.some((n) => /urlencoded/i.test(n));
56
+ return form && !json ? 'form' : 'json';
57
+ }
58
+
59
+ /** Every email the project names, in the files a person writes accounts down in. */
60
+ function emailsIn(root) {
61
+ const found = new Map();
62
+ const read = (rel) => {
63
+ try {
64
+ const full = path.join(root, rel);
65
+ if (fs.statSync(full).size > MAX_FILE) return;
66
+ for (const e of fs.readFileSync(full, 'utf8').match(EMAIL) || []) {
67
+ const email = e.toLowerCase();
68
+ if (/example\.(com|org)$|noreply|no-reply|@users\.noreply/.test(email)) continue;
69
+ if (!found.has(email)) found.set(email, rel);
70
+ }
71
+ } catch (err) {
72
+ /* a file that is not there names nobody */
73
+ }
74
+ };
75
+ for (const name of safeList(root)) if (/^readme/i.test(name) || /^(accounts|users|seed)[\w.-]*\.(md|txt|json)$/i.test(name)) read(name);
76
+ read(roles.POLICY_FILE);
77
+ for (const name of safeList(path.join(root, 'tests'))) if (/\.(spec|test)\.[cm]?[jt]s$/.test(name)) read(path.join('tests', name));
78
+ return found;
79
+ }
80
+
81
+ function safeList(dir) {
82
+ try {
83
+ return fs.readdirSync(dir);
84
+ } catch (err) {
85
+ return [];
86
+ }
87
+ }
88
+
89
+ /**
90
+ * The accounts that can sign in: an email the project names, with its password
91
+ * set as KRYPTHEON_PASSWORD_<name> in .env or the environment. { usable, left }.
92
+ */
93
+ function accountsOf(root, env) {
94
+ const fromFile = secrets.readEnvFile(secrets.envFileFor(root));
95
+ const here = env || process.env;
96
+ const usable = [];
97
+ const left = [];
98
+ for (const [email, where] of emailsIn(root)) {
99
+ const name = 'KRYPTHEON_PASSWORD_' + replay.accountPart(email);
100
+ const value = secrets.isSet(here[name]) ? here[name] : secrets.isSet(fromFile[name]) ? fromFile[name] : '';
101
+ if (value) usable.push({ email, password: value, envName: name, from: where });
102
+ else if (!/^tests[\\/]/.test(where)) left.push({ email, envName: name, from: where, why: 'its password is not set: add ' + name + ' to .env' });
103
+ }
104
+ return { usable, left };
105
+ }
106
+
107
+ /** The words of a field name: recipientEmail, weight_kg -> ['recipient', 'email'], ['weight', 'kg']. */
108
+ function wordsOf(name) {
109
+ return String(name).replace(/([a-z0-9])([A-Z])/g, '$1 $2').toLowerCase().split(/[^a-z0-9]+/).filter(Boolean);
110
+ }
111
+
112
+ const NUMBER_WORDS = new Set(['quantity', 'qty', 'count', 'weight', 'kg', 'amount', 'price', 'cost', 'total', 'limit', 'seats', 'hours', 'minutes', 'duration', 'size', 'age', 'number', 'rupees', 'paise', 'cents', 'value', 'percent', 'rate']);
113
+
114
+ /**
115
+ * A value that passes the field's own checks, or a plain one by its name:
116
+ * an allowed value when the code checks it against a list (here or, by the
117
+ * same field name, on another route), a number when the code turns it into one.
118
+ */
119
+ function sampleValue(field, known, roleNames) {
120
+ if (roleNames && roleNames.length && wordsOf(field.name).includes('role')) return roleNames[0];
121
+ const en = (field.checks || []).find((c) => c.kind === 'enum' && c.values && c.values.length);
122
+ if (en) return en.values[0];
123
+ if (known && known.has(field.name)) return known.get(field.name)[0];
124
+ const w = wordsOf(field.name);
125
+ const last = w[w.length - 1] || '';
126
+ if (w.includes('role')) return null; // a role is never made up: the field is left out
127
+ if (w.includes('email') || w.includes('mail')) return 'kx-test@kryptheon.invalid';
128
+ if (/^(date|day)$/.test(last) || last === 'at' || last === 'on') return new Date().toISOString().slice(0, 10);
129
+ if (/^(is|has)$/.test(w[0]) || /^(enabled|active|private|public)$/.test(last)) return true;
130
+ if (last === 'id' && w.length > 1) return 1;
131
+ if (w.some((x) => NUMBER_WORDS.has(x))) return 1;
132
+ if ((field.checks || []).some((c) => /^(type|range)$/.test(c.kind) && /Number|parse(Int|Float)|isFinite|isInteger/.test(c.code || ''))) return 1;
133
+ if (w.some((x) => /^(url|link|website)$/.test(x))) return 'https://kryptheon.invalid/test';
134
+ if (w.includes('phone') || w.includes('mobile')) return '9999999999';
135
+ return 'Kryptheon test';
136
+ }
137
+
138
+ /** Field name -> allowed values, from every list check the map saw. */
139
+ function allowedByName(m) {
140
+ const out = new Map();
141
+ for (const r of m.routes || []) {
142
+ for (const f of (r.validation && r.validation.fields) || []) {
143
+ for (const c of f.checks || []) if (c.kind === 'enum' && c.values && c.values.length && !out.has(f.name)) out.set(f.name, c.values);
144
+ }
145
+ }
146
+ return out;
147
+ }
148
+
149
+ // A change sent to someone else's record changes one plain text field, never a
150
+ // status, role, amount or anything another check depends on.
151
+ const KEEP_OUT_OF_CHANGES = /^(status|state|role|type|kind|owner|org|tenant|amount|price|cost|total|deleted|archived)$/;
152
+
153
+ /** An address of the map as the roles check writes one: /api/shipments/{id}. */
154
+ function actionOf(r, known, roleNames) {
155
+ const segs = String(r.path).split('/').filter(Boolean).map((s) => (/^:\w+\*?$/.test(s) ? '{id}' : s));
156
+ const params = [];
157
+ segs.forEach((s, i) => {
158
+ if (s === '{id}') params.push({ index: i, value: undefined });
159
+ });
160
+ const data = {};
161
+ const leftOut = [];
162
+ const unchecked = []; // body fields the code reads and checks nothing about
163
+ const fields = []; // every body field with a value Kryptheon can send, and the kinds of check the code makes on it
164
+ for (const f of (r.validation && r.validation.fields) || []) {
165
+ if (f.from !== 'body') continue;
166
+ const v = sampleValue(f, known, roleNames);
167
+ if (v !== null) data[f.name] = v;
168
+ else leftOut.push(f.name);
169
+ if (v === null) continue;
170
+ const info = { name: f.name, line: f.line, file: f.file, status: f.status, kinds: (f.checks || []).map((c) => c.kind), sample: v };
171
+ fields.push(info);
172
+ if (f.status === map.STATUS.NOT) unchecked.push({ name: f.name, line: f.line, file: f.file });
173
+ }
174
+ const fullData = Object.assign({}, data);
175
+ if (/^(PATCH|PUT)$/.test(r.method)) {
176
+ const plain = Object.keys(data).find((k) => typeof data[k] === 'string' && !wordsOf(k).some((x) => KEEP_OUT_OF_CHANGES.test(x)) && !/@/.test(data[k]));
177
+ for (const k of Object.keys(data)) if (plain && k !== plain) delete data[k];
178
+ }
179
+ return {
180
+ method: r.method, template: '/' + segs.join('/'), segs, params, path: '/' + segs.join('/'), status: 200, by: null,
181
+ data: Object.keys(data).length ? data : null, encoding: encodingOf(r), flow: r.file + ':' + r.line, from: { file: r.file, line: r.line },
182
+ mapAuth: r.auth ? r.auth.status : map.STATUS.UNKNOWN, leftOut, unchecked: unchecked, fields: fields, fullData: fullData,
183
+ guardRoles: r.role && r.role.status === map.STATUS.DETECTED && Array.isArray(r.role.roles) ? r.role.roles : null,
184
+ scopedByOrg: Boolean(r.db && r.db.some((d) => d.level === 'organisation')),
185
+ };
186
+ }
187
+
188
+ /**
189
+ * The journey the roles check runs, built from the map instead of recordings:
190
+ * { signIn, accounts, actions, notTried: [{ what, why }] }.
191
+ */
192
+ function journeyFromMap(m, root, env, roleNames) {
193
+ const notTried = [];
194
+ const s = signInOf(m);
195
+ const accounts = accountsOf(root, env);
196
+ const actions = [];
197
+ const known = allowedByName(m);
198
+ for (const r of m.routes || []) {
199
+ const what = r.method + ' ' + (r.path || '(path not read)');
200
+ if (r.unmounted) continue;
201
+ if (r.path === null) {
202
+ notTried.push({ what, why: 'its path could not be read' });
203
+ continue;
204
+ }
205
+ if (s && !s.none && r.path === s.path) continue;
206
+ if (SIGN_OUT.test(r.path)) continue;
207
+ if (SIGN_IN.test(r.path)) {
208
+ notTried.push({ what, why: 'another sign-in route - signing in is done once, through ' + (s.none ? 'none found' : s.method + ' ' + s.path) });
209
+ continue;
210
+ }
211
+ // the 'who am I' route answers each account with itself: nothing of another organization's to aim at
212
+ if (r.method === 'GET' && IDENTITY_ROUTE.test(r.path)) {
213
+ notTried.push({ what, why: 'the "who am I" route: each account is answered with itself, so there is nothing of another to compare (the forged-identity try uses it)' });
214
+ continue;
215
+ }
216
+ if (PLUMBING.test(r.path)) {
217
+ notTried.push({ what, why: 'sign-up, password or health plumbing - not an action to try as another account' });
218
+ continue;
219
+ }
220
+ if (r.kind === 'webhook') {
221
+ notTried.push({ what, why: 'a webhook: the payments check sends it, signed and unsigned' });
222
+ continue;
223
+ }
224
+ if (/^(ANY|ALL)$/.test(r.method)) {
225
+ notTried.push({ what, why: 'it answers every method and the map could not tell which one changes something' });
226
+ continue;
227
+ }
228
+ if (r.auth && r.auth.status === map.STATUS.NOT && r.method === 'GET') {
229
+ notTried.push({ what, why: 'open to anyone by its code: nothing to compare between accounts' });
230
+ continue;
231
+ }
232
+ const action = actionOf(r, known, roleNames);
233
+ // A change whose body would miss a field the code reads (a role is never made
234
+ // up) is not sent: the app could store an empty value in its place.
235
+ if (action.method !== 'GET' && action.leftOut.length) {
236
+ notTried.push({ what, why: 'it reads ' + action.leftOut.join(', ') + ', which Kryptheon never makes up a value for - sending it without could store an empty value' });
237
+ continue;
238
+ }
239
+ actions.push(action);
240
+ }
241
+ return { signIn: s.none ? null : s, signInWhy: s.none ? s.why : null, accounts, actions, notTried };
242
+ }
243
+
244
+ /**
245
+ * What a run would do, without sending a request: the sign-in, who signs in,
246
+ * and each action with the permission it needs (from permissions-policy.json)
247
+ * and whether it can be aimed at another organization's record.
248
+ */
249
+ function planOf(root, options) {
250
+ const opts = options || {};
251
+ const m = opts.map || map.mapProject(root, { packageDir: opts.packageDir || __dirname });
252
+ if (!m.ran) return { ran: false, why: m.why };
253
+ const loaded = roles.loadPolicy(root);
254
+ const policy = loaded.ok ? loaded.policy : null;
255
+ // role names, least powerful first: a role field is sent one of them, as the file names it
256
+ const roleNames = policy ? Object.keys(policy.roles).sort((a, b) => policy.roles[a].length - policy.roles[b].length) : null;
257
+ const journey = journeyFromMap(m, root, opts.env, roleNames);
258
+ const lists = new Set(journey.actions.filter((a) => a.method === 'GET' && !a.params.length).map((a) => a.template));
259
+ const actions = journey.actions.map((a) => {
260
+ const p = policy ? roles.permissionFor(a.method, a.template, policy) : null;
261
+ const listing = a.params.length ? '/' + a.segs.slice(0, a.params[0].index).join('/') : null;
262
+ return {
263
+ method: a.method, template: a.template, from: a.from, body: a.data,
264
+ permission: p && p.permission ? p.permission : null,
265
+ permissionWhy: !policy ? 'no ' + roles.POLICY_FILE : p && p.ambiguous ? p.why : !p ? 'no permission in ' + roles.POLICY_FILE + ' is named like this address' : p.how,
266
+ aimed: !a.params.length ? 'needs no record' : a.params.length > 1 ? 'not aimed: more than one record in the address' : lists.has(listing) ? 'at a record listed by GET ' + listing : 'not aimed: no GET ' + listing + ' lists those records',
267
+ deleteNotSent: a.method === 'DELETE',
268
+ };
269
+ });
270
+ return {
271
+ ran: true, map: m, journey, policy: loaded, actions,
272
+ signIn: journey.signIn, signInWhy: journey.signInWhy,
273
+ accounts: journey.accounts.usable.map((a) => ({ email: a.email, envName: a.envName, from: a.from })),
274
+ accountsLeft: journey.accounts.left.map((a) => ({ email: a.email, why: a.why, from: a.from })),
275
+ notTried: journey.notTried,
276
+ };
277
+ }
278
+
279
+ /** The dry run as lines for a terminal. */
280
+ function planLines(plan) {
281
+ const out = [''];
282
+ if (!plan.ran) return out.concat([' Could not read the code: ' + plan.why, '']);
283
+ out.push(' A dry run: nothing was sent. This is what the API check would try.');
284
+ out.push('');
285
+ out.push(plan.signIn ? ' Sign in: ' + plan.signIn.method + ' ' + plan.signIn.path + ' (' + plan.signIn.emailField + ', ' + plan.signIn.passwordField + ' - ' + plan.signIn.file + ':' + plan.signIn.line + ')' : ' Sign in: not found - ' + plan.signInWhy);
286
+ out.push(' Accounts that can sign in: ' + (plan.accounts.length ? plan.accounts.map((a) => a.email).join(', ') : 'none'));
287
+ for (const a of plan.accountsLeft) out.push(' not usable: ' + a.email + ' (' + a.from + ') - ' + a.why);
288
+ out.push(' Permissions: ' + (plan.policy.ok ? roles.POLICY_FILE + ', ' + Object.keys(plan.policy.policy.roles).length + ' roles' + (plan.policy.policy.tenants ? ', organizations kept apart' : '') : (plan.policy.problems || []).join(' ')));
289
+ out.push('');
290
+ out.push(' Actions (' + plan.actions.length + '):');
291
+ for (const a of plan.actions) {
292
+ out.push(' ' + (a.method + ' ' + a.template).padEnd(44) + (a.permission ? a.permission : '-').padEnd(22) + (a.deleteNotSent ? 'never sent (a delete)' : a.aimed));
293
+ }
294
+ if (plan.notTried.length) {
295
+ out.push('');
296
+ out.push(' Not tried (' + plan.notTried.length + '):');
297
+ for (const n of plan.notTried) out.push(' ' + n.what.padEnd(44) + n.why);
298
+ }
299
+ out.push('');
300
+ return out;
301
+ }
302
+
303
+ // --- stage 2: the wall between organizations, and signing in ------------------
304
+
305
+ const V = authz.VERDICT;
306
+
307
+ /**
308
+ * The policy of a run that judges only the wall between organizations and
309
+ * signing in: every signed-in account may do every mapped action, so who may
310
+ * do what is not judged - that needs the owner's permissions file.
311
+ */
312
+ function wallPolicy(journey) {
313
+ return {
314
+ roles: {}, tenants: true, anyRole: true, boundary: 'a signed-in user may reach only records of their own organization',
315
+ extra: { actions: journey.actions.map((a) => ({ permission: a.method + ' ' + a.template, method: a.method, path: a.template })) },
316
+ };
317
+ }
318
+
319
+ /**
320
+ * Every mapped action tried across organizations (each account aimed at the
321
+ * other organizations' records, from the app's own lists) and with no session
322
+ * at all. Only an app on this computer. { ran, why, plan, result, findings }.
323
+ */
324
+ async function runWall(root, options) {
325
+ const opts = options || {};
326
+ const say = typeof opts.say === 'function' ? opts.say : () => {};
327
+ const plan = planOf(root, opts);
328
+ if (!plan.ran) return { ran: false, why: plan.why };
329
+ const j = plan.journey;
330
+ const no = (why) => ({ ran: false, why: why, plan: plan });
331
+ let origin;
332
+ try {
333
+ origin = authz.assertLocalTarget(opts.origin);
334
+ } catch (err) {
335
+ return no('the app must be on this computer (localhost or 127.0.0.1): ' + (opts.origin || 'no address was given'));
336
+ }
337
+ if (!j.signIn) return no(j.signInWhy);
338
+ if (j.accounts.usable.length < 2) return no('fewer than two accounts have a password in .env, so there is nothing to compare');
339
+ say('Signing in as ' + j.accounts.usable.length + ' accounts, and trying ' + j.actions.length + ' routes across organizations and without signing in...');
340
+ const result = await roles.checkAccess({
341
+ origin: origin, policy: wallPolicy(j), journey: { signIn: j.signIn, actions: j.actions }, accounts: j.accounts.usable,
342
+ fetch: opts.fetch, say: say, stopped: opts.stopped, maxAttempts: opts.maxAttempts, anonymous: true, seed: true,
343
+ only: (p) => p.kind !== 'role',
344
+ });
345
+ // Said without the word "policy": the wall check runs with no permissions file.
346
+ result.notes = (result.notes || []).map((n) => String(n).replace(/^The policy keeps organizations apart, but /, 'Kryptheon looked for organizations, but ').replace(/ - the organization check was not tried\.?$/, ', so the wall between organizations was not tried.'));
347
+ return { ran: true, plan: plan, result: result, findings: findingsOf(result.attempts, j) };
348
+ }
349
+
350
+ /** The proof lines of one try. */
351
+ function proofOf(att) {
352
+ const who = att.kind === 'signin' ? 'With no session (not signed in)' : 'As ' + att.actor.email + (att.actor.org ? ' (organization ' + att.actor.org + ')' : '');
353
+ const lines = [who + (att.cross ? ', aiming at a record of ' + att.targetOrg + (att.target.record != null ? ' (' + att.target.record + ')' : '') : att.target.record != null ? ', on record ' + att.target.record : '') + ':'];
354
+ lines.push(' sent: ' + att.request.method + ' ' + att.request.path + (att.request.body && Object.keys(att.request.body).length ? ' body ' + authz.clip(JSON.stringify(att.request.body), 120) : ''));
355
+ lines.push(' answer: ' + att.response.status + (att.response.location ? ' -> ' + att.response.location : '') + ' ' + authz.clip(att.response.bodyExcerpt, 140));
356
+ if (att.changes && att.changes.length) {
357
+ lines.push(' state: changed after the request (read before and after):');
358
+ for (const c of att.changes.slice(0, 3)) lines.push(' ' + c);
359
+ } else if (att.protectedPiecesSeen) lines.push(' state: the answer held ' + att.protectedPiecesSeen + ' piece(s) of records ' + (att.kind === 'signin' ? 'only a signed-in account sees' : 'of the other organization'));
360
+ else lines.push(' state: unchanged');
361
+ return lines;
362
+ }
363
+
364
+ /** The prompt an AI tool is given for a finding. */
365
+ function fixPrompt(f) {
366
+ const lines = ['Kryptheon sent requests to the running app (on this computer). ' + f.headline, ''];
367
+ if (f.kind === 'tenant') {
368
+ lines.push('Fix it in the server code that handles ' + f.where + (f.from ? ' (' + f.from + ')' : '') + ': look the record up together with the signed-in user\'s organization (WHERE id = ? AND org_id = ?), so another organization\'s record is "not found" - for reads, edits and every sub-address of it. A record id sent in the body (shipmentId, orderId) must be checked the same way.');
369
+ } else {
370
+ lines.push('Fix it in the server code that handles ' + f.where + (f.from ? ' (' + f.from + ')' : '') + ': refuse it with 401 unless a user is signed in, checked on the server before anything is read or changed. If it is meant to be public, make sure it shows and changes nothing private.');
371
+ }
372
+ lines.push('', 'What Kryptheon saw:', '');
373
+ for (const p of f.proof) lines.push(p);
374
+ return lines.join('\n');
375
+ }
376
+
377
+ /**
378
+ * The findings of a run: one per route and kind, only from tries that failed
379
+ * (records of the other side came back, or what the app holds changed). An open
380
+ * route the code itself never guarded may be public on purpose: it is to verify.
381
+ */
382
+ function findingsOf(attempts, journey) {
383
+ const groups = new Map();
384
+ for (const a of attempts) {
385
+ if (a.notTried || a.verdict !== V.FAILURE || (a.kind !== 'tenant' && a.kind !== 'signin')) continue;
386
+ const k = a.kind + '|' + a.method + ' ' + a.template;
387
+ if (!groups.has(k)) groups.set(k, []);
388
+ groups.get(k).push(a);
389
+ }
390
+ const out = [];
391
+ for (const list of groups.values()) {
392
+ const first = list[0];
393
+ const where = first.method + ' ' + first.template;
394
+ const action = (journey.actions || []).find((x) => x.method === first.method && x.template === first.template) || {};
395
+ const unguarded = action.mapAuth === map.STATUS.NOT;
396
+ const f = {
397
+ kind: first.kind, where: where, from: action.from ? action.from.file + ':' + action.from.line : null,
398
+ severity: first.kind === 'tenant' ? 'CRITICAL' : unguarded ? 'MEDIUM' : 'HIGH',
399
+ status: first.kind === 'signin' && unguarded ? 'verification required' : 'confirmed',
400
+ headline: first.kind === 'tenant'
401
+ ? 'A user of one organization can reach another organization\'s record: ' + where + ' answered for a record of a different organization.'
402
+ : unguarded
403
+ ? '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.'
404
+ : 'Anyone can use ' + where + ' without signing in, though the code looks like it checks for a signed-in user.',
405
+ proof: [],
406
+ };
407
+ for (const a of list.slice(0, 3)) f.proof.push.apply(f.proof, proofOf(a));
408
+ if (list.length > 3) f.proof.push('... and ' + (list.length - 3) + ' more like it.');
409
+ f.fixPrompt = fixPrompt(f);
410
+ out.push(f);
411
+ }
412
+ return out;
413
+ }
414
+
415
+ /** What a run left in the app: writes it made that it could not put back. */
416
+ function leftBehind(attempts) {
417
+ return attempts.filter((a) => !a.notTried && a.method !== 'GET' && (a.response.reading === 'accepted' || a.response.reading === 'redirect') && a.restored !== true)
418
+ .map((a) => a.method + ' ' + a.request.path + ' -> ' + a.response.status + (a.restored === null ? ' (its effect was not visible, nothing was put back)' : ' (not put back' + (a.notPutBack && a.notPutBack.length ? ': ' + a.notPutBack[0] : '') + ')'));
419
+ }
420
+
421
+ /** A run as lines for a terminal. */
422
+ function wallLines(run) {
423
+ const out = [''];
424
+ if (!run.ran) return out.concat([' Nothing was tried: ' + run.why, '']);
425
+ const r = run.result;
426
+ const tried = r.attempts.filter((a) => !a.notTried);
427
+ const count = (kind, verdict) => tried.filter((a) => a.kind === kind && a.verdict === verdict).length;
428
+ out.push(' Signed in: ' + (r.accounts.map((a) => a.email + (a.org ? ' (' + a.org + ')' : '')).join(', ') || 'nobody'));
429
+ out.push(' Across organizations: ' + count('tenant', V.DENIED) + ' held, ' + count('tenant', V.FAILURE) + ' got through, ' + count('tenant', V.AMBIGUOUS) + ' could not be judged');
430
+ out.push(' Without signing in: ' + count('signin', V.DENIED) + ' held, ' + count('signin', V.FAILURE) + ' got through, ' + count('signin', V.AMBIGUOUS) + ' could not be judged');
431
+ if (tried.some((a) => a.kind === 'role')) {
432
+ const roleTries = tried.filter((a) => a.kind === 'role');
433
+ const n = (expected, verdict) => roleTries.filter((a) => a.expected === expected && a.verdict === verdict).length;
434
+ out.push(' By role: ' + n('deny', V.DENIED) + ' refused as the policy says, ' + n('deny', V.FAILURE) + ' got through that the policy forbids, ' + n('allow', V.ALLOWED) + ' allowed as the policy says, ' + roleTries.filter((a) => a.verdict === V.AMBIGUOUS).length + ' could not be judged');
435
+ }
436
+ out.push(' Requests sent: ' + r.requests);
437
+ out.push('');
438
+ if (!run.findings.length) out.push(' Nothing got through that the app showed. That covers the routes and records tried - not the ones listed below as not tried.');
439
+ for (const f of run.findings) {
440
+ out.push(' ' + (f.status === 'confirmed' ? 'PROBLEM' : 'TO CHECK') + ' ' + f.severity + ' ' + f.headline + (f.from ? ' (' + f.from + ')' : ''));
441
+ for (const p of f.proof) out.push(' ' + p);
442
+ out.push('');
443
+ }
444
+ const unjudged = tried.filter((a) => a.verdict === V.AMBIGUOUS).concat(r.attempts.filter((a) => a.notTried));
445
+ if (unjudged.length || r.skipped.length || r.notes.length) {
446
+ out.push(' Not tried or not judged:');
447
+ const seen = new Set();
448
+ for (const a of unjudged) {
449
+ const line = (a.kind === 'signin' ? 'no session: ' : a.kind === 'role' ? a.actor.role + ': ' : 'across organizations: ') + a.method + ' ' + a.template + ' - ' + a.reason;
450
+ if (!seen.has(line)) out.push(' ' + line);
451
+ seen.add(line);
452
+ }
453
+ for (const s of r.skipped) out.push(' ' + s.what + ' - ' + s.why);
454
+ for (const n of r.notes) out.push(' ' + n);
455
+ out.push('');
456
+ }
457
+ const left = leftBehind(r.attempts);
458
+ if (left.length) {
459
+ out.push(' Test data left in the app:');
460
+ for (const l of left) out.push(' ' + l);
461
+ out.push('');
462
+ }
463
+ return out;
464
+ }
465
+
466
+ // --- stage 3: who may do what, from the owner's permissions file ---------------
467
+
468
+ const DRAFT_FILE = 'permissions-policy.draft.json';
469
+
470
+ /** `shipments.read`, `shipments.archive`, `team.role`: a name for what a route does. */
471
+ function permissionName(method, template) {
472
+ const statics = template.split('/').filter((s) => s && s !== '{id}' && s !== 'api' && !/^v\d+$/i.test(s)).map((s) => s.toLowerCase().replace(/[^a-z0-9]+/g, '_').replace(/^_|_$/g, ''));
473
+ const resource = statics[0] || 'app';
474
+ const tail = statics.length > 1 ? statics[statics.length - 1] : null;
475
+ const verb = { GET: 'read', HEAD: 'read', POST: 'create', PUT: 'update', PATCH: 'update', DELETE: 'delete' }[method] || method.toLowerCase();
476
+ if (!tail) return resource + '.' + verb;
477
+ return method === 'GET' || method === 'HEAD' ? resource + '.' + tail + '.read' : resource + '.' + tail;
478
+ }
479
+
480
+ /**
481
+ * A draft permissions file written from the code: each role the code names,
482
+ * with the permissions of the routes it lets in today. It says what the code
483
+ * DOES, not what the owner WANTS, so it is marked "draft" and the roles check
484
+ * refuses it until the owner edits it, deletes that line and renames the file.
485
+ * { ok, why, text, roles, permissions }.
486
+ */
487
+ function draftPolicy(plan) {
488
+ if (!plan.ran) return { ok: false, why: plan.why };
489
+ const actions = plan.journey.actions;
490
+ const names = new Set();
491
+ for (const a of actions) for (const r of a.guardRoles || []) names.add(String(r));
492
+ if (names.size < 2) return { ok: false, why: 'the code names ' + (names.size ? 'only one role (' + Array.from(names)[0] + ')' : 'no role') + ' in a role check, so there is nothing to compare. Write ' + roles.POLICY_FILE + ' by hand: list each role and what it may do.' };
493
+ const rolesOut = {};
494
+ for (const r of names) rolesOut[r] = [];
495
+ const list = [];
496
+ for (const a of actions) {
497
+ const perm = permissionName(a.method, a.template);
498
+ list.push({ permission: perm, method: a.method, path: a.template });
499
+ for (const r of names) {
500
+ if ((!a.guardRoles || a.guardRoles.includes(r)) && !rolesOut[r].includes(perm)) rolesOut[r].push(perm);
501
+ }
502
+ }
503
+ const noCheck = actions.filter((a) => !a.guardRoles).map((a) => a.method + ' ' + a.template);
504
+ const json = {
505
+ draft: 'Written by Kryptheon on ' + new Date().toISOString().slice(0, 10) + ' from your code. It lists what your code does TODAY, not what you want. Edit every role to what you want, delete this line, and rename the file to ' + roles.POLICY_FILE + '.',
506
+ notes: [
507
+ 'A role check in the code names the roles on that route. A route with no role check in the code is given to every role below: decide who should NOT have it.',
508
+ noCheck.length ? 'No role check in the code on: ' + noCheck.join(', ') : 'Every route has a role check in the code.',
509
+ 'Only roles that some route names are listed. Add any other role your app has (a read-only one, say) with the permissions it should have.',
510
+ ].concat(plan.notTried.filter((n) => /never makes up/.test(n.why)).map((n) => 'Not in the list and not tried, because it reads a field Kryptheon never makes up a value for: ' + n.what + '. Add it under kryptheon.actions with the permission you want.')),
511
+ roles: rolesOut,
512
+ };
513
+ if (actions.some((a) => a.scopedByOrg)) json.tenantBoundary = 'A signed-in user may reach only records of their own organization. Delete this line if your app has one organization.';
514
+ json.kryptheon = { actions: list };
515
+ return { ok: true, text: JSON.stringify(json, null, 2) + '\n', roles: Object.keys(rolesOut), permissions: Array.from(new Set(list.map((x) => x.permission))), noRoleCheck: noCheck };
516
+ }
517
+
518
+ /** Writes the draft next to package.json - never under the real name, never over an existing file. */
519
+ function writeDraft(root, text) {
520
+ const file = path.join(root, DRAFT_FILE);
521
+ if (fs.existsSync(file)) return { ok: false, file: file, why: DRAFT_FILE + ' is already there; it was not changed. Delete it first if you want a new one.' };
522
+ fs.writeFileSync(file, text, { encoding: 'utf8', flag: 'wx' });
523
+ return { ok: true, file: file };
524
+ }
525
+
526
+ /**
527
+ * Every mapped action tried as every account against the owner's permissions
528
+ * file, across organizations, and with no session. Needs the policy file; only
529
+ * an app on this computer. { ran, why, plan, result, findings, policy }.
530
+ */
531
+ async function runRoles(root, options) {
532
+ const opts = options || {};
533
+ const say = typeof opts.say === 'function' ? opts.say : () => {};
534
+ const plan = planOf(root, opts);
535
+ if (!plan.ran) return { ran: false, why: plan.why };
536
+ const no = (why) => ({ ran: false, why: why, plan: plan });
537
+ if (!plan.policy.ok) {
538
+ return no(plan.policy.missing ? roles.POLICY_FILE + ' is not in this folder. Kryptheon does not guess who may do what. Write it, or let Kryptheon write a draft to edit (' + DRAFT_FILE + ').' : roles.POLICY_FILE + ' cannot be used: ' + (plan.policy.problems || []).join(' '));
539
+ }
540
+ const j = plan.journey;
541
+ let origin;
542
+ try {
543
+ origin = authz.assertLocalTarget(opts.origin);
544
+ } catch (err) {
545
+ return no('the app must be on this computer (localhost or 127.0.0.1): ' + (opts.origin || 'no address was given'));
546
+ }
547
+ if (!j.signIn) return no(j.signInWhy);
548
+ if (j.accounts.usable.length < 2) return no('fewer than two accounts have a password in .env, so there is nothing to compare');
549
+ say('Signing in as ' + j.accounts.usable.length + ' accounts, and trying ' + j.actions.length + ' routes as each, across organizations and without signing in...');
550
+ const result = await roles.checkAccess({
551
+ origin: origin, policy: plan.policy.policy, journey: { signIn: j.signIn, actions: j.actions }, accounts: j.accounts.usable,
552
+ fetch: opts.fetch, say: say, stopped: opts.stopped, maxAttempts: opts.maxAttempts, anonymous: true, seed: true,
553
+ });
554
+ roles.markHints(result.attempts);
555
+ const findings = roles.findingsOf(result.attempts.filter((a) => a.kind !== 'signin'), plan.policy.policy).concat(findingsOf(result.attempts.filter((a) => a.kind === 'signin'), j));
556
+ for (const f of findings) {
557
+ const a = j.actions.find((x) => x.method + ' ' + x.template === f.where);
558
+ if (!f.from && a) f.from = a.from.file + ':' + a.from.line;
559
+ }
560
+ return { ran: true, plan: plan, result: result, findings: findings, policy: plan.policy.policy };
561
+ }
562
+
563
+ // --- stage 4: what the app does with a value it should turn away ----------
564
+
565
+ // A value no field of that name should hold, marked so the app's own list can show whether it was kept.
566
+ function badValueFor(name) {
567
+ const w = wordsOf(name);
568
+ const mark = 'kx-invalid';
569
+ if (w.includes('email') || w.includes('mail')) return { value: mark + '-not-an-email', why: 'not an email address' };
570
+ if (w.some((x) => NUMBER_WORDS.has(x))) return { value: mark + '-not-a-number', why: 'text where a number belongs' };
571
+ if (w.some((x) => /^(date|day|at|on)$/.test(x))) return { value: mark + '-not-a-date', why: 'not a date' };
572
+ return { value: mark + '-' + 'k'.repeat(3000), why: '3000 characters long' };
573
+ }
574
+
575
+ const isTaken = (res) => { const r = authz.replyOf(res); return r.kind === 'accepted' || r.kind === 'redirect'; };
576
+
577
+ /**
578
+ * Bad values sent to the routes that create something, one field at a time,
579
+ * for the fields the code reads and checks nothing about. Judged from the
580
+ * app's own list before and after: the value is kept (a finding), turned away
581
+ * (fine), or taken with nothing to show (to check). A server error is its
582
+ * own finding. Only an app on this computer; one signed-in account; never a
583
+ * delete. Changes to existing records are not tried here.
584
+ * { ran, why, tried, findings, notTried, left }.
585
+ */
586
+ async function runInvalid(root, options) {
587
+ const opts = options || {};
588
+ const say = typeof opts.say === 'function' ? opts.say : () => {};
589
+ const plan = planOf(root, opts);
590
+ if (!plan.ran) return { ran: false, why: plan.why };
591
+ const j = plan.journey;
592
+ const no = (why) => ({ ran: false, why: why });
593
+ let origin;
594
+ try {
595
+ origin = authz.assertLocalTarget(opts.origin);
596
+ } catch (err) {
597
+ return no('the app must be on this computer (localhost or 127.0.0.1): ' + (opts.origin || 'no address was given'));
598
+ }
599
+ if (!j.signIn) return no(j.signInWhy);
600
+ if (!j.accounts.usable.length) return no('no account has a password in .env');
601
+ const who = j.accounts.usable[0];
602
+ const ctx = authz.makeContext({ baseUrl: origin, allowOrigins: [origin], fetch: opts.fetch, passwords: { [who.email]: who.password }, maxRequests: opts.maxRequests || 600 });
603
+ const session = authz.makeSession(ctx, 'account', who.email, who.password);
604
+ const fields = {};
605
+ fields[j.signIn.emailField] = who.email;
606
+ fields[j.signIn.passwordField] = who.password;
607
+ const signed = await session.send(j.signIn.method, j.signIn.path, j.signIn.encoding === 'form' ? { form: fields } : { json: fields });
608
+ const sr = authz.replyOf(signed);
609
+ if (sr.kind === 'refused' || sr.kind === 'server-error' || sr.kind === 'no-answer') return no('could not sign in as ' + who.email + ' (' + sr.note + ')');
610
+ say('Signed in as ' + who.email + '. Sending bad values to routes that create something...');
611
+
612
+ const out = { ran: true, account: who.email, tried: [], findings: [], notTried: [], left: [] };
613
+ const read = async (template) => {
614
+ const res = await session.send('GET', template, {});
615
+ return res.status === 200 ? JSON.stringify(res.json !== undefined ? res.json : res.text) : null;
616
+ };
617
+ for (const a of j.actions) {
618
+ if (!/^(POST|PUT|PATCH)$/.test(a.method) || !(a.fields || []).length) continue;
619
+ const what = a.method + ' ' + a.template;
620
+ const probes = probesOf(a);
621
+ if (!probes.length) continue;
622
+ if (a.params.length || a.method !== 'POST') {
623
+ out.notTried.push({ what: what, why: 'it changes an existing record, and bad values are only tried on routes that create one' });
624
+ continue;
625
+ }
626
+ // A bad value means something only if the same request without it is taken. The full body first; if the app
627
+ // refuses it (a made-up reference such as a sku), a body of just the fields the code checks or plainly requires.
628
+ const send = (b) => session.send(a.method, a.template, a.encoding === 'form' ? { form: b } : { json: b });
629
+ const minimal = {};
630
+ for (const f of a.fields) if (f.kinds.length || wordsOf(f.name).some((x) => REQUIRED_NAME.test(x)) || f.kinds.includes('enum')) minimal[f.name] = f.sample;
631
+ let base = a.fullData;
632
+ let first = await send(base);
633
+ if (!isTaken(first) && Object.keys(minimal).length && JSON.stringify(minimal) !== JSON.stringify(base)) {
634
+ base = minimal;
635
+ first = await send(base);
636
+ }
637
+ if (!isTaken(first)) {
638
+ out.notTried.push({ what: what, why: 'the app refused the request (' + first.status + ') before any bad value was added, so no bad value could be judged' });
639
+ continue;
640
+ }
641
+ out.left.push(what + ' with a valid body (' + Object.keys(base).join(', ') + ') - the starting point of the bad-value tries');
642
+ for (const pr of probes) {
643
+ if (opts.stopped && opts.stopped()) return out;
644
+ const f = pr.field;
645
+ const body = Object.assign({}, base, { [f.name]: pr.value });
646
+ const before = await read(a.template);
647
+ const res = await session.send(a.method, a.template, a.encoding === 'form' ? { form: body } : { json: body });
648
+ const reply = authz.replyOf(res);
649
+ if (reply.kind === 'no-answer' && await appDown(session, a.template)) {
650
+ out.findings.push(downFinding(who.email, what, f, pr, res));
651
+ out.appDown = what + ' with ' + f.name + ' ' + pr.why;
652
+ out.tried.push({ what: what, field: f.name, mode: pr.mode, sent: pr.why, status: 0, reading: 'no-answer', kept: false, verdict: 'stopped the app', from: f.file + ':' + f.line });
653
+ return out;
654
+ }
655
+ const after = await read(a.template);
656
+ const took = reply.kind === 'accepted' || reply.kind === 'redirect';
657
+ const shown = before !== null && after !== null;
658
+ // a marked value is looked for in the list; a blank or negative one has no mark, so the list changing is the sign
659
+ const kept = shown && (pr.mode === 'type' ? after.includes(pr.value.slice(0, 40)) && !before.includes(pr.value.slice(0, 40)) : after !== before);
660
+ let verdict = 'turned away';
661
+ if (reply.kind === 'server-error') verdict = 'crashed';
662
+ else if (took && kept) verdict = 'kept';
663
+ else if (took) verdict = 'taken, not shown';
664
+ out.tried.push({ what: what, field: f.name, mode: pr.mode, sent: pr.why, status: res.status, reading: reply.kind, kept: kept, verdict: verdict, from: f.file + ':' + f.line });
665
+ if (took) out.left.push(what + ' with ' + f.name + ' = ' + pr.why + (kept ? ' (now in the list; Kryptheon never deletes)' : ' (its effect was not visible)'));
666
+ if (verdict !== 'kept' && verdict !== 'crashed') continue;
667
+ // the code itself claims to check this field (a required, length, range or type test) and the bad value got past: confirmed.
668
+ // A field it never checks, or a long text, is a thing to check: the app may mean to accept it.
669
+ // a range test (a stock or limit check) does not say "not below zero", so a negative number is never confirmed from the code alone
670
+ const claimed = pr.mode === 'blank' && f.kinds.some((k) => /^(required|length)$/.test(k));
671
+ const longText = pr.mode === 'type' && /characters long/.test(pr.why);
672
+ const finding = {
673
+ kind: verdict === 'crashed' ? 'invalid-crash' : 'invalid-kept', where: what, field: f.name, from: f.file + ':' + f.line,
674
+ status: verdict === 'crashed' || claimed ? 'confirmed' : 'verification required',
675
+ severity: verdict === 'crashed' || longText ? 'LOW' : 'MEDIUM',
676
+ headline: verdict === 'kept'
677
+ ? what + ' took ' + f.name + ' ' + pr.why + ' and saved the record' + (claimed ? ', though the code checks that field.' : ': nothing in the code stops it.')
678
+ : what + ' crashed (' + res.status + ') when ' + f.name + ' was ' + pr.why + (f.status === map.STATUS.NOT ? ': nothing in the code checks that field.' : '.'),
679
+ proof: ['As ' + who.email + ', sent ' + what + ' with ' + f.name + ' ' + pr.why + ' and every other field valid.', 'The app answered ' + res.status + ' (' + reply.note + ').'].concat(verdict === 'kept' ? ['The app\'s own list ' + a.template + ' ' + (pr.mode === 'type' ? 'shows the value after, and did not before.' : 'changed: a record was added.')] : []),
680
+ };
681
+ finding.fixPrompt = ['Kryptheon sent a bad value to my running app and it was not turned away:', '', finding.headline, '', 'Where: ' + finding.from, '',
682
+ 'First check this is real' + (finding.status === 'confirmed' ? '' : ' (the app may mean to accept it)') + '. If it is, check ' + f.name + ' where the handler reads it: reject a value that is not what the field should hold (blank, negative, the wrong type, too long), on the server, with a 400 and a message, before anything is stored. Change nothing else.', '', 'Afterwards I will run: npx kryptheon api'].join('\n');
683
+ out.findings.push(finding);
684
+ }
685
+ }
686
+ // A write aimed at a record that cannot exist: the app should say no (404, 400). A success is a write on nothing.
687
+ for (const a of j.actions) {
688
+ if (!/^(POST|PUT|PATCH)$/.test(a.method) || a.params.length !== 1) continue;
689
+ if (opts.stopped && opts.stopped()) return out;
690
+ const what = a.method + ' ' + a.template;
691
+ const at = a.template.replace('{id}', GHOST_ID);
692
+ const res = await session.send(a.method, at, a.encoding === 'form' ? { form: a.fullData } : { json: a.fullData });
693
+ const reply = authz.replyOf(res);
694
+ const took = reply.kind === 'accepted' || reply.kind === 'redirect';
695
+ out.tried.push({ what: what, field: '{id}', mode: 'ghost', sent: 'a record that does not exist (' + GHOST_ID + ')', status: res.status, reading: reply.kind, kept: false, verdict: took ? 'taken, not shown' : 'turned away', from: a.from.file + ':' + a.from.line });
696
+ if (!took) continue;
697
+ out.left.push(what.replace('{id}', GHOST_ID) + ' answered ' + res.status + ' (its effect was not visible)');
698
+ const creates = a.method === 'POST';
699
+ const finding = {
700
+ kind: 'ghost-record', where: what, field: '{id}', from: a.from.file + ':' + a.from.line, status: 'verification required', severity: creates ? 'MEDIUM' : 'LOW',
701
+ headline: what + ' answered ' + res.status + ' for record ' + GHOST_ID + ', which does not exist' + (creates ? ': it may have saved something that belongs to nothing.' : '.'),
702
+ proof: ['As ' + who.email + ', sent ' + a.method + ' ' + at + ' with an otherwise valid body.', 'The app answered ' + res.status + ' (' + reply.note + ') instead of saying the record is not there.'],
703
+ };
704
+ finding.fixPrompt = ['Kryptheon sent a request for a record that cannot exist to my running app and it was taken:', '', finding.headline, '', 'Where: ' + finding.from, '',
705
+ 'First check this is real: look at what the handler does when the record is not found. If it saves or answers success anyway, look the record up first and answer 404 when it is not there (and, for a table that points at it, add a foreign key). Change nothing else.', '', 'Afterwards I will run: npx kryptheon api'].join(String.fromCharCode(10));
706
+ out.findings.push(finding);
707
+ }
708
+ return out;
709
+ }
710
+
711
+ const GHOST_ID = '999999999';
712
+
713
+ // --- a job run twice ------------------------------------------------------------
714
+
715
+ // A route that runs a job: /jobs/..., or one named for what jobs do.
716
+ const JOB_PATH = /(^|\/)(jobs?|cron|tasks?\/run|reminders?|digest|notify[\w-]*|sync|overdue|batch)(\/|$)/i;
717
+ // What a job's answer counts: created 3, sent: 12, notified: 2.
718
+ const COUNT_KEY = /^(created|inserted|sent|queued|notified|processed|affected|rows|count|total|emails|events|reminders|jobs)$/i;
719
+
720
+ /** The counts a job's answer gives: { created: 3 }. */
721
+ function countsIn(json) {
722
+ const out = {};
723
+ if (!json || typeof json !== 'object' || Array.isArray(json)) return out;
724
+ for (const k of Object.keys(json)) if (COUNT_KEY.test(k) && typeof json[k] === 'number') out[k] = json[k];
725
+ return out;
726
+ }
727
+
728
+ /**
729
+ * Each job route run twice in a row by an account it lets in, nothing changed in between. The same
730
+ * non-zero count the second time means the job made the same things again (a second notification
731
+ * for the same overdue return): a thing to check, said with both answers. { ran, why, tried, findings, left }.
732
+ */
733
+ async function runJobs(root, options) {
734
+ const opts = options || {};
735
+ const plan = planOf(root, opts);
736
+ if (!plan.ran) return { ran: false, why: plan.why };
737
+ const j = plan.journey;
738
+ const routes = j.actions.filter((a) => a.method === 'POST' && !a.params.length && JOB_PATH.test(a.template));
739
+ if (!routes.length) return { ran: false, why: 'the map found no route that runs a job' };
740
+ let origin;
741
+ try {
742
+ origin = authz.assertLocalTarget(opts.origin);
743
+ } catch (err) {
744
+ return { ran: false, why: 'the app must be on this computer (localhost or 127.0.0.1): ' + (opts.origin || 'no address was given') };
745
+ }
746
+ if (!j.signIn) return { ran: false, why: j.signInWhy };
747
+ const ctx = authz.makeContext({ baseUrl: origin, allowOrigins: [origin], fetch: opts.fetch, passwords: Object.fromEntries(j.accounts.usable.map((a) => [a.email, a.password])), maxRequests: 80 });
748
+ const sessions = [];
749
+ for (const a of j.accounts.usable) {
750
+ const s = authz.makeSession(ctx, 'account', a.email, a.password);
751
+ const f = { [j.signIn.emailField]: a.email, [j.signIn.passwordField]: a.password };
752
+ const r = authz.replyOf(await s.send(j.signIn.method, j.signIn.path, j.signIn.encoding === 'form' ? { form: f } : { json: f }));
753
+ if (r.kind !== 'refused' && r.kind !== 'server-error' && r.kind !== 'no-answer') sessions.push(s);
754
+ }
755
+ if (!sessions.length) return { ran: false, why: 'no account with a password in .env could sign in' };
756
+ const out = { ran: true, tried: [], findings: [], left: [] };
757
+ for (const a of routes) {
758
+ const what = a.method + ' ' + a.template;
759
+ const send = (s) => s.send(a.method, a.template, a.encoding === 'form' ? { form: a.fullData || {} } : { json: a.fullData || {} });
760
+ let first = null;
761
+ let by = null;
762
+ for (const s of sessions) {
763
+ const res = await send(s);
764
+ const reply = authz.replyOf(res);
765
+ if (reply.kind === 'accepted' || reply.kind === 'redirect') { first = res; by = s; break; }
766
+ }
767
+ if (!first) { out.tried.push({ what, verdict: 'no account was let in' }); continue; }
768
+ const second = await send(by);
769
+ const c1 = countsIn(first.json);
770
+ const c2 = countsIn(second.json);
771
+ const again = Object.keys(c1).filter((k) => c1[k] > 0 && c2[k] === c1[k]);
772
+ out.tried.push({ what, by: by.email, first: c1, second: c2, verdict: again.length ? 'made the same again' : Object.keys(c1).length ? 'held' : 'no count to compare' });
773
+ out.left.push(what + ' run twice as ' + by.email + (Object.keys(c1).length ? ' (' + JSON.stringify(c1) + ', then ' + JSON.stringify(c2) + ')' : ''));
774
+ if (!again.length) continue;
775
+ const k = again[0];
776
+ const finding = {
777
+ kind: 'job-repeat', where: what, from: a.from.file + ':' + a.from.line, status: 'verification required', severity: 'MEDIUM',
778
+ headline: 'Run twice in a row, ' + what + ' made the same ' + c2[k] + ' again (' + k + ': ' + c1[k] + ', then ' + k + ': ' + c2[k] + '), with nothing changed in between: the same reminders or records may be made every time it runs.',
779
+ proof: ['As ' + by.email + ', sent ' + what + ' twice, one right after the other.', 'first answer: ' + authz.clip(first.text, 120), 'second answer: ' + authz.clip(second.text, 120)],
780
+ };
781
+ finding.fixPrompt = ['Kryptheon ran a job of my app twice in a row:', '', finding.headline, '', 'Where: ' + finding.from, '',
782
+ 'First check this is real: look at what the job writes. If running it again makes the same rows again, make it idempotent - remember what it already did (a unique key on the item and the run, or a "notified_at" on the item) and skip what was done. Change nothing else.', '', 'Afterwards I will run: npx kryptheon api'].join(String.fromCharCode(10));
783
+ out.findings.push(finding);
784
+ }
785
+ return out;
786
+ }
787
+
788
+
789
+ /** Whether the app has stopped answering: a request that got no answer, and then a plain read that gets none either. */
790
+ async function appDown(session, template) {
791
+ const again = await session.send('GET', template, {});
792
+ if (again.status !== 0) return false;
793
+ const root = await session.send('GET', '/', {});
794
+ return root.status === 0;
795
+ }
796
+
797
+ function downFinding(email, what, f, pr, res) {
798
+ const finding = {
799
+ kind: 'app-down', where: what, field: f.name, from: f.file + ':' + f.line, status: 'confirmed', severity: 'HIGH',
800
+ headline: what + ' with ' + f.name + ' ' + pr.why + ' stopped the app: it answered nothing afterwards, to that or any request. One request like this takes the whole app down for everyone.',
801
+ proof: ['As ' + email + ', sent ' + what + ' with ' + f.name + ' ' + pr.why + ' and every other field valid.', 'No answer (' + (res.error || 'the connection failed') + '), and no answer to a plain read of ' + what.split(' ')[1] + ' and of / right after.', 'Nothing more was sent: the rest of the bad-value tries were not made.'],
802
+ };
803
+ finding.fixPrompt = ['Kryptheon sent one bad value to my running app and the whole app stopped:', '', finding.headline, '', 'Where: ' + finding.from, '',
804
+ 'First check this is real: start the app, send that request, and see the process exit. Then find why an error in this handler is not caught - an async handler whose rejected promise reaches nothing (Express 4 does not catch those), or a database error thrown outside try/catch. Catch it and answer 400 for a value that is not what the field should hold, and add a last-resort error handler so one bad request can never stop the server. Change nothing else.', '', 'Afterwards I will run: npx kryptheon api'].join(String.fromCharCode(10));
805
+ return finding;
806
+ }
807
+
808
+ // Names of fields that are plainly required, so a blank one is worth trying even where the code checks nothing.
809
+ const REQUIRED_NAME = /^(name|title|subject|address|city|street|postcode|postal|zip|recipient|reason|label)$/;
810
+ const NOT_PLAIN_TEXT = /^(email|mail|date|day|at|on|url|link|website|phone|mobile|role|status|state|currency|country|id|key|token)$/;
811
+
812
+ /**
813
+ * The tries on one route that creates something, one field at a time:
814
+ * type - a field the code never checks gets a value of the wrong kind (marked, so the list shows it)
815
+ * blank - a text field the code requires, or is plainly required by its name, is left empty
816
+ * below - a number field gets -1
817
+ */
818
+ function probesOf(a) {
819
+ const out = [];
820
+ for (const f of a.fields || []) {
821
+ const w = wordsOf(f.name);
822
+ if (f.status === map.STATUS.NOT) {
823
+ const bad = badValueFor(f.name);
824
+ out.push({ field: f, mode: 'type', value: bad.value, why: bad.why });
825
+ }
826
+ const checked = f.kinds.some((k) => /^(required|length|range|type)$/.test(k));
827
+ if (typeof f.sample === 'string' && !f.kinds.includes('enum') && !w.some((x) => NOT_PLAIN_TEXT.test(x)) && (checked || w.some((x) => REQUIRED_NAME.test(x)))) {
828
+ out.push({ field: f, mode: 'blank', value: '', why: 'left blank' });
829
+ }
830
+ if (typeof f.sample === 'number' && w[w.length - 1] !== 'id' && (w.some((x) => NUMBER_WORDS.has(x)) || f.kinds.includes('range'))) {
831
+ out.push({ field: f, mode: 'below', value: -1, why: 'a negative number (-1)' });
832
+ }
833
+ }
834
+ return out;
835
+ }
836
+
837
+ // --- stage 5: one report, a verdict on each thing the map pointed at ------------
838
+
839
+ const REPORT_FILE = 'api-last.json';
840
+ const normRoute = (name) => String(name).replace(/:\w+\*?/g, '{id}');
841
+
842
+ /**
843
+ * What the running app said about each "to check" item of the map, by route:
844
+ * confirmed - a try on the running app got through (a finding of this run)
845
+ * held - the matching tries were made and the app refused them
846
+ * not tried - with why (never a guess)
847
+ * The map's kinds and the tries that answer them: no-sign-in <- tries with no
848
+ * session; tenant-from-request and tenant-unscoped <- tries across
849
+ * organizations; no-role <- the roles run (needs the owner's permissions file);
850
+ * a webhook is the payments check's.
851
+ */
852
+ function verdictsFor(toCheck, wall, invalid, roleRun, forged, webhooks) {
853
+ const out = [];
854
+ const attempts = wall && wall.ran ? wall.result.attempts : [];
855
+ const failures = (wall && wall.findings) || [];
856
+ const notes = (wall && wall.plan && wall.plan.journey ? wall.plan.journey.notTried : []).map((n) => ({ what: normRoute(n.what), why: n.why }));
857
+ const asked = (wall && wall.plan && wall.plan.journey ? wall.plan.journey.actions : []).map((a) => a.method + ' ' + a.template);
858
+ for (const t of toCheck || []) {
859
+ const route = normRoute(t.route);
860
+ const v = { route: t.route, kind: t.kind, headline: t.headline };
861
+ const kind = /^no-sign-in$/.test(t.kind) ? 'signin' : /^tenant-/.test(t.kind) ? 'tenant' : /^no-role$/.test(t.kind) ? 'role' : null;
862
+ if (t.kind === 'identity-from-request') {
863
+ const hit = forged && forged.ran && forged.findings.find((f) => f.from === t.where);
864
+ v.verdict = hit ? 'confirmed' : forged && forged.ran && forged.tried.length ? 'held' : 'not tried';
865
+ if (hit) v.finding = hit.headline;
866
+ if (v.verdict === 'not tried') v.why = forged ? forged.why || 'not tried' : 'the forged identity was not tried';
867
+ } else if (/^webhook/.test(t.kind)) {
868
+ const hook = webhooks && webhooks.ran ? webhooks.hooks.find((x) => x.route === t.route) : null;
869
+ const hit = hook && hook.findings.find((f) => /^webhook-(unsigned|forged)$/.test(f.kind) && f.status === 'confirmed');
870
+ if (hit) {
871
+ v.verdict = 'confirmed';
872
+ v.finding = hit.headline;
873
+ } else if (hook && hook.control && hook.control.took && hook.tried.some((x) => x.mode === 'none') && !hook.findings.some((f) => /^webhook-(unsigned|forged)$/.test(f.kind))) {
874
+ v.verdict = 'held';
875
+ } else if (hook && hook.findings.some((f) => /^webhook-(unsigned|forged)$/.test(f.kind))) {
876
+ v.verdict = 'not judged';
877
+ v.why = 'the app answered success without a valid signature, but no change was seen in ' + hook.list;
878
+ } else {
879
+ v.verdict = 'not tried';
880
+ v.why = (hook && hook.notTried ? hook.notTried : webhooks && !webhooks.ran ? webhooks.why : 'a webhook this run did not send to') + '; a provider\'s own webhook is the payments check\'s (npx kryptheon payments)';
881
+ }
882
+ } else if (kind === 'role') {
883
+ // "held" only when the roles run really tried this route as a role the policy forbids
884
+ const tries = roleRun && roleRun.ran ? roleRun.result.attempts.filter((a) => !a.notTried && a.kind === 'role' && a.method + ' ' + a.template === route) : [];
885
+ const rf = roleRun && roleRun.ran ? roleRun.findings.filter((f) => f.where === route && f.kind !== 'tenant' && f.kind !== 'signin') : [];
886
+ if (!roleRun || !roleRun.ran) {
887
+ v.verdict = 'not tried';
888
+ v.why = 'it needs the roles run (--roles) and your permissions-policy.json';
889
+ } else if (rf.length) {
890
+ v.verdict = 'confirmed';
891
+ v.finding = rf[0].headline;
892
+ } else if (tries.length && tries.every((a) => a.verdict === V.DENIED || a.verdict === V.ALLOWED)) {
893
+ v.verdict = 'held';
894
+ } else if (tries.length) {
895
+ v.verdict = 'not judged';
896
+ v.why = (tries.find((a) => a.verdict === V.AMBIGUOUS) || tries[0]).reason;
897
+ } else {
898
+ v.verdict = 'not tried';
899
+ const n = (roleRun.plan && roleRun.plan.journey ? roleRun.plan.journey.notTried : []).find((x) => normRoute(x.what) === route);
900
+ v.why = n ? n.why : 'the roles run made no try of this route as a role that may not use it';
901
+ }
902
+ } else if (!wall || !wall.ran) {
903
+ v.verdict = 'not tried';
904
+ v.why = wall ? wall.why : 'the running app was not tried';
905
+ } else {
906
+ const mine = attempts.filter((a) => !a.notTried && a.kind === kind && a.method + ' ' + a.template === route);
907
+ const hit = failures.find((f) => f.kind === kind && f.where === route);
908
+ if (hit) {
909
+ v.verdict = 'confirmed';
910
+ v.finding = hit.headline;
911
+ } else if (mine.length && mine.some((a) => a.verdict === V.DENIED || a.verdict === V.ALLOWED) && !mine.some((a) => a.verdict === V.AMBIGUOUS)) {
912
+ v.verdict = 'held';
913
+ } else if (mine.length) {
914
+ v.verdict = 'not judged';
915
+ v.why = (mine.find((a) => a.verdict === V.AMBIGUOUS) || mine[0]).reason;
916
+ } else {
917
+ v.verdict = 'not tried';
918
+ const n = notes.find((x) => x.what === route);
919
+ v.why = n ? n.why : asked.includes(route) ? 'no record to aim it at, or the try was capped' : 'it is not among the routes this run could try';
920
+ }
921
+ }
922
+ out.push(v);
923
+ }
924
+ return out;
925
+ }
926
+
927
+ // --- who is calling, as the request says: forged on the running app ----------------
928
+
929
+ const IDENTITY_ROUTE = /(^|\/)(me|whoami|session|profile|account)$/i;
930
+
931
+ /** The id of the person an identity answer names: { id, email } or null. */
932
+ function personIn(json) {
933
+ const look = (v, depth) => {
934
+ if (!v || typeof v !== 'object' || depth > 2 || Array.isArray(v)) return null;
935
+ if (['string', 'number'].includes(typeof v.id) && typeof v.email === 'string') return { id: v.id, email: v.email };
936
+ for (const k of Object.keys(v)) {
937
+ const f = look(v[k], depth + 1);
938
+ if (f) return f;
939
+ }
940
+ return null;
941
+ };
942
+ return look(json, 0);
943
+ }
944
+
945
+ /**
946
+ * For each sign-in check the map says takes who is calling from a cookie or header holding the bare
947
+ * user id: with no password at all, a request carrying another account's id there. Answered as that
948
+ * account = confirmed. Reads only (the app's "who am I" route). { ran, why, findings, tried }.
949
+ */
950
+ async function runForged(root, options) {
951
+ const opts = options || {};
952
+ const say = typeof opts.say === 'function' ? opts.say : () => {};
953
+ const plan = planOf(root, opts);
954
+ if (!plan.ran) return { ran: false, why: plan.why };
955
+ const items = map.toCheck(plan.map).filter((t) => t.kind === 'identity-from-request');
956
+ if (!items.length) return { ran: false, why: 'no sign-in check takes who is calling from a bare id in a cookie or header' };
957
+ let origin;
958
+ try {
959
+ origin = authz.assertLocalTarget(opts.origin);
960
+ } catch (err) {
961
+ return { ran: false, why: 'the app must be on this computer (localhost or 127.0.0.1): ' + (opts.origin || 'no address was given') };
962
+ }
963
+ const j = plan.journey;
964
+ if (!j.signIn) return { ran: false, why: j.signInWhy };
965
+ const me = (plan.map.routes || []).find((r) => r.method === 'GET' && r.path && IDENTITY_ROUTE.test(r.path) && r.auth && r.auth.status === map.STATUS.DETECTED);
966
+ if (!me) return { ran: false, why: 'no "who am I" route (GET /api/me or the like) behind the sign-in check, so an answer cannot be tied to a person' };
967
+ const accounts = j.accounts.usable.slice(0, 4);
968
+ if (accounts.length < 1) return { ran: false, why: 'no account has a password in .env' };
969
+ const passwords = {};
970
+ for (const a of accounts) passwords[a.email] = a.password;
971
+ const ctx = authz.makeContext({ baseUrl: origin, allowOrigins: [origin], fetch: opts.fetch, passwords: passwords, maxRequests: 60 });
972
+ // who each account is, as the app says when they sign in properly
973
+ const people = [];
974
+ for (const a of accounts) {
975
+ const s = authz.makeSession(ctx, 'account', a.email, a.password);
976
+ const fields = { [j.signIn.emailField]: a.email, [j.signIn.passwordField]: a.password };
977
+ const r = authz.replyOf(await s.send(j.signIn.method, j.signIn.path, j.signIn.encoding === 'form' ? { form: fields } : { json: fields }));
978
+ if (r.kind === 'refused' || r.kind === 'server-error' || r.kind === 'no-answer') continue;
979
+ const res = await s.send('GET', me.path, {});
980
+ const who = res.status === 200 ? personIn(res.json) : null;
981
+ if (who && who.email.toLowerCase() === a.email.toLowerCase()) people.push(who);
982
+ }
983
+ if (!people.length) return { ran: false, why: 'signed in, but ' + me.path + ' did not say who anyone is' };
984
+ say('Asking ' + me.path + ' with nobody signed in, carrying another person\'s id where the sign-in check reads it...');
985
+ const out = { ran: true, findings: [], tried: [], route: me.path };
986
+ for (const item of items) {
987
+ const { kind, name } = item.identity;
988
+ for (const person of people) {
989
+ const forger = authz.makeSession(ctx, 'forger', '(nobody signed in)', null);
990
+ if (kind === 'cookie') forger.jar.set(name, String(person.id));
991
+ const res = await forger.send('GET', me.path, kind === 'header' ? { headers: { [name]: String(person.id) } } : {});
992
+ const got = res.status === 200 ? personIn(res.json) : null;
993
+ const isThem = !!(got && String(got.id) === String(person.id) && got.email.toLowerCase() === person.email.toLowerCase());
994
+ out.tried.push({ by: kind + ' ' + name, as: person.email, status: res.status, verdict: isThem ? 'answered as them' : 'refused' });
995
+ if (!isThem) continue;
996
+ const sent = kind === 'cookie' ? 'cookie ' + name + '=' + person.id : 'header ' + name + ': ' + person.id;
997
+ const finding = {
998
+ kind: 'forged-identity', where: 'GET ' + me.path, from: item.where, status: 'confirmed', severity: 'CRITICAL',
999
+ headline: 'Anyone can be any user: with no password and nobody signed in, a request carrying ' + sent + ' was answered as ' + person.email + '. ' + item.headline + '.',
1000
+ proof: ['With nobody signed in (no password sent), set ' + sent + ' - the id ' + me.path + ' gives ' + person.email + ' when they sign in.', 'sent: GET ' + me.path, 'answer: ' + res.status + ' ' + authz.clip(res.text, 160)],
1001
+ };
1002
+ finding.fixPrompt = ['Kryptheon showed that anyone can act as any user of my running app:', '', finding.headline, '', 'Where: ' + item.where, '',
1003
+ 'First check this is real: with no session, send that request yourself. If it is, ' + map.ADVICE['identity-from-request'] + ' Change nothing else.', '', 'Afterwards I will run: npx kryptheon api'].join(String.fromCharCode(10));
1004
+ out.findings.push(finding);
1005
+ break; // one person proves it for this check
1006
+ }
1007
+ }
1008
+ return out;
1009
+ }
1010
+
1011
+ /**
1012
+ * The whole run on an app on this computer: across organizations and without
1013
+ * a session, then bad values, then (only with the owner's permissions file and
1014
+ * when asked) the roles. Nothing here saves; see saveReport.
1015
+ */
1016
+ async function runApi(root, options) {
1017
+ const opts = options || {};
1018
+ const say = typeof opts.say === 'function' ? opts.say : () => {};
1019
+ const m = opts.map || map.mapProject(root, { packageDir: opts.packageDir || __dirname });
1020
+ if (!m.ran) return { ran: false, why: m.why };
1021
+ const toCheck = opts.toCheck || map.toCheck(m);
1022
+ const shared = Object.assign({}, opts, { map: m, say: say });
1023
+ const wall = await runWall(root, shared);
1024
+ const forged = await runForged(root, shared);
1025
+ const webhooks = await webhook.run(root, shared);
1026
+ // the export and the jobs come before the bad values, which may stop the app
1027
+ const exportsRun = await csv.run(root, shared);
1028
+ const jobsRun = await runJobs(root, shared);
1029
+ const invalid = await runInvalid(root, shared);
1030
+ let roleRun = null;
1031
+ if (opts.roles) roleRun = await runRoles(root, shared);
1032
+ const verdicts = verdictsFor(toCheck, wall, invalid, roleRun, forged, webhooks);
1033
+ const seen = new Set();
1034
+ const findings = (wall.ran ? wall.findings : []).concat(forged.ran ? forged.findings : []).concat(webhooks.ran ? webhooks.findings : []).concat(exportsRun.ran ? exportsRun.findings : []).concat(jobsRun.ran ? jobsRun.findings : []).concat(roleRun && roleRun.ran ? roleRun.findings : []).concat(invalid.ran ? invalid.findings : []).filter((f) => {
1035
+ const k = f.kind + '|' + f.where + '|' + (f.field || '');
1036
+ return seen.has(k) ? false : (seen.add(k), true);
1037
+ });
1038
+ const left = (webhooks.ran ? webhooks.left : []).concat(exportsRun.ran ? exportsRun.left : []).concat(jobsRun.ran ? jobsRun.left : []).concat(wall.ran ? wall.result.seeded || [] : []).concat(roleRun && roleRun.ran ? roleRun.result.seeded || [] : []).concat(wall.ran ? leftBehind(wall.result.attempts) : []).concat(invalid.ran ? invalid.left : []).concat(roleRun && roleRun.ran ? leftBehind(roleRun.result.attempts) : []);
1039
+ return {
1040
+ ran: wall.ran || invalid.ran, why: wall.ran || invalid.ran ? null : wall.why, checkedAt: new Date().toISOString(),
1041
+ origin: opts.origin, wall: wall, forged: forged, webhooks: webhooks, exports: exportsRun, jobs: jobsRun, invalid: invalid, roles: roleRun, verdicts: verdicts, findings: findings, left: left,
1042
+ notTried: (wall.plan ? wall.plan.notTried : []).concat(invalid.ran ? invalid.notTried : []),
1043
+ };
1044
+ }
1045
+
1046
+ /** The report as it is kept on disk: no passwords, no raw tries, no plan with accounts. */
1047
+ function reportOf(run) {
1048
+ const wallTries = run.wall && run.wall.ran ? run.wall.result.attempts.filter((a) => !a.notTried).length : 0;
1049
+ return {
1050
+ ran: run.ran, why: run.why || null, checkedAt: run.checkedAt, origin: run.origin || null,
1051
+ accounts: run.wall && run.wall.ran ? run.wall.result.accounts.map((a) => a.email) : run.invalid && run.invalid.ran ? [run.invalid.account] : [],
1052
+ tries: { acrossOrganizationsAndNoSession: wallTries, badValues: run.invalid && run.invalid.ran ? run.invalid.tried.length : 0 },
1053
+ findings: run.findings.map((f) => ({ kind: f.kind, severity: f.severity, status: f.status, where: f.where, from: f.from, headline: f.headline, proof: f.proof, fixPrompt: f.fixPrompt })),
1054
+ webhooks: run.webhooks && run.webhooks.ran ? run.webhooks.hooks.map((h) => ({ route: h.route, from: h.from, signing: h.signing || null, notTried: h.notTried, control: h.control ? { status: h.control.status, took: h.control.took } : null, tries: h.tried.map((t) => ({ what: t.label + ' ' + t.type, record: t.record, status: t.status, changed: t.changed })) })) : [],
1055
+ verdicts: run.verdicts, left: run.left, notTried: run.notTried,
1056
+ };
1057
+ }
1058
+
1059
+ /** Writes api-last.json in the store folder. Never throws. */
1060
+ function saveReport(dir, run) {
1061
+ try {
1062
+ const text = JSON.stringify(reportOf(run), null, 2) + '\n';
1063
+ fs.writeFileSync(path.join(dir, REPORT_FILE), text, 'utf8');
1064
+ return { ok: true, file: path.join(dir, REPORT_FILE) };
1065
+ } catch (err) {
1066
+ return { ok: false, why: err.message };
1067
+ }
1068
+ }
1069
+
1070
+ /** The whole run as lines for a terminal. */
1071
+ function apiLines(run) {
1072
+ if (!run.ran) return ['', ' Nothing was tried: ' + run.why, ''];
1073
+ const out = run.wall && run.wall.ran ? wallLines(run.wall) : ['', ' Across organizations and without a session: not tried (' + (run.wall ? run.wall.why : '') + ')', ''];
1074
+ const counts = { confirmed: 0, held: 0, 'not judged': 0, 'not tried': 0 };
1075
+ for (const v of run.verdicts) counts[v.verdict] = (counts[v.verdict] || 0) + 1;
1076
+ if (run.roles && run.roles.ran) {
1077
+ const rt = run.roles.result.attempts.filter((a) => !a.notTried && a.kind === 'role');
1078
+ const n = (expected, verdict) => rt.filter((a) => a.expected === expected && a.verdict === verdict).length;
1079
+ out.push(' By role (your permissions-policy.json): ' + n('deny', V.DENIED) + ' refused as the policy says, ' + n('deny', V.FAILURE) + ' got through that the policy forbids, ' + n('allow', V.ALLOWED) + ' allowed as the policy says, ' + rt.filter((a) => a.verdict === V.AMBIGUOUS).length + ' could not be judged');
1080
+ for (const f of run.roles.findings.filter((x) => x.kind !== 'tenant' && x.kind !== 'signin')) {
1081
+ out.push(' PROBLEM ' + f.severity + ' ' + f.headline + (f.from ? ' (' + f.from + ')' : ''));
1082
+ for (const l of (f.proof || []).slice(0, 6)) out.push(' ' + l);
1083
+ }
1084
+ out.push('');
1085
+ }
1086
+ if (run.forged && run.forged.ran) {
1087
+ const got = run.forged.tried.filter((t) => t.verdict === 'answered as them').length;
1088
+ out.push(' Who is calling, as the request says (' + run.forged.route + ', nobody signed in): ' + got + ' answered as another person, ' + (run.forged.tried.length - got) + ' refused');
1089
+ for (const f of run.forged.findings) {
1090
+ out.push(' PROBLEM ' + f.severity + ' ' + f.headline + ' (' + f.from + ')');
1091
+ for (const l of f.proof) out.push(' ' + l);
1092
+ }
1093
+ out.push('');
1094
+ }
1095
+ if (run.webhooks && run.webhooks.ran) {
1096
+ for (const h of run.webhooks.hooks) {
1097
+ if (h.notTried) {
1098
+ out.push(' Webhook ' + h.route + ': not tried - ' + h.notTried);
1099
+ continue;
1100
+ }
1101
+ out.push(' Webhook ' + h.route + ' (' + h.signing.header + ', ' + h.signing.algorithm + ' ' + h.signing.encoding + ' over ' + h.signing.signs + '): a correctly signed event was taken; ' + h.tried.filter((t) => t !== h.control && t.took).length + ' of ' + (h.tried.length - 1) + ' other tries were taken, ' + h.tried.filter((t) => t.changed && t !== h.control).length + ' changed a record');
1102
+ for (const f of h.findings) {
1103
+ out.push(' ' + (f.status === 'confirmed' ? 'PROBLEM' : 'TO CHECK') + ' ' + f.severity + ' ' + f.headline + ' (' + f.from + ')');
1104
+ for (const l of f.proof) out.push(' ' + l);
1105
+ }
1106
+ }
1107
+ out.push('');
1108
+ } else if (run.webhooks && run.webhooks.why && !/no webhook route/.test(run.webhooks.why)) {
1109
+ out.push(' Webhooks not tried: ' + run.webhooks.why, '');
1110
+ }
1111
+ if (run.exports && run.exports.ran) {
1112
+ for (const t of run.exports.tried) out.push(' Export ' + t.what + ': ' + (t.verdict === 'raw' ? 'writes ' + t.field + ' as typed' : t.verdict === 'escaped' ? t.field + ' is written with a mark in front of "=" - held' : 'a marked value made through the app did not appear in it - not judged'));
1113
+ for (const f of run.exports.findings) {
1114
+ out.push(' PROBLEM ' + f.severity + ' ' + f.headline + ' (' + f.from + ')');
1115
+ for (const l of f.proof) out.push(' ' + l);
1116
+ }
1117
+ out.push('');
1118
+ }
1119
+ if (run.jobs && run.jobs.ran) {
1120
+ for (const t of run.jobs.tried) out.push(' Job ' + t.what + ' run twice: ' + t.verdict + (t.first ? ' (' + JSON.stringify(t.first) + ', then ' + JSON.stringify(t.second) + ')' : ''));
1121
+ for (const f of run.jobs.findings) {
1122
+ out.push(' TO CHECK ' + f.severity + ' ' + f.headline + ' (' + f.from + ')');
1123
+ for (const l of f.proof) out.push(' ' + l);
1124
+ }
1125
+ out.push('');
1126
+ }
1127
+ if (run.invalid && run.invalid.ran && run.invalid.appDown) {
1128
+ out.push(' THE APP STOPPED ANSWERING after ' + run.invalid.appDown + '. Nothing more was sent; start it again before any other check.');
1129
+ out.push('');
1130
+ }
1131
+ if (run.invalid && run.invalid.ran) {
1132
+ out.push(' Bad values (as ' + run.invalid.account + '): ' + run.invalid.tried.filter((t) => t.verdict === 'turned away').length + ' turned away, ' + run.invalid.tried.filter((t) => t.verdict === 'kept').length + ' kept, ' + run.invalid.tried.filter((t) => t.verdict === 'crashed').length + ' crashed, ' + run.invalid.tried.filter((t) => t.verdict === 'taken, not shown').length + ' taken with nothing to show');
1133
+ for (const f of run.invalid.findings) {
1134
+ out.push(' PROBLEM ' + f.severity + ' ' + f.headline + ' (' + f.from + ')');
1135
+ for (const l of f.proof) out.push(' ' + l);
1136
+ }
1137
+ for (const n of run.invalid.notTried) out.push(' not tried: ' + n.what + ' - ' + n.why);
1138
+ out.push('');
1139
+ } else if (run.invalid) {
1140
+ out.push(' Bad values not tried: ' + run.invalid.why, '');
1141
+ }
1142
+ if (run.verdicts.length) {
1143
+ out.push(' What the map pointed at (' + run.verdicts.length + '): ' + counts.confirmed + ' confirmed on the running app, ' + counts.held + ' held, ' + counts['not judged'] + ' not judged, ' + counts['not tried'] + ' not tried');
1144
+ for (const v of run.verdicts) out.push(' ' + v.verdict.toUpperCase() + ' ' + v.route + ' - ' + v.kind + (v.why ? ' (' + v.why + ')' : ''));
1145
+ out.push('');
1146
+ }
1147
+ const extra = run.invalid && run.invalid.ran ? run.invalid.left : [];
1148
+ if (extra.length) {
1149
+ out.push(' Test data left by bad values:');
1150
+ for (const l of extra) out.push(' ' + l);
1151
+ out.push('');
1152
+ }
1153
+ return out;
1154
+ }
1155
+
1156
+ module.exports = { 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 };