-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from ajinabraham/semgrep_bump
Bump semgrep to 1.86.0
- Loading branch information
Showing
7 changed files
with
666 additions
and
286 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -1,68 +1,29 @@ | ||
# -*- coding: utf_8 -*- | ||
"""Semantic Grep Helpers.""" | ||
import json | ||
import logging | ||
import platform | ||
import multiprocessing | ||
import subprocess | ||
|
||
|
||
def invoke_semgrep(paths, scan_rules, **kwargs): | ||
"""Call Semgrep.""" | ||
def invoke_semgrep(paths, scan_rules): | ||
if platform.system() == 'Windows': | ||
return None | ||
from semgrep import semgrep_main | ||
from semgrep.state import get_state | ||
from semgrep.constants import OutputFormat | ||
from semgrep.output import OutputHandler, OutputSettings | ||
try: | ||
cpu_count = multiprocessing.cpu_count() | ||
except NotImplementedError: | ||
cpu_count = 1 # CPU count is not implemented on Windows | ||
# Semgrep output formatting | ||
state = get_state() | ||
state.terminal.configure( | ||
verbose=False, | ||
debug=False, | ||
quiet=True, | ||
force_color=False, | ||
) | ||
logging.getLogger('semgrep').propagate = False | ||
output_settings = OutputSettings( | ||
output_format=OutputFormat.JSON, | ||
output_destination=None, | ||
output_per_finding_max_lines_limit=None, | ||
output_per_line_max_chars_limit=None, | ||
error_on_findings=False, | ||
verbose_errors=False, | ||
strict=False, | ||
timeout_threshold=3, | ||
) | ||
output_handler = OutputHandler(output_settings) | ||
( | ||
filtered_matches_by_rule, | ||
_, | ||
_, | ||
_, | ||
_, | ||
_, | ||
_, | ||
_, | ||
_, | ||
_, | ||
_, | ||
_, | ||
) = semgrep_main.main( | ||
output_handler=output_handler, | ||
target=[pt.as_posix() for pt in paths], | ||
jobs=cpu_count, | ||
pattern=None, | ||
lang=None, | ||
configs=[scan_rules], | ||
timeout=5, | ||
timeout_threshold=3, | ||
**kwargs, | ||
) | ||
output_handler.rule_matches = [ | ||
m for ms in filtered_matches_by_rule.values() for m in ms | ||
ps = [pt.as_posix() for pt in paths] | ||
command = [ | ||
'semgrep', | ||
'--metrics=off', | ||
'--no-rewrite-rule-ids', | ||
'--json', | ||
'-q', | ||
'--config', | ||
scan_rules, | ||
*ps, | ||
] | ||
return json.loads(output_handler._build_output()) | ||
try: | ||
result = subprocess.run(command, capture_output=True, text=True, check=True) | ||
return json.loads(result.stdout) | ||
except subprocess.CalledProcessError as e: | ||
try: | ||
return json.loads(e.output) | ||
except json.JSONDecodeError: | ||
return {'errors': e.output} |
This file contains 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
Oops, something went wrong.