Skip to content

Commit

Permalink
semgrep rule should report findings
Browse files Browse the repository at this point in the history
  • Loading branch information
clavedeluna committed Jul 24, 2024
1 parent f51dcd2 commit e752fec
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions src/codemodder/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from libcst._position import CodeRange
from typing_extensions import Self

from codemodder.codetf import Finding
from codemodder.codetf import Finding, Rule

from .utils.abc_dataclass import ABCDataclass

Expand Down Expand Up @@ -68,18 +68,39 @@ def match_location(self, pos: CodeRange, node: cst.CSTNode) -> bool:


@dataclass(kw_only=True)
class SarifResult(Result, ABCDataclass):
class SASTResult(Result):
finding_id: str


@dataclass(kw_only=True)
class SarifResult(SASTResult, ABCDataclass):
location_type: ClassVar[Type[SarifLocation]]

@classmethod
def from_sarif(
cls, sarif_result, sarif_run, truncate_rule_id: bool = False
) -> Self:
# avoid circular import
from core_codemods.semgrep.api import semgrep_url_from_id

return cls(
rule_id=cls.extract_rule_id(sarif_result, sarif_run, truncate_rule_id),
rule_id=(
rule_id := cls.extract_rule_id(
sarif_result, sarif_run, truncate_rule_id
)
),
locations=cls.extract_locations(sarif_result),
codeflows=cls.extract_code_flows(sarif_result),
related_locations=cls.extract_related_locations(sarif_result),
finding_id=rule_id,
finding=Finding(
id=rule_id,
rule=Rule(
id=rule_id,
name=rule_id,
url=semgrep_url_from_id(rule_id),
),
),
)

@classmethod
Expand Down Expand Up @@ -126,11 +147,6 @@ def extract_rule_id(cls, result, sarif_run, truncate_rule_id: bool = False) -> s
raise ValueError("Could not extract rule id from sarif result.")


@dataclass(kw_only=True)
class SASTResult(Result):
finding_id: str


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

Expand Down

0 comments on commit e752fec

Please sign in to comment.