-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Split Github Vulnerability Scan into separate SCA & SAST parsers #12773
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Logicmn
wants to merge
18
commits into
DefectDojo:dev
Choose a base branch
from
Logicmn:github-vuln-parser-improvements
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
d17a879
Refactor GithubVulnerability parser and add GithubSAST parser
Logicmn b34a58c
More GithubVulnerability and GithubSAST parser improvements
Logicmn c50be33
Add documentation
Logicmn 2673001
Add tests, update docs, and add hash code fields
Logicmn 3b6ee59
Fix Github vulnerability parser unit test
Logicmn 6c6e697
Unit tests and parser tweaks
Logicmn edc4c7c
Rm files pushed by mistake
Logicmn fd2c43e
Revert certain removals from unit test
Logicmn d6805c8
Add EPSS field population and update unit tests
Logicmn 106e769
Removed some unnecessary comments and formatting
Logicmn 7399641
Ruff formatting
Logicmn 8115ee3
Fix unit tests
Logicmn 745dca2
Ruff formatting
Logicmn d698115
Fix unit test
Logicmn b7d143e
Github Vulnerability parser and docs tweaks, and upgrade instructions
Logicmn 9f9bc42
Politeness
Logicmn 84b0706
Fix dependabot update pr link parsing
Logicmn 2ffb18d
Backwards compatability
Logicmn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
docs/content/en/connecting_your_tools/parsers/file/github_sast.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
title: "Github SAST Scan" | ||
toc_hide: true | ||
--- | ||
Import findings in JSON format from Github Code Scanning REST API: | ||
<https://docs.github.com/en/rest/code-scanning/code-scanning> | ||
|
||
### Sample Scan Data | ||
Sample Github SAST scans can be found [here](https://github.com/DefectDojo/django-DefectDojo/tree/master/unittests/scans/github_sast). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import json | ||
|
||
from dojo.models import Finding | ||
|
||
|
||
class GithubSASTParser: | ||
def get_scan_types(self): | ||
return ["Github SAST Scan"] | ||
|
||
def get_label_for_scan_types(self, scan_type): | ||
return scan_type | ||
|
||
def get_description_for_scan_types(self, scan_type): | ||
return "GitHub SAST report file can be imported in JSON format." | ||
|
||
def get_findings(self, filename, test): | ||
data = json.load(filename) | ||
if not isinstance(data, list): | ||
error_msg = "Invalid SAST report format, expected a JSON list of alerts." | ||
raise TypeError(error_msg) | ||
|
||
findings = [] | ||
for vuln in data: | ||
rule = vuln.get("rule", {}) | ||
inst = vuln.get("most_recent_instance", {}) | ||
loc = inst.get("location", {}) | ||
html_url = vuln.get("html_url") | ||
rule_id = rule.get("id") | ||
title = f"{rule.get('description')} ({rule_id})" | ||
severity = rule.get("security_severity_level", "Info").title() | ||
active = vuln.get("state") == "open" | ||
|
||
# Build description with context | ||
desc_lines = [] | ||
if html_url: | ||
desc_lines.append(f"GitHub Alert: [{html_url}]({html_url})") | ||
owner = repo = None | ||
commit_sha = inst.get("commit_sha") | ||
if html_url: | ||
from urllib.parse import urlparse | ||
|
||
parsed = urlparse(html_url) | ||
parts = parsed.path.strip("/").split("/") | ||
# URL is /<owner>/<repo>/security/... so parts[0]=owner, parts[1]=repo | ||
if len(parts) >= 2: | ||
owner, repo = parts[0], parts[1] | ||
if owner and repo and commit_sha and loc.get("path") and loc.get("start_line"): | ||
file_link = ( | ||
f"{parsed.scheme}://{parsed.netloc}/" | ||
f"{owner}/{repo}/blob/{commit_sha}/" | ||
f"{loc['path']}#L{loc['start_line']}" | ||
) | ||
desc_lines.append(f"Location: [{loc['path']}:{loc['start_line']}]({file_link})") | ||
elif loc.get("path") and loc.get("start_line"): | ||
# fallback if something is missing | ||
desc_lines.append(f"Location: {loc['path']}:{loc['start_line']}") | ||
msg = inst.get("message", {}).get("text") | ||
if msg: | ||
desc_lines.append(f"Message: {msg}") | ||
if severity: | ||
desc_lines.append(f"Rule Severity: {severity}") | ||
if rule.get("full_description"): | ||
desc_lines.append(f"Description: {rule.get('full_description')}") | ||
description = "\n".join(desc_lines) | ||
|
||
finding = Finding( | ||
title=title, | ||
test=test, | ||
description=description, | ||
severity=severity, | ||
active=active, | ||
static_finding=True, | ||
dynamic_finding=False, | ||
vuln_id_from_tool=rule_id, | ||
) | ||
|
||
# File path & line | ||
finding.file_path = loc.get("path") | ||
finding.line = loc.get("start_line") | ||
|
||
if html_url: | ||
finding.url = html_url | ||
|
||
findings.append(finding) | ||
return findings |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason to have this here rather than at the top?