From e752fec37bcf55d5b7af2308b807ea82aad1e194 Mon Sep 17 00:00:00 2001 From: clavedeluna Date: Wed, 24 Jul 2024 11:15:23 -0300 Subject: [PATCH] semgrep rule should report findings --- src/codemodder/result.py | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/src/codemodder/result.py b/src/codemodder/result.py index d6bbdea31..e0c1f109c 100644 --- a/src/codemodder/result.py +++ b/src/codemodder/result.py @@ -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 @@ -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 @@ -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