|
| 1 | +import contextlib |
| 2 | +import json |
| 3 | +from datetime import datetime |
| 4 | + |
| 5 | +from dojo.models import Finding |
| 6 | + |
| 7 | + |
| 8 | +class SemgrepProParser: |
| 9 | + def get_scan_types(self): |
| 10 | + return ["Semgrep Pro JSON Report"] |
| 11 | + |
| 12 | + def get_label_for_scan_types(self, scan_type): |
| 13 | + return scan_type |
| 14 | + |
| 15 | + def get_description_for_scan_types(self, scan_type): |
| 16 | + return "Import Semgrep Pro findings in JSON format" |
| 17 | + |
| 18 | + def get_findings(self, filename, test): |
| 19 | + data = json.load(filename) |
| 20 | + dupes = {} |
| 21 | + |
| 22 | + for item in data.get("findings", []): |
| 23 | + # Ensure required fields have default values |
| 24 | + title = item.get("rule_name", "No title") |
| 25 | + file_path = item.get("location", {}).get("file_path", "") |
| 26 | + line = item.get("location", {}).get("line", 0) |
| 27 | + |
| 28 | + # Map status to active/verified |
| 29 | + status = item.get("status", "new").lower() |
| 30 | + active = status not in {"fixed", "removed"} |
| 31 | + triage_status = item.get("triage_state", "untriaged").lower() |
| 32 | + verified = triage_status != "untriaged" |
| 33 | + |
| 34 | + finding = Finding( |
| 35 | + test=test, |
| 36 | + title=title, |
| 37 | + severity=self.convert_severity(item.get("severity", "INFO")), |
| 38 | + description=self.get_description(item), |
| 39 | + file_path=file_path, |
| 40 | + line=line, |
| 41 | + static_finding=True, |
| 42 | + dynamic_finding=False, |
| 43 | + vuln_id_from_tool=item.get("rule_name"), |
| 44 | + nb_occurences=1, |
| 45 | + active=active, |
| 46 | + verified=verified, |
| 47 | + ) |
| 48 | + |
| 49 | + # Add CWE if available |
| 50 | + if "rule" in item and "cwe_names" in item["rule"]: |
| 51 | + try: |
| 52 | + cwe_name = item["rule"]["cwe_names"][0] # Take first CWE |
| 53 | + finding.cwe = int(cwe_name.split("-")[1].split(":")[0]) |
| 54 | + except (ValueError, IndexError, KeyError): |
| 55 | + finding.cwe = None |
| 56 | + |
| 57 | + # Add references if available |
| 58 | + references = [] |
| 59 | + if "line_of_code_url" in item: |
| 60 | + references.append(f"Line of Code: {item['line_of_code_url']}") |
| 61 | + if "rule" in item: |
| 62 | + if "owasp_names" in item["rule"]: |
| 63 | + references.extend(item["rule"]["owasp_names"]) |
| 64 | + if "cwe_names" in item["rule"]: |
| 65 | + references.extend(item["rule"]["cwe_names"]) |
| 66 | + |
| 67 | + if "external_ticket" in item: |
| 68 | + references.append(f"External Ticket: \n {item['external_ticket']}") |
| 69 | + |
| 70 | + # Add file location details |
| 71 | + if references: |
| 72 | + finding.references = "\n".join(references) |
| 73 | + |
| 74 | + # Add mitigation if available |
| 75 | + mitigation_parts = [] |
| 76 | + if "assistant" in item: |
| 77 | + assistant = item["assistant"] |
| 78 | + if "guidance" in assistant: |
| 79 | + if "summary" in assistant["guidance"]: |
| 80 | + mitigation_parts.append(f"**Guidance Summary:**\n{assistant['guidance']['summary']}") |
| 81 | + if "instructions" in assistant["guidance"]: |
| 82 | + mitigation_parts.append(f"**Instructions:**\n{assistant['guidance']['instructions']}") |
| 83 | + |
| 84 | + if "autofix" in assistant: |
| 85 | + autofix = assistant["autofix"] |
| 86 | + if "fix_code" in autofix: |
| 87 | + mitigation_parts.append(f"**Suggested Fix:**\n```\n{autofix['fix_code']}\n```") |
| 88 | + if autofix.get("explanation"): |
| 89 | + mitigation_parts.append(f"**Fix Explanation:**\n{autofix['explanation']}") |
| 90 | + |
| 91 | + if "autotriage" in assistant: |
| 92 | + autotriage = assistant["autotriage"] |
| 93 | + if "verdict" in autotriage: |
| 94 | + mitigation_parts.append(f"**Auto-triage Verdict:** {autotriage['verdict']}") |
| 95 | + if "reason" in autotriage: |
| 96 | + mitigation_parts.append(f"**Auto-triage Reason:** {autotriage['reason']}") |
| 97 | + |
| 98 | + if "component" in assistant: |
| 99 | + component = assistant["component"] |
| 100 | + if "tag" in component: |
| 101 | + mitigation_parts.append(f"**Component:** {component['tag']}") |
| 102 | + if "risk" in component: |
| 103 | + mitigation_parts.append(f"**Risk Level:** {component['risk']}") |
| 104 | + |
| 105 | + finding.mitigation = "\n\n".join(mitigation_parts) if mitigation_parts else None |
| 106 | + |
| 107 | + # Add unique identifier |
| 108 | + finding.unique_id_from_tool = item.get("match_based_id") |
| 109 | + |
| 110 | + # Add component name and version if available |
| 111 | + if "assistant" in item and "component" in item["assistant"]: |
| 112 | + finding.component_name = item["assistant"]["component"].get("tag") |
| 113 | + |
| 114 | + # Add dates |
| 115 | + if "created_at" in item: |
| 116 | + with contextlib.suppress(ValueError, TypeError): |
| 117 | + finding.date = datetime.strptime(item["created_at"].split(".")[0], "%Y-%m-%dT%H:%M:%S") |
| 118 | + |
| 119 | + # Add impact |
| 120 | + impact_parts = [] |
| 121 | + if "rule" in item and "vulnerability_classes" in item["rule"]: |
| 122 | + impact_parts.extend(item["rule"]["vulnerability_classes"]) |
| 123 | + if "confidence" in item: |
| 124 | + impact_parts.append(f"Confidence: {item['confidence'].capitalize()}") |
| 125 | + if "repository" in item: |
| 126 | + repo = item["repository"] |
| 127 | + impact_parts.append(f"Repository: {repo.get('name', '')} ({repo.get('url', '')})") |
| 128 | + finding.impact = "\n".join(impact_parts) |
| 129 | + |
| 130 | + # Use match_based_id for deduplication if available, otherwise use file location |
| 131 | + dupe_key = finding.unique_id_from_tool or title + str(file_path) + str(line) |
| 132 | + |
| 133 | + if dupe_key in dupes: |
| 134 | + dupes[dupe_key].nb_occurences += 1 |
| 135 | + else: |
| 136 | + dupes[dupe_key] = finding |
| 137 | + |
| 138 | + return list(dupes.values()) |
| 139 | + |
| 140 | + def convert_severity(self, val): |
| 141 | + val = val.upper() |
| 142 | + if val == "ERROR" or val == "HIGH": |
| 143 | + return "High" |
| 144 | + if val == "WARNING" or val == "MEDIUM": |
| 145 | + return "Medium" |
| 146 | + if val == "INFO" or val == "LOW": |
| 147 | + return "Low" |
| 148 | + if val == "CRITICAL": |
| 149 | + return "Critical" |
| 150 | + return "Info" |
| 151 | + |
| 152 | + def get_description(self, item): |
| 153 | + desc = "" |
| 154 | + if "rule_message" in item: |
| 155 | + desc += f"**Message:** {item['rule_message']}\n\n" |
| 156 | + |
| 157 | + if "rule" in item: |
| 158 | + if "message" in item["rule"]: |
| 159 | + desc += f"**Rule Message:** {item['rule']['message']}\n\n" |
| 160 | + if "category" in item["rule"]: |
| 161 | + desc += f"**Category:** {item['rule']['category']}\n\n" |
| 162 | + if "confidence" in item["rule"]: |
| 163 | + desc += f"**Confidence:** {item['rule']['confidence']}\n\n" |
| 164 | + if "vulnerability_classes" in item["rule"]: |
| 165 | + desc += "**Vulnerability Classes:**\n" |
| 166 | + for vuln_class in item["rule"]["vulnerability_classes"]: |
| 167 | + desc += f"- {vuln_class}\n" |
| 168 | + desc += "\n" |
| 169 | + if "cwe_names" in item["rule"]: |
| 170 | + desc += "**CWE References:**\n" |
| 171 | + for cwe in item["rule"]["cwe_names"]: |
| 172 | + desc += f"- {cwe}\n" |
| 173 | + desc += "\n" |
| 174 | + if "owasp_names" in item["rule"]: |
| 175 | + desc += "**OWASP References:**\n" |
| 176 | + for owasp in item["rule"]["owasp_names"]: |
| 177 | + desc += f"- {owasp}\n" |
| 178 | + desc += "\n" |
| 179 | + |
| 180 | + # Add categories |
| 181 | + if "categories" in item: |
| 182 | + desc += "**Categories:**\n" |
| 183 | + for category in item["categories"]: |
| 184 | + desc += f"- {category}\n" |
| 185 | + desc += "\n" |
| 186 | + |
| 187 | + # Add triage information |
| 188 | + if "triage_state" in item: |
| 189 | + desc += f"**Triage State:** {item['triage_state']}\n" |
| 190 | + if "triage_comment" in item: |
| 191 | + desc += f"**Triage Comment:** {item['triage_comment']}\n" |
| 192 | + if "triage_reason" in item: |
| 193 | + desc += f"**Triage Reason:** {item['triage_reason']}\n\n" |
| 194 | + |
| 195 | + return desc |
0 commit comments