Skip to content
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

support sarif output for scan #259

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions guarddog/analyzer/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@ def _format_semgrep_response(self, response, rule=None, targetpath=None):

results[rule_name].append({
'location': location,
'start': result["start"],
'end': result["end"],
'path': result["path"],
'code': code,
'message': result["extra"]["message"]
})
Expand Down
17 changes: 9 additions & 8 deletions guarddog/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from guarddog.analyzer.metadata import get_metadata_detectors
from guarddog.analyzer.sourcecode import SOURCECODE_RULES
from guarddog.ecosystems import ECOSYSTEM
from guarddog.reporters.sarif import report_verify_sarif
from guarddog.reporters.sarif import report_verify_sarif, report_scan_sarif
from guarddog.scanners import get_scanner
from guarddog.scanners.scanner import PackageScanner

Expand Down Expand Up @@ -51,7 +51,7 @@ def verify_options(fn):


def scan_options(fn):
fn = click.option("--output-format", default=None, type=click.Choice(["json"], case_sensitive=False))(fn)
fn = click.option("--output-format", default=None, type=click.Choice(["json", "sarif"], case_sensitive=False))(fn)
fn = click.option("-v", "--version", default=None, help="Specify a version to scan")(fn)
return fn

Expand Down Expand Up @@ -185,12 +185,13 @@ def _scan(identifier, version, rules, exclude_rules, output_format, exit_non_zer

if output_format == "json":
import json as js
if len(results) == 1:
# return only a json like {}
print(js.dumps(results[0]))
else:
# Return a list of result like [{},{}]
print(js.dumps(results))
return_value = js.dumps(results)

elif output_format == "sarif":
return_value = report_scan_sarif(list(ALL_RULES), results, ecosystem)

if output_format is not None:
print(return_value)
else:
for result in results:
print_scan_results(result, result['package'])
Expand Down
47 changes: 47 additions & 0 deletions guarddog/reporters/sarif.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,50 @@ def report_verify_sarif(package_path: str, rule_names: list[str], scan_results:
runs = get_run(results, driver)
log = get_sarif_log([runs])
return json.dumps(log, indent=2)


def report_scan_sarif(rule_names: list[str], scan_results: list[dict],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add a unit test for it? it seems to be failing for a sample package I tried it on:

$ guarddog pypi scan first-exercise --output-format sarif
File "/Users/christophetd/workspace/guarddog/guarddog/cli.py", line 191, in _scan
    return_value = report_scan_sarif(list(ALL_RULES), results, ecosystem)
  File "/Users/christophetd/workspace/guarddog/guarddog/reporters/sarif.py", line 211, in report_scan_sarif
    "startLine": record["start"]["line"],
TypeError: string indices must be integers

ecosystem: ECOSYSTEM) -> str:
rules_documentation = build_rules_help_list()
rules = list(map(
lambda s: get_rule(s, rules_documentation),
rule_names
))
driver = get_driver(rules, ecosystem.value)
results = []

for entry in scan_results:
if entry["issues"] == 0:
continue

scan_result_details = entry["results"]
for rule_name, rule_records in scan_result_details.items():
for record in rule_records:
region = {
"startLine": record["start"]["line"],
"endLine": record["end"]["line"],
"startColumn": record["start"]["col"],
"endColumn": record["end"]["col"],
}
uri = record["path"]
physical_location = get_physical_location(uri, region)
location = get_location(physical_location)
package = entry["package"]

text = f"On package: {package} \n" + "\n".join(map(
lambda x: x["message"],
scan_result_details[rule_name]
)) if type(scan_result_details[rule_name]) == list else scan_result_details[rule_name]
key = f"{rule_name}-{text}"
partial_fingerprints = {
f"guarddog/v1/{rule_name}": hashlib.sha256(key.encode('utf-8')).hexdigest()
}
result = get_result(rule_name,
[location],
text,
partial_fingerprints)
results.append(result)

runs = get_run(results, driver)
log = get_sarif_log([runs])
return json.dumps(log, indent=2)