slither.utils.sarif
Various utils for sarif/vscode
1""" 2Various utils for sarif/vscode 3""" 4import json 5from pathlib import Path 6from typing import List, Dict, Optional, Tuple, Any 7 8 9def _parse_index(key: str) -> Optional[Tuple[int, int]]: 10 if key.count(":") != 2: 11 return None 12 13 try: 14 run = int(key[key.find(":") + 1 : key.rfind(":")]) 15 index = int(key[key.rfind(":") + 1 :]) 16 return run, index 17 except ValueError: 18 return None 19 20 21def _get_indexes(path_to_triage: Path) -> List[Tuple[int, int]]: 22 try: 23 with open(path_to_triage, encoding="utf8") as file_desc: 24 triage = json.load(file_desc) 25 except json.decoder.JSONDecodeError: 26 return [] 27 28 resultIdToNotes: Dict[str, Dict] = triage.get("resultIdToNotes", {}) 29 30 indexes: List[Tuple[int, int]] = [] 31 for key, data in resultIdToNotes.items(): 32 if "status" in data and data["status"] == 1: 33 parsed = _parse_index(key) 34 if parsed: 35 indexes.append(parsed) 36 37 return indexes 38 39 40def read_triage_info(path_to_sarif: Path, path_to_triage: Path) -> List[str]: 41 try: 42 with open(path_to_sarif, encoding="utf8") as file_desc: 43 sarif = json.load(file_desc) 44 except json.decoder.JSONDecodeError: 45 return [] 46 47 runs: List[Dict[str, Any]] = sarif.get("runs", []) 48 49 # Don't support multiple runs for now 50 if len(runs) != 1: 51 return [] 52 53 run_results: List[Dict] = runs[0].get("results", []) 54 55 indexes = _get_indexes(path_to_triage) 56 57 ids: List[str] = [] 58 for run, index in indexes: 59 60 # We dont support multiple runs for now 61 if run != 0: 62 continue 63 try: 64 elem = run_results[index] 65 except KeyError: 66 continue 67 if "partialFingerprints" in elem: 68 if "id" in elem["partialFingerprints"]: 69 ids.append(elem["partialFingerprints"]["id"]) 70 71 return ids
def
read_triage_info(path_to_sarif: pathlib.Path, path_to_triage: pathlib.Path) -> List[str]:
41def read_triage_info(path_to_sarif: Path, path_to_triage: Path) -> List[str]: 42 try: 43 with open(path_to_sarif, encoding="utf8") as file_desc: 44 sarif = json.load(file_desc) 45 except json.decoder.JSONDecodeError: 46 return [] 47 48 runs: List[Dict[str, Any]] = sarif.get("runs", []) 49 50 # Don't support multiple runs for now 51 if len(runs) != 1: 52 return [] 53 54 run_results: List[Dict] = runs[0].get("results", []) 55 56 indexes = _get_indexes(path_to_triage) 57 58 ids: List[str] = [] 59 for run, index in indexes: 60 61 # We dont support multiple runs for now 62 if run != 0: 63 continue 64 try: 65 elem = run_results[index] 66 except KeyError: 67 continue 68 if "partialFingerprints" in elem: 69 if "id" in elem["partialFingerprints"]: 70 ids.append(elem["partialFingerprints"]["id"]) 71 72 return ids