|
| 1 | +"""Provide classes for parsing Bearer analysis results. |
| 2 | +
|
| 3 | +This module defines `BearerFinding` and `BearerAnalysisResult` to process |
| 4 | +the JSON output from a Bearer scan, converting it into the standardized |
| 5 | +format used by CodeSecTools. |
| 6 | +""" |
| 7 | + |
| 8 | +import json |
| 9 | +from pathlib import Path |
| 10 | +from typing import Self |
| 11 | + |
| 12 | +from codesectools.sasts.core.parser import AnalysisResult, Defect |
| 13 | +from codesectools.shared.cwe import CWEs |
| 14 | +from codesectools.utils import MissingFile |
| 15 | + |
| 16 | + |
| 17 | +class BearerFinding(Defect): |
| 18 | + """Represent a single defect found by Bearer.""" |
| 19 | + |
| 20 | + def __init__(self, defect_data: dict, severity: str) -> None: |
| 21 | + """Initialize a BearerFinding instance. |
| 22 | +
|
| 23 | + Args: |
| 24 | + defect_data: A dictionary representing a single finding from the JSON output. |
| 25 | + severity: The severity level of the finding. |
| 26 | +
|
| 27 | + """ |
| 28 | + super().__init__( |
| 29 | + file=Path(defect_data["filename"]).name, |
| 30 | + checker=defect_data["id"], |
| 31 | + category=severity, |
| 32 | + cwe=CWEs.from_id(int(defect_data["cwe_ids"][0])), |
| 33 | + data=defect_data, |
| 34 | + ) |
| 35 | + |
| 36 | + |
| 37 | +class BearerAnalysisResult(AnalysisResult): |
| 38 | + """Represent the complete result of a Bearer analysis.""" |
| 39 | + |
| 40 | + def __init__(self, output_dir: Path, result_data: dict, cmdout: dict) -> None: |
| 41 | + """Initialize a BearerAnalysisResult instance. |
| 42 | +
|
| 43 | + Args: |
| 44 | + output_dir: The directory where the results are stored. |
| 45 | + result_data: Parsed data from the main Bearer JSON output. |
| 46 | + cmdout: A dictionary with metadata from the command execution. |
| 47 | +
|
| 48 | + """ |
| 49 | + super().__init__( |
| 50 | + name=output_dir.name, |
| 51 | + lang=cmdout["lang"], |
| 52 | + files=[], |
| 53 | + defects=[], |
| 54 | + time=cmdout["duration"], |
| 55 | + loc=cmdout["loc"], |
| 56 | + data=(result_data, cmdout), |
| 57 | + ) |
| 58 | + |
| 59 | + for severity, findings in result_data.items(): |
| 60 | + for finding in findings: |
| 61 | + self.files.append(Path(finding["filename"]).name) |
| 62 | + self.defects.append( |
| 63 | + BearerFinding(defect_data=finding, severity=severity) |
| 64 | + ) |
| 65 | + |
| 66 | + @classmethod |
| 67 | + def load_from_output_dir(cls, output_dir: Path) -> Self: |
| 68 | + """Load and parse Bearer analysis results from a directory. |
| 69 | +
|
| 70 | + Read `bearer_output.json` and `cstools_output.json` to construct a complete |
| 71 | + analysis result object. |
| 72 | +
|
| 73 | + Args: |
| 74 | + output_dir: The directory containing the Bearer output files. |
| 75 | +
|
| 76 | + Returns: |
| 77 | + An instance of `BearerAnalysisResult`. |
| 78 | +
|
| 79 | + Raises: |
| 80 | + MissingFile: If a required result file is not found. |
| 81 | +
|
| 82 | + """ |
| 83 | + # Cmdout |
| 84 | + cmdout = json.load((output_dir / "cstools_output.json").open()) |
| 85 | + |
| 86 | + # Analysis outputs |
| 87 | + analysis_output_path = output_dir / "bearer_output.json" |
| 88 | + if analysis_output_path.is_file(): |
| 89 | + analysis_output = json.load(analysis_output_path.open("r")) |
| 90 | + else: |
| 91 | + raise MissingFile(["bearer_output.json"]) |
| 92 | + |
| 93 | + return cls(output_dir, analysis_output, cmdout) |
0 commit comments