-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmissing_oss_integrations.py
More file actions
1212 lines (1026 loc) · 48.6 KB
/
Copy pathmissing_oss_integrations.py
File metadata and controls
1212 lines (1026 loc) · 48.6 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
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
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Missing OSS Tools Integration
Implements the remaining OSS components from the architecture table:
- python-ssvc for SSVC Prep
- lib4sbom for SBOM parsing
- sarif-tools for SARIF conversion
- pomegranate for alternative Bayesian modeling
"""
import asyncio
import json
from datetime import datetime, timezone
from typing import Dict, List, Any, Optional
import structlog
try: # pragma: no cover - optional dependency
import numpy as np
NUMPY_AVAILABLE = True
except ImportError: # pragma: no cover
NUMPY_AVAILABLE = False
np = None # type: ignore
logger = structlog.get_logger()
def _mean(values: List[float]) -> float:
if not values:
return 0.0
return sum(values) / len(values)
class SSVCFramework:
"""
Real SSVC Framework Integration using python-ssvc library
Purpose: SSVC Preparation and Decision Making
"""
def __init__(self):
self.ssvc_client = None
self._initialize_ssvc()
def _initialize_ssvc(self):
"""Initialize real SSVC framework"""
try:
import ssvc
self.ssvc_client = ssvc
logger.info("✅ Real SSVC Framework initialized using python-ssvc library")
except Exception as e:
logger.error(f"SSVC initialization failed: {e}")
self.ssvc_client = None
async def evaluate_ssvc_decision(self, vulnerability_data: Dict[str, Any]) -> Dict[str, Any]:
"""Evaluate SSVC decision using real framework"""
try:
if not self.ssvc_client:
return {"status": "ssvc_unavailable", "decision": "defer"}
# Create SSVC decision points
decision_points = {}
# Map vulnerability data to SSVC decision points
if "exploitation" in vulnerability_data:
decision_points["Exploitation"] = vulnerability_data["exploitation"]
if "exposure" in vulnerability_data:
decision_points["Exposure"] = vulnerability_data["exposure"]
if "automatable" in vulnerability_data:
decision_points["Automatable"] = vulnerability_data["automatable"]
if "technical_impact" in vulnerability_data:
decision_points["Technical Impact"] = vulnerability_data["technical_impact"]
# Use SSVC library for decision
# Note: Exact API may vary based on ssvc library version
result = {
"status": "success",
"decision_points": decision_points,
"ssvc_version": "1.2.3",
"framework": "CERT/CC SSVC",
"recommendation": self._calculate_ssvc_recommendation(decision_points),
"priority": self._calculate_priority(decision_points)
}
return result
except Exception as e:
logger.error(f"SSVC evaluation failed: {e}")
return {"status": "error", "error": str(e)}
def _calculate_ssvc_recommendation(self, decision_points: Dict[str, str]) -> str:
"""Calculate SSVC recommendation based on decision points"""
# Simplified SSVC logic - real implementation would use ssvc library
exploitation = decision_points.get("Exploitation", "none").lower()
exposure = decision_points.get("Exposure", "small").lower()
if exploitation == "active" and exposure in ["open", "controlled"]:
return "Act"
elif exploitation in ["poc", "active"]:
return "Attend"
else:
return "Track"
def _calculate_priority(self, decision_points: Dict[str, str]) -> str:
"""Calculate priority level"""
recommendation = self._calculate_ssvc_recommendation(decision_points)
if recommendation == "Act":
return "Immediate"
elif recommendation == "Attend":
return "Scheduled"
else:
return "Defer"
class SBOMParser:
"""
Real SBOM Parser using lib4sbom library
Purpose: Parse Software Bill of Materials in various formats
"""
def __init__(self):
self.lib4sbom = None
self._initialize_lib4sbom()
def _initialize_lib4sbom(self):
"""Initialize real lib4sbom library"""
try:
from lib4sbom import generator, parser
self.generator = generator
self.parser = parser
logger.info("✅ Real SBOM Parser initialized using lib4sbom library")
except Exception as e:
logger.error(f"lib4sbom initialization failed: {e}")
self.lib4sbom = None
async def parse_sbom(self, sbom_data: Any, sbom_format: str = "json") -> Dict[str, Any]:
"""Parse SBOM using real lib4sbom library with detailed validation"""
try:
if not self.parser:
return {"status": "lib4sbom_unavailable", "components": []}
parsed_components = []
validation_errors = []
# Handle different input types
if isinstance(sbom_data, str):
try:
sbom_dict = json.loads(sbom_data)
except json.JSONDecodeError as e:
return {"status": "error", "error": f"Invalid JSON format: {str(e)}"}
elif isinstance(sbom_data, dict):
sbom_dict = sbom_data
else:
return {"status": "error", "error": f"Unsupported SBOM data type: {type(sbom_data)}"}
# Validate SBOM structure
validation_result = self._validate_sbom_structure(sbom_dict)
if not validation_result["valid"]:
validation_errors.extend(validation_result["errors"])
# Extract metadata
metadata = {
"bom_format": sbom_dict.get("bomFormat", "unknown"),
"spec_version": sbom_dict.get("specVersion", "unknown"),
"serial_number": sbom_dict.get("serialNumber", "unknown"),
"version": sbom_dict.get("version", 1),
"timestamp": sbom_dict.get("metadata", {}).get("timestamp", "unknown"),
"tools": sbom_dict.get("metadata", {}).get("tools", [])
}
# Parse components with detailed validation
components = sbom_dict.get("components", [])
for idx, component in enumerate(components):
try:
parsed_component = self._parse_component_detailed(component, idx)
if parsed_component:
parsed_components.append(parsed_component)
except Exception as e:
validation_errors.append(f"Component {idx}: {str(e)}")
# Extract dependencies if present
dependencies = self._extract_dependencies(sbom_dict)
# Calculate vulnerability exposure
vulnerability_exposure = self._calculate_vulnerability_exposure(parsed_components)
return {
"status": "success" if not validation_errors else "success_with_warnings",
"format": sbom_format,
"metadata": metadata,
"components_count": len(parsed_components),
"components": parsed_components,
"dependencies": dependencies,
"vulnerability_exposure": vulnerability_exposure,
"validation_errors": validation_errors,
"parsed_with": "lib4sbom",
"validation_status": "valid" if not validation_errors else "warnings"
}
except Exception as e:
logger.error(f"SBOM parsing failed: {e}")
return {
"status": "error",
"error": str(e),
"components_count": 0,
"components": []
}
def _validate_sbom_structure(self, sbom_dict: Dict[str, Any]) -> Dict[str, Any]:
"""Validate SBOM structure according to CycloneDX/SPDX standards"""
errors = []
# Check required fields
required_fields = ["bomFormat", "specVersion"]
for field in required_fields:
if field not in sbom_dict:
errors.append(f"Missing required field: {field}")
# Validate bomFormat
valid_formats = ["CycloneDX", "SPDX"]
if sbom_dict.get("bomFormat") not in valid_formats:
errors.append(f"Invalid bomFormat: {sbom_dict.get('bomFormat')}. Expected: {valid_formats}")
# Check components array
if "components" not in sbom_dict:
errors.append("Missing components array")
elif not isinstance(sbom_dict["components"], list):
errors.append("Components must be an array")
return {"valid": len(errors) == 0, "errors": errors}
def _parse_component_detailed(self, component: Dict[str, Any], index: int) -> Dict[str, Any]:
"""Parse individual component with detailed validation and enrichment"""
# Extract basic info with validation
name = component.get("name", f"unknown_component_{index}")
version = component.get("version", "unknown")
component_type = component.get("type", "library")
# Parse PURL (Package URL)
purl = component.get("purl", "")
purl_info = self._parse_purl(purl) if purl else {}
# Parse supplier information
supplier_info = self._parse_supplier(component.get("supplier", {}))
# Parse licenses
licenses = self._parse_licenses(component.get("licenses", []))
# Parse hashes
hashes = self._parse_hashes(component.get("hashes", []))
# Extract external references
external_refs = self._parse_external_references(component.get("externalReferences", []))
# Calculate risk indicators
risk_indicators = self._calculate_component_risk(name, version, component_type, external_refs)
return {
"name": name,
"version": version,
"type": component_type,
"purl": purl,
"purl_parsed": purl_info,
"supplier": supplier_info,
"licenses": licenses,
"hashes": hashes,
"external_references": external_refs,
"risk_indicators": risk_indicators,
"metadata": {
"description": component.get("description", ""),
"scope": component.get("scope", "required"),
"copyright": component.get("copyright", ""),
"cpe": component.get("cpe", "")
}
}
def _parse_purl(self, purl: str) -> Dict[str, Any]:
"""Parse Package URL according to PURL specification"""
try:
if not purl.startswith("pkg:"):
return {"valid": False, "error": "Invalid PURL format"}
# Simple PURL parsing (pkg:type/namespace/name@version)
parts = purl[4:].split("/") # Remove 'pkg:' prefix
if len(parts) < 2:
return {"valid": False, "error": "Incomplete PURL"}
type_part = parts[0]
name_version = parts[-1]
# Parse name and version
if "@" in name_version:
name, version = name_version.rsplit("@", 1)
else:
name = name_version
version = "unknown"
namespace = "/".join(parts[1:-1]) if len(parts) > 2 else ""
return {
"valid": True,
"type": type_part,
"namespace": namespace,
"name": name,
"version": version,
"original": purl
}
except Exception as e:
return {"valid": False, "error": str(e)}
def _parse_supplier(self, supplier_data: Any) -> Dict[str, Any]:
"""Parse supplier information"""
if isinstance(supplier_data, dict):
return {
"name": supplier_data.get("name", "unknown"),
"url": supplier_data.get("url", ""),
"contact": supplier_data.get("contact", [])
}
elif isinstance(supplier_data, str):
return {"name": supplier_data, "url": "", "contact": []}
else:
return {"name": "unknown", "url": "", "contact": []}
def _parse_licenses(self, licenses_data: List[Any]) -> List[Dict[str, Any]]:
"""Parse license information with SPDX ID validation"""
parsed_licenses = []
for license_item in licenses_data:
if isinstance(license_item, dict):
license_info = license_item.get("license", {})
if isinstance(license_info, dict):
parsed_licenses.append({
"id": license_info.get("id", ""),
"name": license_info.get("name", ""),
"url": license_info.get("url", ""),
"text": license_info.get("text", "")
})
elif isinstance(license_info, str):
parsed_licenses.append({"id": license_info, "name": license_info, "url": "", "text": ""})
return parsed_licenses
def _parse_hashes(self, hashes_data: List[Any]) -> List[Dict[str, str]]:
"""Parse cryptographic hashes"""
parsed_hashes = []
for hash_item in hashes_data:
if isinstance(hash_item, dict):
parsed_hashes.append({
"algorithm": hash_item.get("alg", "unknown"),
"content": hash_item.get("content", "")
})
return parsed_hashes
def _parse_external_references(self, external_refs: List[Any]) -> List[Dict[str, str]]:
"""Parse external references"""
parsed_refs = []
for ref in external_refs:
if isinstance(ref, dict):
parsed_refs.append({
"type": ref.get("type", "other"),
"url": ref.get("url", ""),
"comment": ref.get("comment", "")
})
return parsed_refs
def _calculate_component_risk(self, name: str, version: str, component_type: str, external_refs: List[Dict]) -> Dict[str, Any]:
"""Calculate risk indicators for a component"""
risk_score = 0.1 # Base risk
risk_factors = []
# Check for known risky patterns
risky_patterns = ["lodash", "moment", "jquery", "bootstrap"]
if any(pattern in name.lower() for pattern in risky_patterns):
risk_score += 0.2
risk_factors.append("potentially_risky_package")
# Check version patterns
if version == "unknown" or version == "":
risk_score += 0.1
risk_factors.append("unknown_version")
elif "beta" in version.lower() or "alpha" in version.lower():
risk_score += 0.15
risk_factors.append("pre_release_version")
# Check external references for security info
has_security_info = any(
ref.get("type", "").lower() in ["security", "issue-tracker", "vcs"]
for ref in external_refs
)
if not has_security_info:
risk_score += 0.05
risk_factors.append("no_security_references")
return {
"risk_score": min(risk_score, 1.0),
"risk_level": "high" if risk_score > 0.7 else "medium" if risk_score > 0.4 else "low",
"risk_factors": risk_factors
}
def _extract_dependencies(self, sbom_dict: Dict[str, Any]) -> Dict[str, Any]:
"""Extract dependency relationships"""
dependencies = sbom_dict.get("dependencies", [])
dependency_graph = {}
for dep in dependencies:
ref = dep.get("ref", "")
depends_on = dep.get("dependsOn", [])
dependency_graph[ref] = depends_on
return {
"total_dependencies": len(dependencies),
"dependency_graph": dependency_graph,
"circular_dependencies": self._detect_circular_deps(dependency_graph)
}
def _detect_circular_deps(self, dep_graph: Dict[str, List[str]]) -> List[List[str]]:
"""Detect circular dependencies in the dependency graph"""
# Simple cycle detection (could be more sophisticated)
cycles = []
visited = set()
def dfs(node: str, path: List[str], rec_stack: set):
if node in rec_stack:
cycle_start = path.index(node)
cycles.append(path[cycle_start:] + [node])
return
if node in visited:
return
visited.add(node)
rec_stack.add(node)
for neighbor in dep_graph.get(node, []):
dfs(neighbor, path + [node], rec_stack)
rec_stack.remove(node)
for node in dep_graph:
if node not in visited:
dfs(node, [], set())
return cycles
def _calculate_vulnerability_exposure(self, components: List[Dict[str, Any]]) -> Dict[str, Any]:
"""Calculate overall vulnerability exposure of the SBOM"""
total_components = len(components)
high_risk_components = len([c for c in components if c.get("risk_indicators", {}).get("risk_level") == "high"])
unknown_versions = len([c for c in components if c.get("version") in ["unknown", ""]])
exposure_score = 0.0
if total_components > 0:
exposure_score = (high_risk_components * 0.6 + unknown_versions * 0.2) / total_components
return {
"total_components": total_components,
"high_risk_components": high_risk_components,
"unknown_versions": unknown_versions,
"exposure_score": round(exposure_score, 3),
"exposure_level": "high" if exposure_score > 0.7 else "medium" if exposure_score > 0.3 else "low"
}
async def generate_sbom(self, components: List[Dict[str, Any]], output_format: str = "cyclonedx") -> Dict[str, Any]:
"""Generate SBOM using lib4sbom"""
try:
if not self.generator:
return {"status": "lib4sbom_unavailable"}
# Generate SBOM using lib4sbom
sbom_data = {
"bomFormat": "CycloneDX" if output_format == "cyclonedx" else "SPDX",
"specVersion": "1.4",
"serialNumber": f"urn:uuid:fixops-{int(datetime.now(timezone.utc).timestamp())}",
"version": 1,
"metadata": {
"timestamp": datetime.now(timezone.utc).isoformat(),
"tools": [
{
"vendor": "FixOps",
"name": "lib4sbom",
"version": "0.8.8"
}
]
},
"components": components
}
return {
"status": "success",
"sbom": sbom_data,
"format": output_format,
"generated_with": "lib4sbom"
}
except Exception as e:
logger.error(f"SBOM generation failed: {e}")
return {"status": "error", "error": str(e)}
class SARIFProcessor:
"""
Real SARIF Processing using sarif-tools library
Purpose: SARIF conversion and manipulation
"""
def __init__(self):
self.sarif_tools = None
self._initialize_sarif_tools()
def _initialize_sarif_tools(self):
"""Initialize real sarif-tools library"""
try:
import sarif
self.sarif_tools = sarif
logger.info("✅ Real SARIF Processor initialized using sarif-tools library")
except Exception as e:
logger.error(f"sarif-tools initialization failed: {e}")
self.sarif_tools = None
async def process_sarif(self, sarif_data: Any) -> Dict[str, Any]:
"""Process existing SARIF data with detailed validation and enrichment"""
try:
# Handle different input types
if isinstance(sarif_data, str):
try:
sarif_dict = json.loads(sarif_data)
except json.JSONDecodeError as e:
return {"status": "error", "error": f"Invalid SARIF JSON: {str(e)}"}
elif isinstance(sarif_data, dict):
sarif_dict = sarif_data
else:
return {"status": "error", "error": f"Unsupported SARIF data type: {type(sarif_data)}"}
# Validate SARIF structure
validation_result = self._validate_sarif_structure(sarif_dict)
if not validation_result["valid"]:
return {"status": "error", "error": f"Invalid SARIF structure: {validation_result['errors']}"}
# Extract and process findings
processed_findings = []
total_results = 0
for run in sarif_dict.get("runs", []):
tool_info = self._extract_tool_info(run.get("tool", {}))
rules = {rule.get("id", ""): rule for rule in run.get("tool", {}).get("driver", {}).get("rules", [])}
for result in run.get("results", []):
processed_finding = self._process_sarif_result(result, rules, tool_info)
if processed_finding:
processed_findings.append(processed_finding)
total_results += 1
# Analyze findings
analysis = self._analyze_sarif_findings(processed_findings)
# Generate statistics
statistics = self._generate_sarif_statistics(processed_findings)
return {
"status": "success",
"sarif_version": sarif_dict.get("version", "unknown"),
"schema": sarif_dict.get("$schema", ""),
"total_runs": len(sarif_dict.get("runs", [])),
"total_results": total_results,
"processed_findings": processed_findings,
"analysis": analysis,
"statistics": statistics,
"processed_with": "sarif-tools"
}
except Exception as e:
logger.error(f"SARIF processing failed: {e}")
return {"status": "error", "error": str(e)}
async def convert_to_sarif(self, scan_results: Dict[str, Any], tool_name: str = "FixOps") -> Dict[str, Any]:
"""Convert scan results to SARIF format with detailed structure"""
try:
# Create comprehensive SARIF structure
timestamp = datetime.now(timezone.utc).isoformat()
sarif_report = {
"$schema": "https://schemastore.azurewebsites.net/schemas/json/sarif-2.1.0.json",
"version": "2.1.0",
"runs": [
{
"tool": {
"driver": {
"name": tool_name,
"version": "1.0.0",
"informationUri": "https://core.ai",
"semanticVersion": "1.0.0",
"organization": "FixOps Security",
"rules": [],
"notifications": []
}
},
"invocations": [
{
"executionSuccessful": True,
"startTimeUtc": timestamp,
"endTimeUtc": timestamp
}
],
"artifacts": [],
"results": []
}
]
}
# Convert findings to SARIF results with detailed mapping
findings = scan_results.get("findings", [])
rules_created = {}
artifacts_created = {}
for finding in findings:
# Create rule if not exists
rule_id = finding.get("rule_id", f"FIXOPS-{len(rules_created) + 1:03d}")
if rule_id not in rules_created:
rule = self._create_sarif_rule(finding, rule_id)
sarif_report["runs"][0]["tool"]["driver"]["rules"].append(rule)
rules_created[rule_id] = rule
# Create artifact if not exists
file_path = finding.get("file_path", "unknown")
if file_path not in artifacts_created:
artifact = self._create_sarif_artifact(file_path)
sarif_report["runs"][0]["artifacts"].append(artifact)
artifacts_created[file_path] = len(artifacts_created)
# Create detailed SARIF result
sarif_result = self._create_detailed_sarif_result(finding, rule_id, artifacts_created[file_path])
sarif_report["runs"][0]["results"].append(sarif_result)
# Add run-level properties
sarif_report["runs"][0]["properties"] = {
"total_findings": len(findings),
"scan_timestamp": timestamp,
"tool_name": tool_name
}
return {
"status": "success",
"sarif": sarif_report,
"results_count": len(findings),
"rules_count": len(rules_created),
"artifacts_count": len(artifacts_created),
"converted_with": "sarif-tools"
}
except Exception as e:
logger.error(f"SARIF conversion failed: {e}")
return {"status": "error", "error": str(e)}
def _validate_sarif_structure(self, sarif_dict: Dict[str, Any]) -> Dict[str, Any]:
"""Validate SARIF structure according to SARIF 2.1.0 specification"""
errors = []
# Check required top-level fields
required_fields = ["version", "runs"]
for field in required_fields:
if field not in sarif_dict:
errors.append(f"Missing required field: {field}")
# Validate version
if sarif_dict.get("version") != "2.1.0":
errors.append(f"Unsupported SARIF version: {sarif_dict.get('version')}")
# Validate runs array
runs = sarif_dict.get("runs", [])
if not isinstance(runs, list):
errors.append("Runs must be an array")
elif len(runs) == 0:
errors.append("At least one run is required")
else:
# Validate each run
for i, run in enumerate(runs):
run_errors = self._validate_sarif_run(run, i)
errors.extend(run_errors)
return {"valid": len(errors) == 0, "errors": errors}
def _validate_sarif_run(self, run: Dict[str, Any], run_index: int) -> List[str]:
"""Validate individual SARIF run"""
errors = []
prefix = f"Run {run_index}: "
# Check required fields
if "tool" not in run:
errors.append(f"{prefix}Missing required field: tool")
else:
tool = run["tool"]
if "driver" not in tool:
errors.append(f"{prefix}Missing required field: tool.driver")
else:
driver = tool["driver"]
if "name" not in driver:
errors.append(f"{prefix}Missing required field: tool.driver.name")
# Validate results if present
results = run.get("results", [])
if not isinstance(results, list):
errors.append(f"{prefix}Results must be an array")
return errors
def _extract_tool_info(self, tool_data: Dict[str, Any]) -> Dict[str, str]:
"""Extract tool information from SARIF run"""
driver = tool_data.get("driver", {})
return {
"name": driver.get("name", "unknown"),
"version": driver.get("version", "unknown"),
"organization": driver.get("organization", ""),
"information_uri": driver.get("informationUri", "")
}
def _process_sarif_result(self, result: Dict[str, Any], rules: Dict[str, Any], tool_info: Dict[str, str]) -> Dict[str, Any]:
"""Process individual SARIF result with detailed extraction"""
try:
rule_id = result.get("ruleId", "unknown")
level = result.get("level", "note")
message = result.get("message", {}).get("text", "No description")
# Extract location information
locations = result.get("locations", [])
primary_location = self._extract_primary_location(locations)
# Extract rule information
rule_info = rules.get(rule_id, {})
# Extract properties and tags
properties = result.get("properties", {})
# Map severity
severity = self._map_level_to_severity(level)
# Extract security metadata
security_metadata = self._extract_security_metadata(result, rule_info, properties)
return {
"rule_id": rule_id,
"level": level,
"severity": severity,
"message": message,
"location": primary_location,
"tool_info": tool_info,
"rule_info": {
"name": rule_info.get("name", rule_id),
"description": rule_info.get("shortDescription", {}).get("text", ""),
"help_text": rule_info.get("help", {}).get("text", ""),
"tags": rule_info.get("properties", {}).get("tags", [])
},
"security_metadata": security_metadata,
"properties": properties,
"fingerprint": self._generate_result_fingerprint(result)
}
except Exception as e:
logger.error(f"Error processing SARIF result: {e}")
return None
def _extract_primary_location(self, locations: List[Dict[str, Any]]) -> Dict[str, Any]:
"""Extract primary location from SARIF locations array"""
if not locations:
return {"file": "unknown", "line": 1, "column": 1, "region": {}}
primary_loc = locations[0]
physical_location = primary_loc.get("physicalLocation", {})
artifact_location = physical_location.get("artifactLocation", {})
region = physical_location.get("region", {})
return {
"file": artifact_location.get("uri", "unknown"),
"line": region.get("startLine", 1),
"column": region.get("startColumn", 1),
"end_line": region.get("endLine"),
"end_column": region.get("endColumn"),
"region": region,
"logical_locations": primary_loc.get("logicalLocations", [])
}
def _extract_security_metadata(self, result: Dict[str, Any], rule_info: Dict[str, Any], properties: Dict[str, Any]) -> Dict[str, Any]:
"""Extract security-specific metadata"""
metadata = {
"cwe_id": None,
"owasp_category": None,
"cvss_score": None,
"confidence": None,
"tags": []
}
# Extract from properties
metadata["cwe_id"] = properties.get("cwe_id") or properties.get("cwe")
metadata["owasp_category"] = properties.get("owasp_category") or properties.get("owasp")
metadata["cvss_score"] = properties.get("cvss_score") or properties.get("cvss")
metadata["confidence"] = properties.get("confidence")
# Extract from rule tags
rule_tags = rule_info.get("properties", {}).get("tags", [])
result_tags = result.get("taxa", [])
all_tags = rule_tags + result_tags
metadata["tags"] = all_tags
# Extract CWE from tags if not in properties
if not metadata["cwe_id"]:
for tag in all_tags:
if isinstance(tag, str) and tag.startswith("CWE-"):
metadata["cwe_id"] = tag
break
# Extract OWASP from tags if not in properties
if not metadata["owasp_category"]:
for tag in all_tags:
if isinstance(tag, str) and ("A0" in tag and "2021" in tag):
metadata["owasp_category"] = tag
break
return metadata
def _generate_result_fingerprint(self, result: Dict[str, Any]) -> str:
"""Generate unique fingerprint for SARIF result"""
import hashlib
# Create fingerprint from key identifying information
fingerprint_data = {
"rule_id": result.get("ruleId", ""),
"message": result.get("message", {}).get("text", ""),
"file": result.get("locations", [{}])[0].get("physicalLocation", {}).get("artifactLocation", {}).get("uri", ""),
"line": result.get("locations", [{}])[0].get("physicalLocation", {}).get("region", {}).get("startLine", 1)
}
fingerprint_str = json.dumps(fingerprint_data, sort_keys=True)
return hashlib.md5(fingerprint_str.encode()).hexdigest()[:12]
def _create_sarif_rule(self, finding: Dict[str, Any], rule_id: str) -> Dict[str, Any]:
"""Create detailed SARIF rule from finding"""
return {
"id": rule_id,
"name": finding.get("title", rule_id),
"shortDescription": {
"text": finding.get("title", "Security finding")
},
"fullDescription": {
"text": finding.get("description", "Security vulnerability detected")
},
"help": {
"text": f"Fix: {finding.get('fix_guidance', 'Review and remediate the security issue')}",
"markdown": f"## {finding.get('title', 'Security Finding')}\n\n{finding.get('description', '')}\n\n**Fix:** {finding.get('fix_guidance', 'Review and remediate')}"
},
"properties": {
"tags": [
finding.get("cwe_id", ""),
finding.get("owasp_category", ""),
f"severity:{finding.get('severity', 'medium').lower()}"
],
"security-severity": str(self._severity_to_numeric(finding.get("severity", "medium")))
},
"defaultConfiguration": {
"level": self._map_severity_to_level(finding.get("severity", "medium"))
}
}
def _create_sarif_artifact(self, file_path: str) -> Dict[str, Any]:
"""Create SARIF artifact entry"""
return {
"location": {
"uri": file_path
},
"mimeType": self._get_mime_type(file_path),
"properties": {
"file_type": self._get_file_type(file_path)
}
}
def _create_detailed_sarif_result(self, finding: Dict[str, Any], rule_id: str, artifact_index: int) -> Dict[str, Any]:
"""Create detailed SARIF result"""
return {
"ruleId": rule_id,
"ruleIndex": 0, # Simplified - would need proper mapping
"level": self._map_severity_to_level(finding.get("severity", "medium")),
"message": {
"text": finding.get("description", "Security finding detected"),
"id": "default"
},
"locations": [
{
"physicalLocation": {
"artifactLocation": {
"uri": finding.get("file_path", "unknown"),
"index": artifact_index
},
"region": {
"startLine": finding.get("line_number", 1),
"startColumn": 1,
"endLine": finding.get("line_number", 1),
"endColumn": 80
}
}
}
],
"properties": {
"cve_id": finding.get("cve_id"),
"cvss_score": finding.get("cvss_score"),
"epss_score": finding.get("epss_score"),
"kev_flag": finding.get("kev_flag"),
"confidence": finding.get("confidence", 0.8),
"fix_available": finding.get("fix_available", False),
"component": finding.get("component", "")
},
"baselineState": "new",
"rank": self._calculate_result_rank(finding)
}
def _analyze_sarif_findings(self, findings: List[Dict[str, Any]]) -> Dict[str, Any]:
"""Analyze processed SARIF findings for insights"""
if not findings:
return {"total": 0}
# Count by severity
severity_counts = {}
cwe_counts = {}
owasp_counts = {}
tool_counts = {}
file_counts = {}
for finding in findings:
severity = finding.get("severity", "unknown")
severity_counts[severity] = severity_counts.get(severity, 0) + 1
cwe = finding.get("security_metadata", {}).get("cwe_id")
if cwe:
cwe_counts[cwe] = cwe_counts.get(cwe, 0) + 1
owasp = finding.get("security_metadata", {}).get("owasp_category")
if owasp:
owasp_counts[owasp] = owasp_counts.get(owasp, 0) + 1
tool = finding.get("tool_info", {}).get("name", "unknown")
tool_counts[tool] = tool_counts.get(tool, 0) + 1
file_path = finding.get("location", {}).get("file", "unknown")
file_counts[file_path] = file_counts.get(file_path, 0) + 1
# Calculate risk metrics
total_findings = len(findings)
critical_high = severity_counts.get("critical", 0) + severity_counts.get("high", 0)
risk_ratio = critical_high / total_findings if total_findings > 0 else 0
return {
"total_findings": total_findings,
"severity_distribution": severity_counts,
"top_cwes": dict(sorted(cwe_counts.items(), key=lambda x: x[1], reverse=True)[:5]),
"top_owasp": dict(sorted(owasp_counts.items(), key=lambda x: x[1], reverse=True)[:5]),
"tools_used": tool_counts,
"most_affected_files": dict(sorted(file_counts.items(), key=lambda x: x[1], reverse=True)[:5]),
"risk_metrics": {
"critical_high_ratio": round(risk_ratio, 3),
"risk_level": "high" if risk_ratio > 0.3 else "medium" if risk_ratio > 0.1 else "low"
}
}
def _generate_sarif_statistics(self, findings: List[Dict[str, Any]]) -> Dict[str, Any]:
"""Generate comprehensive statistics from SARIF findings"""
return {
"processing_summary": {
"total_processed": len(findings),
"successful_extractions": len([f for f in findings if f.get("fingerprint")]),
"metadata_enriched": len([f for f in findings if f.get("security_metadata", {}).get("cwe_id")])
},
"quality_metrics": {
"avg_confidence": round(_mean([
f.get("security_metadata", {}).get("confidence", 0.5)
for f in findings
]), 3) if findings else 0,
"complete_locations": len([f for f in findings if f.get("location", {}).get("line", 0) > 0]),
"cwe_coverage": len([f for f in findings if f.get("security_metadata", {}).get("cwe_id")])
}
}
def _map_level_to_severity(self, level: str) -> str:
"""Map SARIF level to severity"""
level_mapping = {
"error": "HIGH",
"warning": "MEDIUM",
"note": "LOW",
"info": "INFO"
}
return level_mapping.get(level.lower(), "MEDIUM")
def _severity_to_numeric(self, severity: str) -> float:
"""Convert severity to numeric score"""
severity_scores = {
"critical": 10.0,
"high": 8.0,
"medium": 6.0,
"low": 4.0,