Skip to content

Commit dddf52a

Browse files
authored
Merge pull request #49 from ajinabraham/semgrep_bump
Bump semgrep to 1.86.0
2 parents a1f9856 + 83556ac commit dddf52a

File tree

7 files changed

+666
-286
lines changed

7 files changed

+666
-286
lines changed

.github/workflows/python_test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ jobs:
3636
poetry install --no-interaction --no-ansi
3737
- name: Bandit Scan
3838
run: |
39-
poetry run bandit libsast -r
39+
poetry run bandit -ll libsast -r
4040
- name: Unit test
4141
run: |
4242
poetry run pytest -v --cache-clear tests

libsast/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
__title__ = 'libsast'
1313
__authors__ = 'Ajin Abraham'
1414
__copyright__ = f'Copyright {year} Ajin Abraham, opensecurity.in'
15-
__version__ = '3.0.2'
15+
__version__ = '3.1.0'
1616
__version_info__ = tuple(int(i) for i in __version__.split('.'))
1717
__all__ = [
1818
'Scanner',

libsast/core_sgrep/helpers.py

Lines changed: 20 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,29 @@
11
# -*- coding: utf_8 -*-
22
"""Semantic Grep Helpers."""
33
import json
4-
import logging
54
import platform
6-
import multiprocessing
5+
import subprocess
76

87

9-
def invoke_semgrep(paths, scan_rules, **kwargs):
10-
"""Call Semgrep."""
8+
def invoke_semgrep(paths, scan_rules):
119
if platform.system() == 'Windows':
1210
return None
13-
from semgrep import semgrep_main
14-
from semgrep.state import get_state
15-
from semgrep.constants import OutputFormat
16-
from semgrep.output import OutputHandler, OutputSettings
17-
try:
18-
cpu_count = multiprocessing.cpu_count()
19-
except NotImplementedError:
20-
cpu_count = 1 # CPU count is not implemented on Windows
21-
# Semgrep output formatting
22-
state = get_state()
23-
state.terminal.configure(
24-
verbose=False,
25-
debug=False,
26-
quiet=True,
27-
force_color=False,
28-
)
29-
logging.getLogger('semgrep').propagate = False
30-
output_settings = OutputSettings(
31-
output_format=OutputFormat.JSON,
32-
output_destination=None,
33-
output_per_finding_max_lines_limit=None,
34-
output_per_line_max_chars_limit=None,
35-
error_on_findings=False,
36-
verbose_errors=False,
37-
strict=False,
38-
timeout_threshold=3,
39-
)
40-
output_handler = OutputHandler(output_settings)
41-
(
42-
filtered_matches_by_rule,
43-
_,
44-
_,
45-
_,
46-
_,
47-
_,
48-
_,
49-
_,
50-
_,
51-
_,
52-
_,
53-
_,
54-
) = semgrep_main.main(
55-
output_handler=output_handler,
56-
target=[pt.as_posix() for pt in paths],
57-
jobs=cpu_count,
58-
pattern=None,
59-
lang=None,
60-
configs=[scan_rules],
61-
timeout=5,
62-
timeout_threshold=3,
63-
**kwargs,
64-
)
65-
output_handler.rule_matches = [
66-
m for ms in filtered_matches_by_rule.values() for m in ms
11+
ps = [pt.as_posix() for pt in paths]
12+
command = [
13+
'semgrep',
14+
'--metrics=off',
15+
'--no-rewrite-rule-ids',
16+
'--json',
17+
'-q',
18+
'--config',
19+
scan_rules,
20+
*ps,
6721
]
68-
return json.loads(output_handler._build_output())
22+
try:
23+
result = subprocess.run(command, capture_output=True, text=True, check=True)
24+
return json.loads(result.stdout)
25+
except subprocess.CalledProcessError as e:
26+
try:
27+
return json.loads(e.output)
28+
except json.JSONDecodeError:
29+
return {'errors': e.output}

libsast/core_sgrep/semantic_sgrep.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@ def scan(self, paths: list) -> dict:
4343

4444
def format_output(self, results):
4545
"""Format sgrep results."""
46-
errs = self.findings.get('errors')
46+
errs = results.get('errors')
4747
if errs:
4848
self.findings['errors'] = errs
49+
if not results.get('results'):
50+
return
4951
smatches = self.findings['matches']
5052
for find in results['results']:
5153
file_details = {
@@ -54,7 +56,7 @@ def format_output(self, results):
5456
'match_lines': (find['start']['line'], find['end']['line']),
5557
'match_string': find['extra']['lines'],
5658
}
57-
rule_id = find['check_id'].rsplit('.', 1)[1]
59+
rule_id = find['check_id']
5860
if rule_id in smatches:
5961
smatches[rule_id]['files'].append(file_details)
6062
else:

0 commit comments

Comments
 (0)