kryptheon 0.1.44 → 0.1.46

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,777 @@
1
+ // Input and edge cases: the person's own recorded flows, replayed with one
2
+ // field at a time given a value people do type by accident - nothing, far too
3
+ // much, a tag, emoji, a negative number, a date that does not exist.
4
+ //
5
+ // What it reports, and only with proof from the run itself:
6
+ // - the page threw an error it did not throw on the plain replay;
7
+ // - the server answered 5xx where the plain replay did not;
8
+ // - the app SAVED the value (the write returned 2xx), shown as the field's
9
+ // value in the plain replay's request next to the same field in this one;
10
+ // - a tag typed into a field came back as a real element on the page.
11
+ //
12
+ // What it never reports: a replay that failed. A form that refuses a bad
13
+ // value is doing its job, and the steps after it then fail - that is the
14
+ // good outcome, said as "held". A 429 means the app protected itself, and
15
+ // that run is said as not tested.
16
+ //
17
+ // Every probe run adds data to the app. That is said before it starts and
18
+ // again when it ends (kryptheon-probe.js), and nothing here starts on its own.
19
+
20
+ const fs = require('fs');
21
+ const path = require('path');
22
+ const crypto = require('crypto');
23
+ const replay = require('./kryptheon-replay.js');
24
+ const probe = require('./kryptheon-probe.js');
25
+ const store = require('./kryptheon-store.js');
26
+
27
+ const LONG_LENGTH = 5000;
28
+ // Problems that came from a single run - a 5xx, a page error - are run once
29
+ // more before they are called problems. This many at most, out of the cap.
30
+ const CONFIRM_RUNS = 5;
31
+
32
+ const KINDS = {
33
+ empty: { words: 'an empty value', value: () => '' },
34
+ tag: { words: 'text with an HTML tag', value: (id) => '<i ' + probe.MARKER + '="' + id + '">' + probe.TEST_WORD + '</i>' },
35
+ negative: { words: 'a negative number (-5)', value: () => '-5' },
36
+ date: { words: 'a date that does not exist (2026-13-45)', value: () => '2026-13-45' },
37
+ long: { words: 'a ' + LONG_LENGTH + '-character value', value: (id, field) => longValue(field) },
38
+ emoji: { words: 'emoji', value: () => probe.TEST_WORD + ' \u{1F642}\u{1F680}\u{1F44D}\u{1F3FD}' },
39
+ };
40
+ // The order values are tried in when there is not room for all of them: the
41
+ // ones most likely to find something real come first.
42
+ const KIND_ORDER = ['empty', 'tag', 'negative', 'date', 'long', 'emoji'];
43
+
44
+ function longValue(field) {
45
+ if (field && field.info && field.info.type === 'number') return '9'.repeat(LONG_LENGTH);
46
+ const head = probe.TEST_WORD + ' ';
47
+ return head + 'x'.repeat(LONG_LENGTH - head.length);
48
+ }
49
+
50
+ const NUMBERISH = /\b(qty|quantity|amount|price|cost|count|total|age|units?|hours?|stock|budget|rate|fee|salary|number of|seats|weight|duration|minutes|days)\b/i;
51
+ const DATEISH = /(date|dob|birth|due|deadline|expir|scheduled)/i;
52
+ const TEXTISH = ['', 'text', 'search', 'email', 'url', 'tel'];
53
+
54
+ // --- reading a recording --------------------------------------------------
55
+
56
+ const FILL = /^(\s*)(await\s+)?(page\s*\..+?)\.fill\(\s*(['"])((?:\\.|(?!\4).)*)\4\s*\)\s*;?\s*$/;
57
+
58
+ function unquote(text) {
59
+ return String(text).replace(/\\(u\{[0-9a-fA-F]+\}|u[0-9a-fA-F]{4}|x[0-9a-fA-F]{2}|.)/g, (all, c) => {
60
+ if (c === 'n') return '\n';
61
+ if (c === 't') return '\t';
62
+ if (c === 'r') return '\r';
63
+ if (c[0] === 'u' && c[1] === '{') return String.fromCodePoint(parseInt(c.slice(2, -1), 16));
64
+ if (c[0] === 'u' && c.length === 5) return String.fromCharCode(parseInt(c.slice(1), 16));
65
+ if (c[0] === 'x' && c.length === 3) return String.fromCharCode(parseInt(c.slice(1), 16));
66
+ return c;
67
+ });
68
+ }
69
+
70
+ /** Every fill of a literal value, one line each. Values from .env are not literals and never match. */
71
+ function fillSteps(source) {
72
+ const out = [];
73
+ String(source || '').split('\n').forEach((line, i) => {
74
+ const m = FILL.exec(line.replace(/\r$/, ''));
75
+ if (!m) return;
76
+ out.push({
77
+ line: i + 1,
78
+ indent: m[1],
79
+ locator: m[3],
80
+ value: unquote(m[5]),
81
+ name: replay.nameInLine(line),
82
+ });
83
+ });
84
+ return out;
85
+ }
86
+
87
+ /**
88
+ * The fields worth giving unusual values: not a password, not anything typed
89
+ * while signing in (changing those only fails the sign-in, and can lock the
90
+ * account), each field once. Fields followed by a step that saves come first.
91
+ */
92
+ function candidateFields(source) {
93
+ const text = String(source || '');
94
+ if (replay.findReplayRisks(text).some((r) => r.kind === 'signup')) return [];
95
+ const actions = replay.readActions(text);
96
+ const lines = text.split('\n');
97
+ // The sign-in: every fill up to the last one that reads a secret - a
98
+ // recorded password becomes process.env, so its line is not in fillSteps.
99
+ let signInEnd = 0;
100
+ lines.forEach((line, i) => {
101
+ if (/\.fill\(/.test(line) && (replay.isSecretField(replay.nameInLine(line)) || /process\.env\./.test(line))) signInEnd = i + 1;
102
+ });
103
+ const seen = new Set();
104
+ const out = [];
105
+ for (const step of fillSteps(text)) {
106
+ if (step.line <= signInEnd) continue;
107
+ if (replay.isSecretField(step.name)) continue;
108
+ const key = step.locator.replace(/\s+/g, ' ');
109
+ if (seen.has(key)) continue;
110
+ seen.add(key);
111
+ // Saved by a click on Save/Add/Create... or an Enter, before the flow
112
+ // goes anywhere else.
113
+ let saves = false;
114
+ for (const a of actions) {
115
+ if (a.line <= step.line) continue;
116
+ if (a.isGoto) break;
117
+ if ((a.method === 'click' || a.method === 'dblclick') && a.name && replay.CREATES.test(a.name.trim())) saves = true;
118
+ if (/\.press\(\s*(['"])Enter\1\s*\)/.test(a.text)) saves = true;
119
+ if (saves) break;
120
+ }
121
+ out.push(Object.assign({}, step, { saves: saves }));
122
+ }
123
+ return out.filter((f) => f.saves).concat(out.filter((f) => !f.saves));
124
+ }
125
+
126
+ /** The words a field is called by in a report: its label, else its recorded name. */
127
+ function fieldName(field) {
128
+ const info = field.info || {};
129
+ // The first name that has a word in it: a label that is only an icon
130
+ // ("⌕") says nothing in a report.
131
+ const named = [info.label, field.name, info.placeholder, info.name].find((n) => n && /[\p{L}\p{N}]/u.test(n));
132
+ return String(named || 'a field').replace(/\s*\*\s*$/, '').trim();
133
+ }
134
+
135
+ /** Which unusual values make sense for this field, in the order they are tried. */
136
+ function kindsFor(field) {
137
+ const info = field.info;
138
+ const words = [field.name, info && info.label, info && info.placeholder, info && info.name].filter(Boolean).join(' ');
139
+ if (info && (info.type === 'number' || info.type === 'range')) return ['empty', 'negative', 'long'];
140
+ // A date picker, a select, a checkbox: the browser itself refuses text in
141
+ // them, so only "nothing" is a value a person could really send.
142
+ if (info && !(info.tag === 'textarea' || TEXTISH.includes(info.type))) return ['empty'];
143
+ const kinds = ['empty', 'tag'];
144
+ if (NUMBERISH.test(words)) kinds.push('negative');
145
+ if (DATEISH.test(words)) kinds.push('date');
146
+ kinds.push('long', 'emoji');
147
+ return KIND_ORDER.filter((k) => kinds.includes(k));
148
+ }
149
+
150
+ /** The plain replay: the recording as it is, reading each candidate field's kind from the page. */
151
+ function cleanSource(source, fields) {
152
+ const before = {};
153
+ for (const f of fields) before[f.line] = f.indent + 'await page.__kxField(' + f.line + ', ' + f.locator + ');';
154
+ return probe.probeSource(source, { before: before });
155
+ }
156
+
157
+ /** The recording with one field's value changed. */
158
+ function variantSource(source, field, value) {
159
+ const edits = {};
160
+ edits[field.line] = field.indent + 'await ' + field.locator + '.fill(' + JSON.stringify(value) + ');';
161
+ return probe.probeSource(source, { edits: edits });
162
+ }
163
+
164
+ /**
165
+ * Which variants to run, at most `budget`: across flows and their fields in
166
+ * turn, so one long form cannot use up every run, and the first kind of every
167
+ * field before the second kind of any.
168
+ */
169
+ function chooseVariants(flows, budget) {
170
+ const queues = [];
171
+ for (const flow of flows) {
172
+ for (const field of flow.fields) queues.push({ flow: flow, field: field, kinds: kindsFor(field).slice() });
173
+ }
174
+ const chosen = [];
175
+ let progress = true;
176
+ while (chosen.length < budget && progress) {
177
+ progress = false;
178
+ for (const q of queues) {
179
+ if (chosen.length >= budget) break;
180
+ const kind = q.kinds.shift();
181
+ if (!kind) continue;
182
+ progress = true;
183
+ chosen.push({ flow: q.flow, field: q.field, kind: kind });
184
+ }
185
+ }
186
+ return chosen;
187
+ }
188
+
189
+ // --- judging --------------------------------------------------------------
190
+
191
+ function flatten(value, prefix, out) {
192
+ const into = out || {};
193
+ if (value && typeof value === 'object') {
194
+ const keys = Object.keys(value);
195
+ if (!keys.length) into[prefix || '(body)'] = Array.isArray(value) ? '[]' : '{}';
196
+ for (const k of keys) flatten(value[k], prefix ? prefix + '.' + k : k, into);
197
+ } else {
198
+ into[prefix || '(body)'] = value;
199
+ }
200
+ return into;
201
+ }
202
+
203
+ const ok = (w) => w.status >= 200 && w.status < 300;
204
+ const writesOf = (r) => ((r && r.events) || []).filter((e) => e.kind === 'write');
205
+ const routeOf = (w) => w.method + ' ' + w.host + w.path;
206
+ // A write the app accepted and keeps: not the sign-in, not a delete.
207
+ const leftBehind = (w) => ok(w) && w.method !== 'DELETE' && !w.signIn;
208
+
209
+ /** Writes of the plain replay and of a variant, paired by where they went, in order. */
210
+ function pairWrites(clean, variant) {
211
+ const left = {};
212
+ for (const w of writesOf(clean)) (left[routeOf(w)] = left[routeOf(w)] || []).push(w);
213
+ const pairs = [];
214
+ for (const w of writesOf(variant)) {
215
+ const list = left[routeOf(w)];
216
+ pairs.push({ clean: list && list.length ? list.shift() : null, variant: w });
217
+ }
218
+ return pairs;
219
+ }
220
+
221
+ function show(value) {
222
+ if (value === undefined) return '(left out)';
223
+ if (value === null) return 'null';
224
+ const s = typeof value === 'string' ? value : JSON.stringify(value);
225
+ const cut = s.length > 60 ? s.slice(0, 40) + '... (' + s.length + ' characters)' : s;
226
+ return typeof value === 'string' ? JSON.stringify(cut) : cut;
227
+ }
228
+
229
+ /**
230
+ * Where the field's value went: every write that returned 2xx in the variant,
231
+ * and in it the key that held the field's recorded value in the plain replay.
232
+ * The difference between the two requests is the proof.
233
+ */
234
+ function savedValues(clean, variant, field, typed) {
235
+ const found = [];
236
+ const recorded = String(field.value == null ? '' : field.value).trim();
237
+ for (const pair of pairWrites(clean, variant)) {
238
+ if (!ok(pair.variant)) continue;
239
+ const v = pair.variant.data !== undefined ? flatten(pair.variant.data) : null;
240
+ const c = pair.clean && ok(pair.clean) && pair.clean.data !== undefined ? flatten(pair.clean.data) : null;
241
+ if (c && v && recorded) {
242
+ for (const key of Object.keys(c)) {
243
+ if (String(c[key]).trim() !== recorded) continue;
244
+ if (Object.prototype.hasOwnProperty.call(v, key) && String(v[key]) === String(c[key])) continue;
245
+ found.push({ write: pair.variant, cleanWrite: pair.clean, key: key, was: c[key], now: v[key] });
246
+ }
247
+ }
248
+ // A body that is not data, or a field whose recorded value could not be
249
+ // found in it: the typed value itself, word for word.
250
+ if (!found.some((f) => f.write === pair.variant) && typed && typed.length >= 5) {
251
+ const text = pair.variant.data !== undefined ? JSON.stringify(pair.variant.data) : String(pair.variant.text || '');
252
+ if (text.indexOf(typed) !== -1 || text.indexOf(JSON.stringify(typed).slice(1, -1)) !== -1) {
253
+ found.push({ write: pair.variant, cleanWrite: pair.clean, key: null, was: undefined, now: typed });
254
+ }
255
+ }
256
+ }
257
+ return found;
258
+ }
259
+
260
+ /** Was this the value that was typed, or something the app made of it? */
261
+ function savedAs(kind, typed, now) {
262
+ if (now === undefined) return 'left-out';
263
+ if (kind === 'empty') return now === '' || now === null ? 'as-typed' : String(now).trim() === '' ? 'as-typed' : 'changed';
264
+ if (kind === 'negative') return Number(now) === -5 ? 'as-typed' : 'changed';
265
+ if (kind === 'date') return /2026-13-45|invalid date/i.test(String(now)) ? 'as-typed' : now === null || now === '' ? 'emptied' : 'changed';
266
+ if (kind === 'long') return String(now).length >= LONG_LENGTH ? 'as-typed' : 'changed';
267
+ return String(now) === typed ? 'as-typed' : 'changed';
268
+ }
269
+
270
+ function diffLine(s) {
271
+ return (s.key ? s.key + ': ' + show(s.was) + ' in your recorded run, ' + show(s.now) + ' in this one' : 'the request body holds the typed value');
272
+ }
273
+
274
+ function proofLines(saved) {
275
+ const lines = [];
276
+ for (const s of saved.slice(0, 3)) {
277
+ lines.push('Request: ' + probe.writeWords(s.write));
278
+ lines.push('Difference from your recorded run: ' + diffLine(s));
279
+ }
280
+ return lines;
281
+ }
282
+
283
+ /**
284
+ * What one variant showed, against the plain replay of the same flow. Returns
285
+ * { outcome, findings, held, why } - outcome is 'found', 'held' or
286
+ * 'not tested'. Markup is judged across the whole run (judgeMarkers), since a
287
+ * tag saved by one run is often shown by a later one.
288
+ */
289
+ function judgeVariant(clean, variant, plan) {
290
+ const field = plan.field;
291
+ const kind = plan.kind;
292
+ const name = fieldName(field);
293
+ const words = KINDS[kind].words;
294
+ const where = plan.flow.file + ':' + field.line;
295
+ const base = { flow: plan.flow.title, field: name, kind: kind, where: where, probe: plan.id };
296
+ if (!variant) return { outcome: 'not tested', why: 'this run did not finish', findings: [] };
297
+ const limited = variant.events.filter((e) => e.kind === 'limited');
298
+ if (limited.length) {
299
+ return { outcome: 'not tested', why: 'your app said "too many requests" (429) at ' + limited[0].method + ' ' + limited[0].path + ' - it protected itself, so this value was not tested', findings: [] };
300
+ }
301
+ if (variant.status !== 'passed' && variant.failedLine === field.line + 0 && /Malformed value|Cannot type text into input\[type=number\]|not an <input>/i.test(variant.error || '')) {
302
+ return { outcome: 'held', why: 'the field itself would not take it', findings: [] };
303
+ }
304
+
305
+ const findings = [];
306
+ const cleanErrors = new Set(clean.events.filter((e) => e.kind === 'error').map((e) => e.text));
307
+ const newErrors = variant.events.filter((e) => e.kind === 'error' && !cleanErrors.has(e.text));
308
+ if (newErrors.length) {
309
+ findings.push(Object.assign({}, base, {
310
+ type: 'page-error', severity: 'MEDIUM', status: 'confirmed',
311
+ headline: 'Giving "' + name + '" ' + words + ' makes the page throw an error.',
312
+ proof: ['Flow: ' + plan.flow.title + ' (' + where + ')', 'Typed into "' + name + '": ' + show(plan.value), 'The page threw: ' + newErrors[0].text,
313
+ 'Your recorded run of the same flow threw no such error.'],
314
+ }));
315
+ }
316
+ const cleanServer = new Set(clean.events.filter((e) => e.kind === 'server').map(routeOf));
317
+ const newServer = variant.events.filter((e) => e.kind === 'server' && !cleanServer.has(routeOf(e)));
318
+ if (newServer.length) {
319
+ findings.push(Object.assign({}, base, {
320
+ type: 'server-error', severity: 'HIGH', status: 'confirmed',
321
+ headline: 'Giving "' + name + '" ' + words + ' makes your server fail.',
322
+ proof: ['Flow: ' + plan.flow.title + ' (' + where + ')', 'Typed into "' + name + '": ' + show(plan.value),
323
+ 'Server answered: ' + newServer[0].method + ' ' + newServer[0].host + newServer[0].path + ' -> ' + newServer[0].status,
324
+ 'The same request in your recorded run did not fail.'],
325
+ }));
326
+ }
327
+
328
+ const saved = savedValues(clean, variant, field, plan.value);
329
+ if (saved.length) {
330
+ const as = savedAs(kind, plan.value, saved[0].now);
331
+ const info = field.info || {};
332
+ const problem =
333
+ (kind === 'empty' && info.required && as !== 'changed') ||
334
+ (kind === 'negative' && as === 'as-typed' && ((info.type === 'number' && info.min != null && Number(info.min) >= 0) || NUMBERISH.test(name + ' ' + (field.name || '')))) ||
335
+ (kind === 'date' && (as === 'as-typed' || as === 'changed')) ||
336
+ (kind === 'long' && as === 'as-typed' && info.maxLength != null);
337
+ // An optional field saved empty, a negative number where nothing says it
338
+ // must be positive, a long note with no limit: all normal for some app,
339
+ // so never said. Only what loses what the person typed is worth a look -
340
+ // a date quietly saved as nothing, emoji turned into something else.
341
+ const toCheck = !problem && (
342
+ (kind === 'date' && as === 'emptied') ||
343
+ (kind === 'emoji' && as === 'changed'));
344
+ const savedWords = as === 'changed' ? 'saved it as ' + show(saved[0].now) : as === 'emptied' ? 'saved it as empty' : as === 'left-out' ? 'saved the record without it' : 'saved it';
345
+ const why =
346
+ kind === 'empty' && info.required ? 'The page marks "' + name + '" as required, but the server took it empty.'
347
+ : kind === 'negative' && problem ? '"' + name + '" is a count or amount, and a negative one was stored.'
348
+ : kind === 'date' ? 'There is no 13th month; the server did not check the date.'
349
+ : kind === 'long' && info.maxLength != null ? 'The field allows ' + info.maxLength + ' characters, but the server stored ' + LONG_LENGTH + '.'
350
+ : null;
351
+ if (problem || toCheck) {
352
+ findings.push(Object.assign({}, base, {
353
+ type: 'accepted', severity: problem ? 'MEDIUM' : 'LOW', status: problem ? 'confirmed' : 'verification required',
354
+ headline: problem ? '"' + name + '" accepts ' + words + ', and the app saved it.'
355
+ : '"' + name + '" took ' + words + ' and the app ' + savedWords + ' - check that this is allowed.',
356
+ proof: ['Flow: ' + plan.flow.title + ' (' + where + ')', 'Typed into "' + name + '": ' + show(plan.value)]
357
+ .concat(proofLines(saved), why ? [why] : []),
358
+ }));
359
+ }
360
+ }
361
+
362
+ if (findings.length) return { outcome: 'found', findings: findings };
363
+ const refused = writesOf(variant).filter((w) => w.status >= 400 && w.status < 500);
364
+ return {
365
+ outcome: 'held',
366
+ why: refused.length ? 'the app refused it (' + probe.writeWords(refused[0]) + ')'
367
+ : saved.length ? 'saved, and nothing broke' : 'nothing was saved and nothing broke',
368
+ findings: [],
369
+ };
370
+ }
371
+
372
+ /**
373
+ * A tag typed into a field, found on a page as a real element. Each probe tag
374
+ * carries the id of the run that typed it, so it is put down to that run
375
+ * whichever later run found it - and a tag left by an earlier press of the
376
+ * button, with an id from then, is never counted now.
377
+ */
378
+ function judgeMarkers(results, plans) {
379
+ const byId = {};
380
+ for (const p of plans) if (p.kind === 'tag') byId[p.markerId] = p;
381
+ const seen = {};
382
+ for (const id of Object.keys(results)) {
383
+ const r = results[id];
384
+ if (!r || !r.markers) continue;
385
+ for (const when of ['live', 'reopened']) {
386
+ for (const m of r.markers[when] || []) {
387
+ const plan = byId[m.id];
388
+ if (!plan) continue;
389
+ const s = (seen[m.id] = seen[m.id] || { plan: plan, live: null, reopened: null });
390
+ if (!s[when]) s[when] = Object.assign({ in: id }, m);
391
+ }
392
+ }
393
+ }
394
+ return Object.keys(seen).map((id) => {
395
+ const s = seen[id];
396
+ const plan = s.plan;
397
+ const name = fieldName(plan.field);
398
+ const where = plan.flow.file + ':' + plan.field.line;
399
+ const kept = Boolean(s.reopened);
400
+ const m = s.reopened || s.live;
401
+ return {
402
+ flow: plan.flow.title, field: name, kind: 'tag', where: where, probe: plan.id,
403
+ type: 'markup', severity: kept ? 'HIGH' : 'MEDIUM', status: 'confirmed',
404
+ headline: 'Text typed into "' + name + '" is shown as real HTML - the app does not escape it.',
405
+ proof: ['Flow: ' + plan.flow.title + ' (' + where + ')', 'Typed into "' + name + '": ' + show(plan.value),
406
+ 'Found on the page ' + m.path + ' as a real <' + m.tag + '> element: ' + m.html,
407
+ kept ? 'It was still there when the page was opened again, so it was saved and is shown to whoever opens that page.'
408
+ : 'It was on the page right after typing. When the page was opened again it was not found.'],
409
+ };
410
+ });
411
+ }
412
+
413
+ /** The fix, said for an AI assistant: what was seen, the proof, and what not to do. */
414
+ function fixPrompt(f) {
415
+ const what = {
416
+ 'server-error': 'Make the server reject this value with a clear 4xx error instead of failing. Validate it before using it.',
417
+ 'page-error': 'Make the page handle this value without throwing: validate it, and show the person a message instead.',
418
+ accepted: 'Validate this field on the server - in the API route, database constraint or access rule that saves it - not only in the page.',
419
+ markup: 'Show this value as text. Do not insert it with innerHTML, dangerouslySetInnerHTML, v-html or similar; escape it, or set textContent.',
420
+ }[f.type];
421
+ return [
422
+ 'My app mishandles an unusual value typed into a form. Please fix the app - and nothing else.',
423
+ '',
424
+ 'Kryptheon replayed my recorded flow "' + f.flow + '" with one field changed, and saw this:',
425
+ f.headline,
426
+ '',
427
+ ].concat(f.proof, [
428
+ '',
429
+ what,
430
+ 'Do not edit the recording in tests/ to make this go away.',
431
+ ]).join('\n');
432
+ }
433
+
434
+ // --- running --------------------------------------------------------------
435
+
436
+ function newId() {
437
+ return crypto.randomBytes(4).toString('hex');
438
+ }
439
+
440
+ /**
441
+ * The whole check. `flows` is [{ file, title, source }] - only recordings the
442
+ * caller already found runnable. `options.say` hears progress, `options.onRun`
443
+ * gets the handle of each browser run so a Stop can end it, `options.budget`
444
+ * caps the number of runs (never above probe.MAX_RUNS).
445
+ */
446
+ async function run(root, flows, options) {
447
+ const opts = options || {};
448
+ const say = typeof opts.say === 'function' ? opts.say : () => {};
449
+ const budget = Math.min(probe.MAX_RUNS, opts.budget || probe.MAX_RUNS);
450
+ const runId = newId();
451
+ let stopped = false;
452
+ let current = null;
453
+ const control = {
454
+ stop() {
455
+ stopped = true;
456
+ if (current) current.stop();
457
+ },
458
+ };
459
+ if (typeof opts.onControl === 'function') opts.onControl(control);
460
+ const secretNames = [];
461
+ for (const f of flows) {
462
+ for (const m of String(f.source).matchAll(/process\.env\.([A-Za-z_][A-Za-z0-9_]*)/g)) if (!secretNames.includes(m[1])) secretNames.push(m[1]);
463
+ }
464
+ const runOpts = (labels) => ({ say: say, labels: labels, secretNames: secretNames, gapMs: opts.gapMs, env: opts.env, hide: opts.hide, childEnv: opts.childEnv });
465
+
466
+ const report = {
467
+ check: 'edge', runId: runId, startedAt: new Date().toISOString(), endedAt: null,
468
+ stopped: false, runsPlanned: 0, runsDone: 0, flows: [], findings: [], held: [], notTested: [], testData: [], writeHosts: [],
469
+ };
470
+
471
+ // 1. The plain replay of every flow that has a field to try.
472
+ const live = [];
473
+ for (const f of flows) {
474
+ // A re-check tries one field only: the one its finding is about.
475
+ const fields = candidateFields(f.source).filter((x) => !opts.only || x.line === opts.only.line);
476
+ if (replay.findReplayRisks(f.source).some((r) => r.kind === 'signup')) {
477
+ report.flows.push({ title: f.title, file: f.file, state: 'skipped', why: 'it creates an account, and this check never creates accounts' });
478
+ } else if (!fields.length) {
479
+ report.flows.push({ title: f.title, file: f.file, state: 'skipped', why: 'it types nothing into a form (only sign-in or nothing at all)' });
480
+ } else {
481
+ live.push(Object.assign({}, f, { fields: fields }));
482
+ }
483
+ }
484
+ const flowsToRun = live.slice(0, Math.max(0, budget - 1));
485
+ for (const f of live.slice(flowsToRun.length)) report.flows.push({ title: f.title, file: f.file, state: 'not tested', why: 'over the limit of ' + budget + ' runs' });
486
+ if (!flowsToRun.length) {
487
+ report.endedAt = new Date().toISOString();
488
+ return report;
489
+ }
490
+ say('Replaying ' + flowsToRun.length + (flowsToRun.length === 1 ? ' flow' : ' flows') + ' as recorded first, to know what normal looks like...');
491
+ const cleanProbes = flowsToRun.map((f, i) => ({ id: probe.probeId(i + 1, 'plain ' + f.title), source: cleanSource(f.source, f.fields), flow: f }));
492
+ const cleanLabels = {};
493
+ for (const p of cleanProbes) cleanLabels[p.id] = p.flow.title + ' - as recorded';
494
+ current = probe.runProbes(root, cleanProbes, runOpts(cleanLabels));
495
+ const first = await current.done;
496
+ current = null;
497
+ report.runsDone += Object.keys(first.results).length;
498
+
499
+ const ready = [];
500
+ for (const p of cleanProbes) {
501
+ const r = first.results[p.id];
502
+ const entry = { title: p.flow.title, file: p.flow.file };
503
+ if (!r) Object.assign(entry, { state: 'not tested', why: first.stopped || stopped ? 'stopped before it ran' : 'the replay did not finish' });
504
+ else if (r.events.some((e) => e.kind === 'limited')) Object.assign(entry, { state: 'not tested', why: 'your app said "too many requests" (429) during the plain replay' });
505
+ else if (r.status !== 'passed') Object.assign(entry, { state: 'not tested', why: 'the flow does not pass as recorded (' + (r.error || r.status) + '). Run npx kryptheon check and fix that first' });
506
+ else {
507
+ Object.assign(entry, { state: 'tested' });
508
+ for (const field of p.flow.fields) field.info = r.fields[field.line] || null;
509
+ ready.push({ flow: p.flow, clean: r });
510
+ }
511
+ report.flows.push(entry);
512
+ for (const w of writesOf(r)) {
513
+ if (w.origin && !report.writeHosts.includes(w.origin)) report.writeHosts.push(w.origin);
514
+ if (leftBehind(w)) report.testData.push({ flow: p.flow.title, value: 'as recorded', request: probe.writeWords(w) });
515
+ }
516
+ }
517
+
518
+ // Where the app really saves. A page on this computer can save to a
519
+ // database that is not - a local frontend on a hosted Supabase, say - and
520
+ // the yes that was given named only the page. So the unusual values wait
521
+ // until the person has said yes for that address too; the next press
522
+ // names it before it starts.
523
+ const unconfirmed = probe.liveLooking(report.writeHosts).filter((h) => !(opts.confirmed || []).includes(h));
524
+ if (unconfirmed.length && ready.length) {
525
+ for (const entry of report.flows) {
526
+ if (entry.state !== 'tested') continue;
527
+ entry.state = 'not tested';
528
+ entry.why = 'your app saves to ' + unconfirmed.join(', ') + ', which is not this computer. Press again: you will be asked about that address first';
529
+ }
530
+ ready.length = 0;
531
+ }
532
+
533
+ // 2. One field changed at a time, within what is left of the cap.
534
+ const room = Math.max(0, budget - cleanProbes.length - Math.min(CONFIRM_RUNS, Math.floor((budget - cleanProbes.length) / 5)));
535
+ const chosen = stopped ? []
536
+ : opts.only ? ready.map((x) => ({ flow: x.flow, field: x.flow.fields[0], kind: opts.only.kind }))
537
+ : chooseVariants(ready.map((x) => x.flow), room);
538
+ report.runsPlanned = cleanProbes.length + chosen.length;
539
+ const plans = chosen.map((c, i) => {
540
+ const id = probe.probeId(cleanProbes.length + i + 1, c.kind + ' ' + fieldName(c.field));
541
+ const markerId = runId + '-' + (i + 1);
542
+ const value = KINDS[c.kind].value(markerId, c.field);
543
+ return Object.assign({}, c, { id: id, markerId: markerId, value: value, source: variantSource(c.flow.source, c.field, value) });
544
+ });
545
+ let results = {};
546
+ if (plans.length) {
547
+ say('Trying ' + plans.length + ' unusual ' + (plans.length === 1 ? 'value' : 'values') + ', one field at a time...');
548
+ const labels = {};
549
+ for (const p of plans) labels[p.id] = p.flow.title + ' - "' + fieldName(p.field) + '" given ' + KINDS[p.kind].words;
550
+ current = probe.runProbes(root, plans, runOpts(labels));
551
+ const second = await current.done;
552
+ current = null;
553
+ results = second.results;
554
+ report.runsDone += Object.keys(results).length;
555
+ }
556
+
557
+ const cleanOf = (flow) => ready.find((x) => x.flow === flow).clean;
558
+ const verdicts = plans.map((p) => ({ plan: p, verdict: judgeVariant(cleanOf(p.flow), results[p.id], p) }));
559
+
560
+ // 3. A 5xx or a page error seen once is run once more before it is called
561
+ // a problem: apps have bad moments, and one is not a finding.
562
+ const once = verdicts.filter((v) => v.verdict.findings.some((f) => f.type === 'server-error' || f.type === 'page-error')).slice(0, CONFIRM_RUNS);
563
+ if (once.length && !stopped) {
564
+ say('Running ' + once.length + ' of them again, to be sure the error is not a one-off...');
565
+ const again = once.map((v, i) => Object.assign({}, v.plan, { id: probe.probeId(report.runsPlanned + i + 1, 'again ' + v.plan.kind + ' ' + fieldName(v.plan.field)) }));
566
+ report.runsPlanned += again.length;
567
+ const labels = {};
568
+ for (const p of again) labels[p.id] = p.flow.title + ' - "' + fieldName(p.field) + '" given ' + KINDS[p.kind].words + ', again';
569
+ current = probe.runProbes(root, again, runOpts(labels));
570
+ const third = await current.done;
571
+ current = null;
572
+ report.runsDone += Object.keys(third.results).length;
573
+ once.forEach((v, i) => {
574
+ const second = judgeVariant(cleanOf(v.plan.flow), third.results[again[i].id], again[i]);
575
+ for (const f of v.verdict.findings) {
576
+ if (f.type !== 'server-error' && f.type !== 'page-error') continue;
577
+ if (second.findings.some((g) => g.type === f.type)) f.proof.push('It happened again on a second run.');
578
+ else {
579
+ f.status = 'verification required';
580
+ f.severity = 'LOW';
581
+ f.headline = f.headline.replace(/\.$/, '') + ' - once, not on a second try.';
582
+ f.proof.push(third.results[again[i].id] ? 'A second run with the same value did not show it.' : 'A second run did not finish, so it is not confirmed.');
583
+ }
584
+ }
585
+ for (const w of writesOf(third.results[again[i].id])) if (leftBehind(w)) report.testData.push({ flow: v.plan.flow.title, value: KINDS[v.plan.kind].words, request: probe.writeWords(w) });
586
+ });
587
+ }
588
+
589
+ for (const v of verdicts) {
590
+ const p = v.plan;
591
+ const label = { flow: p.flow.title, field: fieldName(p.field), kind: p.kind, where: p.flow.file + ':' + p.field.line };
592
+ if (v.verdict.outcome === 'not tested') {
593
+ report.notTested.push(Object.assign(label, { why: !results[p.id] && stopped ? 'stopped before it finished' : v.verdict.why }));
594
+ }
595
+ else if (v.verdict.outcome === 'held') report.held.push(Object.assign(label, { why: v.verdict.why }));
596
+ for (const f of v.verdict.findings) report.findings.push(f);
597
+ for (const w of writesOf(results[p.id])) if (leftBehind(w)) report.testData.push({ flow: p.flow.title, value: KINDS[p.kind].words, request: probe.writeWords(w) });
598
+ }
599
+ // Markup, judged across every run of this press.
600
+ const all = Object.assign({}, first.results, results);
601
+ for (const m of judgeMarkers(all, plans)) {
602
+ report.findings.push(m);
603
+ const i = report.held.findIndex((h) => h.where === m.where && h.kind === 'tag');
604
+ if (i !== -1) report.held.splice(i, 1);
605
+ }
606
+ for (const f of report.findings) f.fixPrompt = fixPrompt(f);
607
+ const rank = { HIGH: 0, MEDIUM: 1, LOW: 2 };
608
+ report.findings.sort((a, b) => rank[a.severity] - rank[b.severity]);
609
+
610
+ report.stopped = stopped;
611
+ report.endedAt = new Date().toISOString();
612
+ return report;
613
+ }
614
+
615
+ /**
616
+ * Re-checks one finding: the plain replay of its flow and the one value that
617
+ * found it, nothing else - two runs. The verdict is the same four words every
618
+ * re-check uses. A fixed finding leaves the saved report, so the dashboard
619
+ * stops showing it; a finding still there keeps its place.
620
+ */
621
+ async function recheck(root, flows, finding, options) {
622
+ const opts = options || {};
623
+ const say = typeof opts.say === 'function' ? opts.say : () => {};
624
+ const file = String(finding.where || '').replace(/:\d+$/, '');
625
+ const line = Number((/:(\d+)$/.exec(finding.where || '') || [])[1]);
626
+ const flow = flows.find((f) => f.file === file);
627
+ const unsure = (why) => ({ verdict: 'could not confirm', why: why, newProblems: [] });
628
+ if (!flow) return unsure('the recording ' + file + ' is not there, or cannot be run now');
629
+ const field = candidateFields(flow.source).find((f) => f.line === line);
630
+ if (!field || !KINDS[finding.kind]) return unsure('that field is no longer typed into at line ' + line + ' of ' + file);
631
+ const report = await run(root, [flow], {
632
+ say: say, onControl: opts.onControl, gapMs: opts.gapMs, env: opts.env, hide: opts.hide, childEnv: opts.childEnv,
633
+ only: { line: line, kind: finding.kind }, confirmed: opts.confirmed,
634
+ });
635
+ // Where the app saved is remembered whatever the verdict, so the next
636
+ // question names it.
637
+ const before = load(root, opts.env);
638
+ if (before && report.writeHosts.some((h) => !(before.writeHosts || []).includes(h))) {
639
+ before.writeHosts = Array.from(new Set((before.writeHosts || []).concat(report.writeHosts)));
640
+ save(root, before, opts.env, { asIs: true });
641
+ }
642
+ if (report.stopped) return unsure('it was stopped before it finished');
643
+ const tested = report.flows.find((f) => f.file === file);
644
+ if (!tested || tested.state !== 'tested') return unsure(tested ? tested.why : 'the flow did not run');
645
+ const left = report.notTested.find((n) => n.where === finding.where && n.kind === finding.kind);
646
+ if (left) return unsure(left.why);
647
+ const same = report.findings.find((f) => f.where === finding.where && f.kind === finding.kind && f.type === finding.type);
648
+ const others = report.findings.filter((f) => f !== same && f.status === 'confirmed');
649
+ const result = same
650
+ ? { verdict: 'still open', why: same.proof.slice(2).join(' '), newProblems: others }
651
+ : { verdict: 'fixed', why: '', newProblems: others };
652
+ // The saved report: this finding leaves it when fixed; nothing else in it
653
+ // changes, because nothing else was run again.
654
+ const saved = load(root, opts.env);
655
+ if (saved && result.verdict === 'fixed') {
656
+ saved.findings = saved.findings.filter((f) => !(f.where === finding.where && f.kind === finding.kind && f.type === finding.type));
657
+ save(root, saved, opts.env, { asIs: true });
658
+ }
659
+ result.testData = report.testData;
660
+ return result;
661
+ }
662
+
663
+ /**
664
+ * What a run did not try is not cleared by it. A finding from an earlier run
665
+ * whose value was not tried this time - stopped, rate limited, over the cap,
666
+ * its flow not passing - is kept, and says it is from that earlier run. Only
667
+ * trying the same value again can clear it.
668
+ */
669
+ function carryOver(previous, report, root) {
670
+ if (!previous || !Array.isArray(previous.findings)) return report;
671
+ const tried = new Set(report.held.concat(report.findings).map((x) => x.where + '|' + x.kind));
672
+ for (const f of previous.findings) {
673
+ if (tried.has(f.where + '|' + f.kind)) continue;
674
+ // A recording that is gone takes its findings with it.
675
+ const file = String(f.where).replace(/:\d+$/, '');
676
+ if (root && !fs.existsSync(path.join(root, file))) continue;
677
+ const kept = Object.assign({}, f, { carried: f.carried || previous.endedAt || previous.startedAt });
678
+ if (!f.carried) kept.proof = (f.proof || []).concat(['Seen on an earlier run; this value was not tried on the latest one.']);
679
+ report.findings.push(kept);
680
+ }
681
+ return report;
682
+ }
683
+
684
+ /**
685
+ * Every address a press would reach: the pages the recordings open, and the
686
+ * places the last plain replay saw the app save to. This is what the person
687
+ * is asked about, so a database somewhere else is named before it is written.
688
+ */
689
+ function knownAddresses(root, sources, env) {
690
+ const out = [];
691
+ for (const source of sources) for (const a of probe.addressesOf(source)) if (!out.includes(a)) out.push(a);
692
+ const last = load(root, env);
693
+ for (const h of (last && last.writeHosts) || []) if (!out.includes(h)) out.push(h);
694
+ return out;
695
+ }
696
+
697
+ /** Keeps the report where the dashboard reads it - with what it did not try still said. */
698
+ function save(root, report, env, options) {
699
+ // A re-check writes back the report it read, minus what it proved fixed -
700
+ // carrying the old one over again would bring that finding straight back.
701
+ if (!(options && options.asIs)) carryOver(load(root, env), report, root);
702
+ const file = path.join(store.pathsFor(root, env).dir, 'edge-last.json');
703
+ fs.mkdirSync(path.dirname(file), { recursive: true });
704
+ fs.writeFileSync(file, JSON.stringify(report, null, 2) + '\n', 'utf8');
705
+ return file;
706
+ }
707
+
708
+ function load(root, env) {
709
+ try {
710
+ return JSON.parse(fs.readFileSync(path.join(store.pathsFor(root, env).dir, 'edge-last.json'), 'utf8'));
711
+ } catch (err) {
712
+ return null;
713
+ }
714
+ }
715
+
716
+ /** What is said when it ends, in the terminal and on the page. */
717
+ function summaryLines(report) {
718
+ const lines = [''];
719
+ const problems = report.findings.filter((f) => f.status === 'confirmed');
720
+ const toCheck = report.findings.filter((f) => f.status !== 'confirmed');
721
+ if (report.stopped) lines.push('Stopped after ' + report.runsDone + ' of ' + report.runsPlanned + ' runs. What did not run was NOT tested.');
722
+ // "Nothing found" is said only when something was tried: a press that tried
723
+ // nothing must never read as a clean one.
724
+ const triedAny = report.held.length || report.findings.some((f) => !f.carried);
725
+ lines.push(problems.length ? problems.length + (problems.length === 1 ? ' problem' : ' problems') + ' found:'
726
+ : triedAny ? 'No problem found in what was tried.' : 'Nothing was tested - see why below.');
727
+ for (const f of report.findings) {
728
+ lines.push('');
729
+ lines.push(' ' + f.severity + (f.status === 'confirmed' ? '' : ' (to check)') + ' ' + f.headline);
730
+ for (const p of f.proof) lines.push(' ' + p);
731
+ }
732
+ if (toCheck.length && !problems.length) lines.push('', toCheck.length + (toCheck.length === 1 ? ' thing' : ' things') + ' to check above - not proven problems.');
733
+ if (report.held.length) {
734
+ lines.push('', 'Held (' + report.held.length + '):');
735
+ for (const h of report.held) lines.push(' "' + h.field + '" given ' + KINDS[h.kind].words + ' - ' + h.why);
736
+ }
737
+ const skipped = report.flows.filter((f) => f.state !== 'tested');
738
+ if (report.notTested.length || skipped.length) {
739
+ lines.push('', 'NOT tested:');
740
+ for (const f of skipped) lines.push(' ' + f.title + ' - ' + f.why);
741
+ for (const n of report.notTested) lines.push(' "' + n.field + '" given ' + KINDS[n.kind].words + ' - ' + n.why);
742
+ }
743
+ if (report.testData.length) {
744
+ lines.push('', 'Test data left in your app: ' + report.testData.length + (report.testData.length === 1 ? ' write was' : ' writes were') + ' accepted.');
745
+ lines.push('Most of it contains "' + probe.TEST_WORD + '". Kryptheon does not delete it. The writes:');
746
+ for (const t of report.testData.slice(0, 15)) lines.push(' ' + t.request + ' (' + t.flow + ', ' + t.value + ')');
747
+ if (report.testData.length > 15) lines.push(' ... and ' + (report.testData.length - 15) + ' more');
748
+ }
749
+ lines.push('');
750
+ return lines;
751
+ }
752
+
753
+ module.exports = {
754
+ KINDS: KINDS,
755
+ LONG_LENGTH: LONG_LENGTH,
756
+ fillSteps: fillSteps,
757
+ candidateFields: candidateFields,
758
+ kindsFor: kindsFor,
759
+ fieldName: fieldName,
760
+ cleanSource: cleanSource,
761
+ variantSource: variantSource,
762
+ chooseVariants: chooseVariants,
763
+ flatten: flatten,
764
+ pairWrites: pairWrites,
765
+ savedValues: savedValues,
766
+ savedAs: savedAs,
767
+ judgeVariant: judgeVariant,
768
+ judgeMarkers: judgeMarkers,
769
+ fixPrompt: fixPrompt,
770
+ run: run,
771
+ recheck: recheck,
772
+ save: save,
773
+ carryOver: carryOver,
774
+ knownAddresses: knownAddresses,
775
+ load: load,
776
+ summaryLines: summaryLines,
777
+ };