kryptheon 0.1.52 → 0.1.54

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.
@@ -17,6 +17,7 @@ const crypto = require('crypto');
17
17
  const base = require('@playwright/test');
18
18
 
19
19
  const OUT = process.env.KRYPTHEON_PROBE_OUT || '';
20
+ const READS = process.env.KRYPTHEON_PROBE_READS === '1';
20
21
  const GAP_MS = Number(process.env.KRYPTHEON_PROBE_GAP_MS || 1000);
21
22
  // After a "too many requests", the app is asked to wait this long before the
22
23
  // next run, so one 429 does not turn every run after it into another.
@@ -33,6 +34,7 @@ const SWEEP_BUTTONS = 25;
33
34
  const SECRET_KEY = /(pass(word|code)?|secret|token|otp|card|cvv|cvc|\bpin\b|api[_-]?key|authorization)/i;
34
35
  const MARKER = require('./kryptheon-probe.js').MARKER;
35
36
  const heal = require('./kryptheon-heal.js');
37
+ const replay = require('./kryptheon-replay.js');
36
38
 
37
39
  function sleep(ms) {
38
40
  return new Promise((resolve) => setTimeout(resolve, ms));
@@ -333,6 +335,13 @@ const test = base.test.extend({
333
335
  // too, as in the plain replay (kryptheon-heal.js): the checks run the
334
336
  // recordings the replay passes, and would stop where it carried on. With
335
337
  // nothing remembered, no other rename is followed.
338
+ // The fresh names first: heal keeps the page's own getByRole as it is now
339
+ // (kryptheon-replay.js freshValues).
340
+ try {
341
+ replay.useFreshNames(page, replay.freshValues(fs.readFileSync(testInfo.file, 'utf8')), String(Date.now()).slice(-6));
342
+ } catch (e) {
343
+ /* replayed exactly as recorded */
344
+ }
336
345
  heal.watch(page, {});
337
346
 
338
347
  // A gap before every run, and a longer one after the app said "too many
@@ -504,7 +513,8 @@ const test = base.test.extend({
504
513
  // keeps - it is never listed as test data left behind.
505
514
  const signIn = JSON.stringify(kept).indexOf('[hidden]') !== -1;
506
515
  const event = Object.assign({ kind: 'write', method: method, host: at.host, origin: at.origin, path: at.path, status: status, signIn: signIn },
507
- redirectOf(res.url(), status, req.headers(), res.headers(), secrets), kept);
516
+ redirectOf(res.url(), status, req.headers(), res.headers(), secrets), kept,
517
+ READS ? { encoding: /json/i.test(type) ? 'json' : /x-www-form-urlencoded/i.test(type) ? 'form' : 'other' } : {});
508
518
  const placed = add(event);
509
519
  // What the server answered - often the record it saved, with its id.
510
520
  // That is the proof that two saves made two records, and of what was
@@ -524,6 +534,18 @@ const test = base.test.extend({
524
534
  answers.add(reading);
525
535
  }
526
536
  }
537
+ // What the app was asked to show, for the roles check, which learns the
538
+ // app's own requests from the recordings. Off unless asked for: no other
539
+ // check reads these, and a page makes a great many of them.
540
+ else if (READS && method === 'GET' && /^(fetch|xhr)$/.test(req.resourceType()) && siteOf(res.url()) === siteOf(page.url())) {
541
+ let query = '';
542
+ try {
543
+ query = new URL(res.url()).search;
544
+ } catch (err) {
545
+ query = '';
546
+ }
547
+ add({ kind: 'read', method: 'GET', host: at.host, origin: at.origin, path: at.path, query: hideValues(query, secrets).slice(0, 200), status: status });
548
+ }
527
549
  });
528
550
  // Answers still being read. Counted before a mark and before the end.
529
551
  const answers = new Set();
@@ -89,8 +89,7 @@ function consentLines(what, addresses, options) {
89
89
  what,
90
90
  '',
91
91
  'It runs against: ' + (addresses.length ? addresses.join(', ') : 'the addresses in your recordings'),
92
- 'It ADDS TEST DATA to your app, and that data STAYS there - Kryptheon does not delete it.',
93
- ].concat(marked ? [
92
+ ].concat(options && options.dataLines ? ['It CHANGES DATA in your app.'] : ['It ADDS TEST DATA to your app, and that data STAYS there - Kryptheon does not delete it.']).concat(options && options.dataLines ? options.dataLines : marked ? [
94
93
  'Most of it contains the word "' + TEST_WORD + '", so you can find it. Empty, negative and date values cannot carry it;',
95
94
  'every write that was accepted is listed when it ends.',
96
95
  ] : [
@@ -261,7 +260,7 @@ function runProbes(root, probes, options) {
261
260
  }
262
261
 
263
262
  // Each probe check keeps its last press in the store under this name.
264
- const REPORT_FILES = { edge: 'edge-last.json', reliability: 'reliability-last.json' };
263
+ const REPORT_FILES = { edge: 'edge-last.json', reliability: 'reliability-last.json', roles: 'roles-last.json' };
265
264
 
266
265
  function reportFile(root, check, env) {
267
266
  return path.join(store.pathsFor(root, env).dir, REPORT_FILES[check]);
@@ -75,12 +75,18 @@ const limited = (r) => eventsOf(r).some((e) => e.kind === 'limited');
75
75
  * says it adds or saves something. Not a sign-up - that makes an account -
76
76
  * and not one that only opens a form; that is found out at run time, when a
77
77
  * single click of it adds no record (singleClickWords says what it did).
78
+ * Beyond add/save/create, the words that book, reserve, order, pay or run
79
+ * something: found on blind round 22, where "Book 1 seat", "Reserve" and
80
+ * "Run overdue-notification job" each sent a write and none was tried.
78
81
  */
82
+ const ALSO_SAVES = /^[^\p{L}\p{N}]*(book|reserve|order|buy|pay|checkout|invite|apply|enrol|enroll|schedule|request|place|confirm|run|capture|claim|redeem)\b/iu;
83
+
79
84
  function saveSteps(source) {
80
85
  const lines = String(source || '').split('\n');
81
86
  const out = [];
82
87
  for (const a of clickSteps(source)) {
83
- if (!replay.CREATES.test(a.name.trim()) || replay.SIGNUP.test(a.name)) continue;
88
+ const name = a.name.trim();
89
+ if (!(replay.CREATES.test(name) || ALSO_SAVES.test(name)) || replay.SIGNUP.test(a.name)) continue;
84
90
  const m = CLICK.exec(lines[a.line - 1].replace(/\r$/, ''));
85
91
  if (!m) continue;
86
92
  out.push({ line: a.line, indent: m[1], locator: m[3], name: a.name.trim() });
@@ -119,6 +125,26 @@ function clickSteps(source) {
119
125
  return out;
120
126
  }
121
127
 
128
+ /**
129
+ * The line the sign-in ends on: the first button or link clicked (or Enter
130
+ * pressed) after the last password or .env value typed - a click into a
131
+ * field is not it. Found on blind round 22: the password was typed before
132
+ * the email, and the email, typed after it, was read back as a field the
133
+ * next save should have kept.
134
+ */
135
+ function signInEndOf(source) {
136
+ const lines = String(source || '').split('\n');
137
+ let secret = -1;
138
+ lines.forEach((line, i) => {
139
+ if (/\.fill\(/.test(line) && (replay.isSecretField(replay.nameInLine(line)) || /process\.env\./.test(line))) secret = i;
140
+ });
141
+ if (secret < 0) return 0;
142
+ for (let i = secret + 1; i < lines.length; i++) {
143
+ if (/getByRole\(\s*['"](button|link)['"][^)]*\)\s*\.click\(|type=["']?submit[^)]*\)\s*\.click\(|\.press\([^)]*Enter/.test(lines[i])) return i + 1;
144
+ }
145
+ return secret + 1;
146
+ }
147
+
122
148
  /**
123
149
  * What was typed before a saving step, to look for on the page after it:
124
150
  * each literal value filled in since the last step that saved, three
@@ -126,11 +152,7 @@ function clickSteps(source) {
126
152
  */
127
153
  function typedFor(source, steps, step) {
128
154
  const since = Math.max(0, ...steps.filter((s) => s.line < step.line).map((s) => s.line));
129
- const lines = String(source || '').split('\n');
130
- let signInEnd = 0;
131
- lines.forEach((line, i) => {
132
- if (/\.fill\(/.test(line) && (replay.isSecretField(replay.nameInLine(line)) || /process\.env\./.test(line))) signInEnd = i + 1;
133
- });
155
+ const signInEnd = signInEndOf(source);
134
156
  const out = [];
135
157
  for (const f of edge.fillSteps(source)) {
136
158
  if (f.line <= since || f.line >= step.line || f.line <= signInEnd || replay.isSecretField(f.name)) continue;
@@ -196,11 +218,7 @@ function keepSteps(source) {
196
218
  function keptFields(source, step) {
197
219
  const steps = keepSteps(source);
198
220
  const since = Math.max(0, ...steps.filter((s) => s.line < step.line).map((s) => s.line));
199
- const lines = String(source || '').split('\n');
200
- let signInEnd = 0;
201
- lines.forEach((line, i) => {
202
- if (/\.fill\(/.test(line) && (replay.isSecretField(replay.nameInLine(line)) || /process\.env\./.test(line))) signInEnd = i + 1;
203
- });
221
+ const signInEnd = signInEndOf(source);
204
222
  // Each field once: its last fill is what was saved, and what it held is
205
223
  // read before its first - a typo fixed by typing again is still one edit.
206
224
  const byLocator = new Map();
@@ -220,7 +238,38 @@ function sameValue(a, b) {
220
238
  const x = String(a == null ? '' : a).trim();
221
239
  const y = String(b == null ? '' : b).trim();
222
240
  if (x === y) return true;
223
- return x !== '' && y !== '' && Number.isFinite(Number(x)) && Number.isFinite(Number(y)) && Number(x) === Number(y);
241
+ if (x !== '' && y !== '' && Number.isFinite(Number(x)) && Number.isFinite(Number(y)) && Number(x) === Number(y)) return true;
242
+ // A date field's "2026-10-14" and the server's "2026-10-13T18:30:00.000Z"
243
+ // (the same day, stored at a timezone's midnight) are the same date.
244
+ const day = /^\d{4}-\d{2}-\d{2}(T[\d:.]+(Z|[+-]\d{2}:?\d{2})?)?$/;
245
+ return day.test(x) && day.test(y) && (x.length === 10 || y.length === 10) && Math.abs(Date.parse(x) - Date.parse(y)) < 86400000;
246
+ }
247
+
248
+ /**
249
+ * What the save's own answer says a field now holds, when it names it: the
250
+ * field "Pickup date" as pickup_date, pickupDate or "pickup date", in the
251
+ * answer or one named record inside it ({ shipment: {...} }). The server's
252
+ * word beats the form opened again. Found on blind round 23: the form opened
253
+ * again in a new tab was another form with an empty "Pickup date", and a date
254
+ * the app never cleared was read as cleared. undefined when not named.
255
+ */
256
+ function answeredValue(writes, field) {
257
+ const key = String((field && field.name) || '').toLowerCase().replace(/[^a-z0-9]/g, '');
258
+ if (!key) return undefined;
259
+ for (const w of writes || []) {
260
+ const a = w && w.answer;
261
+ if (!a || typeof a !== 'object' || Array.isArray(a)) continue;
262
+ const rows = [a].concat(Object.values(a).filter((v) => v && typeof v === 'object' && !Array.isArray(v)));
263
+ for (const row of rows) {
264
+ for (const k of Object.keys(row)) {
265
+ if (k.toLowerCase().replace(/[^a-z0-9]/g, '') !== key) continue;
266
+ const v = row[k];
267
+ if (v === null) return '';
268
+ if (typeof v !== 'object') return String(v);
269
+ }
270
+ }
271
+ }
272
+ return undefined;
224
273
  }
225
274
 
226
275
  /**
@@ -268,10 +317,12 @@ function judgeKept(flow, step, runs) {
268
317
  judged += 1;
269
318
  for (const e of back) {
270
319
  const f = fields.find((x) => x.line === e.line);
271
- if (notEdit(f, e) || e.now === null) continue;
272
- const item = { run: counted.indexOf(r) + 1, field: f, was: e.was, now: e.now, write: took[0] };
273
- if (sameValue(e.now, f.value)) kept += 1;
274
- else if (sameValue(e.now, e.was)) lost.push(item);
320
+ const said = f ? answeredValue(took, f) : undefined;
321
+ const now = said !== undefined ? said : e.now;
322
+ if (notEdit(f, e) || now === null) continue;
323
+ const item = { run: counted.indexOf(r) + 1, field: f, was: e.was, now: now, write: took[0], said: said !== undefined };
324
+ if (sameValue(now, f.value)) kept += 1;
325
+ else if (sameValue(now, e.was)) lost.push(item);
275
326
  else changed.push(item);
276
327
  }
277
328
  }
@@ -298,12 +349,14 @@ function judgeKept(flow, step, runs) {
298
349
  findings.push({
299
350
  flow: flow.title, where: flow.file + ':' + step.line, kind: 'kept', type: 'lost', step: step.name, field: first.field.name || 'line ' + first.field.line,
300
351
  severity: sure ? 'MEDIUM' : 'LOW', status: sure ? 'confirmed' : 'verification required',
301
- headline: 'After "' + step.name + '", ' + thing(first) + ' was ' + typedWords(first) + ', but the form opened again still shows ' + JSON.stringify(first.was) + ' - the change was not kept.',
352
+ headline: 'After "' + step.name + '", ' + thing(first) + ' was ' + typedWords(first) + ', but ' + (first.said ? 'the app\'s answer still has ' : 'the form opened again still shows ') +
353
+ JSON.stringify(first.was) + ' - the change was not kept.',
302
354
  proof: [
303
355
  'Flow: ' + flow.title + ' (' + flow.file + ':' + first.field.line + ', saved at line ' + step.line + ')',
304
356
  'Before typing, ' + thing(first) + ' held ' + JSON.stringify(first.was) + '; the recording ' + (String(first.field.value).trim() === '' ? 'cleared it' : 'typed ' + JSON.stringify(first.field.value)) + '.',
305
357
  'The save: ' + words(first.write) + ' - the app took it.',
306
- 'The same form opened again in a new tab: ' + thing(first) + ' shows ' + JSON.stringify(first.now) + ', what it held before.',
358
+ first.said ? 'The server\'s own answer to that save: ' + thing(first) + ' is ' + JSON.stringify(first.now) + ', what it held before.'
359
+ : 'The same form opened again in a new tab: ' + thing(first) + ' shows ' + JSON.stringify(first.now) + ', what it held before.',
307
360
  'Seen in ' + runsLost + ' of ' + judged + (judged === 1 ? ' run' : ' runs') + ' that saved.',
308
361
  ],
309
362
  });
@@ -534,15 +587,24 @@ function recordIds(w) {
534
587
  if (answer == null) return [];
535
588
  const rows = Array.isArray(answer) ? answer : answer && typeof answer === 'object' && answer.data !== undefined
536
589
  ? [].concat(answer.data) : [answer];
590
+ const idOf = (row) => {
591
+ if (!row || typeof row !== 'object') return null;
592
+ for (const k of ['id', 'uuid', '_id']) if (typeof row[k] === 'string' || typeof row[k] === 'number') return String(row[k]);
593
+ return null;
594
+ };
537
595
  const out = [];
538
596
  for (const row of rows) {
539
- if (!row || typeof row !== 'object') continue;
540
- for (const k of ['id', 'uuid', '_id']) {
541
- if (typeof row[k] === 'string' || typeof row[k] === 'number') {
542
- out.push(String(row[k]));
543
- break;
544
- }
597
+ const id = idOf(row);
598
+ if (id !== null) {
599
+ out.push(id);
600
+ continue;
545
601
  }
602
+ // The record wrapped in one named key ({ note: { id: 7 } }). Found on
603
+ // blind round 22: an upsert answered both clicks with the same note, and
604
+ // with its id unread the two answers were taken for two notes.
605
+ const inner = row && typeof row === 'object' && !Array.isArray(row)
606
+ ? Object.values(row).filter((v) => v && typeof v === 'object' && !Array.isArray(v) && idOf(v) !== null) : [];
607
+ if (inner.length === 1) out.push(idOf(inner[0]));
546
608
  }
547
609
  return out;
548
610
  }
@@ -1356,6 +1418,8 @@ module.exports = {
1356
1418
  judgeDouble: judgeDouble,
1357
1419
  judgeShown: judgeShown,
1358
1420
  judgeKept: judgeKept,
1421
+ sameValue: sameValue,
1422
+ answeredValue: answeredValue,
1359
1423
  keepSteps: keepSteps,
1360
1424
  keptFields: keptFields,
1361
1425
  judgeTwice: judgeTwice,
@@ -316,7 +316,128 @@ function timeBudget(source) {
316
316
  return Math.min(BUDGET.max, Math.max(BUDGET.min, BUDGET.base + steps * BUDGET.perStep));
317
317
  }
318
318
 
319
+ // A value the recording types and then finds its own record by: "nothing"
320
+ // typed as a work order's title, then the list row "nothing Morgan Owner"
321
+ // clicked. Replayed, every run adds one more "nothing", the click finds two,
322
+ // and the recording never passes again (blind round 22: three of eleven).
323
+ // Each run types such a value with a number after it - "nothing 482173" -
324
+ // and looks for that; page text compares numbers as '#', so the page still
325
+ // reads the same to the regression check. Emails, addresses and numbers are
326
+ // left as typed: a field may refuse them with anything added.
327
+ const FRESH_SKIP = /@|^https?:|^[\d\s.,:/+#-]+$/;
328
+ const QUOTED = /(['"`])((?:\\.|(?!\1).)*)\1/g;
329
+
330
+ function freshValues(source) {
331
+ const lines = String(source || '').split('\n');
332
+ const fills = [];
333
+ lines.forEach((line, i) => {
334
+ const m = /\.fill\(\s*(['"`])((?:\\.|(?!\1).)*)\1\s*\)/.exec(line);
335
+ if (!m || /process\.env\./.test(line) || isSecretField(nameInLine(line))) return;
336
+ const value = m[2].trim();
337
+ if (value.length >= 3 && !FRESH_SKIP.test(value)) fills.push({ value: value, line: i });
338
+ });
339
+ const out = [];
340
+ for (const f of fills) {
341
+ const later = lines.slice(f.line + 1).some((l) => /getBy(Role|Text|Label|Title|AltText)\(/.test(l) && !/\.fill\(/.test(l) &&
342
+ Array.from(l.matchAll(QUOTED)).some((q) => q[2].includes(f.value)));
343
+ if (later && !out.includes(f.value)) out.push(f.value);
344
+ }
345
+ return out.sort((a, b) => b.length - a.length);
346
+ }
347
+
348
+ // Which page gets which fresh names. Set per page, read by the locators it
349
+ // makes, so two tests in one process never share a run's number.
350
+ const FRESH = new WeakMap();
351
+ const GETTERS = ['getByRole', 'getByText', 'getByLabel', 'getByTitle', 'getByAltText', 'getByPlaceholder'];
352
+
353
+ /** "nothing" -> "nothing 482173" wherever a looked-for or typed text holds it. */
354
+ function freshText(state, text) {
355
+ return state && typeof text === 'string' ? text.replace(state.pattern, (m) => m + ' ' + state.token) : text;
356
+ }
357
+
358
+ function wrapGetters(proto, stateOf) {
359
+ for (const name of GETTERS) {
360
+ const orig = proto[name];
361
+ if (typeof orig !== 'function' || orig.__kxFresh) continue;
362
+ const wrapped = function (arg, options) {
363
+ let state = null;
364
+ try {
365
+ state = stateOf(this);
366
+ } catch (e) {
367
+ state = null;
368
+ }
369
+ if (!state) return orig.call(this, arg, options);
370
+ if (name === 'getByRole') {
371
+ return orig.call(this, arg, options && typeof options.name === 'string' ? Object.assign({}, options, { name: freshText(state, options.name) }) : options);
372
+ }
373
+ return orig.call(this, freshText(state, arg), options);
374
+ };
375
+ wrapped.__kxFresh = true;
376
+ proto[name] = wrapped;
377
+ }
378
+ }
379
+
380
+ /** Gives this page's run its own names for the values freshValues found. */
381
+ function useFreshNames(page, values, token) {
382
+ if (!page || !values || !values.length) return;
383
+ const escaped = values.map((v) => v.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'));
384
+ FRESH.set(page, { pattern: new RegExp(escaped.join('|'), 'g'), token: String(token) });
385
+ const pageProto = Object.getPrototypeOf(page);
386
+ wrapGetters(pageProto, (p) => FRESH.get(p));
387
+ const locatorProto = Object.getPrototypeOf(page.locator('body'));
388
+ wrapGetters(locatorProto, (l) => FRESH.get(l.page()));
389
+ if (!locatorProto.fill.__kxFresh) {
390
+ const fill = locatorProto.fill;
391
+ const wrapped = function (value, options) {
392
+ let state = null;
393
+ try {
394
+ state = FRESH.get(this.page());
395
+ } catch (e) {
396
+ state = null;
397
+ }
398
+ // Only a value typed whole, as the recording typed it, gets the number.
399
+ const typed = state && typeof value === 'string' && new RegExp('^\\s*(?:' + state.pattern.source + ')\\s*$').test(value);
400
+ return fill.call(this, typed ? freshText(state, value) : value, options);
401
+ };
402
+ wrapped.__kxFresh = true;
403
+ locatorProto.fill = wrapped;
404
+ }
405
+ }
406
+
407
+ /**
408
+ * Every text in `value` with this run's " 482173" taken back out (keys too).
409
+ * `shown` is each fresh value as the page signature writes it, numbers as
410
+ * '#' ("nothing 2" -> "nothing #"): there the number is already "#", and
411
+ * "nothing # #" goes back to "nothing #".
412
+ */
413
+ function withoutFresh(value, token, shown) {
414
+ if (!token) return value;
415
+ const forms = (shown || []).filter(Boolean).sort((a, b) => b.length - a.length);
416
+ // One pass, longest form first, so "nothing # #" loses one "#" and keeps
417
+ // the "2" of "nothing 2".
418
+ const numbered = forms.length ? new RegExp('(' + forms.map((f) => f.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')).join('|') + ') #', 'g') : null;
419
+ const strip = (s) => {
420
+ const out = s.split(' ' + token).join('');
421
+ return numbered ? out.replace(numbered, '$1') : out;
422
+ };
423
+ const walk = (v) => {
424
+ if (typeof v === 'string') return strip(v);
425
+ if (Array.isArray(v)) return v.map(walk);
426
+ if (v && typeof v === 'object' && Object.getPrototypeOf(v) === Object.prototype) {
427
+ const out = {};
428
+ for (const k of Object.keys(v)) out[strip(k)] = walk(v[k]);
429
+ return out;
430
+ }
431
+ return v;
432
+ };
433
+ return walk(value);
434
+ }
435
+
319
436
  module.exports = {
437
+ withoutFresh: withoutFresh,
438
+ freshValues: freshValues,
439
+ useFreshNames: useFreshNames,
440
+ freshText: freshText,
320
441
  recordingId: recordingId,
321
442
  timeBudget: timeBudget,
322
443
  BUDGET: BUDGET,
@@ -326,6 +447,7 @@ module.exports = {
326
447
  scrubSecrets: scrubSecrets,
327
448
  dropTrailingLogout: dropTrailingLogout,
328
449
  isSecretField: isSecretField,
450
+ accountPart: accountPart,
329
451
  nameInLine: nameInLine,
330
452
  CREATES: CREATES,
331
453
  ROW_PICK: ROW_PICK,