|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import json |
| 4 | +import os |
| 5 | +import sys |
| 6 | + |
| 7 | + |
| 8 | +def to_result_sarif(path: str, line_number: int, rule_id: str, description: str): |
| 9 | + return { |
| 10 | + "level": "error", |
| 11 | + "locations": [ |
| 12 | + { |
| 13 | + "physicalLocation": { |
| 14 | + "artifactLocation": { |
| 15 | + "uri": path, |
| 16 | + }, |
| 17 | + "region": { |
| 18 | + "startColumn": 0, |
| 19 | + "startLine": line_number, |
| 20 | + }, |
| 21 | + } |
| 22 | + } |
| 23 | + ], |
| 24 | + "message": { |
| 25 | + "text": description, |
| 26 | + }, |
| 27 | + "ruleId": rule_id, |
| 28 | + } |
| 29 | + |
| 30 | + |
| 31 | +def main(argv): |
| 32 | + try: |
| 33 | + ggshield_json = json.load(sys.stdin) |
| 34 | + except json.JSONDecodeError: |
| 35 | + # If no JSON output or empty output, return empty SARIF |
| 36 | + sarif = { |
| 37 | + "$schema": "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json", |
| 38 | + "version": "2.1.0", |
| 39 | + "runs": [{"results": []}], |
| 40 | + } |
| 41 | + print(json.dumps(sarif, indent=2)) |
| 42 | + return |
| 43 | + |
| 44 | + results = [] |
| 45 | + |
| 46 | + # ggshield JSON structure can vary, handle different possible formats |
| 47 | + # Common structure: {"entities_with_incidents": [...]} or {"results": [...]} |
| 48 | + incidents = [] |
| 49 | + |
| 50 | + if "entities_with_incidents" in ggshield_json: |
| 51 | + # Format: {"entities_with_incidents": [{"filename": "...", "incidents": [...]}]} |
| 52 | + for entity in ggshield_json.get("entities_with_incidents", []): |
| 53 | + filename = entity.get("filename", "") |
| 54 | + for incident in entity.get("incidents", []): |
| 55 | + incidents.append( |
| 56 | + { |
| 57 | + "path": filename, |
| 58 | + "line": incident.get("line", 0), |
| 59 | + "type": incident.get("type", "Secret"), |
| 60 | + "match": incident.get("match", ""), |
| 61 | + "index_start": incident.get("index_start", 0), |
| 62 | + "index_end": incident.get("index_end", 0), |
| 63 | + } |
| 64 | + ) |
| 65 | + elif "results" in ggshield_json: |
| 66 | + # Alternative format: {"results": [...]} |
| 67 | + for result in ggshield_json.get("results", []): |
| 68 | + filename = result.get("filename", result.get("path", "")) |
| 69 | + for incident in result.get("incidents", []): |
| 70 | + incidents.append( |
| 71 | + { |
| 72 | + "path": filename, |
| 73 | + "line": incident.get("line", incident.get("line_number", 0)), |
| 74 | + "type": incident.get( |
| 75 | + "type", incident.get("detector_name", "Secret") |
| 76 | + ), |
| 77 | + "match": incident.get("match", incident.get("secret", "")), |
| 78 | + "index_start": incident.get("index_start", 0), |
| 79 | + "index_end": incident.get("index_end", 0), |
| 80 | + } |
| 81 | + ) |
| 82 | + elif isinstance(ggshield_json, list): |
| 83 | + # Format: [{...}, {...}] |
| 84 | + for item in ggshield_json: |
| 85 | + if "filename" in item or "path" in item: |
| 86 | + filename = item.get("filename", item.get("path", "")) |
| 87 | + for incident in item.get("incidents", []): |
| 88 | + incidents.append( |
| 89 | + { |
| 90 | + "path": filename, |
| 91 | + "line": incident.get("line", 0), |
| 92 | + "type": incident.get("type", "Secret"), |
| 93 | + "match": incident.get("match", ""), |
| 94 | + "index_start": incident.get("index_start", 0), |
| 95 | + "index_end": incident.get("index_end", 0), |
| 96 | + } |
| 97 | + ) |
| 98 | + |
| 99 | + # Process incidents and create SARIF results |
| 100 | + for incident in incidents: |
| 101 | + path = incident.get("path", "") |
| 102 | + line_number = incident.get("line", 0) |
| 103 | + rule_id = incident.get("type", "Secret") |
| 104 | + match = incident.get("match", "") |
| 105 | + |
| 106 | + # Create description |
| 107 | + if match: |
| 108 | + # Redact the secret for display |
| 109 | + if len(match) > 20: |
| 110 | + redacted = match[:10] + "..." + match[-5:] |
| 111 | + else: |
| 112 | + redacted = "***REDACTED***" |
| 113 | + description = f"Secret detected ({rule_id}): {redacted}" |
| 114 | + else: |
| 115 | + description = f"Secret detected ({rule_id})" |
| 116 | + |
| 117 | + # Normalize path to be relative to workspace |
| 118 | + if path and os.path.isabs(path): |
| 119 | + # Try to make it relative if possible |
| 120 | + pass |
| 121 | + |
| 122 | + results.append(to_result_sarif(path, line_number, rule_id, description)) |
| 123 | + |
| 124 | + sarif = { |
| 125 | + "$schema": "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json", |
| 126 | + "version": "2.1.0", |
| 127 | + "runs": [{"results": results}], |
| 128 | + } |
| 129 | + |
| 130 | + print(json.dumps(sarif, indent=2)) |
| 131 | + |
| 132 | + |
| 133 | +if __name__ == "__main__": |
| 134 | + main(sys.argv) |
0 commit comments