Skip to content

Commit a91d084

Browse files
authored
Semgrep and codeql finding ids (#939)
* Move finding ID to SARIF base class * Include codeflows when filtering applicable findings
1 parent 7f19192 commit a91d084

File tree

3 files changed

+34
-25
lines changed

3 files changed

+34
-25
lines changed

src/codemodder/codeql.py

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
from typing_extensions import Self
55

6-
from codemodder.codetf import Finding, Rule
76
from codemodder.result import LineInfo, ResultSet, SarifLocation, SarifResult
87
from codemodder.sarifs import AbstractSarifToolDetector
98

@@ -40,29 +39,10 @@ class CodeQLResult(SarifResult):
4039
location_type = CodeQLLocation
4140

4241
@classmethod
43-
def from_sarif(
44-
cls, sarif_result, sarif_run, truncate_rule_id: bool = False
45-
) -> Self:
46-
return cls(
47-
rule_id=(
48-
rule_id := cls.extract_rule_id(
49-
sarif_result, sarif_run, truncate_rule_id
50-
)
51-
),
52-
locations=cls.extract_locations(sarif_result),
53-
codeflows=cls.extract_code_flows(sarif_result),
54-
related_locations=cls.extract_related_locations(sarif_result),
55-
finding_id=rule_id,
56-
finding=Finding(
57-
id=rule_id,
58-
rule=Rule(
59-
id=sarif_result.get("correlationGuid", rule_id),
60-
name=rule_id,
61-
# TODO: map to URL
62-
# url=,
63-
),
64-
),
65-
)
42+
def rule_url_from_id(cls, result: dict, run: dict, rule_id: str) -> str:
43+
del result, run, rule_id
44+
# TODO: Implement this method to return the specific rule URL
45+
return "https://codeql.github.com/codeql-query-help/"
6646

6747

6848
class CodeQLResultSet(ResultSet):

src/codemodder/file_context.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ def get_findings_for_location(self, line_number: int):
5858
location.start.line <= line_number <= location.end.line
5959
for location in result.locations
6060
)
61+
or any(
62+
location.start.line <= line_number <= location.end.line
63+
for codeflow in result.codeflows
64+
for location in codeflow
65+
)
6166
and result.finding is not None
6267
]
6368

src/codemodder/result.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from libcst._position import CodeRange
1212
from typing_extensions import Self
1313

14-
from codemodder.codetf import Finding
14+
from codemodder.codetf import Finding, Rule
1515

1616
from .utils.abc_dataclass import ABCDataclass
1717

@@ -86,6 +86,26 @@ class SarifResult(SASTResult, ABCDataclass):
8686
def from_sarif(
8787
cls, sarif_result, sarif_run, truncate_rule_id: bool = False
8888
) -> Self:
89+
rule_id = cls.extract_rule_id(sarif_result, sarif_run, truncate_rule_id)
90+
finding_id = cls.extract_finding_id(sarif_result) or rule_id
91+
return cls(
92+
rule_id=rule_id,
93+
locations=cls.extract_locations(sarif_result),
94+
codeflows=cls.extract_code_flows(sarif_result),
95+
related_locations=cls.extract_related_locations(sarif_result),
96+
finding_id=finding_id,
97+
finding=Finding(
98+
id=finding_id,
99+
rule=Rule(
100+
id=rule_id,
101+
name=rule_id,
102+
url=cls.rule_url_from_id(sarif_result, sarif_run, rule_id),
103+
),
104+
),
105+
)
106+
107+
@classmethod
108+
def rule_url_from_id(cls, result: dict, run: dict, rule_id: str) -> str:
89109
raise NotImplementedError
90110

91111
@classmethod
@@ -139,6 +159,10 @@ def extract_rule_id(cls, result, sarif_run, truncate_rule_id: bool = False) -> s
139159

140160
raise ValueError("Could not extract rule id from sarif result.")
141161

162+
@classmethod
163+
def extract_finding_id(cls, result) -> str | None:
164+
return result.get("guid") or result.get("correlationGuid")
165+
142166

143167
def same_line(pos: CodeRange, location: Location) -> bool:
144168
return pos.start.line == location.start.line and pos.end.line == location.end.line

0 commit comments

Comments
 (0)