kryptheon 0.1.47 → 0.1.49

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.
@@ -1,303 +1,721 @@
1
- // The fixture the probe copies run under - never the recordings themselves.
2
- //
3
- // A probe is a copy of a recording, changed on purpose: one field given an
4
- // unusual value (kryptheon-edge.js), or one step clicked twice. What matters
5
- // in such a run is not whether it passed - a form that refuses a bad value is
6
- // doing its job - but what the app did: did the page throw, did the server
7
- // answer 5xx, what did the app write, and did a tag typed into a field come
8
- // back as real markup. This writes all of that to one JSON file per run.
9
- //
10
- // It never touches the baselines or the history: a probe is not a check of
11
- // the recorded flow, and must never change what "the last time it worked"
12
- // means for one.
13
-
14
- const fs = require('fs');
15
- const path = require('path');
16
- const base = require('@playwright/test');
17
-
18
- const OUT = process.env.KRYPTHEON_PROBE_OUT || '';
19
- const GAP_MS = Number(process.env.KRYPTHEON_PROBE_GAP_MS || 1000);
20
- // After a "too many requests", the app is asked to wait this long before the
21
- // next run, so one 429 does not turn every run after it into another.
22
- const BACKOFF_MS = Number(process.env.KRYPTHEON_PROBE_BACKOFF_MS || 10000);
23
- const MAX_BODY = 20000;
24
- const WRITE = /^(POST|PUT|PATCH|DELETE)$/;
25
- // Values under these names are never kept, whatever request they were in.
26
- const SECRET_KEY = /(pass(word|code)?|secret|token|otp|card|cvv|cvc|\bpin\b|api[_-]?key|authorization)/i;
27
- const MARKER = require('./kryptheon-probe.js').MARKER;
28
-
29
- function sleep(ms) {
30
- return new Promise((resolve) => setTimeout(resolve, ms));
31
- }
32
-
33
- // Every value the recording reads from .env. They are what a sign-in sends,
34
- // and they must not reach a results file in any form.
35
- function secretValues() {
36
- return String(process.env.KRYPTHEON_PROBE_SECRETS || '')
37
- .split(',')
38
- .map((name) => name.trim())
39
- .filter(Boolean)
40
- .map((name) => process.env[name])
41
- .filter((v) => typeof v === 'string' && v.length >= 3);
42
- }
43
-
44
- function hideValues(text, secrets) {
45
- let s = String(text == null ? '' : text);
46
- for (const v of secrets) s = s.split(v).join('[hidden]');
47
- // encodeURIComponent: a form post carries the password encoded.
48
- for (const v of secrets) s = s.split(encodeURIComponent(v)).join('[hidden]');
49
- return s;
50
- }
51
-
52
- function hideKeys(value) {
53
- if (Array.isArray(value)) return value.map(hideKeys);
54
- if (value && typeof value === 'object') {
55
- const out = {};
56
- for (const k of Object.keys(value)) out[k] = SECRET_KEY.test(k) ? '[hidden]' : hideKeys(value[k]);
57
- return out;
58
- }
59
- return value;
60
- }
61
-
62
- /**
63
- * A request body as it may be kept: secrets taken out, by value and by name.
64
- * JSON and form posts are kept as data, so a later run can be compared field
65
- * by field; anything else only as text.
66
- */
67
- function keptBody(raw, contentType, secrets) {
68
- if (raw == null || raw === '') return { text: '' };
69
- const text = String(raw);
70
- if (/json/i.test(contentType || '') || /^\s*[[{]/.test(text)) {
71
- try {
72
- const data = hideKeys(JSON.parse(text));
73
- return { data: JSON.parse(hideValues(JSON.stringify(data), secrets)) };
74
- } catch (err) {
75
- /* not JSON after all */
76
- }
77
- }
78
- if (/x-www-form-urlencoded/i.test(contentType || '')) {
79
- const data = {};
80
- for (const [k, v] of new URLSearchParams(text)) data[k] = SECRET_KEY.test(k) ? '[hidden]' : v;
81
- return { data: JSON.parse(hideValues(JSON.stringify(data), secrets)) };
82
- }
83
- return { text: hideValues(text, secrets).slice(0, MAX_BODY) };
84
- }
85
-
86
- function where(url) {
87
- try {
88
- const u = new URL(url);
89
- return { host: u.host, origin: u.origin, path: u.pathname };
90
- } catch (err) {
91
- return { host: '', origin: '', path: String(url || '').split('?')[0] };
92
- }
93
- }
94
-
95
- function firstLine(text) {
96
- return String(text || '').replace(/\x1b\[[0-9;]*[A-Za-z]/g, '').split(/\r?\n/).map((l) => l.trim()).filter(Boolean)[0] || '';
97
- }
98
-
99
- // Every element the app made out of a probe tag. An attribute or a text node
100
- // holding the same characters is not one: only markup the browser parsed is.
101
- async function findMarkers(page) {
102
- try {
103
- return await page.evaluate((attr) => Array.prototype.slice.call(document.querySelectorAll('[' + attr + ']')).slice(0, 10).map((el) => ({
104
- id: el.getAttribute(attr),
105
- tag: el.tagName.toLowerCase(),
106
- html: el.outerHTML.slice(0, 200),
107
- path: location.pathname,
108
- })), MARKER);
109
- } catch (err) {
110
- return [];
111
- }
112
- }
113
-
114
- const test = base.test.extend({
115
- page: async ({ page }, use, testInfo) => {
116
- const secrets = secretValues();
117
- const events = [];
118
- const fields = {};
119
- let seq = 0;
120
- // Returns the event as kept, so what arrives later (a write's answer) is
121
- // added to the very object that is saved - not to a copy of it.
122
- const add = (e) => {
123
- const kept = Object.assign({ seq: ++seq }, e);
124
- events.push(kept);
125
- return kept;
126
- };
127
-
128
- // A gap before every run, and a longer one after the app said "too many
129
- // requests": the app is being asked many times in a row, and a rate limit
130
- // is the app protecting itself, not a problem in it.
131
- let backoff = false;
132
- try {
133
- backoff = Boolean(OUT) && fs.existsSync(path.join(OUT, 'rate-limited'));
134
- } catch (err) {
135
- backoff = false;
136
- }
137
- await sleep(backoff ? BACKOFF_MS : GAP_MS);
138
-
139
- page.on('pageerror', (err) => add({ kind: 'error', text: hideValues(firstLine((err && err.message) || String(err)), secrets).slice(0, 300) }));
140
- page.on('response', (res) => {
141
- const req = res.request();
142
- const method = req.method();
143
- const status = res.status();
144
- const at = where(res.url());
145
- if (status === 429) {
146
- add({ kind: 'limited', method: method, host: at.host, path: at.path, status: status });
147
- try {
148
- if (OUT) fs.writeFileSync(path.join(OUT, 'rate-limited'), '1');
149
- } catch (err) {
150
- /* a note, never a failure */
151
- }
152
- return;
153
- }
154
- if (status >= 500) add({ kind: 'server', method: method, host: at.host, path: at.path, status: status });
155
- if (WRITE.test(method)) {
156
- let raw = null;
157
- try {
158
- raw = req.postData();
159
- } catch (err) {
160
- raw = null;
161
- }
162
- const type = (req.headers() || {})['content-type'] || '';
163
- const kept = keptBody(raw, type, secrets);
164
- // A write that carried a secret is the sign-in, not data the app
165
- // keeps - it is never listed as test data left behind.
166
- const signIn = JSON.stringify(kept).indexOf('[hidden]') !== -1;
167
- const event = Object.assign({ kind: 'write', method: method, host: at.host, origin: at.origin, path: at.path, status: status, signIn: signIn }, kept);
168
- const placed = add(event);
169
- // What the server answered - often the record it saved, with its id.
170
- // That is the proof that two saves made two records, and of what was
171
- // really stored. Never for the sign-in, whose answer is a session.
172
- // Read after the event is placed, so its order among the marks is
173
- // the order the answer arrived in.
174
- if (!signIn && /json/i.test((res.headers() || {})['content-type'] || '')) {
175
- const reading = res.text().then((text) => {
176
- if (text && text.length <= MAX_BODY * 5) {
177
- try {
178
- placed.answer = JSON.parse(hideValues(JSON.stringify(hideKeys(JSON.parse(text))), secrets));
179
- } catch (err) {
180
- /* not JSON after all: no answer kept */
181
- }
182
- }
183
- }, () => {}).then(() => answers.delete(reading));
184
- answers.add(reading);
185
- }
186
- }
187
- });
188
- // Answers still being read. Counted before a mark and before the end.
189
- const answers = new Set();
190
- const answersRead = async () => {
191
- for (let waited = 0; answers.size && waited < 5000; waited += 100) await sleep(100);
192
- };
193
-
194
- // Called from inside a probe copy, around the steps that matter.
195
- page.__kxMark = async (name) => {
196
- // Let what the last step started arrive before it is counted: a moment
197
- // for a second click's request to leave, then every write still on its
198
- // way. A load state alone is not enough - once a page has gone quiet it
199
- // says so at once, even while a new save is in flight.
200
- await sleep(300);
201
- for (let waited = 0; inFlight.size && waited < 10000; waited += 100) await sleep(100);
202
- await answersRead();
203
- try {
204
- await page.waitForLoadState('networkidle', { timeout: 3000 });
205
- } catch (err) {
206
- /* a page that never goes quiet is still marked */
207
- }
208
- add({ kind: 'mark', name: String(name) });
209
- };
210
- // What kind of field a recorded fill typed into, read from the page
211
- // itself: whether it is required, a number, a date, how long it may be.
212
- page.__kxField = async (line, locator) => {
213
- try {
214
- fields[line] = await locator.first().evaluate((el) => {
215
- const label = (el.labels && el.labels[0] && el.labels[0].textContent) || '';
216
- return {
217
- tag: el.tagName.toLowerCase(),
218
- type: (el.getAttribute('type') || '').toLowerCase(),
219
- required: el.required === true || el.getAttribute('aria-required') === 'true' || /\*\s*$/.test(label.trim()),
220
- min: el.getAttribute('min'),
221
- maxLength: el.maxLength > 0 ? el.maxLength : null,
222
- pattern: el.getAttribute('pattern'),
223
- label: label.replace(/\s+/g, ' ').trim().slice(0, 80),
224
- placeholder: (el.getAttribute('placeholder') || '').slice(0, 80),
225
- name: (el.getAttribute('name') || el.id || '').slice(0, 80),
226
- };
227
- }, null, { timeout: 5000 });
228
- } catch (err) {
229
- fields[line] = null; // not knowing is never read as "not required"
230
- }
231
- };
232
-
233
- // Writes still on their way. A recording often ends right after the step
234
- // that saves, and the answer to that save is the proof - so the run waits
235
- // for it before it looks at anything or opens the page again, which would
236
- // cut the answer off.
237
- const inFlight = new Set();
238
- page.on('request', (req) => {
239
- if (WRITE.test(req.method())) inFlight.add(req);
240
- });
241
- const settled = (req) => inFlight.delete(req);
242
- page.on('requestfinished', settled);
243
- page.on('requestfailed', settled);
244
-
245
- const started = Date.now();
246
- await use(page);
247
-
248
- for (let waited = 0; inFlight.size && waited < 10000; waited += 100) await sleep(100);
249
- await answersRead();
250
- try {
251
- await page.waitForLoadState('networkidle', { timeout: 3000 });
252
- } catch (err) {
253
- /* a page that never goes quiet is still read */
254
- }
255
-
256
- // Markup made from a probe tag: on the page the flow ended on, and on the
257
- // same address opened again - which is what proves it was saved and is
258
- // shown to whoever opens it next. A GET, never a reload: reloading a page
259
- // that came from a form post would send the form again.
260
- const markers = { live: await findMarkers(page), reopened: [] };
261
- try {
262
- const url = page.url();
263
- if (/^https?:/.test(url)) {
264
- await page.goto(url, { waitUntil: 'load', timeout: 15000 });
265
- try {
266
- await page.waitForLoadState('networkidle', { timeout: 5000 });
267
- } catch (err) {
268
- /* the markers are read either way */
269
- }
270
- markers.reopened = await findMarkers(page);
271
- }
272
- } catch (err) {
273
- /* the page could not be opened again; what was live still counts */
274
- }
275
-
276
- const error = testInfo.error || (testInfo.errors && testInfo.errors[0]) || null;
277
- const file = path.basename(testInfo.file);
278
- let failedLine = null;
279
- if (error && error.stack) {
280
- const m = new RegExp(file.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + ':(\\d+)').exec(error.stack);
281
- if (m) failedLine = Number(m[1]);
282
- }
283
- const result = {
284
- id: file.replace(/\.kxprobe\.js$/, ''),
285
- status: testInfo.status,
286
- error: error ? hideValues(firstLine(error.message), secrets).slice(0, 300) : null,
287
- failedLine: failedLine,
288
- events: events,
289
- fields: fields,
290
- markers: markers,
291
- ms: Date.now() - started,
292
- };
293
- if (OUT) {
294
- try {
295
- fs.writeFileSync(path.join(OUT, result.id + '.json'), JSON.stringify(result), 'utf8');
296
- } catch (err) {
297
- /* the run is then reported as not finished, never as clean */
298
- }
299
- }
300
- },
301
- });
302
-
303
- module.exports = { test: test, expect: base.expect, keptBody: keptBody, hideValues: hideValues, MARKER: MARKER };
1
+ // The fixture the probe copies run under - never the recordings themselves.
2
+ //
3
+ // A probe is a copy of a recording, changed on purpose: one field given an
4
+ // unusual value (kryptheon-edge.js), or one step clicked twice. What matters
5
+ // in such a run is not whether it passed - a form that refuses a bad value is
6
+ // doing its job - but what the app did: did the page throw, did the server
7
+ // answer 5xx, what did the app write, and did a tag typed into a field come
8
+ // back as real markup. This writes all of that to one JSON file per run.
9
+ //
10
+ // It never touches the baselines or the history: a probe is not a check of
11
+ // the recorded flow, and must never change what "the last time it worked"
12
+ // means for one.
13
+
14
+ const fs = require('fs');
15
+ const path = require('path');
16
+ const crypto = require('crypto');
17
+ const base = require('@playwright/test');
18
+
19
+ const OUT = process.env.KRYPTHEON_PROBE_OUT || '';
20
+ const GAP_MS = Number(process.env.KRYPTHEON_PROBE_GAP_MS || 1000);
21
+ // After a "too many requests", the app is asked to wait this long before the
22
+ // next run, so one 429 does not turn every run after it into another.
23
+ const BACKOFF_MS = Number(process.env.KRYPTHEON_PROBE_BACKOFF_MS || 10000);
24
+ const MAX_BODY = 20000;
25
+ const WRITE = /^(POST|PUT|PATCH|DELETE)$/;
26
+ // Buttons the phone sweep never clicks: ones that save, remove, pay, send or
27
+ // sign out. Every write is blocked while it clicks anyway; these are left
28
+ // alone so a click that is meant to change something is never even tried.
29
+ const NOT_CLICKED = /\b(save|submit|create|add|new|delete|remove|destroy|archive|cancel subscription|pay|buy|checkout|purchase|order|send|post|publish|upload|import|invite|sign[\s_-]?(out|off)|log[\s_-]?(out|off)|reset|clear|empty|close account|deactivate)\b/i;
30
+ const SWEEP_PAGES = 3;
31
+ const SWEEP_BUTTONS = 25;
32
+ // Values under these names are never kept, whatever request they were in.
33
+ const SECRET_KEY = /(pass(word|code)?|secret|token|otp|card|cvv|cvc|\bpin\b|api[_-]?key|authorization)/i;
34
+ const MARKER = require('./kryptheon-probe.js').MARKER;
35
+
36
+ function sleep(ms) {
37
+ return new Promise((resolve) => setTimeout(resolve, ms));
38
+ }
39
+
40
+ // Every value the recording reads from .env. They are what a sign-in sends,
41
+ // and they must not reach a results file in any form.
42
+ function secretValues() {
43
+ return String(process.env.KRYPTHEON_PROBE_SECRETS || '')
44
+ .split(',')
45
+ .map((name) => name.trim())
46
+ .filter(Boolean)
47
+ .map((name) => process.env[name])
48
+ .filter((v) => typeof v === 'string' && v.length >= 3);
49
+ }
50
+
51
+ function hideValues(text, secrets) {
52
+ let s = String(text == null ? '' : text);
53
+ for (const v of secrets) s = s.split(v).join('[hidden]');
54
+ // encodeURIComponent: a form post carries the password encoded.
55
+ for (const v of secrets) s = s.split(encodeURIComponent(v)).join('[hidden]');
56
+ return s;
57
+ }
58
+
59
+ function hideKeys(value) {
60
+ if (Array.isArray(value)) return value.map(hideKeys);
61
+ if (value && typeof value === 'object') {
62
+ const out = {};
63
+ for (const k of Object.keys(value)) out[k] = SECRET_KEY.test(k) ? '[hidden]' : hideKeys(value[k]);
64
+ return out;
65
+ }
66
+ return value;
67
+ }
68
+
69
+ /**
70
+ * A request body as it may be kept: secrets taken out, by value and by name.
71
+ * JSON and form posts are kept as data, so a later run can be compared field
72
+ * by field; anything else only as text.
73
+ */
74
+ function keptBody(raw, contentType, secrets) {
75
+ if (raw == null || raw === '') return { text: '' };
76
+ const text = String(raw);
77
+ if (/json/i.test(contentType || '') || /^\s*[[{]/.test(text)) {
78
+ try {
79
+ const data = hideKeys(JSON.parse(text));
80
+ return { data: JSON.parse(hideValues(JSON.stringify(data), secrets)) };
81
+ } catch (err) {
82
+ /* not JSON after all */
83
+ }
84
+ }
85
+ if (/x-www-form-urlencoded/i.test(contentType || '')) {
86
+ const data = {};
87
+ for (const [k, v] of new URLSearchParams(text)) data[k] = SECRET_KEY.test(k) ? '[hidden]' : v;
88
+ return { data: JSON.parse(hideValues(JSON.stringify(data), secrets)) };
89
+ }
90
+ return { text: hideValues(text, secrets).slice(0, MAX_BODY) };
91
+ }
92
+
93
+ /**
94
+ * A request body with one value in it changed, or null when that key is not
95
+ * in it. `key` is the path to the value as flatten() writes it (items.0.qty).
96
+ * A number stays a number when the new value reads as one; an empty value
97
+ * where a number was is sent as null, the way JSON says "no number".
98
+ */
99
+ function sentStraight(raw, contentType, key, value) {
100
+ if (raw == null || raw === '' || !key) return null;
101
+ const text = String(raw);
102
+ if (/json/i.test(contentType || '') || /^\s*[[{]/.test(text)) {
103
+ let data;
104
+ try {
105
+ data = JSON.parse(text);
106
+ } catch (err) {
107
+ data = undefined;
108
+ }
109
+ if (data !== undefined) {
110
+ const parts = String(key).split('.');
111
+ let at = data;
112
+ for (const part of parts.slice(0, -1)) {
113
+ if (!at || typeof at !== 'object' || !Object.prototype.hasOwnProperty.call(at, part)) return null;
114
+ at = at[part];
115
+ }
116
+ const last = parts[parts.length - 1];
117
+ if (!at || typeof at !== 'object' || !Object.prototype.hasOwnProperty.call(at, last)) return null;
118
+ const was = at[last];
119
+ const v = String(value);
120
+ at[last] = typeof was !== 'number' ? v : v.trim() === '' ? null : Number.isFinite(Number(v)) ? Number(v) : v;
121
+ return JSON.stringify(data);
122
+ }
123
+ }
124
+ if (/x-www-form-urlencoded/i.test(contentType || '')) {
125
+ const form = new URLSearchParams(text);
126
+ if (!form.has(key)) return null;
127
+ form.set(key, String(value));
128
+ return form.toString();
129
+ }
130
+ return null;
131
+ }
132
+
133
+ // A key a request carries so that sending it twice saves it once - an
134
+ // idempotency key, a request key. A copy sent straight is a save of its own,
135
+ // so it gets a new one: with the form's own, the server answered with the
136
+ // record the form had just made, and the value was never stored or refused
137
+ // (blind round 17: confidence_pct 101, sent with the form's request_key).
138
+ const ONCE_KEY = /^(idempotency|request|client[_-]?request|dedupe)[_-]?(key|id)$|^nonce$|idempotency/i;
139
+
140
+ function freshKeys(body, changed) {
141
+ if (body == null) return body;
142
+ let data;
143
+ try {
144
+ data = JSON.parse(body);
145
+ } catch (err) {
146
+ return body;
147
+ }
148
+ if (!data || typeof data !== 'object' || Array.isArray(data)) return body;
149
+ let fresh = false;
150
+ for (const k of Object.keys(data)) {
151
+ if (k === changed || !ONCE_KEY.test(k) || typeof data[k] !== 'string' || !data[k]) continue;
152
+ data[k] = crypto.randomUUID();
153
+ fresh = true;
154
+ }
155
+ return fresh ? JSON.stringify(data) : body;
156
+ }
157
+
158
+ function where(url) {
159
+ try {
160
+ const u = new URL(url);
161
+ return { host: u.host, origin: u.origin, path: u.pathname };
162
+ } catch (err) {
163
+ return { host: '', origin: '', path: String(url || '').split('?')[0] };
164
+ }
165
+ }
166
+
167
+ function firstLine(text) {
168
+ return String(text || '').replace(/\x1b\[[0-9;]*[A-Za-z]/g, '').split(/\r?\n/).map((l) => l.trim()).filter(Boolean)[0] || '';
169
+ }
170
+
171
+ // Every element the app made out of a probe tag. An attribute or a text node
172
+ // holding the same characters is not one: only markup the browser parsed is.
173
+ async function findMarkers(page) {
174
+ try {
175
+ return await page.evaluate((attr) => Array.prototype.slice.call(document.querySelectorAll('[' + attr + ']')).slice(0, 10).map((el) => ({
176
+ id: el.getAttribute(attr),
177
+ tag: el.tagName.toLowerCase(),
178
+ html: el.outerHTML.slice(0, 200),
179
+ path: location.pathname,
180
+ })), MARKER);
181
+ } catch (err) {
182
+ return [];
183
+ }
184
+ }
185
+
186
+ /**
187
+ * The records the app made from a probe tag: the id (or uuid, or _id) its
188
+ * answer names, for each accepted write whose request carried the tag.
189
+ */
190
+ function taggedRecordIds(events) {
191
+ const out = [];
192
+ for (const e of events) {
193
+ if (e.kind !== 'write' || e.signIn || e.status < 200 || e.status >= 300 || e.answer == null) continue;
194
+ const sent = e.data !== undefined ? JSON.stringify(e.data) : String(e.text || '');
195
+ if (sent.indexOf(MARKER) === -1) continue;
196
+ const answer = e.answer;
197
+ const rows = Array.isArray(answer) ? answer : answer && typeof answer === 'object' && answer.data !== undefined ? [].concat(answer.data) : [answer];
198
+ for (const row of rows) {
199
+ if (!row || typeof row !== 'object') continue;
200
+ for (const k of ['id', 'uuid', '_id']) {
201
+ if ((typeof row[k] === 'string' || typeof row[k] === 'number') && String(row[k]).length && !out.includes(String(row[k]))) {
202
+ out.push(String(row[k]));
203
+ break;
204
+ }
205
+ }
206
+ }
207
+ }
208
+ return out;
209
+ }
210
+
211
+ // Links on this page to a record's own page: same site, and the record's id
212
+ // as a whole part of the address - /orders/42, /order?id=42 - never 142.
213
+ async function recordLinks(page, ids) {
214
+ if (!ids.length) return [];
215
+ try {
216
+ return await page.evaluate((list) => {
217
+ const found = [];
218
+ for (const a of document.querySelectorAll('a[href]')) {
219
+ let u;
220
+ try {
221
+ u = new URL(a.getAttribute('href'), location.href);
222
+ } catch (err) {
223
+ continue;
224
+ }
225
+ if (u.origin !== location.origin || !/^https?:$/.test(u.protocol)) continue;
226
+ const parts = u.pathname.split('/').map((x) => decodeURIComponent(x)).concat(Array.from(u.searchParams.values()));
227
+ if (list.some((id) => parts.includes(id)) && !found.includes(u.href) && u.href !== location.href) found.push(u.href);
228
+ }
229
+ return found.slice(0, 3);
230
+ }, ids);
231
+ } catch (err) {
232
+ return [];
233
+ }
234
+ }
235
+
236
+ const test = base.test.extend({
237
+ page: async ({ page }, use, testInfo) => {
238
+ const secrets = secretValues();
239
+ const events = [];
240
+ const fields = {};
241
+ let seq = 0;
242
+ // Returns the event as kept, so what arrives later (a write's answer) is
243
+ // added to the very object that is saved - not to a copy of it.
244
+ const add = (e) => {
245
+ const kept = Object.assign({ seq: ++seq }, e);
246
+ events.push(kept);
247
+ return kept;
248
+ };
249
+
250
+ // A gap before every run, and a longer one after the app said "too many
251
+ // requests": the app is being asked many times in a row, and a rate limit
252
+ // is the app protecting itself, not a problem in it.
253
+ let backoff = false;
254
+ try {
255
+ backoff = Boolean(OUT) && fs.existsSync(path.join(OUT, 'rate-limited'));
256
+ } catch (err) {
257
+ backoff = false;
258
+ }
259
+ await sleep(backoff ? BACKOFF_MS : GAP_MS);
260
+
261
+ // The form's own checks - required, min, max, type: when one stops a
262
+ // submit, the browser fires "invalid" on the field, and nothing is sent.
263
+ // Kept, so a value the browser stopped is said as that and never as one
264
+ // the app handled - its server never saw it.
265
+ try {
266
+ await page.exposeBinding('__kxInvalid', (source, info) => {
267
+ add({ kind: 'invalid', field: String((info && info.field) || ''), message: String((info && info.message) || '') });
268
+ });
269
+ await page.addInitScript(() => {
270
+ document.addEventListener('invalid', (e) => {
271
+ const el = e.target;
272
+ if (!el || typeof el.getAttribute !== 'function') return;
273
+ const label = (el.labels && el.labels[0] && el.labels[0].textContent) || el.getAttribute('aria-label') ||
274
+ el.getAttribute('placeholder') || el.getAttribute('name') || el.id || '';
275
+ try {
276
+ window.__kxInvalid({ field: label.replace(/\s+/g, ' ').trim().slice(0, 80), message: String(el.validationMessage || '').slice(0, 160) });
277
+ } catch (err) {
278
+ /* a note, never a failure */
279
+ }
280
+ }, true);
281
+ });
282
+ } catch (err) {
283
+ /* without it, a stopped submit is judged as before */
284
+ }
285
+
286
+ page.on('pageerror', (err) => add({ kind: 'error', text: hideValues(firstLine((err && err.message) || String(err)), secrets).slice(0, 300) }));
287
+ // The pages the flow went to, in order - where the phone sweep clicks.
288
+ const visited = [];
289
+ page.on('framenavigated', (frame) => {
290
+ if (frame !== page.mainFrame()) return;
291
+ const url = frame.url();
292
+ if (/^https?:/.test(url) && !visited.includes(url)) visited.push(url);
293
+ });
294
+
295
+ // At phone size, every button on the pages the flow went to is clicked,
296
+ // one at a time, and what the page logged as an error after each click
297
+ // is kept. Every write is blocked while it does this - nothing is saved,
298
+ // changed or removed - and a click that tried to write is said as that,
299
+ // since what it would have done was not seen. Buttons that save, remove
300
+ // or sign out are not clicked at all.
301
+ page.__kxSweep = async () => {
302
+ const sweep = { pages: [], clicks: [], skipped: [], onLoad: [] };
303
+ const heard = [];
304
+ const blocked = [];
305
+ const onConsole = (msg) => {
306
+ if (msg.type() === 'error') heard.push({ type: 'console', text: hideValues(firstLine(msg.text()), secrets).slice(0, 300) });
307
+ };
308
+ const onError = (err) => heard.push({ type: 'page', text: hideValues(firstLine((err && err.message) || String(err)), secrets).slice(0, 300) });
309
+ try {
310
+ await page.unrouteAll({ behavior: 'ignoreErrors' }).catch(() => {});
311
+ await page.route(() => true, (route) => {
312
+ const req = route.request();
313
+ if (!WRITE.test(req.method())) return route.continue();
314
+ blocked.push(req.method() + ' ' + where(req.url()).path);
315
+ return route.abort('blockedbyclient');
316
+ });
317
+ page.on('console', onConsole);
318
+ page.on('pageerror', onError);
319
+ for (const url of visited.slice(-SWEEP_PAGES)) {
320
+ const at = where(url).path;
321
+ try {
322
+ await page.goto(url, { waitUntil: 'load', timeout: 15000 });
323
+ await page.waitForLoadState('networkidle', { timeout: 5000 }).catch(() => {});
324
+ } catch (err) {
325
+ continue;
326
+ }
327
+ sweep.pages.push(at);
328
+ for (const e of heard.splice(0)) sweep.onLoad.push(Object.assign({ path: at }, e));
329
+ blocked.splice(0);
330
+ const buttons = page.getByRole('button');
331
+ const total = Math.min(await buttons.count().catch(() => 0), SWEEP_BUTTONS);
332
+ for (let i = 0; i < total; i++) {
333
+ const b = buttons.nth(i);
334
+ let name = '';
335
+ try {
336
+ if (!(await b.isVisible())) continue;
337
+ name = String((await b.getAttribute('aria-label')) || (await b.innerText({ timeout: 1000 })) || '').replace(/\s+/g, ' ').trim().slice(0, 60);
338
+ } catch (err) {
339
+ continue;
340
+ }
341
+ if (NOT_CLICKED.test(name)) {
342
+ if (!sweep.skipped.includes(name)) sweep.skipped.push(name);
343
+ continue;
344
+ }
345
+ const click = { path: at, name: name || '(a button with no name)', errors: [], blocked: [] };
346
+ try {
347
+ await b.click({ timeout: 2000 });
348
+ await sleep(400);
349
+ } catch (err) {
350
+ click.failed = hideValues(firstLine(err && err.message), secrets).slice(0, 160);
351
+ }
352
+ click.errors = heard.splice(0);
353
+ click.blocked = blocked.splice(0);
354
+ sweep.clicks.push(click);
355
+ await page.keyboard.press('Escape').catch(() => {});
356
+ if (page.url() !== url) {
357
+ try {
358
+ await page.goto(url, { waitUntil: 'load', timeout: 15000 });
359
+ await page.waitForLoadState('networkidle', { timeout: 5000 }).catch(() => {});
360
+ } catch (err) {
361
+ break;
362
+ }
363
+ heard.splice(0);
364
+ blocked.splice(0);
365
+ }
366
+ }
367
+ }
368
+ } catch (err) {
369
+ sweep.failed = hideValues(firstLine(err && err.message), secrets).slice(0, 200);
370
+ } finally {
371
+ page.off('console', onConsole);
372
+ page.off('pageerror', onError);
373
+ }
374
+ add(Object.assign({ kind: 'sweep' }, sweep));
375
+ };
376
+ page.on('response', (res) => {
377
+ const req = res.request();
378
+ const method = req.method();
379
+ const status = res.status();
380
+ const at = where(res.url());
381
+ if (status === 429) {
382
+ add({ kind: 'limited', method: method, host: at.host, path: at.path, status: status });
383
+ try {
384
+ if (OUT) fs.writeFileSync(path.join(OUT, 'rate-limited'), '1');
385
+ } catch (err) {
386
+ /* a note, never a failure */
387
+ }
388
+ return;
389
+ }
390
+ if (status >= 500) add({ kind: 'server', method: method, host: at.host, path: at.path, status: status });
391
+ if (WRITE.test(method)) {
392
+ let raw = null;
393
+ try {
394
+ raw = req.postData();
395
+ } catch (err) {
396
+ raw = null;
397
+ }
398
+ const type = (req.headers() || {})['content-type'] || '';
399
+ const kept = keptBody(raw, type, secrets);
400
+ // A write that carried a secret is the sign-in, not data the app
401
+ // keeps - it is never listed as test data left behind.
402
+ 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);
404
+ const placed = add(event);
405
+ // What the server answered - often the record it saved, with its id.
406
+ // That is the proof that two saves made two records, and of what was
407
+ // really stored. Never for the sign-in, whose answer is a session.
408
+ // Read after the event is placed, so its order among the marks is
409
+ // the order the answer arrived in.
410
+ if (!signIn && /json/i.test((res.headers() || {})['content-type'] || '')) {
411
+ const reading = res.text().then((text) => {
412
+ if (text && text.length <= MAX_BODY * 5) {
413
+ try {
414
+ placed.answer = JSON.parse(hideValues(JSON.stringify(hideKeys(JSON.parse(text))), secrets));
415
+ } catch (err) {
416
+ /* not JSON after all: no answer kept */
417
+ }
418
+ }
419
+ }, () => {}).then(() => answers.delete(reading));
420
+ answers.add(reading);
421
+ }
422
+ }
423
+ });
424
+ // Answers still being read. Counted before a mark and before the end.
425
+ const answers = new Set();
426
+ const answersRead = async () => {
427
+ // Copies sent straight go one after another, after the form's own answer.
428
+ for (let waited = 0; answers.size && waited < 20000; waited += 100) await sleep(100);
429
+ };
430
+
431
+ // A value the form's own checks stopped, sent straight to the server: the
432
+ // form is given the value that was recorded, and its own request - with
433
+ // the app's own sign-in, cookies and headers - has only this one value
434
+ // changed on its way out. Once, on the first request that carries it.
435
+ page.__kxSendStraight = async (spec) => {
436
+ let sent = false;
437
+ await page.route(() => true, async (route) => {
438
+ const req = route.request();
439
+ const at = where(req.url());
440
+ if (!sent && req.method() === spec.method && at.host === spec.host && at.path === spec.path) {
441
+ let raw = null;
442
+ try {
443
+ raw = req.postData();
444
+ } catch (err) {
445
+ raw = null;
446
+ }
447
+ const body = sentStraight(raw, (req.headers() || {})['content-type'] || '', spec.key, spec.value);
448
+ if (body !== null) {
449
+ sent = true;
450
+ add({ kind: 'sent-straight', method: spec.method, host: at.host, path: at.path, key: spec.key });
451
+ return route.continue({ postData: body });
452
+ }
453
+ }
454
+ return route.continue();
455
+ });
456
+ };
457
+
458
+ // Several values the form's own checks would stop, sent straight to the
459
+ // server in one run: the form's request goes out as recorded, and once
460
+ // its answer is back, one copy of it per value - same address, headers
461
+ // and sign-in, only that one value changed - goes out one after another,
462
+ // never at the same moment, so an answer is about its value and not about
463
+ // two requests racing. Each copy's answer is kept as a write of its own,
464
+ // marked with the value it carried. Found on blind round 17: one run per
465
+ // value left no room in the cap, and not one was ever sent.
466
+ page.__kxSendStraightMany = async (spec) => {
467
+ let sent = false;
468
+ await page.route(() => true, async (route) => {
469
+ const req = route.request();
470
+ const at = where(req.url());
471
+ if (sent || req.method() !== spec.method || at.host !== spec.host || at.path !== spec.path) return route.continue();
472
+ sent = true;
473
+ let raw = null;
474
+ try {
475
+ raw = req.postData();
476
+ } catch (err) {
477
+ raw = null;
478
+ }
479
+ const headers = {};
480
+ for (const [k, v] of Object.entries(req.headers() || {})) if (!/^(content-length|host|connection|cookie)$/i.test(k) && k[0] !== ':') headers[k] = v;
481
+ const type = headers['content-type'] || '';
482
+ const original = page.waitForResponse((res) => res.request() === req, { timeout: 30000 }).catch(() => null);
483
+ const copies = original.then(async () => {
484
+ for (const variant of spec.variants) {
485
+ const body = freshKeys(sentStraight(raw, type, variant.key, variant.value), variant.key);
486
+ if (body === null) {
487
+ add({ kind: 'straight-skipped', straightId: variant.id, key: variant.key });
488
+ continue;
489
+ }
490
+ add({ kind: 'sent-straight', straightId: variant.id, method: spec.method, host: at.host, path: at.path, key: variant.key });
491
+ try {
492
+ const res = await page.request.fetch(req.url(), { method: req.method(), headers: headers, data: body, failOnStatusCode: false });
493
+ const status = res.status();
494
+ if (status === 429) add({ kind: 'limited', straightId: variant.id, method: spec.method, host: at.host, path: at.path, status: status });
495
+ if (status >= 500) add({ kind: 'server', straightId: variant.id, method: spec.method, host: at.host, path: at.path, status: status });
496
+ 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)));
498
+ if (/json/i.test((res.headers() || {})['content-type'] || '')) {
499
+ try {
500
+ const text = await res.text();
501
+ if (text && text.length <= MAX_BODY * 5) placed.answer = JSON.parse(hideValues(JSON.stringify(hideKeys(JSON.parse(text))), secrets));
502
+ } catch (err) {
503
+ /* no answer kept */
504
+ }
505
+ }
506
+ } catch (err) {
507
+ add({ kind: 'copy-failed', straightId: variant.id, method: spec.method, host: at.host, path: at.path, why: hideValues(firstLine(err && err.message), secrets).slice(0, 200) });
508
+ }
509
+ }
510
+ }).then(() => answers.delete(copies));
511
+ answers.add(copies);
512
+ return route.continue();
513
+ });
514
+ };
515
+
516
+ // Two of the same save at the same moment: when the page sends the
517
+ // request a step saves with, a copy of it - same address, same body,
518
+ // same headers, so the same sign-in - goes out alongside it. The copy's
519
+ // answer is kept as a write of its own, marked as the copy. Once, on the
520
+ // first request that matches.
521
+ page.__kxTwice = async (spec) => {
522
+ let sent = false;
523
+ await page.route(() => true, async (route) => {
524
+ const req = route.request();
525
+ const at = where(req.url());
526
+ if (sent || req.method() !== spec.method || at.host !== spec.host || at.path !== spec.path) return route.continue();
527
+ sent = true;
528
+ let raw = null;
529
+ try {
530
+ raw = req.postData();
531
+ } catch (err) {
532
+ raw = null;
533
+ }
534
+ const headers = {};
535
+ for (const [k, v] of Object.entries(req.headers() || {})) if (!/^(content-length|host|connection|cookie)$/i.test(k) && k[0] !== ':') headers[k] = v;
536
+ const type = headers['content-type'] || '';
537
+ 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 })
539
+ .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));
541
+ if (/json/i.test((res.headers() || {})['content-type'] || '')) {
542
+ try {
543
+ const text = await res.text();
544
+ if (text && text.length <= MAX_BODY * 5) placed.answer = JSON.parse(hideValues(JSON.stringify(hideKeys(JSON.parse(text))), secrets));
545
+ } catch (err) {
546
+ /* no answer kept */
547
+ }
548
+ }
549
+ }, (err) => {
550
+ add({ kind: 'copy-failed', method: req.method(), host: at.host, path: at.path, why: hideValues(firstLine(err && err.message), secrets).slice(0, 200) });
551
+ })
552
+ .then(() => answers.delete(copy));
553
+ answers.add(copy);
554
+ return route.continue();
555
+ });
556
+ };
557
+
558
+ // How many times each of these texts is on the page now - to tell
559
+ // whether what a step saved shows up. `fresh` reads the same address
560
+ // opened in a new tab instead: a GET, never a reload, so nothing is sent
561
+ // again.
562
+ const countIn = (p, texts) => p.evaluate((list) => {
563
+ const body = document.body ? document.body.innerText : '';
564
+ return list.map((t) => body.split(t).length - 1);
565
+ }, texts);
566
+ page.__kxSeen = async (name, texts, fresh) => {
567
+ let other = null;
568
+ try {
569
+ if (!fresh) {
570
+ add({ kind: 'seen', name: String(name), counts: await countIn(page, texts) });
571
+ return;
572
+ }
573
+ const url = page.url();
574
+ if (!/^https?:/.test(url)) return;
575
+ other = await page.context().newPage();
576
+ await other.goto(url, { waitUntil: 'load', timeout: 15000 });
577
+ try {
578
+ await other.waitForLoadState('networkidle', { timeout: 5000 });
579
+ } catch (err) {
580
+ /* counted either way */
581
+ }
582
+ add({ kind: 'seen', name: String(name), counts: await countIn(other, texts) });
583
+ } catch (err) {
584
+ /* not counted: said as not tested by whoever reads it */
585
+ } finally {
586
+ if (other) await other.close().catch(() => {});
587
+ }
588
+ };
589
+
590
+ // Called from inside a probe copy, around the steps that matter.
591
+ page.__kxMark = async (name) => {
592
+ // Let what the last step started arrive before it is counted: a moment
593
+ // for a second click's request to leave, then every write still on its
594
+ // way. A load state alone is not enough - once a page has gone quiet it
595
+ // says so at once, even while a new save is in flight.
596
+ await sleep(300);
597
+ for (let waited = 0; inFlight.size && waited < 10000; waited += 100) await sleep(100);
598
+ await answersRead();
599
+ try {
600
+ await page.waitForLoadState('networkidle', { timeout: 3000 });
601
+ } catch (err) {
602
+ /* a page that never goes quiet is still marked */
603
+ }
604
+ add({ kind: 'mark', name: String(name) });
605
+ };
606
+ // What kind of field a recorded fill typed into, read from the page
607
+ // itself: whether it is required, a number, a date, how long it may be.
608
+ page.__kxField = async (line, locator) => {
609
+ try {
610
+ fields[line] = await locator.first().evaluate((el) => {
611
+ const label = (el.labels && el.labels[0] && el.labels[0].textContent) || '';
612
+ return {
613
+ tag: el.tagName.toLowerCase(),
614
+ type: (el.getAttribute('type') || '').toLowerCase(),
615
+ required: el.required === true || el.getAttribute('aria-required') === 'true' || /\*\s*$/.test(label.trim()),
616
+ min: el.getAttribute('min'),
617
+ max: el.getAttribute('max'),
618
+ maxLength: el.maxLength > 0 ? el.maxLength : null,
619
+ pattern: el.getAttribute('pattern'),
620
+ label: label.replace(/\s+/g, ' ').trim().slice(0, 80),
621
+ placeholder: (el.getAttribute('placeholder') || '').slice(0, 80),
622
+ name: (el.getAttribute('name') || el.id || '').slice(0, 80),
623
+ };
624
+ }, null, { timeout: 5000 });
625
+ } catch (err) {
626
+ fields[line] = null; // not knowing is never read as "not required"
627
+ }
628
+ };
629
+
630
+ // Writes still on their way. A recording often ends right after the step
631
+ // that saves, and the answer to that save is the proof - so the run waits
632
+ // for it before it looks at anything or opens the page again, which would
633
+ // cut the answer off.
634
+ const inFlight = new Set();
635
+ page.on('request', (req) => {
636
+ if (WRITE.test(req.method())) inFlight.add(req);
637
+ });
638
+ const settled = (req) => inFlight.delete(req);
639
+ page.on('requestfinished', settled);
640
+ page.on('requestfailed', settled);
641
+
642
+ const started = Date.now();
643
+ await use(page);
644
+
645
+ for (let waited = 0; inFlight.size && waited < 10000; waited += 100) await sleep(100);
646
+ await answersRead();
647
+ try {
648
+ await page.waitForLoadState('networkidle', { timeout: 3000 });
649
+ } catch (err) {
650
+ /* a page that never goes quiet is still read */
651
+ }
652
+
653
+ // Markup made from a probe tag: on the page the flow ended on, and on the
654
+ // same address opened again - which is what proves it was saved and is
655
+ // shown to whoever opens it next. A GET, never a reload: reloading a page
656
+ // that came from a form post would send the form again.
657
+ const markers = { live: await findMarkers(page), reopened: [], detail: [], detailPath: null };
658
+ // A record saved with a tag in it: its own page is opened too, not only
659
+ // the page the flow ended on. A list often escapes what its record's own
660
+ // page does not (blind round 17). Found by a link on the page to an
661
+ // address that holds the id the server's answer named.
662
+ const ids = taggedRecordIds(events);
663
+ let links = await recordLinks(page, ids);
664
+ try {
665
+ const url = page.url();
666
+ if (/^https?:/.test(url)) {
667
+ await page.goto(url, { waitUntil: 'load', timeout: 15000 });
668
+ try {
669
+ await page.waitForLoadState('networkidle', { timeout: 5000 });
670
+ } catch (err) {
671
+ /* the markers are read either way */
672
+ }
673
+ markers.reopened = await findMarkers(page);
674
+ if (!links.length) links = await recordLinks(page, ids);
675
+ }
676
+ } catch (err) {
677
+ /* the page could not be opened again; what was live still counts */
678
+ }
679
+ if (links.length) {
680
+ try {
681
+ await page.goto(links[0], { waitUntil: 'load', timeout: 15000 });
682
+ try {
683
+ await page.waitForLoadState('networkidle', { timeout: 5000 });
684
+ } catch (err) {
685
+ /* the markers are read either way */
686
+ }
687
+ markers.detailPath = new URL(links[0]).pathname;
688
+ markers.detail = await findMarkers(page);
689
+ } catch (err) {
690
+ /* the record's page would not open; the other pages still count */
691
+ }
692
+ }
693
+
694
+ const error = testInfo.error || (testInfo.errors && testInfo.errors[0]) || null;
695
+ const file = path.basename(testInfo.file);
696
+ let failedLine = null;
697
+ if (error && error.stack) {
698
+ const m = new RegExp(file.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + ':(\\d+)').exec(error.stack);
699
+ if (m) failedLine = Number(m[1]);
700
+ }
701
+ const result = {
702
+ id: file.replace(/\.kxprobe\.js$/, ''),
703
+ status: testInfo.status,
704
+ error: error ? hideValues(firstLine(error.message), secrets).slice(0, 300) : null,
705
+ failedLine: failedLine,
706
+ events: events,
707
+ fields: fields,
708
+ markers: markers,
709
+ ms: Date.now() - started,
710
+ };
711
+ if (OUT) {
712
+ try {
713
+ fs.writeFileSync(path.join(OUT, result.id + '.json'), JSON.stringify(result), 'utf8');
714
+ } catch (err) {
715
+ /* the run is then reported as not finished, never as clean */
716
+ }
717
+ }
718
+ },
719
+ });
720
+
721
+ module.exports = { test: test, expect: base.expect, keptBody: keptBody, hideValues: hideValues, taggedRecordIds: taggedRecordIds, sentStraight: sentStraight, MARKER: MARKER };