-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtest_output.py
More file actions
816 lines (673 loc) · 29.8 KB
/
test_output.py
File metadata and controls
816 lines (673 loc) · 29.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
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
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
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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
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
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
592
593
594
595
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
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
753
754
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
809
810
811
812
813
814
815
816
import pytest
from socketsecurity.output import OutputHandler
from socketsecurity.core.classes import Diff, Issue
import json
class TestOutputHandler:
@pytest.fixture
def handler(self):
from socketsecurity.config import CliConfig
from unittest.mock import Mock
config = Mock(spec=CliConfig)
config.disable_blocking = False
config.strict_blocking = False
config.sarif_file = None
config.sarif_reachability = "all"
config.sarif_scope = "diff"
config.sarif_grouping = "instance"
config.sarif_reachability = "all"
config.sbom_file = None
return OutputHandler(config, Mock())
def test_report_pass_with_blocking_issues(self, handler):
diff = Diff()
diff.new_alerts = [Issue(error=True)]
assert not handler.report_pass(diff)
def test_report_pass_with_blocking_disabled(self):
from socketsecurity.config import CliConfig
from unittest.mock import Mock
config = Mock(spec=CliConfig)
config.disable_blocking = True
config.strict_blocking = False
handler = OutputHandler(config, Mock())
diff = Diff()
diff.new_alerts = [Issue(error=True)]
assert handler.report_pass(diff)
def test_json_output_format(self, handler, caplog):
import logging
diff = Diff()
diff.id = "test-scan-id"
diff.diff_url = "https://socket.dev/test"
test_issue = Issue(
title="Test",
severity="high",
description="Test description",
error=True,
key="test-key",
type="test-type",
pkg_type="npm",
pkg_name="test-package",
pkg_version="1.0.0",
purl="pkg:npm/test-package@1.0.0"
)
diff.new_alerts = [test_issue]
with caplog.at_level(logging.INFO, logger="socketcli"):
handler.output_console_json(diff)
output = json.loads(caplog.messages[-1])
assert output["new_alerts"][0]["title"] == "Test"
assert output["new_alerts"][0]["severity"] == "high"
assert output["new_alerts"][0]["error"] is True
assert output["new_alerts"][0]["description"] == "Test description"
def test_json_output_includes_unchanged_alerts_with_strict_blocking(self, caplog):
import logging
from socketsecurity.config import CliConfig
from unittest.mock import Mock
config = Mock(spec=CliConfig)
config.disable_blocking = False
config.strict_blocking = True
config.sbom_file = None
handler = OutputHandler(config, Mock())
diff = Diff()
diff.id = "test-scan-id"
diff.diff_url = "https://socket.dev/test"
diff.new_alerts = [
Issue(
title="New",
severity="high",
description="new",
error=True,
key="new-key",
type="test-type",
pkg_type="npm",
pkg_name="new-package",
pkg_version="1.0.0",
purl="pkg:npm/new-package@1.0.0",
)
]
diff.unchanged_alerts = [
Issue(
title="Existing",
severity="high",
description="existing",
error=True,
key="existing-key",
type="test-type",
pkg_type="npm",
pkg_name="existing-package",
pkg_version="1.0.0",
purl="pkg:npm/existing-package@1.0.0",
)
]
with caplog.at_level(logging.INFO, logger="socketcli"):
handler.output_console_json(diff)
output = json.loads(caplog.messages[-1])
assert len(output["new_alerts"]) == 2
titles = {a["title"] for a in output["new_alerts"]}
assert titles == {"New", "Existing"}
def test_sbom_file_saving(self, handler, tmp_path):
# Test SBOM file is created correctly
diff = Diff()
diff.sbom = {"test": "data"}
sbom_path = tmp_path / "test.json"
handler.save_sbom_file(diff, str(sbom_path))
assert sbom_path.exists()
def test_report_pass_with_strict_blocking_new_alerts(self):
"""Test that strict-blocking fails on new blocking alerts"""
from socketsecurity.config import CliConfig
from unittest.mock import Mock
# Create config with strict_blocking
config = Mock(spec=CliConfig)
config.disable_blocking = False
config.strict_blocking = True
handler = OutputHandler(config, Mock())
diff = Diff()
diff.new_alerts = [Issue(error=True, warn=False)]
diff.unchanged_alerts = []
assert not handler.report_pass(diff)
def test_report_pass_with_strict_blocking_unchanged_alerts(self):
"""Test that strict-blocking fails on unchanged blocking alerts"""
from socketsecurity.config import CliConfig
from unittest.mock import Mock
config = Mock(spec=CliConfig)
config.disable_blocking = False
config.strict_blocking = True
handler = OutputHandler(config, Mock())
diff = Diff()
diff.new_alerts = []
diff.unchanged_alerts = [Issue(error=True, warn=False)]
assert not handler.report_pass(diff)
def test_report_pass_with_strict_blocking_both_alerts(self):
"""Test that strict-blocking fails when both new and unchanged alerts exist"""
from socketsecurity.config import CliConfig
from unittest.mock import Mock
config = Mock(spec=CliConfig)
config.disable_blocking = False
config.strict_blocking = True
handler = OutputHandler(config, Mock())
diff = Diff()
diff.new_alerts = [Issue(error=True, warn=False)]
diff.unchanged_alerts = [Issue(error=True, warn=False)]
assert not handler.report_pass(diff)
def test_report_pass_with_strict_blocking_only_warnings(self):
"""Test that strict-blocking passes when only warnings (not errors) exist"""
from socketsecurity.config import CliConfig
from unittest.mock import Mock
config = Mock(spec=CliConfig)
config.disable_blocking = False
config.strict_blocking = True
handler = OutputHandler(config, Mock())
diff = Diff()
diff.new_alerts = [Issue(error=False, warn=True)]
diff.unchanged_alerts = [Issue(error=False, warn=True)]
assert handler.report_pass(diff)
def test_report_pass_strict_blocking_disabled(self):
"""Test that strict-blocking without the flag passes with unchanged alerts"""
from socketsecurity.config import CliConfig
from unittest.mock import Mock
config = Mock(spec=CliConfig)
config.disable_blocking = False
config.strict_blocking = False # Flag not set
handler = OutputHandler(config, Mock())
diff = Diff()
diff.new_alerts = []
diff.unchanged_alerts = [Issue(error=True, warn=False)]
# Should pass because strict_blocking is False
assert handler.report_pass(diff)
def test_disable_blocking_overrides_strict_blocking(self):
"""Test that disable-blocking takes precedence over strict-blocking"""
from socketsecurity.config import CliConfig
from unittest.mock import Mock
config = Mock(spec=CliConfig)
config.disable_blocking = True
config.strict_blocking = True
handler = OutputHandler(config, Mock())
diff = Diff()
diff.new_alerts = [Issue(error=True, warn=False)]
diff.unchanged_alerts = [Issue(error=True, warn=False)]
# Should pass because disable_blocking takes precedence
assert handler.report_pass(diff)
def test_sarif_file_output(self, tmp_path):
"""Test that --sarif-file writes SARIF report to a file"""
from socketsecurity.config import CliConfig
from unittest.mock import Mock
sarif_path = tmp_path / "report.sarif"
config = Mock(spec=CliConfig)
config.sarif_file = str(sarif_path)
config.sarif_scope = "diff"
config.sbom_file = None
handler = OutputHandler(config, Mock())
diff = Diff()
diff.id = "test-scan-id"
diff.new_alerts = [Issue(
pkg_name="test-package",
pkg_version="1.0.0",
severity="high",
title="Test Vulnerability",
description="Test description",
type="malware",
url="https://socket.dev/test",
manifests="package.json",
pkg_type="npm",
key="test-key",
purl="pkg:npm/test-package@1.0.0",
error=True,
)]
handler.output_console_sarif(diff)
assert sarif_path.exists()
with open(sarif_path) as f:
sarif_data = json.load(f)
assert sarif_data["version"] == "2.1.0"
def test_sarif_reachability_reachable_filters_non_reachable(self, tmp_path):
"""Test that --sarif-reachability reachable uses .socket.facts.json reachability."""
from socketsecurity.config import CliConfig
from unittest.mock import Mock
sarif_path = tmp_path / "report.sarif"
facts_path = tmp_path / ".socket.facts.json"
config = Mock(spec=CliConfig)
config.sarif_file = str(sarif_path)
config.sarif_reachability = "reachable"
config.sarif_scope = "diff"
config.sbom_file = None
config.target_path = str(tmp_path)
config.reach_output_file = ".socket.facts.json"
handler = OutputHandler(config, Mock())
facts_path.write_text(json.dumps({
"components": [
{
"type": "npm",
"name": "reachable-pkg",
"version": "1.0.0",
"vulnerabilities": [{"ghsaId": "GHSA-AAAA-BBBB-CCCC", "severity": "HIGH"}],
"reachability": [{
"ghsa_id": "GHSA-AAAA-BBBB-CCCC",
"reachability": [{"type": "reachable"}]
}]
},
{
"type": "npm",
"name": "unreachable-pkg",
"version": "1.0.0",
"vulnerabilities": [{"ghsaId": "GHSA-DDDD-EEEE-FFFF", "severity": "HIGH"}],
"reachability": [{
"ghsa_id": "GHSA-DDDD-EEEE-FFFF",
"reachability": [{"type": "unreachable"}]
}]
}
]
}))
def make_issue(name, error, ghsa_id):
return Issue(
pkg_name=name,
pkg_version="1.0.0",
severity="high",
title=f"Vuln in {name}",
description="test",
type="vulnerability",
manifests="package.json",
pkg_type="npm",
key=f"key-{name}",
purl=f"pkg:npm/{name}@1.0.0",
error=error,
props={"ghsaId": ghsa_id},
)
diff = Diff()
diff.id = "test-scan-id"
diff.new_alerts = [
make_issue("reachable-pkg", error=False, ghsa_id="GHSA-AAAA-BBBB-CCCC"),
make_issue("unreachable-pkg", error=True, ghsa_id="GHSA-DDDD-EEEE-FFFF"),
]
handler.output_console_sarif(diff)
with open(sarif_path) as f:
sarif_data = json.load(f)
rule_ids = [r["ruleId"] for r in sarif_data["runs"][0]["results"]]
assert any("reachable-pkg" in r for r in rule_ids)
assert not any("unreachable-pkg" in r for r in rule_ids)
def test_sarif_reachability_reachable_falls_back_to_blocking_when_facts_missing(self, tmp_path):
"""Test that missing facts file falls back to historical blocking filter."""
from socketsecurity.config import CliConfig
from unittest.mock import Mock
sarif_path = tmp_path / "report.sarif"
config = Mock(spec=CliConfig)
config.sarif_file = str(sarif_path)
config.sarif_reachability = "reachable"
config.sarif_scope = "diff"
config.sbom_file = None
config.target_path = str(tmp_path)
config.reach_output_file = ".socket.facts.json"
handler = OutputHandler(config, Mock())
diff = Diff()
diff.id = "test-scan-id"
diff.new_alerts = [
Issue(pkg_name="blocking-pkg", pkg_version="1.0.0", severity="high",
title="Vuln", description="test", type="vulnerability",
manifests="package.json", pkg_type="npm", key="k1",
purl="pkg:npm/blocking-pkg@1.0.0", error=True),
Issue(pkg_name="warn-pkg", pkg_version="1.0.0", severity="low",
title="Vuln", description="test", type="vulnerability",
manifests="package.json", pkg_type="npm", key="k2",
purl="pkg:npm/warn-pkg@1.0.0", error=False),
]
handler.output_console_sarif(diff)
with open(sarif_path) as f:
sarif_data = json.load(f)
rule_ids = [r["ruleId"] for r in sarif_data["runs"][0]["results"]]
assert any("blocking-pkg" in r for r in rule_ids)
assert not any("warn-pkg" in r for r in rule_ids)
def test_sarif_output_includes_unchanged_with_strict_blocking(self, tmp_path):
"""Strict blocking should include unchanged alerts in diff-scope SARIF output."""
from socketsecurity.config import CliConfig
from unittest.mock import Mock
sarif_path_strict_false = tmp_path / "strict-false.sarif"
sarif_path_strict_true = tmp_path / "strict-true.sarif"
def build_handler(strict_blocking, output_path):
config = Mock(spec=CliConfig)
config.sarif_file = str(output_path)
config.sarif_reachability = "all"
config.sarif_scope = "diff"
config.sbom_file = None
config.strict_blocking = strict_blocking
config.target_path = str(tmp_path)
config.reach_output_file = ".socket.facts.json"
return OutputHandler(config, Mock())
def build_diff():
diff = Diff()
diff.id = "test-scan-id"
diff.new_alerts = [
Issue(pkg_name="pkg-a", pkg_version="1.0.0", severity="high",
title="Vuln A", description="test", type="vulnerability",
manifests="package.json", pkg_type="npm", key="a",
purl="pkg:npm/pkg-a@1.0.0", error=True),
]
diff.unchanged_alerts = [
Issue(pkg_name="pkg-old", pkg_version="1.0.0", severity="high",
title="Old Vuln", description="test", type="vulnerability",
manifests="package.json", pkg_type="npm", key="old",
purl="pkg:npm/pkg-old@1.0.0", error=True),
]
return diff
handler_false = build_handler(False, sarif_path_strict_false)
handler_true = build_handler(True, sarif_path_strict_true)
handler_false.output_console_sarif(build_diff())
handler_true.output_console_sarif(build_diff())
with open(sarif_path_strict_false) as f:
sarif_false = json.load(f)
with open(sarif_path_strict_true) as f:
sarif_true = json.load(f)
false_rule_ids = [r["ruleId"] for r in sarif_false["runs"][0]["results"]]
true_rule_ids = [r["ruleId"] for r in sarif_true["runs"][0]["results"]]
assert any("pkg-a" in r for r in false_rule_ids)
assert not any("pkg-old" in r for r in false_rule_ids)
assert any("pkg-a" in r for r in true_rule_ids)
assert any("pkg-old" in r for r in true_rule_ids)
def test_sarif_reachability_all_includes_all(self, tmp_path):
"""Test that --sarif-reachability all includes all alerts."""
from socketsecurity.config import CliConfig
from unittest.mock import Mock
sarif_path = tmp_path / "report.sarif"
config = Mock(spec=CliConfig)
config.sarif_file = str(sarif_path)
config.sarif_reachability = "all"
config.sarif_scope = "diff"
config.sbom_file = None
handler = OutputHandler(config, Mock())
diff = Diff()
diff.id = "test-scan-id"
diff.new_alerts = [
Issue(pkg_name="blocking-pkg", pkg_version="1.0.0", severity="high",
title="Vuln", description="test", type="vulnerability",
manifests="package.json", pkg_type="npm", key="k1",
purl="pkg:npm/blocking-pkg@1.0.0", error=True),
Issue(pkg_name="non-blocking-pkg", pkg_version="1.0.0", severity="low",
title="Vuln", description="test", type="vulnerability",
manifests="package.json", pkg_type="npm", key="k2",
purl="pkg:npm/non-blocking-pkg@1.0.0", error=False),
]
handler.output_console_sarif(diff)
with open(sarif_path) as f:
sarif_data = json.load(f)
rule_ids = [r["ruleId"] for r in sarif_data["runs"][0]["results"]]
assert any("blocking-pkg" in r for r in rule_ids)
assert any("non-blocking-pkg" in r for r in rule_ids)
assert "$schema" in sarif_data
assert len(sarif_data["runs"]) == 1
def test_sarif_no_file_when_not_configured(self, tmp_path):
"""Test that no file is written when --sarif-file is not set"""
from socketsecurity.config import CliConfig
from unittest.mock import Mock
config = Mock(spec=CliConfig)
config.sarif_file = None
config.sarif_scope = "diff"
config.sbom_file = None
handler = OutputHandler(config, Mock())
diff = Diff()
diff.id = "test-scan-id"
diff.new_alerts = []
handler.output_console_sarif(diff)
# No files should be created in tmp_path
assert list(tmp_path.iterdir()) == []
def test_sarif_file_nested_directory(self, tmp_path):
"""Test that --sarif-file creates parent directories if needed"""
from socketsecurity.config import CliConfig
from unittest.mock import Mock
sarif_path = tmp_path / "nested" / "dir" / "report.sarif"
config = Mock(spec=CliConfig)
config.sarif_file = str(sarif_path)
config.sarif_scope = "diff"
config.sbom_file = None
handler = OutputHandler(config, Mock())
diff = Diff()
diff.id = "test-scan-id"
diff.new_alerts = []
handler.output_console_sarif(diff)
assert sarif_path.exists()
with open(sarif_path) as f:
sarif_data = json.load(f)
assert sarif_data["version"] == "2.1.0"
def test_sarif_scope_full_before_after_reachable_filtering_snapshot(self, tmp_path):
"""Full-scope SARIF should show before/after changes with reachable-only filtering."""
from socketsecurity.config import CliConfig
from unittest.mock import Mock
facts_path = tmp_path / ".socket.facts.json"
all_path = tmp_path / "full-all.sarif"
reachable_path = tmp_path / "full-reachable.sarif"
facts_path.write_text(json.dumps({
"components": [
{
"type": "npm",
"name": "pkg-reach",
"version": "1.0.0",
"manifestFiles": ["package.json"],
"vulnerabilities": [{"ghsaId": "GHSA-1111-2222-3333", "severity": "HIGH"}],
"reachability": [{
"ghsa_id": "GHSA-1111-2222-3333",
"reachability": [{"type": "reachable"}]
}]
},
{
"type": "npm",
"name": "pkg-unreach",
"version": "2.0.0",
"manifestFiles": ["package-lock.json"],
"vulnerabilities": [{"ghsaId": "GHSA-4444-5555-6666", "severity": "HIGH"}],
"reachability": [{
"ghsa_id": "GHSA-4444-5555-6666",
"reachability": [{"type": "unreachable"}]
}]
}
]
}))
def build_handler(output_path, reachable_only):
config = Mock(spec=CliConfig)
config.sarif_file = str(output_path)
config.sarif_reachability = "reachable" if reachable_only else "all"
config.sarif_scope = "full"
config.sbom_file = None
config.target_path = str(tmp_path)
config.reach_output_file = ".socket.facts.json"
return OutputHandler(config, Mock())
diff = Diff()
diff.id = "test-scan-id"
diff.new_alerts = [] # Full scope should not depend on diff alerts
handler_all = build_handler(all_path, reachable_only=False)
handler_all.output_console_sarif(diff)
with open(all_path) as f:
sarif_all = json.load(f)
handler_reachable = build_handler(reachable_path, reachable_only=True)
handler_reachable.output_console_sarif(diff)
with open(reachable_path) as f:
sarif_reachable = json.load(f)
all_results = sarif_all["runs"][0]["results"]
reachable_results = sarif_reachable["runs"][0]["results"]
# Before: includes reachable + unreachable
assert len(all_results) == 2
# After applying reachable-only: only reachable remains
assert len(reachable_results) == 1
assert reachable_results[0]["properties"]["reachability"] == "reachable"
def test_sarif_scope_full_works_when_diff_not_run(self, tmp_path):
"""Full scope should still emit SARIF when diff id is NO_DIFF_RAN."""
from socketsecurity.config import CliConfig
from unittest.mock import Mock
facts_path = tmp_path / ".socket.facts.json"
out_path = tmp_path / "full-no-diff.sarif"
facts_path.write_text(json.dumps({
"components": [
{
"type": "npm",
"name": "pkg-reach",
"version": "1.0.0",
"manifestFiles": ["package.json"],
"vulnerabilities": [{"ghsaId": "GHSA-1111-2222-3333", "severity": "HIGH"}],
"reachability": [{
"ghsa_id": "GHSA-1111-2222-3333",
"reachability": [{"type": "reachable"}]
}]
}
]
}))
config = Mock(spec=CliConfig)
config.sarif_file = str(out_path)
config.sarif_reachability = "reachable"
config.sarif_scope = "full"
config.sbom_file = None
config.target_path = str(tmp_path)
config.reach_output_file = ".socket.facts.json"
handler = OutputHandler(config, Mock())
diff = Diff()
diff.id = "NO_DIFF_RAN"
diff.new_alerts = []
handler.output_console_sarif(diff)
with open(out_path) as f:
sarif = json.load(f)
assert len(sarif["runs"][0]["results"]) == 1
def test_sarif_scope_full_dedupes_duplicate_manifest_uris(self, tmp_path):
"""Full scope should not emit duplicate results for duplicate manifest entries."""
from socketsecurity.config import CliConfig
from unittest.mock import Mock
facts_path = tmp_path / ".socket.facts.json"
out_path = tmp_path / "full-dedup.sarif"
facts_path.write_text(json.dumps({
"components": [
{
"type": "npm",
"name": "pkg-reach",
"version": "1.0.0",
"manifestFiles": ["package.json", "package.json"],
"vulnerabilities": [{"ghsaId": "GHSA-1111-2222-3333", "severity": "HIGH"}],
"reachability": [{
"ghsa_id": "GHSA-1111-2222-3333",
"reachability": [{"type": "reachable"}]
}]
}
]
}))
config = Mock(spec=CliConfig)
config.sarif_file = str(out_path)
config.sarif_reachability = "reachable"
config.sarif_scope = "full"
config.sbom_file = None
config.target_path = str(tmp_path)
config.reach_output_file = ".socket.facts.json"
handler = OutputHandler(config, Mock())
diff = Diff(id="snapshot", new_alerts=[])
handler.output_console_sarif(diff)
with open(out_path) as f:
sarif = json.load(f)
assert len(sarif["runs"][0]["results"]) == 1
def test_sarif_scope_full_with_sarif_file_suppresses_stdout(self, tmp_path, capsys):
"""Full scope + --sarif-file should avoid printing massive SARIF JSON to stdout."""
from socketsecurity.config import CliConfig
from unittest.mock import Mock
facts_path = tmp_path / ".socket.facts.json"
out_path = tmp_path / "full-suppressed.sarif"
facts_path.write_text(json.dumps({
"components": [
{
"type": "npm",
"name": "pkg-reach",
"version": "1.0.0",
"manifestFiles": ["package.json"],
"vulnerabilities": [{"ghsaId": "GHSA-1111-2222-3333", "severity": "HIGH"}],
"reachability": [{
"ghsa_id": "GHSA-1111-2222-3333",
"reachability": [{"type": "reachable"}]
}]
}
]
}))
config = Mock(spec=CliConfig)
config.sarif_file = str(out_path)
config.sarif_reachability = "reachable"
config.sarif_scope = "full"
config.sbom_file = None
config.target_path = str(tmp_path)
config.reach_output_file = ".socket.facts.json"
handler = OutputHandler(config, Mock())
diff = Diff(id="snapshot", new_alerts=[])
handler.output_console_sarif(diff)
captured = capsys.readouterr()
assert captured.out == ""
assert out_path.exists()
def test_sarif_scope_full_alert_grouping_dedupes_versions(self, tmp_path):
from socketsecurity.config import CliConfig
from unittest.mock import Mock
out_path = tmp_path / "full-alert-grouping.sarif"
facts_path = tmp_path / ".socket.facts.json"
facts_path.write_text(json.dumps({
"components": [
{
"type": "npm",
"name": "tmp",
"version": "0.1.0",
"manifestFiles": [{"path": "package-lock.json"}],
"vulnerabilities": [{"ghsaId": "GHSA-x", "range": "<0.2.4", "severity": "high"}],
"reachability": [{"ghsa_id": "GHSA-x", "reachability": [{"type": "reachable"}]}],
},
{
"type": "npm",
"name": "tmp",
"version": "0.0.24",
"manifestFiles": [{"path": "package-lock.json"}],
"vulnerabilities": [{"ghsaId": "GHSA-x", "range": "<0.2.4", "severity": "high"}],
"reachability": [{"ghsa_id": "GHSA-x", "reachability": [{"type": "reachable"}]}],
},
]
}))
config = Mock(spec=CliConfig)
config.sarif_file = str(out_path)
config.sarif_reachability = "all"
config.sarif_scope = "full"
config.sarif_grouping = "alert"
config.sarif_reachability = "reachable"
config.sbom_file = None
config.target_path = str(tmp_path)
config.reach_output_file = ".socket.facts.json"
handler = OutputHandler(config, Mock())
diff = Diff(id="snapshot", new_alerts=[])
handler.output_console_sarif(diff)
with open(out_path) as f:
sarif = json.load(f)
results = sarif["runs"][0]["results"]
assert len(results) == 1
props = results[0]["properties"]
assert sorted(props["versions"]) == ["0.0.24", "0.1.0"]
assert props["reachability"] == "reachable"
def test_sarif_scope_full_potentially_filter(self, tmp_path):
from socketsecurity.config import CliConfig
from unittest.mock import Mock
out_path = tmp_path / "full-potentially.sarif"
facts_path = tmp_path / ".socket.facts.json"
facts_path.write_text(json.dumps({
"components": [
{
"type": "npm",
"name": "alpha",
"version": "1.0.0",
"manifestFiles": [{"path": "package-lock.json"}],
"vulnerabilities": [{"ghsaId": "GHSA-reach", "range": "<2.0.0", "severity": "high"}],
"reachability": [{"ghsa_id": "GHSA-reach", "reachability": [{"type": "reachable"}]}],
},
{
"type": "npm",
"name": "beta",
"version": "1.0.0",
"manifestFiles": [{"path": "package-lock.json"}],
"vulnerabilities": [{"ghsaId": "GHSA-unknown", "range": "<2.0.0", "severity": "high"}],
"reachability": [{"ghsa_id": "GHSA-unknown", "reachability": [{"type": "unknown"}]}],
},
]
}))
config = Mock(spec=CliConfig)
config.sarif_file = str(out_path)
config.sarif_reachability = "all"
config.sarif_scope = "full"
config.sarif_grouping = "instance"
config.sarif_reachability = "potentially"
config.sbom_file = None
config.target_path = str(tmp_path)
config.reach_output_file = ".socket.facts.json"
handler = OutputHandler(config, Mock())
diff = Diff(id="snapshot", new_alerts=[])
handler.output_console_sarif(diff)
with open(out_path) as f:
sarif = json.load(f)
results = sarif["runs"][0]["results"]
assert len(results) == 1
assert results[0]["ruleId"].startswith("beta==1.0.0")