-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathsemgrep.py
More file actions
145 lines (125 loc) · 4.67 KB
/
semgrep.py
File metadata and controls
145 lines (125 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import itertools
import json
import subprocess
import uuid
from pathlib import Path
from tempfile import NamedTemporaryFile
from typing import Iterable, Optional
from sarif_pydantic import Sarif
from typing_extensions import Self, override
from codemodder.context import CodemodExecutionContext
from codemodder.logging import logger
from codemodder.result import (
LocationModel,
Result,
ResultModel,
ResultSet,
SarifLocation,
SarifResult,
)
from codemodder.sarifs import AbstractSarifToolDetector, Run
class SemgrepSarifToolDetector(AbstractSarifToolDetector):
@classmethod
def detect(cls, run_data: Run) -> bool:
return "semgrep" in run_data.tool.driver.name.lower()
class SemgrepLocation(SarifLocation):
@staticmethod
def get_snippet(sarif_location: LocationModel) -> str | None:
return (
sarif_location.physical_location.region.snippet.text
if sarif_location.physical_location
and sarif_location.physical_location.region
and sarif_location.physical_location.region.snippet
else SarifLocation.get_snippet(sarif_location)
)
class SemgrepResult(SarifResult):
location_type = SemgrepLocation
@override
@classmethod
def rule_url_from_id(
cls, result: ResultModel, run: Run, rule_id: str
) -> str | None:
del result, run
from core_codemods.semgrep.api import semgrep_url_from_id
return semgrep_url_from_id(rule_id)
class SemgrepResultSet(ResultSet):
@classmethod
def from_sarif(cls, sarif_file: str | Path, truncate_rule_id: bool = False) -> Self:
data = Sarif.model_validate_json(Path(sarif_file).read_text())
result_set = cls()
for sarif_run in data.runs:
for result in sarif_run.results or []:
sarif_result = SemgrepResult.from_sarif(
result, sarif_run, truncate_rule_id
)
result_set.add_result(sarif_result)
result_set.store_tool_data(sarif_run.tool.model_dump())
return result_set
class InternalSemgrepResultSet(SemgrepResultSet):
@override
def results_for_rule_and_file(
self, context: CodemodExecutionContext, rule_id: str, file: Path
) -> list[Result]:
del context
paths_for_rule = self.get(rule_id, {})
# Do not normalize the path
return paths_for_rule.get(file, [])
def run(
execution_context: CodemodExecutionContext,
yaml_files: Iterable[Path],
files_to_analyze: Optional[Iterable[Path]] = None,
) -> SemgrepResultSet:
"""
Runs Semgrep and outputs a dict with the results organized by rule_id.
"""
if not yaml_files:
raise ValueError("No Semgrep rules were provided")
with NamedTemporaryFile(
prefix="semgrep", suffix=".sarif", mode="w+"
) as temp_sarif_file:
command = [
"semgrep",
"scan",
"--no-error",
"--dataflow-traces",
"--sarif",
"-o",
temp_sarif_file.name,
]
command.extend(
itertools.chain.from_iterable(
map(lambda f: ["--config", str(f)], yaml_files)
)
)
command.extend(map(str, files_to_analyze or [execution_context.directory]))
logger.debug("semgrep command: `%s`", " ".join(command))
call = subprocess.run(
command,
shell=False,
check=False,
stdout=None if execution_context.verbose else subprocess.PIPE,
stderr=None if execution_context.verbose else subprocess.PIPE,
)
# Insert guid in results
temp_sarif_file.seek(0)
sarif = Sarif.model_validate(json.load(temp_sarif_file))
for run in sarif.runs:
for result in run.results or []:
if not result.guid:
result.guid = uuid.uuid4()
temp_sarif_file.seek(0)
temp_sarif_file.write(sarif.model_dump_json(exclude_none=True, by_alias=True))
temp_sarif_file.seek(0)
if call.returncode != 0:
if not execution_context.verbose:
logger.error("captured semgrep stderr: %s", call.stderr)
try:
logger.error("semgrep sarif output: %s", temp_sarif_file.read())
except Exception as e:
logger.error("failed to read semgrep sarif output: %s", e)
raise subprocess.CalledProcessError(call.returncode, command)
# semgrep prepends the folders into the rule-id, we want the base name only
results = InternalSemgrepResultSet.from_sarif(
temp_sarif_file.name, truncate_rule_id=True
)
return results