|
| 1 | +import json |
| 2 | +from datetime import datetime |
| 3 | + |
| 4 | + |
| 5 | +def sarif_to_gitlab(sarif_file, output_file): |
| 6 | + # Load SARIF data |
| 7 | + with open(sarif_file, 'r') as file: |
| 8 | + sarif_data = json.load(file) |
| 9 | + |
| 10 | + # Initialize GitLab report structure |
| 11 | + gitlab_report = { |
| 12 | + "version": "15.0.0", |
| 13 | + "vulnerabilities": [], |
| 14 | + "remediations": [], |
| 15 | + "scan": { |
| 16 | + "scanner": { |
| 17 | + "id": "custom_sarif_import", |
| 18 | + "name": "Custom SARIF Importer", |
| 19 | + "version": "1.0", |
| 20 | + "vendor": { |
| 21 | + "name": "Custom Vendor" # Updated to be an object |
| 22 | + } |
| 23 | + }, |
| 24 | + "analyzer": { |
| 25 | + "name": "Custom Analyzer", |
| 26 | + "version": "1.0", |
| 27 | + "id": "custom_analyzer", # Example ID, adjust as needed |
| 28 | + "vendor": { |
| 29 | + "name": "Custom Analyzer Vendor" # Example vendor, adjust as needed |
| 30 | + } |
| 31 | + }, |
| 32 | + "start_time": datetime.now().strftime("%Y-%m-%dT%H:%M:%S"), |
| 33 | + "end_time": datetime.now().strftime("%Y-%m-%dT%H:%M:%S"), |
| 34 | + "status": "success", |
| 35 | + "type": "sast" |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + # Map SARIF data to GitLab format |
| 40 | + for run in sarif_data.get("runs", []): |
| 41 | + for result in run.get("results", []): |
| 42 | + vulnerability = { |
| 43 | + "id": result.get("ruleId"), |
| 44 | + "category": "sast", |
| 45 | + "name": result.get("message", {}).get("text"), |
| 46 | + "message": result.get("message", {}).get("text"), |
| 47 | + "description": result.get("message", {}).get("text"), |
| 48 | + "severity": map_severity(result.get("properties", {}).get("nightvision-risk")), # Mapping function to ensure correct severity |
| 49 | + "confidence": result.get("properties", {}).get("nightvision-confidence"), |
| 50 | + "solution": "Please refer to the rule documentation.", |
| 51 | + "scanner": gitlab_report["scan"]["scanner"], |
| 52 | + "identifiers": [{"type": "cve", "name": result.get("ruleId"), "value": result.get("ruleId")}], |
| 53 | + "location": { |
| 54 | + "file": result.get("locations", [{}])[0].get("physicalLocation", {}).get("artifactLocation", {}).get("uri"), |
| 55 | + } |
| 56 | + } |
| 57 | + # Conditionally add start and end lines if they are numbers |
| 58 | + if isinstance(result.get("locations", [{}])[0].get("physicalLocation", {}).get("region", {}).get("startLine"), int): |
| 59 | + vulnerability["location"]["start_line"] = result.get("locations", [{}])[0].get("physicalLocation", {}).get("region", {}).get("startLine") |
| 60 | + if isinstance(result.get("locations", [{}])[0].get("physicalLocation", {}).get("region", {}).get("endLine"), int): |
| 61 | + vulnerability["location"]["end_line"] = result.get("locations", [{}])[0].get("physicalLocation", {}).get("region", {}).get("endLine") |
| 62 | + |
| 63 | + gitlab_report["vulnerabilities"].append(vulnerability) |
| 64 | + |
| 65 | + # Write GitLab report to file |
| 66 | + with open(output_file, 'w') as file: |
| 67 | + json.dump(gitlab_report, file, indent=4) |
| 68 | + |
| 69 | +def map_severity(sarif_severity): |
| 70 | + """Map SARIF severity to GitLab severity levels.""" |
| 71 | + severity_mapping = { |
| 72 | + "CRITICAL": "Critical", |
| 73 | + "HIGH": "High", |
| 74 | + "MEDIUM": "Medium", |
| 75 | + "LOW": "Low", |
| 76 | + "INFO": "Info", |
| 77 | + } |
| 78 | + return severity_mapping.get(sarif_severity, "Unknown") |
| 79 | + |
| 80 | +# Example usage |
| 81 | +sarif_to_gitlab('results.sarif', 'gitlab_security_report.json') |
0 commit comments