Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/codemodder/codeql.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,11 @@ class CodeQLResult(SarifResult):
def from_sarif(
cls, sarif_result, sarif_run, truncate_rule_id: bool = False
) -> Self:
rule_id = cls.extract_rule_id(sarif_result, sarif_run, truncate_rule_id)
text_for_rule = get_text_for_rule(rule_id, sarif_run)
finding_msg = f"{sarif_result['message']['text']}\n{text_for_rule}"
return cls(
rule_id=(
rule_id := cls.extract_rule_id(
sarif_result, sarif_run, truncate_rule_id
)
),
rule_id=rule_id,
locations=cls.extract_locations(sarif_result),
codeflows=cls.extract_code_flows(sarif_result),
related_locations=cls.extract_related_locations(sarif_result),
Expand All @@ -62,6 +61,7 @@ def from_sarif(
# url=,
),
),
finding_msg=finding_msg,
)


Expand All @@ -80,3 +80,12 @@ def from_sarif(cls, sarif_file: str | Path, truncate_rule_id: bool = False) -> S
)
result_set.add_result(codeql_result)
return result_set


# TODO: cache, make hashable
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a ton of bandwidth so left it as a todo, since we're passing dict of lists and some other unhashable data here, it's not super fast to do, but we should do it at some point

def get_text_for_rule(rule_id: str, sarif_run: dict) -> str:
for ext in sarif_run["tool"]["extensions"]:
for rule in ext.get("rules", []):
if rule["id"] == rule_id:
return f"{rule.get('fullDescription', {}).get('text', '')}\n{rule.get('help', {}).get('text', '')}"
return ""
1 change: 1 addition & 0 deletions src/codemodder/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def __hash__(self):
@dataclass(frozen=True, kw_only=True)
class SASTResult(Result):
finding_id: str
finding_msg: str | None


@dataclass(frozen=True, kw_only=True)
Expand Down
1 change: 1 addition & 0 deletions src/codemodder/semgrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def from_sarif(
url=semgrep_url_from_id(rule_id),
),
),
finding_msg="TODO",
)


Expand Down
1 change: 1 addition & 0 deletions src/core_codemods/defectdojo/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def from_result(cls, result: dict) -> Self:
url=None,
),
),
finding_msg="TODO",
)

@override
Expand Down
1 change: 1 addition & 0 deletions src/core_codemods/sonar/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def from_result(cls, result: dict) -> Self:
url=sonar_url_from_id(rule_id),
),
),
finding_msg="TODO",
)

def match_location(self, pos, node):
Expand Down
11 changes: 9 additions & 2 deletions tests/test_codeql.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,18 @@ def test_from_sarif(self):
"driver": {"name": "CodeQL"},
"extensions": [
{
"name": "codeql/python-queries",
"rules": [
{"id": "python/sql-injection"},
{
"id": "python/sql-injection",
"fullDescription": {
"text": "Some lengthy description."
},
"help": {"text": "Description\n"},
},
{"id": "cs/web/missing-x-frame-options"},
{"id": "cs/web/xss"},
]
],
},
],
},
Expand Down
Loading