|
| 1 | +"""Generates SARIF reports for aggregated SAST analysis results.""" |
| 2 | + |
| 3 | +from typing import Optional |
| 4 | + |
| 5 | +from codesectools.sasts.all.report import Report |
| 6 | +from codesectools.sasts.core.parser.format.SARIF import ( |
| 7 | + ArtifactLocation, |
| 8 | + Location, |
| 9 | + Message, |
| 10 | + PhysicalLocation, |
| 11 | + PropertyBag, |
| 12 | + Region, |
| 13 | + ReportingDescriptor, |
| 14 | + Result, |
| 15 | + Run, |
| 16 | + Tool, |
| 17 | + ToolComponent, |
| 18 | +) |
| 19 | +from codesectools.sasts.core.parser.format.SARIF import ( |
| 20 | + StaticAnalysisResultsFormatSarifVersion210JsonSchema as SARIF, |
| 21 | +) |
| 22 | + |
| 23 | + |
| 24 | +class SARIFReport(Report): |
| 25 | + """Generate SARIF reports for SAST analysis results. |
| 26 | +
|
| 27 | + Attributes: |
| 28 | + format (str): The format of the report, which is "SARIF". |
| 29 | + project (str): The name of the project. |
| 30 | + all_sast (AllSAST): The AllSAST manager instance. |
| 31 | + report_dir (Path): The directory where reports are saved. |
| 32 | + result (AllSASTAnalysisResult): The parsed analysis results. |
| 33 | + report_data (dict): The data prepared for rendering the report. |
| 34 | +
|
| 35 | + """ |
| 36 | + |
| 37 | + format = "SARIF" |
| 38 | + |
| 39 | + def generate(self) -> None: |
| 40 | + """Generate the SARIF report.""" |
| 41 | + included_defect_file = self.report_data.keys() |
| 42 | + |
| 43 | + runs: list[Run] = [] |
| 44 | + for analysis_result in self.result.analysis_results.values(): |
| 45 | + results: list[Result] = [] |
| 46 | + rules: list[ReportingDescriptor] = [] |
| 47 | + rule_ids = set() |
| 48 | + |
| 49 | + for defect in analysis_result.defects: |
| 50 | + if defect.filepath_str not in included_defect_file: |
| 51 | + continue |
| 52 | + |
| 53 | + relative_uri = defect.filepath.relative_to( |
| 54 | + analysis_result.source_path |
| 55 | + ).as_posix() |
| 56 | + |
| 57 | + region: Optional[Region] = None |
| 58 | + if defect.lines: |
| 59 | + start_line_num = min(defect.lines) |
| 60 | + end_line_num = ( |
| 61 | + max(defect.lines) if len(defect.lines) > 1 else start_line_num |
| 62 | + ) |
| 63 | + |
| 64 | + region = Region(start_line=start_line_num, end_line=end_line_num) |
| 65 | + |
| 66 | + physical_location = PhysicalLocation( |
| 67 | + artifact_location=ArtifactLocation( |
| 68 | + uri=relative_uri, uri_base_id="%SRCROOT%" |
| 69 | + ), |
| 70 | + region=region, |
| 71 | + ) |
| 72 | + |
| 73 | + result = Result( |
| 74 | + rule_id=defect.checker, |
| 75 | + level=defect.level, |
| 76 | + message=Message(text=defect.message), |
| 77 | + locations=[Location(physical_location=physical_location)], # ty:ignore[missing-argument] |
| 78 | + properties=PropertyBag( |
| 79 | + __root__={"cwe": str(defect.cwe)} # ty:ignore[unknown-argument] |
| 80 | + ), |
| 81 | + ) # ty:ignore[missing-argument] |
| 82 | + results.append(result) |
| 83 | + |
| 84 | + if defect.checker not in rule_ids: |
| 85 | + rules.append(ReportingDescriptor(id=defect.checker)) # ty:ignore[missing-argument] |
| 86 | + rule_ids.add(defect.checker) |
| 87 | + |
| 88 | + tool = Tool( |
| 89 | + driver=ToolComponent( |
| 90 | + name=analysis_result.sast_name, |
| 91 | + rules=rules, |
| 92 | + ) # ty:ignore[missing-argument] |
| 93 | + ) # ty:ignore[missing-argument] |
| 94 | + |
| 95 | + run = Run( |
| 96 | + tool=tool, |
| 97 | + results=results, |
| 98 | + original_uri_base_ids={ |
| 99 | + "%SRCROOT%": ArtifactLocation( |
| 100 | + uri=analysis_result.source_path.resolve().as_uri() |
| 101 | + ) |
| 102 | + }, |
| 103 | + properties=PropertyBag( |
| 104 | + __root__={ |
| 105 | + "lines_of_codes": analysis_result.lines_of_codes, |
| 106 | + "analysis_time_seconds": analysis_result.time, |
| 107 | + "language": analysis_result.lang, |
| 108 | + } # ty:ignore[unknown-argument] |
| 109 | + ), |
| 110 | + ) # ty:ignore[missing-argument] |
| 111 | + |
| 112 | + runs.append(run) |
| 113 | + |
| 114 | + sarif_report = SARIF( |
| 115 | + version="2.1.0", |
| 116 | + runs=runs, |
| 117 | + ) |
| 118 | + |
| 119 | + sarif_file = (self.report_dir / self.result.name).with_suffix(".sarif") |
| 120 | + sarif_file.write_text( |
| 121 | + sarif_report.model_dump_json(by_alias=True, exclude_none=True, indent=2) |
| 122 | + ) |
0 commit comments