kryptheon 0.1.49 → 0.1.51

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.
@@ -32,6 +32,7 @@ const SWEEP_BUTTONS = 25;
32
32
  // Values under these names are never kept, whatever request they were in.
33
33
  const SECRET_KEY = /(pass(word|code)?|secret|token|otp|card|cvv|cvc|\bpin\b|api[_-]?key|authorization)/i;
34
34
  const MARKER = require('./kryptheon-probe.js').MARKER;
35
+ const heal = require('./kryptheon-heal.js');
35
36
 
36
37
  function sleep(ms) {
37
38
  return new Promise((resolve) => setTimeout(resolve, ms));
@@ -164,6 +165,88 @@ function where(url) {
164
165
  }
165
166
  }
166
167
 
168
+ /**
169
+ * Where a write was sent from and, when the answer was a redirect, where it
170
+ * sent the browser: { from, to }, each a path (with its query) on the same
171
+ * site, or a whole address on another. A form that posts and redirects - Post,
172
+ * Redirect, Get - answers 302 when it saved; where the redirect goes says
173
+ * whether it did (blind round 19: every form save was read as refused).
174
+ */
175
+ function redirectOf(url, status, reqHeaders, resHeaders, secrets) {
176
+ const out = {};
177
+ const own = (u) => {
178
+ try {
179
+ const at = new URL(u, url);
180
+ const same = at.origin === new URL(url).origin;
181
+ return hideValues((same ? '' : at.origin) + at.pathname + at.search, secrets).slice(0, 300);
182
+ } catch (err) {
183
+ return null;
184
+ }
185
+ };
186
+ const referer = (reqHeaders || {}).referer;
187
+ if (referer) out.from = own(referer);
188
+ const location = (resHeaders || {}).location;
189
+ if (status >= 300 && status < 400 && location) out.to = own(location);
190
+ return out;
191
+ }
192
+
193
+ /**
194
+ * The same address, give or take a record's id: /courses/5/notes and
195
+ * /courses/14/notes are the same save to two records. Found on blind round 21:
196
+ * a recording opened the third course in a list, the input and reliability
197
+ * runs had added courses by then, and the note went to another course's address
198
+ * - so the copy was never sent and the note saved twice was not tested.
199
+ */
200
+ function samePath(a, b) {
201
+ const shape = (p) => String(p || '').replace(/\/(\d+|[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}|[0-9a-f]{24})(?=\/|$)/gi, '/:id');
202
+ return a === b || shape(a) === shape(b);
203
+ }
204
+
205
+ /** Made by an embedded frame of another origin than the page's own. */
206
+ function fromOtherFrame(req, page) {
207
+ let frame;
208
+ try {
209
+ frame = req.frame();
210
+ } catch (err) {
211
+ return false;
212
+ }
213
+ if (!frame || frame === page.mainFrame()) return false;
214
+ const origin = (url) => { try { return new URL(url).origin; } catch (err) { return ''; } };
215
+ const own = origin(page.url());
216
+ return Boolean(own) && origin(frame.url()) !== own;
217
+ }
218
+
219
+ /**
220
+ * The site of a host: its last two labels, or three under a short
221
+ * second-level name (example.co.in). Close enough to tell razorpay.com from
222
+ * the app's own site.
223
+ */
224
+ function siteOf(url) {
225
+ let host;
226
+ try {
227
+ host = new URL(url).hostname;
228
+ } catch (err) {
229
+ return '';
230
+ }
231
+ if (/^[\d.]+$/.test(host) || host === 'localhost' || !host.includes('.')) return host;
232
+ const parts = host.split('.');
233
+ const short = parts.length >= 3 && /^(co|com|net|org|ac|gov|edu)$/.test(parts[parts.length - 2]) && parts[parts.length - 1].length === 2;
234
+ return parts.slice(short ? -3 : -2).join('.');
235
+ }
236
+
237
+ /**
238
+ * A vendor's script calling its own servers: the page loaded a script from
239
+ * another site, and this write goes to that same site. Found on the round 18
240
+ * bench after the frame rule: Razorpay's checkout.js runs in the page itself,
241
+ * not in its iframe, and posts to lumberjack.razorpay.com - which was still
242
+ * taken as the app saving. The app's own code (served from the app, even when
243
+ * it writes to a database elsewhere) is not this.
244
+ */
245
+ function vendorCall(url, page, vendorSites) {
246
+ const site = siteOf(url);
247
+ return Boolean(site) && site !== siteOf(page.url()) && vendorSites.has(site);
248
+ }
249
+
167
250
  function firstLine(text) {
168
251
  return String(text || '').replace(/\x1b\[[0-9;]*[A-Za-z]/g, '').split(/\r?\n/).map((l) => l.trim()).filter(Boolean)[0] || '';
169
252
  }
@@ -246,6 +329,11 @@ const test = base.test.extend({
246
329
  events.push(kept);
247
330
  return kept;
248
331
  };
332
+ // A text step whose count moved since it was recorded is followed here
333
+ // too, as in the plain replay (kryptheon-heal.js): the checks run the
334
+ // recordings the replay passes, and would stop where it carried on. With
335
+ // nothing remembered, no other rename is followed.
336
+ heal.watch(page, {});
249
337
 
250
338
  // A gap before every run, and a longer one after the app said "too many
251
339
  // requests": the app is being asked many times in a row, and a rate limit
@@ -373,6 +461,8 @@ const test = base.test.extend({
373
461
  }
374
462
  add(Object.assign({ kind: 'sweep' }, sweep));
375
463
  };
464
+ // Sites the page loaded a script from that are not its own (vendorCall).
465
+ const vendorSites = new Set();
376
466
  page.on('response', (res) => {
377
467
  const req = res.request();
378
468
  const method = req.method();
@@ -388,6 +478,19 @@ const test = base.test.extend({
388
478
  return;
389
479
  }
390
480
  if (status >= 500) add({ kind: 'server', method: method, host: at.host, path: at.path, status: status });
481
+ // A request an embedded frame from another origin makes on its own -
482
+ // a payment provider's checkout, its fraud and analytics calls - is not
483
+ // the app saving anything. Found on a blind test (round 18): Razorpay's
484
+ // checkout iframe posted to Razorpay, Stripe, Sentry and sardine.ai,
485
+ // those hosts were taken as the app's own, and the input and
486
+ // reliability checks refused to run until they were ticked as "my own
487
+ // test copy". Only frames of another origin: the page itself writing to
488
+ // a remote database is still a write, and still asked about.
489
+ if (req.resourceType() === 'script' && siteOf(res.url()) !== siteOf(page.url())) vendorSites.add(siteOf(res.url()));
490
+ if (WRITE.test(method) && (fromOtherFrame(req, page) || vendorCall(res.url(), page, vendorSites))) {
491
+ add({ kind: 'embedded', method: method, host: at.host, origin: at.origin, path: at.path, status: status });
492
+ return;
493
+ }
391
494
  if (WRITE.test(method)) {
392
495
  let raw = null;
393
496
  try {
@@ -400,7 +503,8 @@ const test = base.test.extend({
400
503
  // A write that carried a secret is the sign-in, not data the app
401
504
  // keeps - it is never listed as test data left behind.
402
505
  const signIn = JSON.stringify(kept).indexOf('[hidden]') !== -1;
403
- const event = Object.assign({ kind: 'write', method: method, host: at.host, origin: at.origin, path: at.path, status: status, signIn: signIn }, kept);
506
+ 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);
404
508
  const placed = add(event);
405
509
  // What the server answered - often the record it saved, with its id.
406
510
  // That is the proof that two saves made two records, and of what was
@@ -437,7 +541,7 @@ const test = base.test.extend({
437
541
  await page.route(() => true, async (route) => {
438
542
  const req = route.request();
439
543
  const at = where(req.url());
440
- if (!sent && req.method() === spec.method && at.host === spec.host && at.path === spec.path) {
544
+ if (!sent && req.method() === spec.method && at.host === spec.host && samePath(at.path, spec.path)) {
441
545
  let raw = null;
442
546
  try {
443
547
  raw = req.postData();
@@ -468,7 +572,7 @@ const test = base.test.extend({
468
572
  await page.route(() => true, async (route) => {
469
573
  const req = route.request();
470
574
  const at = where(req.url());
471
- if (sent || req.method() !== spec.method || at.host !== spec.host || at.path !== spec.path) return route.continue();
575
+ if (sent || req.method() !== spec.method || at.host !== spec.host || !samePath(at.path, spec.path)) return route.continue();
472
576
  sent = true;
473
577
  let raw = null;
474
578
  try {
@@ -489,12 +593,28 @@ const test = base.test.extend({
489
593
  }
490
594
  add({ kind: 'sent-straight', straightId: variant.id, method: spec.method, host: at.host, path: at.path, key: variant.key });
491
595
  try {
492
- const res = await page.request.fetch(req.url(), { method: req.method(), headers: headers, data: body, failOnStatusCode: false });
596
+ // Not followed: a redirect is the answer, as the page's own was.
597
+ const res = await page.request.fetch(req.url(), { method: req.method(), headers: headers, data: body, failOnStatusCode: false, maxRedirects: 0 });
493
598
  const status = res.status();
494
599
  if (status === 429) add({ kind: 'limited', straightId: variant.id, method: spec.method, host: at.host, path: at.path, status: status });
495
600
  if (status >= 500) add({ kind: 'server', straightId: variant.id, method: spec.method, host: at.host, path: at.path, status: status });
601
+ const moved = redirectOf(req.url(), status, headers, res.headers(), secrets);
496
602
  const placed = add(Object.assign({ kind: 'write', straightId: variant.id, method: spec.method, host: at.host, origin: at.origin, path: at.path, status: status, signIn: false },
497
- keptBody(body, type, secrets)));
603
+ moved, keptBody(body, type, secrets)));
604
+ // A form that redirects sends no record back. The page it
605
+ // redirects to, opened, is what it shows it kept: whether the
606
+ // value that was sent is on it.
607
+ if (moved.to && moved.to[0] === '/' && String(variant.value).trim().length >= 2) {
608
+ try {
609
+ const next = await page.request.get(new URL(res.headers().location, req.url()).href, { failOnStatusCode: false });
610
+ const html = await next.text();
611
+ const text = html.replace(/<script[\s\S]*?<\/script>|<style[\s\S]*?<\/style>/gi, ' ').replace(/<[^>]+>/g, ' ')
612
+ .replace(/&amp;/g, '&').replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&quot;/g, '"').replace(/&#39;/g, "'");
613
+ placed.shownAfter = next.status() < 400 && new RegExp('(^|[^\\w.-])' + String(variant.value).trim().replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + '($|[^\\w.])').test(text);
614
+ } catch (err) {
615
+ /* the page after it could not be opened: nothing said */
616
+ }
617
+ }
498
618
  if (/json/i.test((res.headers() || {})['content-type'] || '')) {
499
619
  try {
500
620
  const text = await res.text();
@@ -523,7 +643,7 @@ const test = base.test.extend({
523
643
  await page.route(() => true, async (route) => {
524
644
  const req = route.request();
525
645
  const at = where(req.url());
526
- if (sent || req.method() !== spec.method || at.host !== spec.host || at.path !== spec.path) return route.continue();
646
+ if (sent || req.method() !== spec.method || at.host !== spec.host || !samePath(at.path, spec.path)) return route.continue();
527
647
  sent = true;
528
648
  let raw = null;
529
649
  try {
@@ -535,9 +655,10 @@ const test = base.test.extend({
535
655
  for (const [k, v] of Object.entries(req.headers() || {})) if (!/^(content-length|host|connection|cookie)$/i.test(k) && k[0] !== ':') headers[k] = v;
536
656
  const type = headers['content-type'] || '';
537
657
  const kept = keptBody(raw, type, secrets);
538
- const copy = page.request.fetch(req.url(), { method: req.method(), headers: headers, data: raw == null ? undefined : raw, failOnStatusCode: false })
658
+ const copy = page.request.fetch(req.url(), { method: req.method(), headers: headers, data: raw == null ? undefined : raw, failOnStatusCode: false, maxRedirects: 0 })
539
659
  .then(async (res) => {
540
- const placed = add(Object.assign({ kind: 'write', copy: true, method: req.method(), host: at.host, origin: at.origin, path: at.path, status: res.status(), signIn: false }, kept));
660
+ const placed = add(Object.assign({ kind: 'write', copy: true, method: req.method(), host: at.host, origin: at.origin, path: at.path, status: res.status(), signIn: false },
661
+ redirectOf(req.url(), res.status(), headers, res.headers(), secrets), kept));
541
662
  if (/json/i.test((res.headers() || {})['content-type'] || '')) {
542
663
  try {
543
664
  const text = await res.text();
@@ -627,6 +748,52 @@ const test = base.test.extend({
627
748
  }
628
749
  };
629
750
 
751
+ // What a field held before the recording typed into it, and on which
752
+ // page. A form that edits something shows what is saved; one for
753
+ // something new is empty.
754
+ const was = {};
755
+ const cut = (v) => hideValues(String(v), secrets).slice(0, 200);
756
+ page.__kxWas = async (line, locator) => {
757
+ try {
758
+ was[line] = { value: await locator.first().inputValue({ timeout: 2000 }), url: page.url() };
759
+ } catch (err) {
760
+ /* not a field with a value: not read back */
761
+ }
762
+ };
763
+ // After a save: each form it typed into opened again in a new tab - a
764
+ // GET, so nothing is sent again - and each field read, to say whether
765
+ // what was typed was kept. Found on blind round 19: an edit that saved
766
+ // everything but the description, and a date that could not be cleared,
767
+ // both answered 302 and looked like saves.
768
+ page.__kxReadBack = async (name, list) => {
769
+ const byUrl = {};
770
+ for (const f of list) {
771
+ const w = was[f.line];
772
+ if (w && /^https?:/.test(w.url)) (byUrl[w.url] = byUrl[w.url] || []).push(f);
773
+ }
774
+ for (const url of Object.keys(byUrl)) {
775
+ let other = null;
776
+ try {
777
+ other = await page.context().newPage();
778
+ await other.goto(url, { waitUntil: 'load', timeout: 15000 });
779
+ await other.waitForLoadState('networkidle', { timeout: 5000 }).catch(() => {});
780
+ for (const f of byUrl[url]) {
781
+ let now = null;
782
+ try {
783
+ now = await f.get(other).first().inputValue({ timeout: 3000 });
784
+ } catch (err) {
785
+ now = null;
786
+ }
787
+ add({ kind: 'readback', name: String(name), line: f.line, was: cut(was[f.line].value), now: now === null ? null : cut(now), at: where(url).path });
788
+ }
789
+ } catch (err) {
790
+ /* not read back: said as not tested by whoever reads it */
791
+ } finally {
792
+ if (other) await other.close().catch(() => {});
793
+ }
794
+ }
795
+ };
796
+
630
797
  // Writes still on their way. A recording often ends right after the step
631
798
  // that saves, and the answer to that save is the proof - so the run waits
632
799
  // for it before it looks at anything or opens the page again, which would