From b5951f2368792f7d9cc0933ca9d7d1a34c2f6e88 Mon Sep 17 00:00:00 2001 From: clavedeluna Date: Thu, 25 Jul 2024 10:41:05 -0300 Subject: [PATCH] each sarif result should impl from_sarif --- src/codemodder/codeql.py | 26 ++++++++++++++++++++++++++ src/codemodder/result.py | 25 ++----------------------- src/codemodder/semgrep.py | 28 ++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 23 deletions(-) diff --git a/src/codemodder/codeql.py b/src/codemodder/codeql.py index cbfe8cfb..041ed3e3 100644 --- a/src/codemodder/codeql.py +++ b/src/codemodder/codeql.py @@ -3,6 +3,7 @@ from typing_extensions import Self +from codemodder.codetf import Finding, Rule from codemodder.result import LineInfo, ResultSet, SarifLocation, SarifResult from codemodder.sarifs import AbstractSarifToolDetector @@ -38,6 +39,31 @@ def from_sarif(cls, sarif_location) -> Self: class CodeQLResult(SarifResult): location_type = CodeQLLocation + @classmethod + def from_sarif( + cls, sarif_result, sarif_run, truncate_rule_id: bool = False + ) -> Self: + return cls( + 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, + # TODO: map to URL + # url=, + ), + ), + ) + class CodeQLResultSet(ResultSet): @classmethod diff --git a/src/codemodder/result.py b/src/codemodder/result.py index e0c1f109..8dd66c55 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, Rule +from codemodder.codetf import Finding from .utils.abc_dataclass import ABCDataclass @@ -80,28 +80,7 @@ class SarifResult(SASTResult, ABCDataclass): 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=( - 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), - ), - ), - ) + raise NotImplementedError @classmethod def extract_locations(cls, sarif_result) -> list[Location]: diff --git a/src/codemodder/semgrep.py b/src/codemodder/semgrep.py index d92b79ca..84a3293b 100644 --- a/src/codemodder/semgrep.py +++ b/src/codemodder/semgrep.py @@ -7,6 +7,7 @@ from typing_extensions import Self, override +from codemodder.codetf import Finding, Rule from codemodder.context import CodemodExecutionContext from codemodder.logging import logger from codemodder.result import LineInfo, Result, ResultSet, SarifLocation, SarifResult @@ -43,6 +44,33 @@ def from_sarif(cls, sarif_location) -> Self: class SemgrepResult(SarifResult): location_type = SemgrepLocation + @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=( + 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), + ), + ), + ) + class SemgrepResultSet(ResultSet): @classmethod