Skip to content
Merged
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
64 changes: 64 additions & 0 deletions .github/scripts/split_sarif_runs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/usr/bin/env python3
"""Split a multi-run SARIF log into one SARIF file per run.

GitHub code scanning rejects SARIF uploads that contain multiple runs with the
same tool/category. This helper keeps shared metadata and writes each run to an
individual file with a deterministic, unique runAutomationDetails.id.
"""

from __future__ import annotations

import argparse
import json
import re
from copy import deepcopy
from pathlib import Path
from typing import Any


def slugify(value: str) -> str:
slug = re.sub(r"[^A-Za-z0-9_.-]+", "-", value.strip()).strip("-._")
return slug or "run"


def tool_name(run: dict[str, Any], index: int) -> str:
driver = run.get("tool", {}).get("driver", {})
return str(driver.get("name") or driver.get("semanticVersion") or f"run-{index + 1}")
Comment on lines +24 to +26
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Si la clé "tool" ou "driver" est explicitement définie à null dans le fichier SARIF, run.get("tool", {}) retournera None (car la clé existe mais sa valeur est null). Cela provoquera une erreur AttributeError: 'NoneType' object has no attribute 'get' lors de l'appel suivant.

Pour éviter cela, utilisez l'opérateur or pour vous assurer d'obtenir un dictionnaire par défaut si la valeur est falsy (comme None).

Suggested change
def tool_name(run: dict[str, Any], index: int) -> str:
driver = run.get("tool", {}).get("driver", {})
return str(driver.get("name") or driver.get("semanticVersion") or f"run-{index + 1}")
def tool_name(run: dict[str, Any], index: int) -> str:
tool = run.get("tool") or {}
driver = tool.get("driver") or {}
return str(driver.get("name") or driver.get("semanticVersion") or f"run-{index + 1}")



def split_sarif(input_path: Path, output_dir: Path, category_prefix: str) -> list[Path]:
sarif = json.loads(input_path.read_text(encoding="utf-8"))
runs = sarif.get("runs")
Comment on lines +30 to +31
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Si le fichier JSON est valide mais contient un tableau à la racine (par exemple []) au lieu d'un objet, json.loads retournera une list. L'appel sarif.get("runs") lèvera alors une exception AttributeError.

Il est préférable de valider que sarif est bien un dictionnaire avant d'appeler .get().

Suggested change
sarif = json.loads(input_path.read_text(encoding="utf-8"))
runs = sarif.get("runs")
sarif = json.loads(input_path.read_text(encoding="utf-8"))
if not isinstance(sarif, dict):
raise ValueError(f"{input_path} is not a valid SARIF object")
runs = sarif.get("runs")

if not isinstance(runs, list) or not runs:
raise ValueError(f"{input_path} does not contain any SARIF runs")

output_dir.mkdir(parents=True, exist_ok=True)
written: list[Path] = []

for index, run in enumerate(runs):
run_copy = deepcopy(run)
category = f"{category_prefix}/{index + 1}-{slugify(tool_name(run_copy, index))}"
run_copy["automationDetails"] = {**run_copy.get("automationDetails", {}), "id": category}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

De la même manière, si "automationDetails" est explicitement défini à null dans le run SARIF, run_copy.get("automationDetails", {}) retournera None, ce qui provoquera une erreur TypeError: 'NoneType' object is not a mapping lors du dépaquetage {**...}.

Il est plus sûr d'utiliser run_copy.get("automationDetails") or {} pour parer à cette éventualité.

Suggested change
run_copy["automationDetails"] = {**run_copy.get("automationDetails", {}), "id": category}
automation_details = run_copy.get("automationDetails") or {}
run_copy["automationDetails"] = {**automation_details, "id": category}


output = {**sarif, "runs": [run_copy]}
destination = output_dir / f"{index + 1:03d}-{slugify(tool_name(run_copy, index))}.sarif"
destination.write_text(json.dumps(output, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
written.append(destination)

return written


def main() -> None:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("input", type=Path, help="SARIF file to split")
parser.add_argument("output_dir", type=Path, help="Directory where split SARIF files are written")
parser.add_argument("--category-prefix", default="codacy", help="Prefix for generated runAutomationDetails.id values")
args = parser.parse_args()

written = split_sarif(args.input, args.output_dir, args.category_prefix)
for path in written:
print(path)


if __name__ == "__main__":
main()
8 changes: 6 additions & 2 deletions .github/workflows/codacy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,12 @@ jobs:
# This will handover control about PR rejection to the GitHub side
max-allowed-issues: 2147483647

# Upload the SARIF file generated in the previous step
# Split Codacy's multi-run SARIF output so every upload has a unique category.
- name: Split SARIF runs
run: python .github/scripts/split_sarif_runs.py results.sarif sarif-results --category-prefix codacy

# Upload the SARIF files generated in the previous step.
- name: Upload SARIF results file
uses: github/codeql-action/upload-sarif@v4
with:
sarif_file: results.sarif
sarif_file: sarif-results
28 changes: 12 additions & 16 deletions .github/workflows/ossar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,30 @@ permissions:

jobs:
OSSAR-Scan:
# OSSAR runs on windows-latest.
# ubuntu-latest and macos-latest support coming soon
# OSSAR currently requires a Windows runner. Pin the image to avoid
# windows-latest migration surprises.
permissions:
contents: read # for actions/checkout to fetch code
security-events: write # for github/codeql-action/upload-sarif to upload SARIF results
actions: read # only required for a private repository by github/codeql-action/upload-sarif to get the Action run status
runs-on: windows-latest
runs-on: windows-2022

steps:
- name: Checkout repository
uses: actions/checkout@v6

# Ensure a compatible version of dotnet is installed.
# The [Microsoft Security Code Analysis CLI](https://aka.ms/mscadocs) is built with dotnet v3.1.201.
# A version greater than or equal to v3.1.201 of dotnet must be installed on the agent in order to run this action.
# GitHub hosted runners already have a compatible version of dotnet installed and this step may be skipped.
# For self-hosted runners, ensure dotnet version 3.1.201 or later is installed by including this action:
# - name: Install .NET
# uses: actions/setup-dotnet@v4
# with:
# dotnet-version: '3.1.x'

# Run open source static analysis tools
# Ensure a compatible version of .NET is installed for OSSAR/MSDO.
- name: Install .NET
uses: actions/setup-dotnet@v5
with:
dotnet-version: '6.0.x'

# Run open source static analysis tools.
- name: Run OSSAR
uses: github/ossar-action@v2
uses: github/ossar-action@v2.0.0
id: ossar

# Upload results to the Security tab
# Upload results to the Security tab.
- name: Upload OSSAR results
uses: github/codeql-action/upload-sarif@v4
with:
Expand Down
Loading