import subprocess import json import time from fastapi import FastAPI app = FastAPI() @app.post("/scan/{repo_name}") async def scan_repository(repo_name: str): repo_path = f"/src/{repo_name}" issues_list = [] start_time = time.time() result = subprocess.run( ["semgrep", "scan", "--config=p/r2c-security-audit", "--json", "--quiet", repo_path], capture_output=True, text=True ) duration = round(time.time() - start_time, 2) try: if result.stdout.strip(): scan_data = json.loads(result.stdout) for item in scan_data.get("results", []): issues_list.append({ "tool": "Semgrep", "file": item.get("path", "").replace(f"{repo_path}/", ""), "line": item.get("start", {}).get("line"), "code": item.get("check_id"), "severity": item.get("extra", {}).get("severity", "MEDIUM").upper(), "message": item.get("extra", {}).get("message", "") }) is_executable = True stderr_output = result.stderr except Exception as e: is_executable = False stderr_output = f"Erreur parsing JSON Semgrep: {str(e)}\n{result.stderr}" return { "results": { "is_executable": is_executable, "runtime": { "exit_code": 0, # <-- ON FORCE À 0 ICI : Tout s'est bien passé pour le scanner "stdout": "Scan statique universel effectué avec succès.", "stderr": stderr_output, "duration_seconds": duration, "timeout_triggered": False }, "issues": issues_list } }