|
| 1 | +import json |
| 2 | +from urllib.parse import urlparse |
| 3 | + |
| 4 | +from dojo.models import Finding |
| 5 | + |
| 6 | + |
| 7 | +class GithubSASTParser: |
| 8 | + def get_scan_types(self): |
| 9 | + return ["Github SAST Scan"] |
| 10 | + |
| 11 | + def get_label_for_scan_types(self, scan_type): |
| 12 | + return scan_type |
| 13 | + |
| 14 | + def get_description_for_scan_types(self, scan_type): |
| 15 | + return "GitHub SAST report file can be imported in JSON format." |
| 16 | + |
| 17 | + def get_findings(self, filename, test): |
| 18 | + data = json.load(filename) |
| 19 | + if not isinstance(data, list): |
| 20 | + error_msg = "Invalid SAST report format, expected a JSON list of alerts." |
| 21 | + raise TypeError(error_msg) |
| 22 | + |
| 23 | + findings = [] |
| 24 | + for vuln in data: |
| 25 | + rule = vuln.get("rule", {}) |
| 26 | + inst = vuln.get("most_recent_instance", {}) |
| 27 | + loc = inst.get("location", {}) |
| 28 | + html_url = vuln.get("html_url") |
| 29 | + rule_id = rule.get("id") |
| 30 | + title = f"{rule.get('description')} ({rule_id})" |
| 31 | + severity = rule.get("security_severity_level", "Info").title() |
| 32 | + active = vuln.get("state") == "open" |
| 33 | + |
| 34 | + # Build description with context |
| 35 | + desc_lines = [] |
| 36 | + if html_url: |
| 37 | + desc_lines.append(f"GitHub Alert: [{html_url}]({html_url})") |
| 38 | + owner = repo = None |
| 39 | + commit_sha = inst.get("commit_sha") |
| 40 | + if html_url: |
| 41 | + parsed = urlparse(html_url) |
| 42 | + parts = parsed.path.strip("/").split("/") |
| 43 | + # URL is /<owner>/<repo>/security/... so parts[0]=owner, parts[1]=repo |
| 44 | + if len(parts) >= 2: |
| 45 | + owner, repo = parts[0], parts[1] |
| 46 | + if owner and repo and commit_sha and loc.get("path") and loc.get("start_line"): |
| 47 | + file_link = ( |
| 48 | + f"{parsed.scheme}://{parsed.netloc}/" |
| 49 | + f"{owner}/{repo}/blob/{commit_sha}/" |
| 50 | + f"{loc['path']}#L{loc['start_line']}" |
| 51 | + ) |
| 52 | + desc_lines.append(f"Location: [{loc['path']}:{loc['start_line']}]({file_link})") |
| 53 | + elif loc.get("path") and loc.get("start_line"): |
| 54 | + # fallback if something is missing |
| 55 | + desc_lines.append(f"Location: {loc['path']}:{loc['start_line']}") |
| 56 | + msg = inst.get("message", {}).get("text") |
| 57 | + if msg: |
| 58 | + desc_lines.append(f"Message: {msg}") |
| 59 | + if severity: |
| 60 | + desc_lines.append(f"Rule Severity: {severity}") |
| 61 | + if rule.get("full_description"): |
| 62 | + desc_lines.append(f"Description: {rule.get('full_description')}") |
| 63 | + description = "\n".join(desc_lines) |
| 64 | + |
| 65 | + finding = Finding( |
| 66 | + title=title, |
| 67 | + test=test, |
| 68 | + description=description, |
| 69 | + severity=severity, |
| 70 | + active=active, |
| 71 | + static_finding=True, |
| 72 | + dynamic_finding=False, |
| 73 | + vuln_id_from_tool=rule_id, |
| 74 | + ) |
| 75 | + |
| 76 | + # File path & line |
| 77 | + finding.file_path = loc.get("path") |
| 78 | + finding.line = loc.get("start_line") |
| 79 | + |
| 80 | + if html_url: |
| 81 | + finding.url = html_url |
| 82 | + |
| 83 | + findings.append(finding) |
| 84 | + return findings |
0 commit comments