@yagyeshvyas/vibeguard 1.0.0 → 1.1.0
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/CHANGELOG.md +126 -63
- package/LICENSE +21 -0
- package/README.md +499 -870
- package/bin/cli.js +2083 -1954
- package/ci/vibeguard.yml +42 -41
- package/package.json +2 -2
- package/src/auto.js +6 -1
- package/src/engine.js +294 -234
- package/src/mcp-server.js +1814 -1799
- package/src/report.js +325 -312
- package/src/rules-pack.js +9 -2
- package/src/rules.js +1506 -1451
- package/src/scanner.js +805 -685
- package/src/shell-guard.js +291 -275
- package/src/taint-ast.js +161 -9
- package/src/taint.js +14 -1
package/src/scanner.js
CHANGED
|
@@ -1,685 +1,805 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const fs = require('fs');
|
|
4
|
-
const path = require('path');
|
|
5
|
-
const crypto = require('crypto');
|
|
6
|
-
|
|
7
|
-
// Stable fingerprint: survives line moves (no line number), used for SARIF
|
|
8
|
-
// partialFingerprints and the scan --new-only baseline diff.
|
|
9
|
-
function fingerprintOf(f) {
|
|
10
|
-
return crypto
|
|
11
|
-
.createHash('sha1')
|
|
12
|
-
.update(`${f.ruleId} ${f.file} ${(f.snippet || '').trim()}`)
|
|
13
|
-
.digest('hex')
|
|
14
|
-
.slice(0, 16);
|
|
15
|
-
}
|
|
16
|
-
const rules = require('./rules');
|
|
17
|
-
const {
|
|
18
|
-
SEVERITY_ORDER,
|
|
19
|
-
lineRules,
|
|
20
|
-
fileRules,
|
|
21
|
-
crossFileRules,
|
|
22
|
-
makeFinding,
|
|
23
|
-
matchAll,
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
//
|
|
29
|
-
//
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
'
|
|
46
|
-
'
|
|
47
|
-
'
|
|
48
|
-
'
|
|
49
|
-
'
|
|
50
|
-
'
|
|
51
|
-
'
|
|
52
|
-
'
|
|
53
|
-
'.
|
|
54
|
-
'
|
|
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
|
-
if (base
|
|
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
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
if (
|
|
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
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
if (
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
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
|
-
const
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
severity:
|
|
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
|
-
const
|
|
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
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
const
|
|
396
|
-
|
|
397
|
-
:
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
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
|
-
const
|
|
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
|
-
const
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
const
|
|
519
|
-
const
|
|
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
|
-
|
|
592
|
-
//
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
const
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
const
|
|
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
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
const crypto = require('crypto');
|
|
6
|
+
|
|
7
|
+
// Stable fingerprint: survives line moves (no line number), used for SARIF
|
|
8
|
+
// partialFingerprints and the scan --new-only baseline diff.
|
|
9
|
+
function fingerprintOf(f) {
|
|
10
|
+
return crypto
|
|
11
|
+
.createHash('sha1')
|
|
12
|
+
.update(`${f.ruleId} ${f.file} ${(f.snippet || '').trim()}`)
|
|
13
|
+
.digest('hex')
|
|
14
|
+
.slice(0, 16);
|
|
15
|
+
}
|
|
16
|
+
const rules = require('./rules');
|
|
17
|
+
const {
|
|
18
|
+
SEVERITY_ORDER,
|
|
19
|
+
lineRules,
|
|
20
|
+
fileRules,
|
|
21
|
+
crossFileRules,
|
|
22
|
+
makeFinding,
|
|
23
|
+
matchAll,
|
|
24
|
+
requiredLiteral,
|
|
25
|
+
isCommentLine,
|
|
26
|
+
} = rules;
|
|
27
|
+
|
|
28
|
+
// Load optional project config (.vibeguardrc.json / vibeguard.config.json).
|
|
29
|
+
// Currently supports { "shapingFunctions": ["toDTO", ...] } to extend the
|
|
30
|
+
// redaction allowlist. Best-effort: bad/missing config = defaults.
|
|
31
|
+
function loadConfig(root) {
|
|
32
|
+
for (const name of ['.vibeguardrc.json', 'vibeguard.config.json']) {
|
|
33
|
+
try {
|
|
34
|
+
const p = path.join(root, name);
|
|
35
|
+
if (fs.existsSync(p)) return JSON.parse(fs.readFileSync(p, 'utf8'));
|
|
36
|
+
} catch {
|
|
37
|
+
/* ignore malformed config */
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return {};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Directories we never walk into.
|
|
44
|
+
const SKIP_DIRS = new Set([
|
|
45
|
+
'node_modules',
|
|
46
|
+
'.git',
|
|
47
|
+
'.vibeguard', // our own cache/baseline artifacts — never scan them
|
|
48
|
+
'dist',
|
|
49
|
+
'build',
|
|
50
|
+
'.next',
|
|
51
|
+
'out',
|
|
52
|
+
'coverage',
|
|
53
|
+
'.cache',
|
|
54
|
+
'vendor',
|
|
55
|
+
'.venv',
|
|
56
|
+
'venv',
|
|
57
|
+
'__pycache__',
|
|
58
|
+
]);
|
|
59
|
+
|
|
60
|
+
// Only text-ish source/config files. Everything else is skipped as binary/noise.
|
|
61
|
+
const SCAN_EXT = new Set([
|
|
62
|
+
'.js', '.jsx', '.ts', '.tsx', '.mjs', '.cjs',
|
|
63
|
+
'.py', '.rb', '.php', '.go', '.java', '.cs', '.rs',
|
|
64
|
+
'.sh', '.bash', '.sql',
|
|
65
|
+
'.json', '.yml', '.yaml', '.toml', '.ini', '.xml',
|
|
66
|
+
'.env', '.html', '.vue', '.svelte',
|
|
67
|
+
'.txt', '.md', '.pem', '.key',
|
|
68
|
+
'.tf', '.hcl', '.dockerfile',
|
|
69
|
+
]);
|
|
70
|
+
|
|
71
|
+
// Files without a "normal" extension we still want to read (dotfiles etc.).
|
|
72
|
+
const SCAN_BASENAMES = new Set([
|
|
73
|
+
'.env', '.env.local', '.env.development', '.env.production',
|
|
74
|
+
'.npmrc', 'Dockerfile', '.dockerenv',
|
|
75
|
+
]);
|
|
76
|
+
|
|
77
|
+
const MAX_FILE_BYTES = 2 * 1024 * 1024; // 2MB — skip anything huge.
|
|
78
|
+
|
|
79
|
+
function shouldScanFile(filePath) {
|
|
80
|
+
const base = path.basename(filePath);
|
|
81
|
+
if (base === '.vibeguard-baseline.json') return false; // our own artifact
|
|
82
|
+
if (SCAN_BASENAMES.has(base)) return true;
|
|
83
|
+
if (base.startsWith('.env')) return true;
|
|
84
|
+
const ext = path.extname(filePath).toLowerCase();
|
|
85
|
+
return SCAN_EXT.has(ext);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Files staged for commit (git diff --cached), as a Set of absolute paths.
|
|
89
|
+
// Returns null when not a git repo / git unavailable, so the caller can decide.
|
|
90
|
+
function getStagedFiles(root) {
|
|
91
|
+
try {
|
|
92
|
+
const { execFileSync } = require('child_process');
|
|
93
|
+
const out = execFileSync('git', ['diff', '--cached', '--name-only', '--diff-filter=ACM'], {
|
|
94
|
+
cwd: root,
|
|
95
|
+
encoding: 'utf8',
|
|
96
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
97
|
+
});
|
|
98
|
+
const set = new Set();
|
|
99
|
+
for (const rel of out.split(/\r?\n/)) {
|
|
100
|
+
const t = rel.trim();
|
|
101
|
+
if (t) set.add(path.resolve(root, t));
|
|
102
|
+
}
|
|
103
|
+
return set;
|
|
104
|
+
} catch {
|
|
105
|
+
return null; // not a git repo, or git not installed
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Recursively collect scannable files.
|
|
110
|
+
function walk(dir, out, rootStat) {
|
|
111
|
+
let entries;
|
|
112
|
+
try {
|
|
113
|
+
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
114
|
+
} catch {
|
|
115
|
+
return out;
|
|
116
|
+
}
|
|
117
|
+
for (const entry of entries) {
|
|
118
|
+
const full = path.join(dir, entry.name);
|
|
119
|
+
if (entry.isDirectory()) {
|
|
120
|
+
if (SKIP_DIRS.has(entry.name)) continue;
|
|
121
|
+
walk(full, out, rootStat);
|
|
122
|
+
} else if (entry.isFile()) {
|
|
123
|
+
if (!shouldScanFile(full)) continue;
|
|
124
|
+
try {
|
|
125
|
+
const st = fs.statSync(full);
|
|
126
|
+
if (st.size > MAX_FILE_BYTES) continue;
|
|
127
|
+
} catch {
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
out.push(full);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return out;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Naive check: is character offset `idx` inside a quoted string literal on this
|
|
137
|
+
// line? Tracks ' " ` state left-to-right. Used to suppress code-pattern matches
|
|
138
|
+
// that are actually text (e.g. the word eval() inside a message string).
|
|
139
|
+
function isInsideString(line, idx) {
|
|
140
|
+
let quote = null;
|
|
141
|
+
for (let i = 0; i < idx && i < line.length; i++) {
|
|
142
|
+
const c = line[i];
|
|
143
|
+
if (quote) {
|
|
144
|
+
if (c === '\\') i++; // skip escaped char
|
|
145
|
+
else if (c === quote) quote = null;
|
|
146
|
+
} else if (c === '"' || c === "'" || c === '`') {
|
|
147
|
+
quote = c;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return quote !== null;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Cheap binary sniff: NUL byte in the first chunk.
|
|
154
|
+
function looksBinary(buf) {
|
|
155
|
+
const n = Math.min(buf.length, 8000);
|
|
156
|
+
for (let i = 0; i < n; i++) if (buf[i] === 0) return true;
|
|
157
|
+
return false;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// `diag` (optional): a collector { degraded: [] } into which each analysis pass
|
|
161
|
+
// records a failure instead of swallowing it silently. A security scanner that
|
|
162
|
+
// fails open MUST surface that it did so — a quietly-skipped pass is a coverage
|
|
163
|
+
// hole the user never sees. Backward compatible: omit diag and behavior is old.
|
|
164
|
+
function scanFileContent(absPath, relPath, content, tree, diag) {
|
|
165
|
+
const noteDegraded = (pass, err) => {
|
|
166
|
+
if (diag && Array.isArray(diag.degraded)) {
|
|
167
|
+
diag.degraded.push({ file: relPath, pass, error: String((err && err.message) || err || 'unknown') });
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
let findings = [];
|
|
171
|
+
const lines = content.split(/\r?\n/);
|
|
172
|
+
|
|
173
|
+
// fileFilter depends only on the path, not the line — compute the applicable
|
|
174
|
+
// rule set ONCE per file instead of re-testing (and recompiling) a fileFilter
|
|
175
|
+
// regex on every line. Compiled fileFilters are cached on the rule object.
|
|
176
|
+
const activeRules = [];
|
|
177
|
+
for (const rule of lineRules) {
|
|
178
|
+
if (rule.fileFilter) {
|
|
179
|
+
let ff = rule._ffRe;
|
|
180
|
+
if (!ff) {
|
|
181
|
+
ff = typeof rule.fileFilter === 'string' ? new RegExp(rule.fileFilter) : rule.fileFilter;
|
|
182
|
+
rule._ffRe = ff;
|
|
183
|
+
}
|
|
184
|
+
if (!ff.test(relPath)) continue;
|
|
185
|
+
}
|
|
186
|
+
// Precompute a mandatory literal once per rule (cached). '' = none extractable.
|
|
187
|
+
if (rule._lit === undefined) rule._lit = requiredLiteral(rule.re.source) || '';
|
|
188
|
+
activeRules.push(rule);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
for (let i = 0; i < lines.length; i++) {
|
|
192
|
+
const line = lines[i];
|
|
193
|
+
if (line.length > 4000) continue; // minified blob line — skip.
|
|
194
|
+
const commented = isCommentLine(line);
|
|
195
|
+
const lineLower = line.toLowerCase();
|
|
196
|
+
for (const rule of activeRules) {
|
|
197
|
+
if (rule.skipComments && commented) continue;
|
|
198
|
+
// Cheap literal prefilter: if the rule's mandatory literal isn't on this
|
|
199
|
+
// line, the regex cannot match — skip it without running the regex.
|
|
200
|
+
if (rule._lit && !lineLower.includes(rule._lit)) continue;
|
|
201
|
+
for (const hit of matchAll(rule.re, line)) {
|
|
202
|
+
if (rule.skipInString && isInsideString(line, hit.index)) continue;
|
|
203
|
+
if (rule.filter && !rule.filter(line, hit, lines)) continue;
|
|
204
|
+
findings.push(
|
|
205
|
+
makeFinding(rule, {
|
|
206
|
+
file: relPath,
|
|
207
|
+
line: i + 1,
|
|
208
|
+
column: hit.index + 1,
|
|
209
|
+
snippet: redactSnippet(line.trim(), hit.match),
|
|
210
|
+
})
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// Dataflow/taint analysis: follows user input across lines to a dangerous sink.
|
|
217
|
+
try {
|
|
218
|
+
const { analyzeTaint } = require('./taint');
|
|
219
|
+
for (const t of analyzeTaint(content, lines, relPath, tree)) findings.push(t);
|
|
220
|
+
} catch (err) {
|
|
221
|
+
noteDegraded('taint', err);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// Python taint analysis for .py files
|
|
225
|
+
if (relPath && relPath.endsWith('.py')) {
|
|
226
|
+
try {
|
|
227
|
+
const { analyzePythonTaint } = require('./engine');
|
|
228
|
+
for (const t of analyzePythonTaint(content, lines, relPath)) findings.push(t);
|
|
229
|
+
} catch (err) {
|
|
230
|
+
noteDegraded('python-taint', err);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// Optional AST pass (acorn) — precise eval/exec/Function/mass-assignment/nosql.
|
|
235
|
+
try {
|
|
236
|
+
const { analyzeAst, AST_SUPERSEDES } = require('./ast');
|
|
237
|
+
const ast = analyzeAst(content, relPath, tree); // tree cached by the caller
|
|
238
|
+
if (ast.parsed) {
|
|
239
|
+
// AST is precise for these categories — drop the regex heuristics for this
|
|
240
|
+
// file so we don't double-report or carry their false positives.
|
|
241
|
+
findings = findings.filter((f) => !AST_SUPERSEDES.has(f.ruleId));
|
|
242
|
+
}
|
|
243
|
+
for (const a of ast.findings) findings.push(a);
|
|
244
|
+
} catch (err) {
|
|
245
|
+
noteDegraded('ast', err);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// File-level rules (need whole-file context).
|
|
249
|
+
for (const rule of fileRules) {
|
|
250
|
+
let hits = [];
|
|
251
|
+
try {
|
|
252
|
+
hits = rule.run(content, lines, relPath) || [];
|
|
253
|
+
} catch (err) {
|
|
254
|
+
noteDegraded(`file-rule:${rule.id || 'unknown'}`, err);
|
|
255
|
+
hits = [];
|
|
256
|
+
}
|
|
257
|
+
for (const h of hits) {
|
|
258
|
+
findings.push(
|
|
259
|
+
makeFinding(rule, {
|
|
260
|
+
file: relPath,
|
|
261
|
+
line: h.line,
|
|
262
|
+
column: h.column || 1,
|
|
263
|
+
snippet: h.snippet,
|
|
264
|
+
message: h.message,
|
|
265
|
+
fix: h.fix,
|
|
266
|
+
severity: h.severity, // per-hit overrides (makeFinding falls back to rule)
|
|
267
|
+
confidence: h.confidence,
|
|
268
|
+
ruleId: h.ruleId,
|
|
269
|
+
title: h.title,
|
|
270
|
+
})
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
return findings;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// Never echo a full secret back into a report/log. Show a masked snippet.
|
|
278
|
+
function redactSnippet(line, match) {
|
|
279
|
+
if (!match || match.length < 8) return truncate(line);
|
|
280
|
+
const masked = match.slice(0, 4) + '****' + match.slice(-2);
|
|
281
|
+
return truncate(line.split(match).join(masked));
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function truncate(s) {
|
|
285
|
+
return s.length > 160 ? s.slice(0, 157) + '...' : s;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// ---------------------------------------------------------------------------
|
|
289
|
+
// Project-level rules
|
|
290
|
+
// ---------------------------------------------------------------------------
|
|
291
|
+
|
|
292
|
+
function projectScan(root) {
|
|
293
|
+
const findings = [];
|
|
294
|
+
const gitignorePath = path.join(root, '.gitignore');
|
|
295
|
+
let gitignore = '';
|
|
296
|
+
try {
|
|
297
|
+
gitignore = fs.readFileSync(gitignorePath, 'utf8');
|
|
298
|
+
} catch {
|
|
299
|
+
gitignore = '';
|
|
300
|
+
}
|
|
301
|
+
const ignoreLines = gitignore
|
|
302
|
+
.split(/\r?\n/)
|
|
303
|
+
.map((l) => l.trim())
|
|
304
|
+
.filter(Boolean);
|
|
305
|
+
|
|
306
|
+
const envIsIgnored = ignoreLines.some((l) =>
|
|
307
|
+
/^\.env($|\.|\/|\*)/.test(l) || l === '.env*' || l === '*.env'
|
|
308
|
+
);
|
|
309
|
+
|
|
310
|
+
// .env present but not gitignored -> critical.
|
|
311
|
+
const envFiles = ['.env', '.env.local', '.env.production', '.env.development'];
|
|
312
|
+
for (const name of envFiles) {
|
|
313
|
+
const p = path.join(root, name);
|
|
314
|
+
if (fs.existsSync(p)) {
|
|
315
|
+
if (!envIsIgnored) {
|
|
316
|
+
findings.push({
|
|
317
|
+
ruleId: 'project.env-not-ignored',
|
|
318
|
+
severity: 'critical',
|
|
319
|
+
title: '.env not in .gitignore',
|
|
320
|
+
message: `${name} exists but is not covered by .gitignore — secrets can be committed and pushed.`,
|
|
321
|
+
fix: `Add ".env" (and ".env.*") to .gitignore, then remove it from git history if already committed (git rm --cached ${name}).`,
|
|
322
|
+
file: name,
|
|
323
|
+
line: 1,
|
|
324
|
+
column: 1,
|
|
325
|
+
snippet: `${name} present, .gitignore does not cover it`,
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// A committed .git folder inside a subpath, or a tracked .env that is world-exposed
|
|
332
|
+
// via a public dir — flag .env living under a web/public/static dir.
|
|
333
|
+
const exposedDirs = ['public', 'static', 'dist', 'www'];
|
|
334
|
+
for (const d of exposedDirs) {
|
|
335
|
+
for (const name of ['.env', '.env.local']) {
|
|
336
|
+
const p = path.join(root, d, name);
|
|
337
|
+
if (fs.existsSync(p)) {
|
|
338
|
+
findings.push({
|
|
339
|
+
ruleId: 'project.env-exposed',
|
|
340
|
+
severity: 'critical',
|
|
341
|
+
title: '.env inside a public directory',
|
|
342
|
+
message: `${d}/${name} sits in a publicly served directory and may be downloadable.`,
|
|
343
|
+
fix: `Move ${name} out of ${d}/ and load config from environment variables.`,
|
|
344
|
+
file: `${d}/${name}`,
|
|
345
|
+
line: 1,
|
|
346
|
+
column: 1,
|
|
347
|
+
snippet: `${d}/${name} is web-served`,
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// package.json present but no lockfile -> non-reproducible installs.
|
|
354
|
+
if (fs.existsSync(path.join(root, 'package.json'))) {
|
|
355
|
+
const lockfiles = ['package-lock.json', 'yarn.lock', 'pnpm-lock.yaml', 'npm-shrinkwrap.json'];
|
|
356
|
+
if (!lockfiles.some((l) => fs.existsSync(path.join(root, l)))) {
|
|
357
|
+
findings.push({
|
|
358
|
+
ruleId: 'package.no-lockfile',
|
|
359
|
+
severity: 'low',
|
|
360
|
+
confidence: 'high',
|
|
361
|
+
title: 'No dependency lockfile',
|
|
362
|
+
message: 'package.json exists but no lockfile is committed — installs are not reproducible and pull the newest matching versions (supply-chain drift).',
|
|
363
|
+
fix: 'Commit a lockfile (package-lock.json / yarn.lock / pnpm-lock.yaml).',
|
|
364
|
+
file: 'package.json',
|
|
365
|
+
line: 1,
|
|
366
|
+
column: 1,
|
|
367
|
+
snippet: 'no lockfile found',
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
return findings;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
// ---------------------------------------------------------------------------
|
|
376
|
+
// Grading
|
|
377
|
+
// ---------------------------------------------------------------------------
|
|
378
|
+
|
|
379
|
+
const TIER = ['critical', 'high', 'medium', 'low'];
|
|
380
|
+
|
|
381
|
+
// Grade off EFFECTIVE severity: low-confidence findings are heuristics ("review
|
|
382
|
+
// this"), so they count one tier lower and low-confidence 'low' is ignored for
|
|
383
|
+
// the grade. This keeps a single heuristic hint from tanking an otherwise clean
|
|
384
|
+
// project, while confirmed issues still drive the grade hard.
|
|
385
|
+
function effectiveSeverity(f) {
|
|
386
|
+
if (f.confidence === 'low') {
|
|
387
|
+
const i = TIER.indexOf(f.severity);
|
|
388
|
+
return TIER[i + 1] || null; // low-confidence 'low' -> null (ignored)
|
|
389
|
+
}
|
|
390
|
+
return f.severity;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
// Minimal glob -> RegExp: supports *, **, and gitignore-style "**/" (zero+ dirs).
|
|
394
|
+
function globToRe(glob) {
|
|
395
|
+
const esc = glob.replace(/[.+^${}()|[\]\\]/g, '\\$&');
|
|
396
|
+
const body = esc.replace(/\*\*\/|\*\*|\*/g, (m) =>
|
|
397
|
+
m === '**/' ? '(?:.*/)?' : m === '**' ? '.*' : '[^/]*'
|
|
398
|
+
);
|
|
399
|
+
return new RegExp('^' + body + '$');
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
// Inline suppression comments:
|
|
403
|
+
// // vibeguard-ignore-line [rule1, rule2] -> suppress on this line
|
|
404
|
+
// // vibeguard-ignore-next-line [rule1, ...] -> suppress on the next line
|
|
405
|
+
// No rule list = suppress everything on the target line. Works with //, #, /* */.
|
|
406
|
+
function buildIgnoreMap(lines) {
|
|
407
|
+
const map = new Map(); // targetLine(1-based) -> Set(ruleIds) | '*'
|
|
408
|
+
const re = /(?:\/\/|#|\/\*|\*)\s*vibeguard-ignore(-next)?-line\b([^*\n]*)/i;
|
|
409
|
+
for (let i = 0; i < lines.length; i++) {
|
|
410
|
+
const m = re.exec(lines[i]);
|
|
411
|
+
if (!m) continue;
|
|
412
|
+
const target = m[1] ? i + 2 : i + 1; // next-line -> i+2 (1-based)
|
|
413
|
+
const rulesPart = (m[2] || '').replace(/\*\/.*/, '').trim();
|
|
414
|
+
const rules = rulesPart
|
|
415
|
+
? rulesPart.split(/[\s,]+/).filter(Boolean)
|
|
416
|
+
: '*';
|
|
417
|
+
const existing = map.get(target);
|
|
418
|
+
if (existing === '*' || rules === '*') map.set(target, '*');
|
|
419
|
+
else map.set(target, new Set([...(existing || []), ...rules]));
|
|
420
|
+
}
|
|
421
|
+
return map;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
function applyInlineSuppressions(findings, contents) {
|
|
425
|
+
const byFile = new Map();
|
|
426
|
+
for (const c of contents) byFile.set(c.rel, buildIgnoreMap(c.lines));
|
|
427
|
+
return findings.filter((f) => {
|
|
428
|
+
const map = byFile.get(f.file);
|
|
429
|
+
if (!map) return true;
|
|
430
|
+
const entry = map.get(f.line);
|
|
431
|
+
if (!entry) return true;
|
|
432
|
+
if (entry === '*') return false;
|
|
433
|
+
return !entry.has(f.ruleId);
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
// Apply project config: drop ignored rules/paths, remap severities.
|
|
438
|
+
// config = { ignoreRules:[ruleId], ignorePaths:[glob], ignore:[{rule,path}],
|
|
439
|
+
// severityOverrides:{ruleId: 'low'} }
|
|
440
|
+
function applyConfigFilters(findings, config) {
|
|
441
|
+
if (!config || typeof config !== 'object') return findings;
|
|
442
|
+
const ignoreRules = new Set(Array.isArray(config.ignoreRules) ? config.ignoreRules : []);
|
|
443
|
+
const ignorePathRes = (Array.isArray(config.ignorePaths) ? config.ignorePaths : []).map(globToRe);
|
|
444
|
+
const pairIgnores = (Array.isArray(config.ignore) ? config.ignore : []).filter(
|
|
445
|
+
(x) => x && typeof x === 'object'
|
|
446
|
+
);
|
|
447
|
+
const sevOverride = config.severityOverrides && typeof config.severityOverrides === 'object'
|
|
448
|
+
? config.severityOverrides
|
|
449
|
+
: {};
|
|
450
|
+
const validSev = new Set(['critical', 'high', 'medium', 'low']);
|
|
451
|
+
|
|
452
|
+
return findings
|
|
453
|
+
.filter((f) => {
|
|
454
|
+
if (ignoreRules.has(f.ruleId)) return false;
|
|
455
|
+
if (ignorePathRes.some((re) => re.test(f.file))) return false;
|
|
456
|
+
for (const pair of pairIgnores) {
|
|
457
|
+
const ruleMatch = !pair.rule || pair.rule === f.ruleId;
|
|
458
|
+
const pathMatch = !pair.path || globToRe(pair.path).test(f.file) || f.file.includes(pair.path);
|
|
459
|
+
if (ruleMatch && pathMatch) return false;
|
|
460
|
+
}
|
|
461
|
+
return true;
|
|
462
|
+
})
|
|
463
|
+
.map((f) => {
|
|
464
|
+
const ov = sevOverride[f.ruleId];
|
|
465
|
+
if (ov && validSev.has(ov)) return { ...f, severity: ov };
|
|
466
|
+
return f;
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
function computeGrade(findings) {
|
|
471
|
+
// Reported counts are the real severities (what the user sees).
|
|
472
|
+
const counts = { critical: 0, high: 0, medium: 0, low: 0 };
|
|
473
|
+
for (const f of findings) counts[f.severity] = (counts[f.severity] || 0) + 1;
|
|
474
|
+
|
|
475
|
+
// Grade counts use effective severity.
|
|
476
|
+
const g = { critical: 0, high: 0, medium: 0, low: 0 };
|
|
477
|
+
for (const f of findings) {
|
|
478
|
+
const s = effectiveSeverity(f);
|
|
479
|
+
if (s) g[s] += 1;
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
let grade;
|
|
483
|
+
if (g.critical > 0) grade = 'F';
|
|
484
|
+
else if (g.high > 0) grade = g.high >= 3 ? 'D' : 'C';
|
|
485
|
+
else if (g.medium > 0) grade = g.medium >= 3 ? 'C' : 'B';
|
|
486
|
+
else if (g.low > 0) grade = 'B';
|
|
487
|
+
else grade = 'A';
|
|
488
|
+
|
|
489
|
+
return { grade, counts };
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
// Collapse noise: on a single file:line, a specific rule supersedes the generic
|
|
493
|
+
// credential catch-all (e.g. a weak session secret shouldn't also report as a
|
|
494
|
+
// bare "hardcoded credential"). Also drop exact duplicate (ruleId+file+line).
|
|
495
|
+
//
|
|
496
|
+
// Additionally: when a dataflow-confirmed taint.* finding and a structural ast.*
|
|
497
|
+
// finding land on the same file:line and describe the same vulnerability category,
|
|
498
|
+
// keep only the taint finding (higher confidence) and drop the ast.* duplicate.
|
|
499
|
+
// Map: taint.command-injection ↔ ast.command-injection, etc.
|
|
500
|
+
const TAINT_TO_AST = {
|
|
501
|
+
'taint.command-injection': 'ast.command-injection',
|
|
502
|
+
'taint.sql-injection': 'ast.sql-injection',
|
|
503
|
+
'taint.code-injection': 'ast.eval-dynamic',
|
|
504
|
+
'taint.path-traversal': 'ast.path-traversal',
|
|
505
|
+
'taint.ssrf': 'ast.ssrf',
|
|
506
|
+
};
|
|
507
|
+
|
|
508
|
+
function dedupeFindings(findings) {
|
|
509
|
+
const linesWithSpecific = new Set();
|
|
510
|
+
for (const f of findings) {
|
|
511
|
+
if (f.ruleId !== 'secret.generic-credential') {
|
|
512
|
+
linesWithSpecific.add(`${f.file}:${f.line}`);
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
// Build a set of taint.* ruleIds that fired, keyed by file:line, so we can
|
|
517
|
+
// drop the corresponding ast.* finding on the same file:line.
|
|
518
|
+
const taintKeys = new Set();
|
|
519
|
+
for (const f of findings) {
|
|
520
|
+
if (TAINT_TO_AST[f.ruleId]) {
|
|
521
|
+
taintKeys.add(`${f.file}:${f.line}:${TAINT_TO_AST[f.ruleId]}`);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
const seen = new Set();
|
|
526
|
+
const out = [];
|
|
527
|
+
for (const f of findings) {
|
|
528
|
+
if (
|
|
529
|
+
f.ruleId === 'secret.generic-credential' &&
|
|
530
|
+
linesWithSpecific.has(`${f.file}:${f.line}`)
|
|
531
|
+
) {
|
|
532
|
+
continue; // superseded by a more specific rule
|
|
533
|
+
}
|
|
534
|
+
// Drop ast.* finding when a taint.* finding already covers the same file:line.
|
|
535
|
+
const astCounterpart = Object.entries(TAINT_TO_AST).find(([, ast]) => ast === f.ruleId);
|
|
536
|
+
if (astCounterpart && taintKeys.has(`${f.file}:${f.line}:${f.ruleId}`)) {
|
|
537
|
+
continue; // superseded by the dataflow-confirmed taint finding
|
|
538
|
+
}
|
|
539
|
+
const key = `${f.ruleId}:${f.file}:${f.line}:${f.column}`;
|
|
540
|
+
if (seen.has(key)) continue;
|
|
541
|
+
seen.add(key);
|
|
542
|
+
out.push(f);
|
|
543
|
+
}
|
|
544
|
+
return out;
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
function sortFindings(findings) {
|
|
548
|
+
return findings.slice().sort((a, b) => {
|
|
549
|
+
const s = SEVERITY_ORDER[a.severity] - SEVERITY_ORDER[b.severity];
|
|
550
|
+
if (s !== 0) return s;
|
|
551
|
+
if (a.file !== b.file) return a.file < b.file ? -1 : 1;
|
|
552
|
+
return a.line - b.line;
|
|
553
|
+
});
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
/**
|
|
557
|
+
* Scan a directory. Returns a structured result object.
|
|
558
|
+
* @param {string} dir
|
|
559
|
+
* @param {object} [opts]
|
|
560
|
+
*/
|
|
561
|
+
function scan(dir, opts = {}) {
|
|
562
|
+
const root = path.resolve(dir);
|
|
563
|
+
// Apply project config (extends the redaction allowlist). Reset each scan so
|
|
564
|
+
// config never leaks between runs (important for tests / MCP long sessions).
|
|
565
|
+
const config = loadConfig(root);
|
|
566
|
+
rules.configure(config);
|
|
567
|
+
|
|
568
|
+
const allFiles = walk(root, [], null);
|
|
569
|
+
|
|
570
|
+
// Incremental mode: only scan files whose content changed since the last scan
|
|
571
|
+
// (SHA-256 hash cache under .vibeguard/cache). First run has an empty cache so
|
|
572
|
+
// everything is "changed" (full scan, populates cache); later runs are near
|
|
573
|
+
// instant. Cross-file analysis needs ALL files, so it is skipped here — this
|
|
574
|
+
// mode is per-file (line/AST/taint), intended for pre-commit / watch loops.
|
|
575
|
+
let files = allFiles;
|
|
576
|
+
let incremental = null;
|
|
577
|
+
let staged = null;
|
|
578
|
+
if (opts.staged) {
|
|
579
|
+
// Git-aware: scan only files staged for the current commit. Perfect for a
|
|
580
|
+
// pre-commit hook — fast and scoped to exactly what's being committed.
|
|
581
|
+
const stagedSet = getStagedFiles(root);
|
|
582
|
+
if (stagedSet) {
|
|
583
|
+
files = allFiles.filter((abs) => stagedSet.has(path.resolve(abs)));
|
|
584
|
+
staged = { total: allFiles.length, scanned: files.length };
|
|
585
|
+
}
|
|
586
|
+
} else if (opts.changed) {
|
|
587
|
+
const { getChangedFiles } = require('./engine');
|
|
588
|
+
const res = getChangedFiles(root, allFiles);
|
|
589
|
+
files = res.changed;
|
|
590
|
+
incremental = { total: res.total, scanned: res.changed.length, cached: res.cached };
|
|
591
|
+
}
|
|
592
|
+
// Both modes scan a partial file set → cross-file analysis (which needs the
|
|
593
|
+
// whole project) must be skipped and reported as such.
|
|
594
|
+
const partialScan = !!incremental || !!staged;
|
|
595
|
+
|
|
596
|
+
let findings = [];
|
|
597
|
+
const contents = []; // {rel, content, lines} for cross-file analysis
|
|
598
|
+
const trees = new Map(); // rel -> parsed AST (one parse per file, shared)
|
|
599
|
+
const astMod = require('./ast');
|
|
600
|
+
|
|
601
|
+
// Engine mode + coverage diagnostics. `astAvailable` is false when the acorn
|
|
602
|
+
// optional deps aren't installed → precise eval/exec/taint/mass-assignment
|
|
603
|
+
// passes silently downgrade to regex. We surface that instead of hiding it.
|
|
604
|
+
const astAvailable = astMod.isAvailable();
|
|
605
|
+
const diag = { degraded: [] };
|
|
606
|
+
const parseFailed = []; // JS/TS files acorn IS installed for but couldn't parse
|
|
607
|
+
|
|
608
|
+
for (const abs of files) {
|
|
609
|
+
let buf;
|
|
610
|
+
try {
|
|
611
|
+
buf = fs.readFileSync(abs);
|
|
612
|
+
} catch {
|
|
613
|
+
continue;
|
|
614
|
+
}
|
|
615
|
+
if (looksBinary(buf)) continue;
|
|
616
|
+
const rel = path.relative(root, abs).split(path.sep).join('/');
|
|
617
|
+
const content = buf.toString('utf8');
|
|
618
|
+
// Parse JS/TS once here; the AST pass, interprocedural, and cross-file all reuse it.
|
|
619
|
+
let tree = null;
|
|
620
|
+
if (/\.(?:js|jsx|ts|tsx|mjs|cjs)$/.test(rel)) {
|
|
621
|
+
try {
|
|
622
|
+
tree = astMod.parseSource(content, rel);
|
|
623
|
+
} catch {
|
|
624
|
+
tree = null;
|
|
625
|
+
}
|
|
626
|
+
if (tree) trees.set(rel, tree);
|
|
627
|
+
// acorn installed but this file didn't parse → AST rules skipped for it.
|
|
628
|
+
// (When acorn is absent entirely that's engine-mode, reported separately.)
|
|
629
|
+
else if (astAvailable) parseFailed.push(rel);
|
|
630
|
+
}
|
|
631
|
+
findings = findings.concat(scanFileContent(abs, rel, content, tree, diag));
|
|
632
|
+
contents.push({ rel, content, lines: content.split(/\r?\n/) });
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
// Cross-file passes need the WHOLE project in `contents`. In a partial scan
|
|
636
|
+
// (--changed / --staged) we only loaded a subset, so skip them (they would
|
|
637
|
+
// produce wrong results from a partial view) — the documented tradeoff.
|
|
638
|
+
if (!partialScan) {
|
|
639
|
+
// Cross-file taint: request data flowing across module boundaries into a sink.
|
|
640
|
+
try {
|
|
641
|
+
const { analyzeCrossFile } = require('./crosstaint');
|
|
642
|
+
for (const cf of analyzeCrossFile(contents, trees)) findings.push(cf);
|
|
643
|
+
} catch {
|
|
644
|
+
/* cross-file taint is optional (needs acorn) */
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
// Cross-file rules see every file at once (e.g. Supabase RLS across migrations).
|
|
648
|
+
for (const rule of crossFileRules) {
|
|
649
|
+
let hits = [];
|
|
650
|
+
try {
|
|
651
|
+
hits = rule.run(contents) || [];
|
|
652
|
+
} catch {
|
|
653
|
+
hits = [];
|
|
654
|
+
}
|
|
655
|
+
for (const h of hits) {
|
|
656
|
+
findings.push(
|
|
657
|
+
makeFinding(rule, {
|
|
658
|
+
file: h.file,
|
|
659
|
+
line: h.line,
|
|
660
|
+
column: h.column || 1,
|
|
661
|
+
snippet: h.snippet,
|
|
662
|
+
message: h.message,
|
|
663
|
+
fix: h.fix,
|
|
664
|
+
})
|
|
665
|
+
);
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
} // end !incremental cross-file block
|
|
669
|
+
|
|
670
|
+
findings = findings.concat(projectScan(root));
|
|
671
|
+
|
|
672
|
+
// Dependency vulnerabilities (opt-in; the CLI enables it by default, unit
|
|
673
|
+
// tests leave it off so they stay offline and deterministic).
|
|
674
|
+
let depsInfo = null;
|
|
675
|
+
if (opts.deps) {
|
|
676
|
+
try {
|
|
677
|
+
const { scanDependencies } = require('./deps');
|
|
678
|
+
const dep = scanDependencies(root);
|
|
679
|
+
findings = findings.concat(dep.findings);
|
|
680
|
+
depsInfo = { ran: dep.ran, notes: dep.notes };
|
|
681
|
+
} catch (err) {
|
|
682
|
+
depsInfo = { ran: { npm: false, pip: false }, notes: [String(err && err.message)] };
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
// Deep mode: fold in external best-in-class scanners if present (opt-in).
|
|
687
|
+
let externalInfo = null;
|
|
688
|
+
if (opts.deep) {
|
|
689
|
+
try {
|
|
690
|
+
const { runExternal } = require('./external');
|
|
691
|
+
const ext = runExternal(root);
|
|
692
|
+
findings = findings.concat(ext.findings);
|
|
693
|
+
externalInfo = { ran: ext.ran, notes: ext.notes };
|
|
694
|
+
} catch (err) {
|
|
695
|
+
externalInfo = { ran: {}, notes: [String(err && err.message)] };
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
// --no-suppress (opts.noSuppress): report everything, ignoring inline
|
|
700
|
+
// `vibeguard-ignore` comments and the heuristic FP filter. Config policy
|
|
701
|
+
// (ignoreRules/ignorePaths) is deliberate, versioned, and kept. This gives CI
|
|
702
|
+
// a trust mode that a careless/hostile inline comment in a PR cannot silence.
|
|
703
|
+
let suppressedFindings = [];
|
|
704
|
+
if (!opts.noSuppress) {
|
|
705
|
+
findings = applyInlineSuppressions(findings, contents);
|
|
706
|
+
const { applyInlineSuppressionsWithCount } = require('./suppress');
|
|
707
|
+
const supResult = applyInlineSuppressionsWithCount(findings, contents);
|
|
708
|
+
findings = supResult.kept;
|
|
709
|
+
suppressedFindings = supResult.suppressed;
|
|
710
|
+
}
|
|
711
|
+
findings = applyConfigFilters(findings, config);
|
|
712
|
+
|
|
713
|
+
// Merge plugin rules
|
|
714
|
+
try {
|
|
715
|
+
const { loadPlugins, getPluginRules } = require('./plugin');
|
|
716
|
+
loadPlugins(root, config);
|
|
717
|
+
const pluginRules = getPluginRules();
|
|
718
|
+
if (pluginRules.length > 0) {
|
|
719
|
+
const { matchAll, makeFinding } = require('./rules');
|
|
720
|
+
for (const [rel, content] of Object.entries(contents)) {
|
|
721
|
+
const lines = content.split(/\r?\n/);
|
|
722
|
+
for (let i = 0; i < lines.length; i++) {
|
|
723
|
+
const line = lines[i];
|
|
724
|
+
const commented = /^\s*(\/\/|#|--|\/\*)/.test(line);
|
|
725
|
+
for (const rule of pluginRules) {
|
|
726
|
+
if (rule.skipComments && commented) continue;
|
|
727
|
+
for (const hit of matchAll(rule.re, line)) {
|
|
728
|
+
findings.push(
|
|
729
|
+
makeFinding(rule, {
|
|
730
|
+
file: rel,
|
|
731
|
+
line: i + 1,
|
|
732
|
+
column: hit.index + 1,
|
|
733
|
+
snippet: line.trim().slice(0, 200),
|
|
734
|
+
})
|
|
735
|
+
);
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
} catch {
|
|
742
|
+
/* plugins are best-effort */
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
findings = dedupeFindings(findings);
|
|
746
|
+
|
|
747
|
+
// Context-aware false positive suppression (skipped under --no-suppress).
|
|
748
|
+
if (!opts.noSuppress) {
|
|
749
|
+
try {
|
|
750
|
+
const { suppressFindings } = require('./suppress');
|
|
751
|
+
findings = findings.filter((f) => {
|
|
752
|
+
const matching = contents.find((c) => c.rel === f.file);
|
|
753
|
+
if (matching) {
|
|
754
|
+
return suppressFindings([f], matching.content, f.file).length > 0;
|
|
755
|
+
}
|
|
756
|
+
return true;
|
|
757
|
+
});
|
|
758
|
+
} catch { /* suppression is best-effort */ }
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
findings = sortFindings(findings);
|
|
762
|
+
for (const f of findings) f.fingerprint = fingerprintOf(f);
|
|
763
|
+
|
|
764
|
+
const { grade, counts } = computeGrade(findings);
|
|
765
|
+
|
|
766
|
+
// Coverage transparency: engine mode + every pass that failed open. Callers
|
|
767
|
+
// (CLI, MCP, CI) can warn the user that a scan ran degraded rather than let a
|
|
768
|
+
// silent parse/taint failure masquerade as a clean result.
|
|
769
|
+
const engine = {
|
|
770
|
+
astAvailable,
|
|
771
|
+
mode: astAvailable ? 'ast' : 'regex-only',
|
|
772
|
+
};
|
|
773
|
+
const diagnostics = {
|
|
774
|
+
degradedPasses: diag.degraded,
|
|
775
|
+
parseFailedFiles: parseFailed,
|
|
776
|
+
degradedFileCount: new Set([...diag.degraded.map((d) => d.file), ...parseFailed]).size,
|
|
777
|
+
};
|
|
778
|
+
|
|
779
|
+
return {
|
|
780
|
+
root,
|
|
781
|
+
scannedFiles: files.length,
|
|
782
|
+
findings,
|
|
783
|
+
suppressedFindings,
|
|
784
|
+
suppressedCount: suppressedFindings.length,
|
|
785
|
+
grade,
|
|
786
|
+
counts,
|
|
787
|
+
depsInfo,
|
|
788
|
+
externalInfo,
|
|
789
|
+
engine,
|
|
790
|
+
diagnostics,
|
|
791
|
+
incremental, // null on a full scan; {total, scanned, cached} with --changed
|
|
792
|
+
staged, // null unless --staged; {total, scanned}
|
|
793
|
+
generatedAt: new Date().toISOString(),
|
|
794
|
+
};
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
module.exports = {
|
|
798
|
+
scan,
|
|
799
|
+
scanFileContent,
|
|
800
|
+
computeGrade,
|
|
801
|
+
sortFindings,
|
|
802
|
+
walk,
|
|
803
|
+
shouldScanFile,
|
|
804
|
+
SKIP_DIRS,
|
|
805
|
+
};
|