kryptheon 0.1.46 → 0.1.48
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.
- package/README.md +32 -0
- package/bin/kryptheon.js +3525 -3436
- package/kryptheon-code.js +50 -0
- package/kryptheon-dashboard-page.js +23 -6
- package/kryptheon-dashboard.html +1 -1
- package/kryptheon-dashboard.js +1166 -1121
- package/kryptheon-edge.js +1112 -777
- package/kryptheon-fixture.js +117 -0
- package/kryptheon-heal.js +254 -215
- package/kryptheon-look.js +306 -259
- package/kryptheon-probe-fixture.js +721 -267
- package/kryptheon-probe.js +45 -2
- package/kryptheon-reliability.js +984 -0
- package/kryptheon-replay.js +0 -0
- package/kryptheon-reporter.js +1108 -1080
- package/kryptheon-signature.js +69 -8
- package/package.json +104 -102
package/kryptheon-dashboard.js
CHANGED
|
@@ -1,1121 +1,1166 @@
|
|
|
1
|
-
// The local dashboard: one page on this machine that shows everything
|
|
2
|
-
// Kryptheon knows about one project, and switches its checks on and off.
|
|
3
|
-
//
|
|
4
|
-
// npx kryptheon (in a terminal) or npx kryptheon dashboard
|
|
5
|
-
//
|
|
6
|
-
// Everything here reads the project's store (kryptheon-store.js); nothing is
|
|
7
|
-
// kept anywhere else, and nothing leaves the machine. The server listens on
|
|
8
|
-
// 127.0.0.1 only.
|
|
9
|
-
//
|
|
10
|
-
// Two rules the page is built around:
|
|
11
|
-
//
|
|
12
|
-
// - A check that does not exist yet is shown as "coming soon". Never as
|
|
13
|
-
// passed, never as green, never counted. An empty row that looks like a
|
|
14
|
-
// clean one is the most expensive lie this product could tell.
|
|
15
|
-
// - Every value shown comes from a file that may hold text from the app being
|
|
16
|
-
// checked - a failing page's heading, a line of the app's own code. The page
|
|
17
|
-
// writes all of it with textContent, so the dashboard cannot become the XSS
|
|
18
|
-
// it is there to find. Its own code is read by kryptheon-code.js in its check.
|
|
19
|
-
|
|
20
|
-
const fs = require('fs');
|
|
21
|
-
const path = require('path');
|
|
22
|
-
const http = require('http');
|
|
23
|
-
const crypto = require('crypto');
|
|
24
|
-
const { spawnSync } = require('child_process');
|
|
25
|
-
const store = require('./kryptheon-store.js');
|
|
26
|
-
const changes = require('./kryptheon-changes.js');
|
|
27
|
-
const autostart = require('./kryptheon-autostart.js');
|
|
28
|
-
const VERSION = require('./package.json').version;
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Every check the dashboard knows the name of, in the order the person asked
|
|
32
|
-
* for them. `available` is true only for what is actually built. What runs it
|
|
33
|
-
* and where its results come from is said here, once.
|
|
34
|
-
*/
|
|
35
|
-
const CHECKS = [
|
|
36
|
-
{ id: 'regression', label: 'Regression (recorded flows)', available: true,
|
|
37
|
-
what: 'Replays every recording in tests/ and compares each page with the last time it worked.' },
|
|
38
|
-
{ id: 'frontend', label: 'Frontend: text inserted as HTML', available: true,
|
|
39
|
-
what: 'Reads your frontend code for values put into the page with innerHTML and similar (npx kryptheon code).' },
|
|
40
|
-
{ id: 'data', label: 'Database: access rules, duplicates, orphans', available: true,
|
|
41
|
-
what: 'Attacks a copy of your Postgres/Supabase database (kryptheon-night). Paste the connection string in Start here; it is used for one run and never saved.' },
|
|
42
|
-
// Coming soon, in the order they are being built (2026-10-13). Integration
|
|
43
|
-
// and age/eligibility rules were taken off: neither can be checked without
|
|
44
|
-
// the app's own rules, so listing them promised what is not planned.
|
|
45
|
-
{ id: 'build', label: 'Build', available: false },
|
|
46
|
-
{ id: 'dependency', label: 'Dependencies', available: false },
|
|
47
|
-
{ id: 'runtime', label: 'Runtime', available: false },
|
|
48
|
-
{ id: 'performance', label: 'Performance', available: false },
|
|
49
|
-
{ id: 'edge', label: 'Input and edge cases', available: true, asks: true,
|
|
50
|
-
what: 'Replays your recorded forms with one field at a time given an unusual value - empty, 5000 characters, an HTML tag, emoji, a negative number, a date that does not exist - and reports only what it can prove: a page error, a 5xx, a bad value saved, a tag shown as HTML. It adds test data to your app, so it runs only when you press its button.' },
|
|
51
|
-
{ id: 'reliability', label: 'Reliability', available:
|
|
52
|
-
|
|
53
|
-
{ id: '
|
|
54
|
-
{ id: '
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
const
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
function
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
return
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
*
|
|
362
|
-
*
|
|
363
|
-
*
|
|
364
|
-
*
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
const
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
* the
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
//
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
const
|
|
407
|
-
|
|
408
|
-
const
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
'
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
};
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
const
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
recordings:
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
}
|
|
702
|
-
}
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
*
|
|
707
|
-
*
|
|
708
|
-
*
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
const
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
}
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
*
|
|
750
|
-
*
|
|
751
|
-
*/
|
|
752
|
-
function
|
|
753
|
-
const
|
|
754
|
-
const
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
}
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
if (
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
*
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
}
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
}
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
return
|
|
988
|
-
}
|
|
989
|
-
if (url.pathname === '/api/
|
|
990
|
-
const
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
}
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
};
|
|
1
|
+
// The local dashboard: one page on this machine that shows everything
|
|
2
|
+
// Kryptheon knows about one project, and switches its checks on and off.
|
|
3
|
+
//
|
|
4
|
+
// npx kryptheon (in a terminal) or npx kryptheon dashboard
|
|
5
|
+
//
|
|
6
|
+
// Everything here reads the project's store (kryptheon-store.js); nothing is
|
|
7
|
+
// kept anywhere else, and nothing leaves the machine. The server listens on
|
|
8
|
+
// 127.0.0.1 only.
|
|
9
|
+
//
|
|
10
|
+
// Two rules the page is built around:
|
|
11
|
+
//
|
|
12
|
+
// - A check that does not exist yet is shown as "coming soon". Never as
|
|
13
|
+
// passed, never as green, never counted. An empty row that looks like a
|
|
14
|
+
// clean one is the most expensive lie this product could tell.
|
|
15
|
+
// - Every value shown comes from a file that may hold text from the app being
|
|
16
|
+
// checked - a failing page's heading, a line of the app's own code. The page
|
|
17
|
+
// writes all of it with textContent, so the dashboard cannot become the XSS
|
|
18
|
+
// it is there to find. Its own code is read by kryptheon-code.js in its check.
|
|
19
|
+
|
|
20
|
+
const fs = require('fs');
|
|
21
|
+
const path = require('path');
|
|
22
|
+
const http = require('http');
|
|
23
|
+
const crypto = require('crypto');
|
|
24
|
+
const { spawnSync } = require('child_process');
|
|
25
|
+
const store = require('./kryptheon-store.js');
|
|
26
|
+
const changes = require('./kryptheon-changes.js');
|
|
27
|
+
const autostart = require('./kryptheon-autostart.js');
|
|
28
|
+
const VERSION = require('./package.json').version;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Every check the dashboard knows the name of, in the order the person asked
|
|
32
|
+
* for them. `available` is true only for what is actually built. What runs it
|
|
33
|
+
* and where its results come from is said here, once.
|
|
34
|
+
*/
|
|
35
|
+
const CHECKS = [
|
|
36
|
+
{ id: 'regression', label: 'Regression (recorded flows)', available: true,
|
|
37
|
+
what: 'Replays every recording in tests/ and compares each page with the last time it worked.' },
|
|
38
|
+
{ id: 'frontend', label: 'Frontend: text inserted as HTML', available: true,
|
|
39
|
+
what: 'Reads your frontend code for values put into the page with innerHTML and similar (npx kryptheon code).' },
|
|
40
|
+
{ id: 'data', label: 'Database: access rules, duplicates, orphans', available: true,
|
|
41
|
+
what: 'Attacks a copy of your Postgres/Supabase database (kryptheon-night). Paste the connection string in Start here; it is used for one run and never saved.' },
|
|
42
|
+
// Coming soon, in the order they are being built (2026-10-13). Integration
|
|
43
|
+
// and age/eligibility rules were taken off: neither can be checked without
|
|
44
|
+
// the app's own rules, so listing them promised what is not planned.
|
|
45
|
+
{ id: 'build', label: 'Build', available: false },
|
|
46
|
+
{ id: 'dependency', label: 'Dependencies', available: false },
|
|
47
|
+
{ id: 'runtime', label: 'Runtime', available: false },
|
|
48
|
+
{ id: 'performance', label: 'Performance', available: false },
|
|
49
|
+
{ id: 'edge', label: 'Input and edge cases', available: true, asks: true,
|
|
50
|
+
what: 'Replays your recorded forms with one field at a time given an unusual value - empty, 5000 characters, an HTML tag, emoji, a negative number, a number just past the lowest or highest the page allows, a date that does not exist - and reports only what it can prove: a page error, a 5xx, a bad value saved, a tag shown as HTML. It adds test data to your app, so it runs only when you press its button.' },
|
|
51
|
+
{ id: 'reliability', label: 'Reliability', available: true, asks: true,
|
|
52
|
+
what: 'Replays each recorded flow three times and reports one that sometimes works and sometimes fails, with the failed run as proof; and clicks each step that saves twice, quickly, reporting when that saves twice, with both answers as proof; on an app on this computer, sends each save twice at the same moment, reporting a server that makes two records; and looks for what each save added on the page right after it, reporting a page that shows it only when opened again; and at phone size clicks every button with saving blocked, reporting a click that makes the page throw an error. It adds test data to your app, so it runs only when you press its button.' },
|
|
53
|
+
{ id: 'api', label: 'API', available: false },
|
|
54
|
+
{ id: 'backend', label: 'Backend', available: false },
|
|
55
|
+
{ id: 'synthetic', label: 'Synthetic test users', available: false },
|
|
56
|
+
];
|
|
57
|
+
|
|
58
|
+
const CHECK_IDS = new Set(CHECKS.map((c) => c.id));
|
|
59
|
+
|
|
60
|
+
function readJson(file) {
|
|
61
|
+
try {
|
|
62
|
+
let raw = fs.readFileSync(file, 'utf8');
|
|
63
|
+
if (raw.charCodeAt(0) === 0xfeff) raw = raw.slice(1);
|
|
64
|
+
return JSON.parse(raw);
|
|
65
|
+
} catch (err) {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function readHistory(file, keep) {
|
|
71
|
+
let raw;
|
|
72
|
+
try {
|
|
73
|
+
raw = fs.readFileSync(file, 'utf8');
|
|
74
|
+
} catch (err) {
|
|
75
|
+
return [];
|
|
76
|
+
}
|
|
77
|
+
const runs = [];
|
|
78
|
+
for (const line of raw.split('\n')) {
|
|
79
|
+
if (!line.trim()) continue;
|
|
80
|
+
try {
|
|
81
|
+
runs.push(JSON.parse(line));
|
|
82
|
+
} catch (err) {
|
|
83
|
+
/* a damaged line is skipped, not fatal */
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return runs.slice(-keep);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* The re-check that already proved a finding fixed, when it is no longer
|
|
91
|
+
* open: the latest attempt for that id, if it said fixed. Null otherwise.
|
|
92
|
+
* So a second `recheck <id>` says "already fixed by the re-check at ..."
|
|
93
|
+
* instead of "no open finding" (blind round 17).
|
|
94
|
+
*/
|
|
95
|
+
function clearedBy(root, id, env) {
|
|
96
|
+
const key = String(id || '').trim();
|
|
97
|
+
if (!key || buildState(root, env).findings.some((f) => f.id === key)) return null;
|
|
98
|
+
const history = readHistory(store.pathsFor(root, env).fixes, 500).filter(Boolean);
|
|
99
|
+
// The latest attempt that says anything about this id: its own re-check,
|
|
100
|
+
// or another re-check whose replay cleared it on the way.
|
|
101
|
+
for (let i = history.length - 1; i >= 0; i--) {
|
|
102
|
+
const a = history[i];
|
|
103
|
+
if (a.id === key) return a.verdict === 'fixed' ? a : null;
|
|
104
|
+
const with_ = (a.alsoFixed || []).find((x) => x && x.id === key);
|
|
105
|
+
if (with_) return { id: key, where: with_.where, headline: with_.headline, verdict: 'fixed', at: a.at, by: a.id };
|
|
106
|
+
}
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** A time as the dashboard shows it: 13 Oct, 17:20. */
|
|
111
|
+
function localTime(iso) {
|
|
112
|
+
const d = new Date(iso);
|
|
113
|
+
return isNaN(d.getTime()) ? String(iso || '') : d.toLocaleString('en-GB', { day: 'numeric', month: 'short', hour: '2-digit', minute: '2-digit' });
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Which checks are switched on. Available checks default to on; others can never be. */
|
|
117
|
+
function readConfig(paths) {
|
|
118
|
+
const saved = readJson(paths.config) || {};
|
|
119
|
+
const enabled = {};
|
|
120
|
+
for (const c of CHECKS) {
|
|
121
|
+
enabled[c.id] = c.available ? (saved.enabled && typeof saved.enabled[c.id] === 'boolean' ? saved.enabled[c.id] : true) : false;
|
|
122
|
+
}
|
|
123
|
+
return { enabled: enabled };
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Switches one check on or off, and keeps it. Refuses what does not exist and
|
|
128
|
+
* what is not built: a "coming soon" check switched on would read as a
|
|
129
|
+
* promise that something is being checked.
|
|
130
|
+
*/
|
|
131
|
+
function setCheck(paths, id, on) {
|
|
132
|
+
if (!CHECK_IDS.has(id)) return { ok: false, why: 'there is no check called "' + id + '"' };
|
|
133
|
+
const check = CHECKS.find((c) => c.id === id);
|
|
134
|
+
if (!check.available) return { ok: false, why: check.label + ' is coming soon - not built yet, so it cannot be switched on' };
|
|
135
|
+
if (typeof on !== 'boolean') return { ok: false, why: 'on must be true or false' };
|
|
136
|
+
const config = readConfig(paths);
|
|
137
|
+
config.enabled[id] = on;
|
|
138
|
+
fs.mkdirSync(path.dirname(paths.config), { recursive: true });
|
|
139
|
+
fs.writeFileSync(paths.config, JSON.stringify(config, null, 2) + '\n', 'utf8');
|
|
140
|
+
return { ok: true, config: config };
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** What git says changed, read only. Null when this is not a git repository. */
|
|
144
|
+
function recentChanges(root) {
|
|
145
|
+
const git = (args) => {
|
|
146
|
+
const r = spawnSync('git', args, { cwd: root, encoding: 'utf8', timeout: 5000 });
|
|
147
|
+
return r.status === 0 ? String(r.stdout || '') : null;
|
|
148
|
+
};
|
|
149
|
+
const inside = git(['rev-parse', '--is-inside-work-tree']);
|
|
150
|
+
if (!inside || inside.trim() !== 'true') return null;
|
|
151
|
+
const log = git(['log', '-5', '--pretty=format:%h%x09%cI%x09%s']) || '';
|
|
152
|
+
const status = git(['status', '--porcelain']) || '';
|
|
153
|
+
return {
|
|
154
|
+
commits: log.split('\n').filter(Boolean).map((l) => {
|
|
155
|
+
const [hash, when, subject] = l.split('\t');
|
|
156
|
+
return { hash: hash, when: when, subject: subject };
|
|
157
|
+
}),
|
|
158
|
+
uncommitted: status.split('\n').filter(Boolean).map((l) => ({ state: l.slice(0, 2).trim(), file: l.slice(3) })),
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** " - the risky part: x" for a frontend finding that names one, else "". */
|
|
163
|
+
function riskyPartsLine(f) {
|
|
164
|
+
const parts = (f.parts || []).filter((p) => p && p.text && p.text !== f.expression);
|
|
165
|
+
if (!parts.length) return '';
|
|
166
|
+
return ' - the risky part' + (parts.length === 1 ? ': ' : 's: ') + parts.map((p) => p.text).join(', ');
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** When this flow last passed, from the run history, or null. */
|
|
170
|
+
function lastPassOf(history, title) {
|
|
171
|
+
for (let i = history.length - 1; i >= 0; i--) {
|
|
172
|
+
const t = (history[i].tests || []).find((x) => x.title === title);
|
|
173
|
+
if (t && t.status === 'passed') return history[i].runAt;
|
|
174
|
+
}
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* The fix prompt for a broken recorded flow, from what the run kept: what was
|
|
180
|
+
* seen, what was expected, where, the evidence, and how to see it again. Only
|
|
181
|
+
* what the run recorded is said; nothing is guessed.
|
|
182
|
+
*/
|
|
183
|
+
function flowPrompt(t, lastPass) {
|
|
184
|
+
const f = t.failure || {};
|
|
185
|
+
const o = f.observations || {};
|
|
186
|
+
const file = String(f.file || t.specFile || 'tests').split('\\').join('/');
|
|
187
|
+
const extra = String(f.rawMessage || '').split(/\r?\n/).slice(1)
|
|
188
|
+
.map((l) => l.trim()).filter((l) => l && !/^\s*at\s/.test(l) && l.length < 200).slice(0, 6);
|
|
189
|
+
const lines = [
|
|
190
|
+
'My app has a broken flow. Please fix the app - and nothing else.',
|
|
191
|
+
'',
|
|
192
|
+
'Kryptheon replayed the recorded flow "' + t.title + '" (' + file + (f.line ? ', line ' + f.line : '') + ') and it failed.',
|
|
193
|
+
'',
|
|
194
|
+
'Observed: ' + (f.plainLanguage || 'the replay failed'),
|
|
195
|
+
].concat(extra.map((l) => ' ' + l), [
|
|
196
|
+
'Expected: the same result as ' + (lastPass ? 'the last time it passed (' + lastPass + ')' : 'when it was recorded') + '.',
|
|
197
|
+
]);
|
|
198
|
+
if (o.url) lines.push('Where: the browser was on ' + o.url + (o.pageShowed ? ', and the page showed ' + o.pageShowed : '') + '.');
|
|
199
|
+
if (f.locator) lines.push('Step that failed: ' + f.locator);
|
|
200
|
+
if (o.failedRequests && o.failedRequests.length) lines.push('Failed requests: ' + o.failedRequests.slice(0, 5).map((r) => (r.method || 'GET') + ' ' + (r.path || r.url) + ' -> ' + r.status).join('; '));
|
|
201
|
+
if (o.consoleErrors && o.consoleErrors.length) lines.push('Console errors: ' + o.consoleErrors.slice(0, 3).join(' | '));
|
|
202
|
+
if (f.screenshot) lines.push('Evidence: a screenshot of the failure at ' + f.screenshot);
|
|
203
|
+
lines.push('');
|
|
204
|
+
lines.push('To see it yourself: open ' + (o.url || 'the app') + ' and repeat the steps in ' + file + ', or run npx kryptheon check');
|
|
205
|
+
lines.push('');
|
|
206
|
+
lines.push('Fix the app so this flow works again. Do not edit the recording to make it pass. If the new behaviour ' +
|
|
207
|
+
'is intended, say so instead of changing code - it can be accepted with npx kryptheon accept "' + t.title + '"');
|
|
208
|
+
return lines.join('\n');
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function countRecordings(root) {
|
|
212
|
+
try {
|
|
213
|
+
// Recordings only - not the project's own unit tests in tests/.
|
|
214
|
+
return require('./kryptheon-testfiles.js').recordingNames(path.join(root, 'tests')).length;
|
|
215
|
+
} catch (err) {
|
|
216
|
+
return 0;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* What a probe check asks before it runs: the addresses the recordings open,
|
|
222
|
+
* and the words said about them (kryptheon-probe.js). Read from the
|
|
223
|
+
* recordings every time, so what the page shows is what would really run.
|
|
224
|
+
*/
|
|
225
|
+
function probeAsk(root, what, env, options) {
|
|
226
|
+
const probe = require('./kryptheon-probe.js');
|
|
227
|
+
const sources = [];
|
|
228
|
+
try {
|
|
229
|
+
const dir = path.join(root, 'tests');
|
|
230
|
+
for (const name of require('./kryptheon-testfiles.js').recordingNames(dir)) sources.push(fs.readFileSync(path.join(dir, name), 'utf8'));
|
|
231
|
+
} catch (err) {
|
|
232
|
+
/* no recordings: nothing to ask about */
|
|
233
|
+
}
|
|
234
|
+
// The pages, and where the app was last seen saving (kryptheon-probe.js).
|
|
235
|
+
const addresses = probe.knownAddresses(root, sources, env);
|
|
236
|
+
return { addresses: addresses, live: probe.liveLooking(addresses), lines: probe.consentLines(what, addresses, options) };
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
const PROBE_WHAT = {
|
|
240
|
+
edge: 'Input and edge cases: replays your recorded forms with one field at a time given an unusual value (empty, 5000 characters, an HTML tag, emoji, a negative number, a number just past the lowest or highest the page allows, a date that does not exist). At most 40 runs.',
|
|
241
|
+
reliability: 'Reliability: replays each recorded flow three times, clicks each step that saves (Save, Add, Create...) twice, quickly, sends each save twice at the same moment (only to an app on this computer), looks for what each save added on the page right after it, and at phone size clicks every button with saving blocked. At most 40 runs.',
|
|
242
|
+
};
|
|
243
|
+
// The reliability check types nothing of its own, so nothing it saves can
|
|
244
|
+
// carry the kryptheon-test marker - and its words do not promise one.
|
|
245
|
+
const PROBE_WORDS = { reliability: { marker: false } };
|
|
246
|
+
const PROBE_CHECKS = ['edge', 'reliability'];
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* The order someone new should go in, one step at a time. A step is "done"
|
|
250
|
+
* only when it has actually run and left a record - never because nothing was
|
|
251
|
+
* found - and a switched-off check says "off", so an empty list is never read
|
|
252
|
+
* as a clean one.
|
|
253
|
+
*/
|
|
254
|
+
// How often a step is done. Said on every step and button, because the two
|
|
255
|
+
// kinds are done at different moments and mixing them up is how a check stops
|
|
256
|
+
// being run: "one time" is setting up, "after every change" is the habit.
|
|
257
|
+
const ONE_TIME = 'one time';
|
|
258
|
+
const EVERY_CHANGE = 'after every change';
|
|
259
|
+
|
|
260
|
+
function startSteps(checks, seen) {
|
|
261
|
+
const on = (id) => (checks.find((c) => c.id === id) || {}).enabled;
|
|
262
|
+
const nightly = seen.nightly || null;
|
|
263
|
+
// With no code on this computer - the home folder's dashboard - there is
|
|
264
|
+
// no code to read, and "Read your frontend code" would only say it read
|
|
265
|
+
// nothing. The app is checked by its address.
|
|
266
|
+
return startStepList(on, seen, nightly).filter((s) => !(seen.webOnly && s.id === 'frontend'));
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function startStepList(on, seen, nightly) {
|
|
270
|
+
return [
|
|
271
|
+
{
|
|
272
|
+
id: 'frontend',
|
|
273
|
+
title: 'Read your frontend code',
|
|
274
|
+
when: EVERY_CHANGE,
|
|
275
|
+
why: 'Finds places where text could be put into your page as HTML. Needs nothing from you.',
|
|
276
|
+
state: !on('frontend') ? 'off' : seen.codeAt ? 'done' : 'todo',
|
|
277
|
+
at: seen.codeAt,
|
|
278
|
+
action: on('frontend') ? { kind: 'run', check: 'frontend', label: seen.codeAt ? 'Read it again' : 'Read my code', command: RUN_COMMAND.frontend } : null,
|
|
279
|
+
},
|
|
280
|
+
{
|
|
281
|
+
id: 'record',
|
|
282
|
+
title: 'Record your main flows',
|
|
283
|
+
when: ONE_TIME,
|
|
284
|
+
whenMore: 'again only when that flow changes',
|
|
285
|
+
// The lesson of a test where 26 bugs were planted before recording and
|
|
286
|
+
// none was caught: what is recorded is taken as right.
|
|
287
|
+
why: 'Sign up, log in, pay - do each once while Kryptheon watches. Record while your app works: what it sees is taken as right, so a bug already there when you record becomes part of "working". Start your app first, then put its address here.',
|
|
288
|
+
state: seen.recordings ? 'done' : 'todo',
|
|
289
|
+
at: null,
|
|
290
|
+
count: seen.recordings,
|
|
291
|
+
action: { kind: 'record', label: seen.recordings ? 'Record another' : 'Record', command: 'npx kryptheon record <your app address>' },
|
|
292
|
+
},
|
|
293
|
+
{
|
|
294
|
+
id: 'regression',
|
|
295
|
+
title: 'Replay them',
|
|
296
|
+
when: EVERY_CHANGE,
|
|
297
|
+
why: 'Runs every recording against your app and compares each page with the last time it worked. Start your app first.',
|
|
298
|
+
state: !on('regression') ? 'off' : !seen.recordings ? 'waiting' : seen.lastRunAt ? 'done' : 'todo',
|
|
299
|
+
at: seen.lastRunAt,
|
|
300
|
+
action: on('regression') && seen.recordings ? { kind: 'run', check: 'regression', label: seen.lastRunAt ? 'Replay again' : 'Replay now', command: RUN_COMMAND.regression } : null,
|
|
301
|
+
},
|
|
302
|
+
{
|
|
303
|
+
id: 'connect',
|
|
304
|
+
title: 'Connect your database',
|
|
305
|
+
when: ONE_TIME,
|
|
306
|
+
why: 'Paste your connection string - the steps are next to the box. It goes only to this dashboard on your machine and to kryptheon-night for the length of one run. It is never saved and never shown again; reload the page and it is gone. Try it on a test project first.',
|
|
307
|
+
state: !on('data') ? 'off' : seen.nightAt ? 'done' : 'todo',
|
|
308
|
+
at: null,
|
|
309
|
+
action: on('data') ? { kind: 'connect' } : null,
|
|
310
|
+
},
|
|
311
|
+
{
|
|
312
|
+
id: 'data',
|
|
313
|
+
title: 'Check your database',
|
|
314
|
+
when: EVERY_CHANGE,
|
|
315
|
+
why: 'Attacks a copy inside your Postgres or Supabase database and reports what got in; your own rows are not touched. You are shown exactly what it will do, and it waits for your yes.',
|
|
316
|
+
state: !on('data') ? 'off' : seen.nightAt ? 'done' : 'todo',
|
|
317
|
+
at: seen.nightAt,
|
|
318
|
+
action: on('data') ? { kind: 'db', action: 'scan', label: 'Check my database', command: DB_ACTIONS.scan.command } : null,
|
|
319
|
+
},
|
|
320
|
+
{
|
|
321
|
+
id: 'nightly',
|
|
322
|
+
title: 'Check it every night',
|
|
323
|
+
when: ONE_TIME,
|
|
324
|
+
// Offered, never pressed for: an app can be checked well without it,
|
|
325
|
+
// so it is not what "do this next" points at.
|
|
326
|
+
optional: true,
|
|
327
|
+
why: 'Sets the same check up inside your database, to run by itself every night, and keeps the answer there for this page. It stays until you remove it.',
|
|
328
|
+
state: !on('data') ? 'off' : nightly && nightly.installed && nightly.active ? 'done' : 'todo',
|
|
329
|
+
at: nightly && nightly.ranAt ? nightly.ranAt : null,
|
|
330
|
+
action: on('data') ? { kind: 'nightly', installed: Boolean(nightly && nightly.installed), at: nightly ? nightly.scheduled || null : null } : null,
|
|
331
|
+
},
|
|
332
|
+
];
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* The one thing to do now. Open confirmed problems come before everything; then
|
|
337
|
+
* the first step not taken; then what only needs a look; then nothing.
|
|
338
|
+
*/
|
|
339
|
+
function nextAction(findings, steps) {
|
|
340
|
+
const confirmed = findings.filter((f) => f.status === 'confirmed').length;
|
|
341
|
+
const toVerify = findings.length - confirmed;
|
|
342
|
+
if (confirmed) {
|
|
343
|
+
return { kind: 'fix', view: 'findings', count: confirmed,
|
|
344
|
+
title: confirmed === 1 ? 'Fix 1 confirmed problem' : 'Fix ' + confirmed + ' confirmed problems' };
|
|
345
|
+
}
|
|
346
|
+
const step = steps.find((s) => s.state === 'todo' && !s.optional);
|
|
347
|
+
if (step) return { kind: 'step', step: step.id, title: step.title };
|
|
348
|
+
if (toVerify) {
|
|
349
|
+
return { kind: 'verify', view: 'findings', count: toVerify,
|
|
350
|
+
title: toVerify === 1 ? 'Look at 1 thing to verify' : 'Look at ' + toVerify + ' things to verify' };
|
|
351
|
+
}
|
|
352
|
+
return { kind: 'quiet', title: 'Nothing open' };
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* One prompt that fixes every open finding at once, for pasting into an AI
|
|
357
|
+
* tool, or '' when fewer than two findings have a prompt (one is its own).
|
|
358
|
+
* Each finding's own prompt goes in as it is, in the order the page shows
|
|
359
|
+
* them, most severe first. Around them are the rules that keep a fix from
|
|
360
|
+
* breaking something else: only what is listed, the smallest change, one at
|
|
361
|
+
* a time with the app checked between, nothing weakened to make a finding go
|
|
362
|
+
* away, ask rather than guess. The re-checks come once, at the end.
|
|
363
|
+
*
|
|
364
|
+
* items: [{ id, severity, headline, where, prompt }] - prompt without its
|
|
365
|
+
* re-check line.
|
|
366
|
+
*/
|
|
367
|
+
function combinedPrompt(items) {
|
|
368
|
+
const withPrompt = items.filter((f) => f.prompt);
|
|
369
|
+
if (withPrompt.length < 2) return '';
|
|
370
|
+
const n = withPrompt.length;
|
|
371
|
+
const lines = [
|
|
372
|
+
'Kryptheon checked this project and found ' + n + ' problems. Fix all ' + n + ', carefully - not breaking anything matters more than speed.',
|
|
373
|
+
'',
|
|
374
|
+
'Rules for every fix:',
|
|
375
|
+
'1. Fix only the problems listed below. Do not refactor, rename, reformat or "improve" anything else, and do not touch a file a fix does not need.',
|
|
376
|
+
'2. Make the smallest change that fixes each one. Every feature must keep working exactly as it does now for the people meant to use it.',
|
|
377
|
+
'3. Never delete or weaken a check, test, validation or security rule to make a problem go away, and never delete or rewrite existing data.',
|
|
378
|
+
'4. Do them one at a time, in the order below. After each one, make sure the app still builds and its existing tests still pass before starting the next.',
|
|
379
|
+
'5. If a fix needs a decision only I can make, or you are not sure a change is safe, stop and ask me instead of guessing.',
|
|
380
|
+
'6. When you are done, list every file you changed and why, one line each.',
|
|
381
|
+
];
|
|
382
|
+
withPrompt.forEach((f, i) => {
|
|
383
|
+
lines.push('', '======== Problem ' + (i + 1) + ' of ' + n + ' - ' + f.severity + ': ' + f.headline + (f.where ? ' (' + f.where + ')' : ''), '', f.prompt.trim());
|
|
384
|
+
});
|
|
385
|
+
lines.push('', '======== When all ' + n + ' are fixed',
|
|
386
|
+
'',
|
|
387
|
+
'Prove each fix: run these one at a time (or press Re-check on each finding in the Kryptheon dashboard). A fix counts only when it says FIXED, and a re-check also says if anything else newly broke - if it does, fix that before anything else.',
|
|
388
|
+
'');
|
|
389
|
+
withPrompt.forEach((f) => lines.push(' npx kryptheon recheck ' + f.id));
|
|
390
|
+
return lines.join('\n');
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/**
|
|
394
|
+
* One sentence about the nightly run inside the database, or null when
|
|
395
|
+
* kryptheon-night has never been asked. Its four states are four different
|
|
396
|
+
* answers, and none of the other three may read as "nothing found": not set
|
|
397
|
+
* up, set up and never run, set up with nothing scheduled, and stopped.
|
|
398
|
+
*/
|
|
399
|
+
function nightlyLine(nightly) {
|
|
400
|
+
if (!nightly) return null;
|
|
401
|
+
// The database runs on UTC. Found on a real read-back: "03:00" was 08:30
|
|
402
|
+
// in India, and nothing said so - so both are said, and the moment it was
|
|
403
|
+
// read is said in this computer's own time.
|
|
404
|
+
const local = (iso) => new Date(iso).toLocaleString('en-GB', { day: 'numeric', month: 'short', hour: '2-digit', minute: '2-digit' });
|
|
405
|
+
const day = (iso) => String(iso || '').slice(0, 16).replace('T', ' ') + ' UTC (' + local(iso) + ' your time)';
|
|
406
|
+
const read = ' (read ' + local(nightly.readAt) + ')';
|
|
407
|
+
if (!nightly.installed) return 'Nightly run in your database: not set up. Press "Set up nightly check" in Start here.' + read;
|
|
408
|
+
const idle = nightly.active ? '' : ' Nothing is scheduled, so it is not running - press "Set it up again" in Start here.';
|
|
409
|
+
if (!nightly.ranAt) return 'Nightly run in your database: set up, and it has not run yet.' + idle + read;
|
|
410
|
+
const when = 'Nightly run in your database, ' + day(nightly.ranAt) + ': ';
|
|
411
|
+
if (nightly.stopped) return when + 'it could not check - ' + nightly.stopped + idle + read;
|
|
412
|
+
// A function anyone can call is a thing to check, not a problem proven -
|
|
413
|
+
// counted apart, the way the terminal counts it.
|
|
414
|
+
const n = (nightly.findings || []).filter((f) => f.status !== 'verification required').length;
|
|
415
|
+
const toCheck = (nightly.findings || []).length - n;
|
|
416
|
+
const skipped = (nightly.notChecked || []).length;
|
|
417
|
+
return when + (n ? n + (n === 1 ? ' problem found.' : ' problems found.')
|
|
418
|
+
: 'nothing got through ' + (nightly.attacksRun || 0) + ' attacks.') +
|
|
419
|
+
(toCheck ? ' ' + toCheck + (toCheck === 1 ? ' thing' : ' things') + ' to check.' : '') +
|
|
420
|
+
(skipped ? ' ' + skipped + ' ' + (skipped === 1 ? 'part was' : 'parts were') + ' not tested.' : '') + idle + read;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* Everything the page shows, from the store alone. Findings from every check
|
|
425
|
+
* are put into one list, each carrying the same fields - severity, status,
|
|
426
|
+
* evidence, confidence, where, what, and the fix - so the page can show them
|
|
427
|
+
* the same way whichever check found them.
|
|
428
|
+
*/
|
|
429
|
+
function buildState(root, env) {
|
|
430
|
+
const paths = store.pathsFor(root, env);
|
|
431
|
+
const config = readConfig(paths);
|
|
432
|
+
const history = readHistory(paths.history, 20);
|
|
433
|
+
const baselines = readJson(paths.baselines) || {};
|
|
434
|
+
const code = readJson(paths.codeFindings);
|
|
435
|
+
const night = readJson(paths.nightLast);
|
|
436
|
+
// What the nightly run inside the database last said, kept by
|
|
437
|
+
// kryptheon-night whenever it connects. Not the same answer as night:
|
|
438
|
+
// that one is the scan last run from this terminal.
|
|
439
|
+
const nightly = readJson(paths.nightNightly);
|
|
440
|
+
|
|
441
|
+
const findings = [];
|
|
442
|
+
|
|
443
|
+
// Recorded flows: the last run's broken tests are the findings. They were
|
|
444
|
+
// replayed in a real browser, so they are confirmed at runtime.
|
|
445
|
+
const lastRun = history.length ? history[history.length - 1] : null;
|
|
446
|
+
if (lastRun && config.enabled.regression) {
|
|
447
|
+
for (const t of lastRun.tests || []) {
|
|
448
|
+
if (t.status !== 'failed' && t.status !== 'timedOut') continue;
|
|
449
|
+
findings.push({
|
|
450
|
+
check: 'regression',
|
|
451
|
+
severity: 'HIGH',
|
|
452
|
+
status: 'confirmed',
|
|
453
|
+
evidence: 'runtime confirmed',
|
|
454
|
+
confidence: 'high',
|
|
455
|
+
where: String((t.failure && t.failure.file) || t.specFile || 'tests').split('\\').join('/') +
|
|
456
|
+
(t.failure && t.failure.line ? ':' + t.failure.line : ''),
|
|
457
|
+
headline: 'The recorded flow "' + t.title + '" broke.',
|
|
458
|
+
detail: (t.failure && t.failure.plainLanguage) || '',
|
|
459
|
+
fixPrompt: flowPrompt(t, lastPassOf(history, t.title)),
|
|
460
|
+
when: lastRun.runAt,
|
|
461
|
+
});
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
if (code && config.enabled.frontend) {
|
|
466
|
+
for (const f of code.findings || []) {
|
|
467
|
+
findings.push({
|
|
468
|
+
check: 'frontend',
|
|
469
|
+
severity: f.severity,
|
|
470
|
+
status: f.status,
|
|
471
|
+
evidence: f.evidence,
|
|
472
|
+
confidence: f.confidence,
|
|
473
|
+
where: f.file + ':' + f.line,
|
|
474
|
+
headline: f.headline,
|
|
475
|
+
// The part of a long template that decides it, so the person reading
|
|
476
|
+
// it looks at one value instead of sixty lines of markup.
|
|
477
|
+
detail: f.sink + ': ' + f.expression + riskyPartsLine(f),
|
|
478
|
+
fixPrompt: f.fixPrompt,
|
|
479
|
+
when: code.checkedAt,
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
// Input and edge cases, and reliability: the last press of each one's
|
|
485
|
+
// button. Each finding carries its proof, and what it was about - the
|
|
486
|
+
// field and value, the flow, the step - so a re-check can try exactly that
|
|
487
|
+
// again.
|
|
488
|
+
const probeLast = {};
|
|
489
|
+
for (const id of PROBE_CHECKS) {
|
|
490
|
+
const last = (probeLast[id] = readJson(path.join(paths.dir, require('./kryptheon-probe.js').REPORT_FILES[id])));
|
|
491
|
+
if (!last || !config.enabled[id]) continue;
|
|
492
|
+
for (const f of last.findings || []) {
|
|
493
|
+
findings.push({
|
|
494
|
+
check: id,
|
|
495
|
+
severity: f.severity,
|
|
496
|
+
status: f.status,
|
|
497
|
+
evidence: f.status === 'confirmed' ? 'runtime confirmed' : 'seen once',
|
|
498
|
+
confidence: f.status === 'confirmed' ? 'high' : 'medium',
|
|
499
|
+
where: f.where,
|
|
500
|
+
headline: f.headline,
|
|
501
|
+
detail: (f.proof || []).join('\n'),
|
|
502
|
+
fixPrompt: f.fixPrompt || '',
|
|
503
|
+
when: last.endedAt || last.startedAt,
|
|
504
|
+
kind: f.kind,
|
|
505
|
+
type: f.type,
|
|
506
|
+
});
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
let nightWhen = null;
|
|
511
|
+
try {
|
|
512
|
+
nightWhen = fs.statSync(paths.nightLast).mtime.toISOString();
|
|
513
|
+
} catch (err) {
|
|
514
|
+
/* never run */
|
|
515
|
+
}
|
|
516
|
+
if (night && config.enabled.data) {
|
|
517
|
+
for (const f of night.findings || []) {
|
|
518
|
+
findings.push({
|
|
519
|
+
check: 'data',
|
|
520
|
+
severity: f.severity,
|
|
521
|
+
status: f.status || 'confirmed',
|
|
522
|
+
evidence: (f.status || 'confirmed') === 'confirmed' ? 'runtime confirmed' : 'code analysis',
|
|
523
|
+
confidence: (f.status || 'confirmed') === 'confirmed' ? 'high' : 'medium',
|
|
524
|
+
where: f.table + (f.column ? '.' + f.column : ''),
|
|
525
|
+
headline: f.headline,
|
|
526
|
+
detail: f.body || '',
|
|
527
|
+
fixPrompt: f.fixPrompt || '',
|
|
528
|
+
when: nightWhen,
|
|
529
|
+
});
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
// The nightly run's findings, said as coming from it. One both answers
|
|
534
|
+
// share is shown once, with the newer date - the same problem found twice
|
|
535
|
+
// is still one problem to fix.
|
|
536
|
+
if (nightly && nightly.ranAt && config.enabled.data) {
|
|
537
|
+
for (const f of nightly.findings || []) {
|
|
538
|
+
const item = {
|
|
539
|
+
check: 'data',
|
|
540
|
+
from: 'nightly',
|
|
541
|
+
severity: f.severity,
|
|
542
|
+
status: f.status || 'confirmed',
|
|
543
|
+
evidence: (f.status || 'confirmed') === 'confirmed' ? 'runtime confirmed' : 'code analysis',
|
|
544
|
+
confidence: (f.status || 'confirmed') === 'confirmed' ? 'high' : 'medium',
|
|
545
|
+
where: f.table + (f.column ? '.' + f.column : ''),
|
|
546
|
+
headline: f.headline,
|
|
547
|
+
detail: f.body || '',
|
|
548
|
+
fixPrompt: f.fixPrompt || '',
|
|
549
|
+
when: nightly.ranAt,
|
|
550
|
+
};
|
|
551
|
+
const same = findings.find((o) => o.check === 'data' && changes.findingKey(o) === changes.findingKey(item));
|
|
552
|
+
if (!same) findings.push(item);
|
|
553
|
+
else {
|
|
554
|
+
same.from = 'both';
|
|
555
|
+
if (!same.when || item.when > same.when) same.when = item.when;
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
const rank = { 'HIGH CRITICAL': 0, CRITICAL: 0, HIGH: 1, MEDIUM: 2, LOW: 3 };
|
|
561
|
+
findings.sort((a, b) => (rank[a.severity] ?? 9) - (rank[b.severity] ?? 9) ||
|
|
562
|
+
(a.status === 'confirmed' ? 0 : 1) - (b.status === 'confirmed' ? 0 : 1));
|
|
563
|
+
|
|
564
|
+
// Every finding gets a short id and the last word of its fix prompt: how to
|
|
565
|
+
// prove the fix. Added here, where the loop lives, so every check's prompt
|
|
566
|
+
// ends the same way and the detection code itself stays untouched.
|
|
567
|
+
const attempts = readHistory(paths.fixes, 500);
|
|
568
|
+
const forAll = [];
|
|
569
|
+
for (const f of findings) {
|
|
570
|
+
f.id = changes.findingId(f);
|
|
571
|
+
f.attempts = attempts.filter((a) => a.id === f.id).map((a) => ({ at: a.at, verdict: a.verdict, why: a.why || '', newProblems: (a.newProblems || []).length }));
|
|
572
|
+
forAll.push({ id: f.id, severity: f.severity, headline: f.headline, where: f.where, prompt: f.fixPrompt || '' });
|
|
573
|
+
if (f.fixPrompt && PROBE_CHECKS.includes(f.check)) {
|
|
574
|
+
// Re-checking runs the flow in the app again, so it adds a little test
|
|
575
|
+
// data: --yes says yes for an app on this computer, and is refused for
|
|
576
|
+
// any other address - there the person must answer.
|
|
577
|
+
f.fixPrompt += '\n\nWhen you have fixed it, prove it: run npx kryptheon recheck ' + f.id + ' --yes' +
|
|
578
|
+
' (it runs the flow in the app again, adding a little test data; --yes works only for an app running on this computer). ' +
|
|
579
|
+
'It is fixed only when that says FIXED.';
|
|
580
|
+
} else if (f.fixPrompt) {
|
|
581
|
+
f.fixPrompt += '\n\nWhen you have fixed it, prove it: run npx kryptheon recheck ' + f.id +
|
|
582
|
+
' (or press Re-check on this finding in the Kryptheon dashboard). It is fixed only when that says FIXED.';
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
// Fixes that were proven, newest first, while their finding is still gone.
|
|
586
|
+
const open = new Set(findings.map((f) => f.id));
|
|
587
|
+
const proven = attempts.filter((a) => a.verdict === 'fixed' && !open.has(a.id)).reverse().slice(0, 10)
|
|
588
|
+
.map((a) => ({ id: a.id, at: a.at, where: a.where, headline: a.headline, newProblems: (a.newProblems || []).length }));
|
|
589
|
+
|
|
590
|
+
const recordings = countRecordings(root);
|
|
591
|
+
const checks = CHECKS.map((c) => {
|
|
592
|
+
let state;
|
|
593
|
+
if (!c.available) state = 'coming soon';
|
|
594
|
+
else if (!config.enabled[c.id]) state = 'switched off';
|
|
595
|
+
else if (c.id === 'regression') state = !recordings ? 'no recordings yet' : !lastRun ? 'never run' : lastRun.failed ? 'problems found' : 'last run held';
|
|
596
|
+
else if (PROBE_CHECKS.includes(c.id)) {
|
|
597
|
+
const last = probeLast[c.id];
|
|
598
|
+
state = !last ? 'never run'
|
|
599
|
+
: findings.some((f) => f.check === c.id && f.status === 'confirmed') ? 'problems found'
|
|
600
|
+
: findings.some((f) => f.check === c.id) ? 'things to check'
|
|
601
|
+
: last.stopped ? 'stopped part way'
|
|
602
|
+
: !(last.flows || []).some((x) => x.state === 'tested') ? 'nothing was tested'
|
|
603
|
+
: 'last run held';
|
|
604
|
+
}
|
|
605
|
+
else if (c.id === 'frontend') state = !code ? 'never run' : (code.findings || []).length ? 'things to check' : 'nothing found';
|
|
606
|
+
else if (c.id === 'data') {
|
|
607
|
+
const ranNightly = nightly && nightly.ranAt && !nightly.stopped;
|
|
608
|
+
state = !night && !ranNightly ? 'never run here'
|
|
609
|
+
: findings.some((f) => f.check === 'data') ? 'problems found' : 'last run held';
|
|
610
|
+
}
|
|
611
|
+
return {
|
|
612
|
+
id: c.id, label: c.label, available: c.available, enabled: config.enabled[c.id], state: state, what: c.what || '',
|
|
613
|
+
nightly: c.id === 'data' ? nightlyLine(nightly) : null,
|
|
614
|
+
// A check that adds data to the app: never in "Run all", never on its
|
|
615
|
+
// own - only its own button, after the page has shown these words.
|
|
616
|
+
asks: c.asks && config.enabled[c.id] && recordings > 0 ? probeAsk(root, PROBE_WHAT[c.id], env, PROBE_WORDS[c.id]) : null,
|
|
617
|
+
lastRun: probeLast[c.id] ? {
|
|
618
|
+
at: probeLast[c.id].endedAt, stopped: !!probeLast[c.id].stopped, runsDone: probeLast[c.id].runsDone, runsPlanned: probeLast[c.id].runsPlanned,
|
|
619
|
+
held: (probeLast[c.id].held || []).length,
|
|
620
|
+
notTested: (probeLast[c.id].notTested || []).length + (probeLast[c.id].flows || []).filter((x) => x.state !== 'tested').length,
|
|
621
|
+
testData: (probeLast[c.id].testData || []).length,
|
|
622
|
+
} : null,
|
|
623
|
+
// Runnable from the page: what needs nothing the page cannot supply. The
|
|
624
|
+
// database check needs a connection string, which stays in the terminal.
|
|
625
|
+
runnable: c.available && (c.id === 'frontend' || (c.id === 'regression' && recordings > 0)),
|
|
626
|
+
};
|
|
627
|
+
});
|
|
628
|
+
|
|
629
|
+
const webOnly = isHomeKept(root, env);
|
|
630
|
+
const steps = startSteps(checks, {
|
|
631
|
+
webOnly: webOnly,
|
|
632
|
+
codeAt: code ? code.checkedAt : null,
|
|
633
|
+
recordings: recordings,
|
|
634
|
+
lastRunAt: lastRun ? lastRun.runAt : null,
|
|
635
|
+
nightAt: [night ? nightWhen : null, nightly && nightly.ranAt].filter(Boolean).sort().pop() || null,
|
|
636
|
+
nightly: nightly,
|
|
637
|
+
});
|
|
638
|
+
|
|
639
|
+
// Every look Kryptheon took - by `verify`, or by the dashboard's own watch -
|
|
640
|
+
// newest first: what changed, what was run, and what came of it.
|
|
641
|
+
const looks = readHistory(paths.changes, 15).reverse().map((e) => ({
|
|
642
|
+
at: e.at,
|
|
643
|
+
by: e.by || 'verify',
|
|
644
|
+
first: !!e.first,
|
|
645
|
+
files: (e.files || []).slice(0, 30),
|
|
646
|
+
fileCount: (e.files || []).length,
|
|
647
|
+
parts: e.parts || [],
|
|
648
|
+
dependencies: e.dependencies || [],
|
|
649
|
+
checks: e.checks || [],
|
|
650
|
+
newFindings: e.newFindings || 0,
|
|
651
|
+
fixed: e.fixed || 0,
|
|
652
|
+
goneWithFile: e.goneWithFile || 0,
|
|
653
|
+
}));
|
|
654
|
+
|
|
655
|
+
return {
|
|
656
|
+
project: { name: path.basename(root), root: root, storeDir: paths.dir },
|
|
657
|
+
// Which kryptheon this dashboard is: a newer `dashboard` asks, and takes
|
|
658
|
+
// the place of an older one still running, instead of opening it.
|
|
659
|
+
kryptheon: VERSION,
|
|
660
|
+
looks: looks,
|
|
661
|
+
proven: proven,
|
|
662
|
+
checks: checks,
|
|
663
|
+
findings: findings,
|
|
664
|
+
allFixPrompt: combinedPrompt(forAll),
|
|
665
|
+
runs: history.slice().reverse().map((r) => ({ runAt: r.runAt, status: r.status, passed: r.passed, failed: r.failed, durationMs: r.durationMs })),
|
|
666
|
+
recordings: recordings,
|
|
667
|
+
steps: steps,
|
|
668
|
+
// Which app this dashboard is for: its web address, kept here, and whether
|
|
669
|
+
// its code is on this computer at all. The dashboard started from the home
|
|
670
|
+
// folder (Win+R) holds recordings only - the app is a web address.
|
|
671
|
+
app: {
|
|
672
|
+
address: readAppAddress(paths),
|
|
673
|
+
webOnly: webOnly,
|
|
674
|
+
folder: root,
|
|
675
|
+
canChooseFolder: process.platform === 'win32' || process.platform === 'darwin',
|
|
676
|
+
},
|
|
677
|
+
next: nextAction(findings, steps),
|
|
678
|
+
baselines: Object.keys(baselines).length,
|
|
679
|
+
changes: recentChanges(root),
|
|
680
|
+
// Part 3 is where a version gets verified. Until then this says so,
|
|
681
|
+
// rather than leaving a blank that could be read either way.
|
|
682
|
+
verification: { state: 'not verified', why: 'Kryptheon Verified is not built yet. No version of this project has been verified.' },
|
|
683
|
+
// Whether this dashboard starts by itself when the person signs in.
|
|
684
|
+
autostart: (() => { const s = autostart.status(root, env); return { available: s.available, on: s.on, why: s.why || '' }; })(),
|
|
685
|
+
};
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
/* --------------------------------------------------------------------------
|
|
689
|
+
Jobs: what the page starts, one at a time, with its progress and its end.
|
|
690
|
+
-------------------------------------------------------------------------- */
|
|
691
|
+
|
|
692
|
+
// What each run is called on the page, and the command that does the same in
|
|
693
|
+
// a terminal - shown under every button, for when the page cannot be used.
|
|
694
|
+
const RUN_TITLE = { frontend: 'Reading your code', regression: 'Replaying your recordings', edge: 'Trying unusual inputs in your forms', reliability: 'Replaying your flows three times, and double-clicking' };
|
|
695
|
+
const RUN_COMMAND = { frontend: 'npx kryptheon code', regression: 'npx kryptheon check', edge: 'npx kryptheon edge', reliability: 'npx kryptheon reliability' };
|
|
696
|
+
const DB_ACTIONS = {
|
|
697
|
+
scan: { title: 'Checking your database', command: 'npx kryptheon-night' },
|
|
698
|
+
install: { title: 'Setting up the nightly check', command: 'npx kryptheon-night install' },
|
|
699
|
+
night: { title: 'Reading last night\'s result', command: 'npx kryptheon-night night' },
|
|
700
|
+
uninstall: { title: 'Removing the nightly check', command: 'npx kryptheon-night uninstall' },
|
|
701
|
+
recheck: { title: 'Re-checking in your database', command: 'npx kryptheon-night' },
|
|
702
|
+
};
|
|
703
|
+
|
|
704
|
+
/**
|
|
705
|
+
* What a fresh database check says about one database finding. Fixed only
|
|
706
|
+
* when the check ran, the finding is gone, and nothing about its table went
|
|
707
|
+
* untested - a table that could not be tested loses its findings too, and
|
|
708
|
+
* that is not a fix. A finding still shown only from the nightly run is the
|
|
709
|
+
* nightly run's older answer, not this one's.
|
|
710
|
+
*/
|
|
711
|
+
function databaseVerdict(target, before, after, ran, saved) {
|
|
712
|
+
const tableOf = (where) => String(where || '').split('.')[0];
|
|
713
|
+
let verdict;
|
|
714
|
+
let why = '';
|
|
715
|
+
const untested = ((saved && saved.notChecked) || []).find((n) => tableOf(n.table) === tableOf(target.where));
|
|
716
|
+
if (!ran || !ran.ok) {
|
|
717
|
+
verdict = 'could not confirm';
|
|
718
|
+
why = (ran && ran.why) || 'the database check could not run';
|
|
719
|
+
} else if (after.some((f) => f.id === target.id && f.from !== 'nightly')) {
|
|
720
|
+
verdict = 'still open';
|
|
721
|
+
} else if (untested) {
|
|
722
|
+
verdict = 'could not confirm';
|
|
723
|
+
why = 'part of ' + tableOf(target.where) + ' could not be tested this time: ' + untested.why;
|
|
724
|
+
} else {
|
|
725
|
+
verdict = 'fixed';
|
|
726
|
+
}
|
|
727
|
+
const had = new Set(before.map((f) => f.id));
|
|
728
|
+
return {
|
|
729
|
+
id: target.id,
|
|
730
|
+
at: new Date().toISOString(),
|
|
731
|
+
check: 'data',
|
|
732
|
+
where: target.where,
|
|
733
|
+
headline: target.headline,
|
|
734
|
+
verdict: verdict,
|
|
735
|
+
why: why,
|
|
736
|
+
newProblems: after.filter((f) => !had.has(f.id) && f.check === 'data').map((f) => ({
|
|
737
|
+
id: f.id, check: f.check, severity: f.severity, status: f.status, where: f.where, headline: f.headline,
|
|
738
|
+
})),
|
|
739
|
+
};
|
|
740
|
+
}
|
|
741
|
+
const JOB_LINES = 400;
|
|
742
|
+
|
|
743
|
+
/**
|
|
744
|
+
* The address to record, or null. Refused: anything that is not one line,
|
|
745
|
+
* anything that could be read as an option by the command it is passed to,
|
|
746
|
+
* and anything with a scheme other than http(s).
|
|
747
|
+
*/
|
|
748
|
+
/**
|
|
749
|
+
* The dashboard started from the home folder: the kryptheon-tests folder
|
|
750
|
+
* there, which holds recordings and nothing of the app's own code.
|
|
751
|
+
*/
|
|
752
|
+
function isHomeKept(root, env) {
|
|
753
|
+
const environment = env || process.env;
|
|
754
|
+
const home = environment.USERPROFILE || environment.HOME || require('os').homedir();
|
|
755
|
+
const tidy = (p) => (process.platform === 'win32' ? path.resolve(p).toLowerCase() : path.resolve(p));
|
|
756
|
+
return path.basename(root) === 'kryptheon-tests' && tidy(path.dirname(root)) === tidy(home);
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
/** The app's web address, as saved on the page - or empty. */
|
|
760
|
+
function readAppAddress(paths) {
|
|
761
|
+
const saved = readJson(path.join(paths.dir, 'app.json'));
|
|
762
|
+
return saved && typeof saved.address === 'string' ? saved.address : '';
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
function saveAppAddress(paths, address) {
|
|
766
|
+
try {
|
|
767
|
+
fs.mkdirSync(paths.dir, { recursive: true });
|
|
768
|
+
fs.writeFileSync(path.join(paths.dir, 'app.json'), JSON.stringify({ address: address, savedAt: new Date().toISOString() }, null, 2));
|
|
769
|
+
return true;
|
|
770
|
+
} catch (err) {
|
|
771
|
+
return false;
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
function appAddress(value) {
|
|
776
|
+
if (typeof value !== 'string') return null;
|
|
777
|
+
const text = value.trim();
|
|
778
|
+
if (!text || text.length > 2000 || /[\s\x00-\x1f]/.test(text) || text.startsWith('-')) return null;
|
|
779
|
+
if (/^[a-z][a-z0-9+.-]*:\/\//i.test(text)) return /^https?:\/\/[^/]/i.test(text) ? text : null;
|
|
780
|
+
// A path never starts with "//": "http//localhost:3000" is a mistyped
|
|
781
|
+
// scheme, not a host called "http".
|
|
782
|
+
return /^[a-z0-9.-]+(:\d{1,5})?(\/(?!\/)\S*)?$/i.test(text) ? text : null;
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
/**
|
|
786
|
+
* What was meant, when an address was typed with its start mistyped:
|
|
787
|
+
* "ttp://127.0.0.1:4180" (blind round 13, the first letter lost), "htp://",
|
|
788
|
+
* "http//", "http:/", "htps://". Null when it does not look like that.
|
|
789
|
+
*/
|
|
790
|
+
function meantAddress(value) {
|
|
791
|
+
const text = String(value || '').trim();
|
|
792
|
+
const m = /^(h?t{1,3}p{1,2}(s?)|htps|hhttps?)(:\/{0,3}|\/{1,3}|:)(?!\/)(\S+)$/i.exec(text);
|
|
793
|
+
if (!m) return null;
|
|
794
|
+
const secure = /s$/i.test(m[1]);
|
|
795
|
+
const fixed = (secure ? 'https://' : 'http://') + m[4];
|
|
796
|
+
return fixed !== text && appAddress(fixed) ? fixed : null;
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
function notAnAddress(value, example) {
|
|
800
|
+
const meant = meantAddress(value);
|
|
801
|
+
return meant
|
|
802
|
+
? 'that is not a web address - did you mean ' + meant + '?'
|
|
803
|
+
: 'that is not a web address - it should look like ' + example;
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
/** The connection string as given, or null when there is none or it is absurd. */
|
|
807
|
+
function connectionFrom(value) {
|
|
808
|
+
if (typeof value !== 'string') return null;
|
|
809
|
+
const text = value.trim();
|
|
810
|
+
return text && text.length <= 4000 ? text : null;
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
/**
|
|
814
|
+
* Every form in which a connection string could leak back out: the whole
|
|
815
|
+
* string, without the quotes it may have been pasted with, and its password
|
|
816
|
+
* both as typed and decoded. Longest first, so a password inside the whole
|
|
817
|
+
* string is never left half-hidden.
|
|
818
|
+
*/
|
|
819
|
+
function secretsOf(connection) {
|
|
820
|
+
const raw = String(connection || '').trim();
|
|
821
|
+
if (!raw) return [];
|
|
822
|
+
const found = [raw, raw.replace(/^["']|["']$/g, '')];
|
|
823
|
+
try {
|
|
824
|
+
const password = new URL(found[1]).password;
|
|
825
|
+
if (password) {
|
|
826
|
+
found.push(password);
|
|
827
|
+
try { found.push(decodeURIComponent(password)); } catch (err) { /* kept as typed */ }
|
|
828
|
+
}
|
|
829
|
+
} catch (err) {
|
|
830
|
+
/* not a URL: the string itself is still hidden */
|
|
831
|
+
}
|
|
832
|
+
return Array.from(new Set(found.filter((s) => s.length >= 4))).sort((a, b) => b.length - a.length);
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
function hideSecrets(text, secrets) {
|
|
836
|
+
let out = String(text);
|
|
837
|
+
for (const s of secrets || []) out = out.split(s).join('[hidden]');
|
|
838
|
+
return out;
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
/** The same, through every string in a value. */
|
|
842
|
+
function hideSecretsIn(value, secrets) {
|
|
843
|
+
if (!secrets || !secrets.length) return value;
|
|
844
|
+
if (typeof value === 'string') return hideSecrets(value, secrets);
|
|
845
|
+
if (Array.isArray(value)) return value.map((v) => hideSecretsIn(v, secrets));
|
|
846
|
+
if (value && typeof value === 'object') {
|
|
847
|
+
const out = {};
|
|
848
|
+
for (const k of Object.keys(value)) out[k] = hideSecretsIn(value[k], secrets);
|
|
849
|
+
return out;
|
|
850
|
+
}
|
|
851
|
+
return value;
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
/**
|
|
855
|
+
* The one job that may run, and the last one that did. Its lines are what the
|
|
856
|
+
* work said as it went, scrubbed of any secret it was given before they are
|
|
857
|
+
* kept; when it ends, the secrets are let go.
|
|
858
|
+
*/
|
|
859
|
+
function createJobs() {
|
|
860
|
+
let current = null;
|
|
861
|
+
let count = 0;
|
|
862
|
+
const view = (job) => job && {
|
|
863
|
+
id: job.id, kind: job.kind, title: job.title, command: job.command,
|
|
864
|
+
startedAt: job.startedAt, endedAt: job.endedAt, running: job.running,
|
|
865
|
+
lines: job.lines.slice(), result: job.result,
|
|
866
|
+
// A run that can be ended part way says so, and the page shows Stop.
|
|
867
|
+
stoppable: Boolean(job.running && job.stopper), stopping: Boolean(job.stopping),
|
|
868
|
+
};
|
|
869
|
+
return {
|
|
870
|
+
get: () => view(current),
|
|
871
|
+
busy: () => Boolean(current && current.running),
|
|
872
|
+
/** Ends the running job part way, if it can be. What it finished is kept. */
|
|
873
|
+
stop() {
|
|
874
|
+
if (!current || !current.running || !current.stopper) return false;
|
|
875
|
+
current.stopping = true;
|
|
876
|
+
current.stopper();
|
|
877
|
+
return true;
|
|
878
|
+
},
|
|
879
|
+
start(kind, title, command, work, givenSecrets) {
|
|
880
|
+
if (current && current.running) return null;
|
|
881
|
+
let secrets = givenSecrets || [];
|
|
882
|
+
const job = {
|
|
883
|
+
id: ++count, kind: kind, title: title, command: command || '',
|
|
884
|
+
startedAt: new Date().toISOString(), endedAt: null, running: true, lines: [], result: null,
|
|
885
|
+
};
|
|
886
|
+
current = job;
|
|
887
|
+
const say = (text) => {
|
|
888
|
+
if (!job.running) return;
|
|
889
|
+
// Colour codes are for terminals; on the page they are noise.
|
|
890
|
+
const clean = hideSecrets(String(text == null ? '' : text), secrets).replace(/\x1b\[[0-9;]*[A-Za-z]/g, '');
|
|
891
|
+
for (const line of clean.split(/\r?\n/)) job.lines.push(line.slice(0, 500));
|
|
892
|
+
if (job.lines.length > JOB_LINES) job.lines.splice(0, job.lines.length - JOB_LINES);
|
|
893
|
+
};
|
|
894
|
+
const finish = (result) => {
|
|
895
|
+
job.result = hideSecretsIn(result && typeof result === 'object' ? result : { ok: false, why: 'the run gave no answer' }, secrets);
|
|
896
|
+
job.running = false;
|
|
897
|
+
job.endedAt = new Date().toISOString();
|
|
898
|
+
secrets = null;
|
|
899
|
+
};
|
|
900
|
+
Promise.resolve()
|
|
901
|
+
.then(() => work(say, { onStop: (fn) => (job.stopper = fn) }))
|
|
902
|
+
.then(finish, (err) => finish({ ok: false, why: err && err.message ? err.message : String(err) }));
|
|
903
|
+
return view(job);
|
|
904
|
+
},
|
|
905
|
+
};
|
|
906
|
+
}
|
|
907
|
+
|
|
908
|
+
/**
|
|
909
|
+
* The server. Local only, and closed to other web pages:
|
|
910
|
+
* - it listens on 127.0.0.1, never on the network;
|
|
911
|
+
* - it answers only when the Host header is this address, so a page on some
|
|
912
|
+
* other site cannot reach it through DNS rebinding;
|
|
913
|
+
* - anything that changes something needs the token that is written into the
|
|
914
|
+
* page this server hands out, which another site cannot read.
|
|
915
|
+
*/
|
|
916
|
+
function createServer(root, options) {
|
|
917
|
+
const opts = options || {};
|
|
918
|
+
const env = opts.env || process.env;
|
|
919
|
+
const token = crypto.randomBytes(24).toString('hex');
|
|
920
|
+
const page = fs.readFileSync(path.join(__dirname, 'kryptheon-dashboard.html'), 'utf8');
|
|
921
|
+
let port = 0;
|
|
922
|
+
const jobs = createJobs();
|
|
923
|
+
|
|
924
|
+
const allowedHosts = () => new Set(['127.0.0.1:' + port, 'localhost:' + port]);
|
|
925
|
+
const send = (res, code, type, body) => {
|
|
926
|
+
res.writeHead(code, {
|
|
927
|
+
'content-type': type,
|
|
928
|
+
'cache-control': 'no-store',
|
|
929
|
+
'x-content-type-options': 'nosniff',
|
|
930
|
+
'referrer-policy': 'no-referrer',
|
|
931
|
+
// The page loads nothing from anywhere else, and says so.
|
|
932
|
+
'content-security-policy': "default-src 'none'; script-src 'self'; style-src 'self' 'unsafe-inline'; connect-src 'self'; img-src 'self' data:; font-src 'self'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'",
|
|
933
|
+
});
|
|
934
|
+
res.end(body);
|
|
935
|
+
};
|
|
936
|
+
// Files served as they are from next to this one. A missing one is a
|
|
937
|
+
// missing picture or font, never a stopped dashboard.
|
|
938
|
+
const ASSETS = {
|
|
939
|
+
'/logo.png': ['image/png', opts.logoFile || path.join(__dirname, 'kryptheon-logo.png')],
|
|
940
|
+
'/fonts/archivo.woff2': ['font/woff2', path.join(__dirname, 'kryptheon-archivo.woff2')],
|
|
941
|
+
'/fonts/spacemono.woff2': ['font/woff2', path.join(__dirname, 'kryptheon-spacemono.woff2')],
|
|
942
|
+
};
|
|
943
|
+
const json = (res, code, value) => send(res, code, 'application/json; charset=utf-8', JSON.stringify(value));
|
|
944
|
+
|
|
945
|
+
const server = http.createServer((req, res) => {
|
|
946
|
+
if (!allowedHosts().has(String(req.headers.host || ''))) return send(res, 403, 'text/plain', 'Forbidden');
|
|
947
|
+
const url = new URL(req.url, 'http://127.0.0.1');
|
|
948
|
+
|
|
949
|
+
if (req.method === 'GET' && url.pathname === '/') {
|
|
950
|
+
return send(res, 200, 'text/html; charset=utf-8', page.replace('%%TOKEN%%', token));
|
|
951
|
+
}
|
|
952
|
+
if (req.method === 'GET' && url.pathname === '/dashboard.js') {
|
|
953
|
+
return send(res, 200, 'text/javascript; charset=utf-8', fs.readFileSync(path.join(__dirname, 'kryptheon-dashboard-page.js'), 'utf8'));
|
|
954
|
+
}
|
|
955
|
+
if (req.method === 'GET' && Object.prototype.hasOwnProperty.call(ASSETS, url.pathname)) {
|
|
956
|
+
const [type, file] = ASSETS[url.pathname];
|
|
957
|
+
let body;
|
|
958
|
+
try {
|
|
959
|
+
body = fs.readFileSync(file);
|
|
960
|
+
} catch (err) {
|
|
961
|
+
return send(res, 404, 'text/plain', 'Not found');
|
|
962
|
+
}
|
|
963
|
+
return send(res, 200, type, body);
|
|
964
|
+
}
|
|
965
|
+
if (req.method === 'GET' && url.pathname === '/api/state') {
|
|
966
|
+
return json(res, 200, buildState(root, env));
|
|
967
|
+
}
|
|
968
|
+
if (req.method === 'GET' && url.pathname === '/api/job') {
|
|
969
|
+
return json(res, 200, { job: jobs.get() });
|
|
970
|
+
}
|
|
971
|
+
if (req.method === 'POST') {
|
|
972
|
+
if (req.headers['x-kryptheon-token'] !== token) return json(res, 403, { ok: false, why: 'missing or wrong token' });
|
|
973
|
+
let body = '';
|
|
974
|
+
req.on('data', (chunk) => {
|
|
975
|
+
body += chunk;
|
|
976
|
+
if (body.length > 10000) req.destroy();
|
|
977
|
+
});
|
|
978
|
+
req.on('end', () => {
|
|
979
|
+
let input;
|
|
980
|
+
try {
|
|
981
|
+
input = JSON.parse(body || '{}');
|
|
982
|
+
} catch (err) {
|
|
983
|
+
return json(res, 400, { ok: false, why: 'not JSON' });
|
|
984
|
+
}
|
|
985
|
+
if (url.pathname === '/api/check') {
|
|
986
|
+
const r = setCheck(store.pathsFor(root, env), String(input.id || ''), input.on);
|
|
987
|
+
return json(res, r.ok ? 200 : 400, r);
|
|
988
|
+
}
|
|
989
|
+
if (url.pathname === '/api/autostart') {
|
|
990
|
+
const r = autostart.setAutostart(root, input.on, env);
|
|
991
|
+
return json(res, r.ok ? 200 : 400, { ok: r.ok, on: !!r.on, why: r.why || '' });
|
|
992
|
+
}
|
|
993
|
+
// Everything below starts a job: it answers at once with the job, and
|
|
994
|
+
// the page follows it on /api/job. One at a time - two browsers
|
|
995
|
+
// replaying against one app, or two attacks on one database, would
|
|
996
|
+
// interfere, and the results would describe neither.
|
|
997
|
+
const begin = (kind, title, command, work, secrets) => {
|
|
998
|
+
const job = jobs.start(kind, title, command, work, secrets);
|
|
999
|
+
return job ? json(res, 202, { ok: true, job: job }) : json(res, 409, { ok: false, why: 'a run is already going', job: jobs.get() });
|
|
1000
|
+
};
|
|
1001
|
+
if (url.pathname === '/api/stop') {
|
|
1002
|
+
return jobs.stop() ? json(res, 200, { ok: true, job: jobs.get() }) : json(res, 409, { ok: false, why: 'nothing that can be stopped is running' });
|
|
1003
|
+
}
|
|
1004
|
+
// A check that adds test data to the app. Only with the person's yes
|
|
1005
|
+
// from the page, and for an address that is not this computer, with
|
|
1006
|
+
// their tick that it is their own test copy as well. The addresses
|
|
1007
|
+
// are read again here, from the recordings - never taken from the page.
|
|
1008
|
+
// The addresses the person said yes to that are not this computer:
|
|
1009
|
+
// exactly the ones the page showed when it was ticked.
|
|
1010
|
+
let confirmed = [];
|
|
1011
|
+
const probeRefusal = (id) => {
|
|
1012
|
+
const check = buildState(root, env).checks.find((c) => c.id === id);
|
|
1013
|
+
if (!check || !check.asks) return (check ? check.label : '"' + id + '"') + ' cannot be run from here';
|
|
1014
|
+
if (input.consent !== true) return 'it adds test data to your app, so it runs only after you say yes';
|
|
1015
|
+
if (check.asks.live.length && input.liveConfirmed !== true) {
|
|
1016
|
+
return check.asks.live.join(', ') + ' is not this computer - tick that it is your own test copy first';
|
|
1017
|
+
}
|
|
1018
|
+
confirmed = input.liveConfirmed === true ? check.asks.live : [];
|
|
1019
|
+
return null;
|
|
1020
|
+
};
|
|
1021
|
+
if (url.pathname === '/api/probe' && typeof opts.probe === 'function') {
|
|
1022
|
+
const id = String(input.id || '');
|
|
1023
|
+
const refused = probeRefusal(id);
|
|
1024
|
+
if (refused) return json(res, 400, { ok: false, why: refused });
|
|
1025
|
+
return begin('probe', RUN_TITLE[id], RUN_COMMAND[id], (say, control) => opts.probe(id, say, control, confirmed));
|
|
1026
|
+
}
|
|
1027
|
+
if (url.pathname === '/api/recheck' && typeof opts.recheck === 'function') {
|
|
1028
|
+
const id = String(input.id || '');
|
|
1029
|
+
if (!/^[0-9a-f]{8}$/.test(id)) return json(res, 400, { ok: false, why: 'not a finding id' });
|
|
1030
|
+
const open = buildState(root, env).findings.find((f) => f.id === id);
|
|
1031
|
+
if (!open) {
|
|
1032
|
+
const done = clearedBy(root, id, env);
|
|
1033
|
+
return json(res, 404, { ok: false, why: done
|
|
1034
|
+
? (done.by ? 'already fixed: the re-check of ' + done.by + ' at ' + localTime(done.at) + ' replayed it too, and it was gone' : 'already fixed by the re-check at ' + localTime(done.at))
|
|
1035
|
+
: 'no open finding with that id' });
|
|
1036
|
+
}
|
|
1037
|
+
if (PROBE_CHECKS.includes(open.check)) {
|
|
1038
|
+
const refused = probeRefusal(open.check);
|
|
1039
|
+
if (refused) return json(res, 400, { ok: false, why: refused });
|
|
1040
|
+
if (typeof opts.probeRecheck !== 'function') return json(res, 400, { ok: false, why: 'this finding cannot be re-checked from here' });
|
|
1041
|
+
return begin('recheck', 'Re-checking: ' + open.headline, 'npx kryptheon recheck ' + id, (say, control) => opts.probeRecheck(id, say, control, confirmed));
|
|
1042
|
+
}
|
|
1043
|
+
return begin('recheck', 'Re-checking: ' + open.headline, 'npx kryptheon recheck ' + id, (say) => opts.recheck(id, say));
|
|
1044
|
+
}
|
|
1045
|
+
if (url.pathname === '/api/run' && typeof opts.run === 'function') {
|
|
1046
|
+
const id = String(input.id || '');
|
|
1047
|
+
const check = buildState(root, env).checks.find((c) => c.id === id);
|
|
1048
|
+
if (!check || !check.runnable || !check.enabled) {
|
|
1049
|
+
return json(res, 400, { ok: false, why: (check ? check.label : '"' + id + '"') + ' cannot be run from here' });
|
|
1050
|
+
}
|
|
1051
|
+
return begin('run', RUN_TITLE[id] || check.label, RUN_COMMAND[id] || '', (say) => opts.run(id, say));
|
|
1052
|
+
}
|
|
1053
|
+
if (url.pathname === '/api/record' && typeof opts.record === 'function') {
|
|
1054
|
+
const address = appAddress(input.url);
|
|
1055
|
+
if (!address) return json(res, 400, { ok: false, why: notAnAddress(input.url, 'http://localhost:3000') });
|
|
1056
|
+
const phone = input.phone === true;
|
|
1057
|
+
return begin('record', 'Recording ' + address + (phone ? ' at phone size' : ''),
|
|
1058
|
+
'npx kryptheon record ' + address + (phone ? ' --phone' : ''), (say) => opts.record(address, say, { phone: phone }));
|
|
1059
|
+
}
|
|
1060
|
+
if (url.pathname === '/api/db' && typeof opts.db === 'function') {
|
|
1061
|
+
const action = String(input.action || '');
|
|
1062
|
+
if (!DB_ACTIONS[action]) return json(res, 400, { ok: false, why: 'no database action called "' + action + '"' });
|
|
1063
|
+
const connection = connectionFrom(input.connection);
|
|
1064
|
+
if (!connection) return json(res, 400, { ok: false, why: 'paste your connection string first' });
|
|
1065
|
+
if (action === 'recheck') {
|
|
1066
|
+
// One database finding, re-checked: the check runs again and that
|
|
1067
|
+
// finding is judged, the way `npx kryptheon recheck` judges the rest.
|
|
1068
|
+
const id = String(input.id || '');
|
|
1069
|
+
if (!/^[0-9a-f]{8}$/.test(id)) return json(res, 400, { ok: false, why: 'not a finding id' });
|
|
1070
|
+
const target = buildState(root, env).findings.find((f) => f.id === id && f.check === 'data');
|
|
1071
|
+
if (!target) return json(res, 404, { ok: false, why: 'no open database finding with that id' });
|
|
1072
|
+
return begin('db', 'Re-checking: ' + target.headline, DB_ACTIONS.recheck.command, (say) => {
|
|
1073
|
+
const before = buildState(root, env).findings;
|
|
1074
|
+
return Promise.resolve(opts.db('scan', connection, say)).then((ran) => {
|
|
1075
|
+
const attempt = databaseVerdict(target, before, buildState(root, env).findings, ran, readJson(store.pathsFor(root, env).nightLast));
|
|
1076
|
+
try {
|
|
1077
|
+
fs.appendFileSync(store.pathsFor(root, env).fixes, JSON.stringify(attempt) + '\n', 'utf8');
|
|
1078
|
+
} catch (err) {
|
|
1079
|
+
attempt.saveError = err.message;
|
|
1080
|
+
}
|
|
1081
|
+
return Object.assign({ ok: true, summary: attempt.verdict }, attempt);
|
|
1082
|
+
});
|
|
1083
|
+
}, secretsOf(connection));
|
|
1084
|
+
}
|
|
1085
|
+
// Handed to the job and nowhere else. The job's lines and result are
|
|
1086
|
+
// scrubbed of it, and it is dropped the moment the job ends.
|
|
1087
|
+
return begin('db', DB_ACTIONS[action].title, DB_ACTIONS[action].command,
|
|
1088
|
+
(say) => opts.db(action, connection, say), secretsOf(connection));
|
|
1089
|
+
}
|
|
1090
|
+
if (url.pathname === '/api/app') {
|
|
1091
|
+
// The app's web address: saved for this project, so Record and the
|
|
1092
|
+
// outside check start from it. Empty clears it.
|
|
1093
|
+
const raw = typeof input.address === 'string' ? input.address.trim() : '';
|
|
1094
|
+
const address = raw ? appAddress(raw) : '';
|
|
1095
|
+
if (address === null) return json(res, 400, { ok: false, why: notAnAddress(raw, 'https://my-app.lovable.app') });
|
|
1096
|
+
const full = address && !/^https?:\/\//i.test(address) ? 'https://' + address : address;
|
|
1097
|
+
return saveAppAddress(store.pathsFor(root, env), full)
|
|
1098
|
+
? json(res, 200, { ok: true, address: full })
|
|
1099
|
+
: json(res, 500, { ok: false, why: 'it could not be saved on this computer' });
|
|
1100
|
+
}
|
|
1101
|
+
if (url.pathname === '/api/choose-folder' && typeof opts.chooseFolder === 'function' && typeof opts.openFolder === 'function') {
|
|
1102
|
+
// The person's own folder picker, on this computer; then a dashboard
|
|
1103
|
+
// for that folder in a window of its own. Nothing is run in it here.
|
|
1104
|
+
return Promise.resolve(opts.chooseFolder()).then((folder) => {
|
|
1105
|
+
if (!folder) return json(res, 200, { ok: false, cancelled: true, why: 'no folder was chosen' });
|
|
1106
|
+
const opened = opts.openFolder(folder);
|
|
1107
|
+
return json(res, opened.ok ? 200 : 400, Object.assign({ folder: folder }, opened));
|
|
1108
|
+
}, (err) => json(res, 500, { ok: false, why: err.message }));
|
|
1109
|
+
}
|
|
1110
|
+
if (url.pathname === '/api/db/words' && typeof opts.words === 'function') {
|
|
1111
|
+
// What kryptheon-night itself would say about this string - its help,
|
|
1112
|
+
// its consent screens, its warnings - asked of it without connecting.
|
|
1113
|
+
const connection = input.connection == null || input.connection === '' ? '' : connectionFrom(input.connection);
|
|
1114
|
+
if (connection === null) return json(res, 400, { ok: false, why: 'that connection string is too long' });
|
|
1115
|
+
const secrets = secretsOf(connection);
|
|
1116
|
+
return Promise.resolve(opts.words(connection)).then(
|
|
1117
|
+
(words) => json(res, 200, hideSecretsIn({ ok: true, words: words }, secrets)),
|
|
1118
|
+
(err) => json(res, 500, hideSecretsIn({ ok: false, why: err.message }, secrets)),
|
|
1119
|
+
);
|
|
1120
|
+
}
|
|
1121
|
+
return json(res, 404, { ok: false, why: 'no such action' });
|
|
1122
|
+
});
|
|
1123
|
+
return;
|
|
1124
|
+
}
|
|
1125
|
+
return send(res, 404, 'text/plain', 'Not found');
|
|
1126
|
+
});
|
|
1127
|
+
|
|
1128
|
+
return {
|
|
1129
|
+
server: server,
|
|
1130
|
+
token: token,
|
|
1131
|
+
listen(wanted) {
|
|
1132
|
+
return new Promise((resolve, reject) => {
|
|
1133
|
+
server.once('error', reject);
|
|
1134
|
+
server.listen(wanted || 0, '127.0.0.1', () => {
|
|
1135
|
+
port = server.address().port;
|
|
1136
|
+
resolve(port);
|
|
1137
|
+
});
|
|
1138
|
+
});
|
|
1139
|
+
},
|
|
1140
|
+
close() {
|
|
1141
|
+
return new Promise((resolve) => server.close(() => resolve()));
|
|
1142
|
+
},
|
|
1143
|
+
};
|
|
1144
|
+
}
|
|
1145
|
+
|
|
1146
|
+
module.exports = {
|
|
1147
|
+
CHECKS: CHECKS,
|
|
1148
|
+
buildState: buildState,
|
|
1149
|
+
clearedBy: clearedBy,
|
|
1150
|
+
localTime: localTime,
|
|
1151
|
+
flowPrompt: flowPrompt,
|
|
1152
|
+
appAddress: appAddress,
|
|
1153
|
+
meantAddress: meantAddress,
|
|
1154
|
+
secretsOf: secretsOf,
|
|
1155
|
+
hideSecrets: hideSecrets,
|
|
1156
|
+
hideSecretsIn: hideSecretsIn,
|
|
1157
|
+
createJobs: createJobs,
|
|
1158
|
+
DB_ACTIONS: DB_ACTIONS,
|
|
1159
|
+
databaseVerdict: databaseVerdict,
|
|
1160
|
+
startSteps: startSteps,
|
|
1161
|
+
nextAction: nextAction,
|
|
1162
|
+
combinedPrompt: combinedPrompt,
|
|
1163
|
+
readConfig: readConfig,
|
|
1164
|
+
setCheck: setCheck,
|
|
1165
|
+
createServer: createServer,
|
|
1166
|
+
};
|