SASTs
codesectools.sasts.core
Initializes the core SAST module.
Modules:
Name | Description |
---|---|
cli |
Provides a factory for building command-line interfaces for SAST tools. |
graphics |
Provides classes for generating plots and visualizations from analysis results. |
parser |
Defines the core abstract classes for parsing SAST tool results. |
cli
Provides a factory for building command-line interfaces for SAST tools.
This module contains the CLIFactory
class, which simplifies the creation of
standardized typer
CLI commands (analyze, benchmark, list, plot) for any
SAST integration.
CLIFactory
CLIFactory(sast: SAST, custom_messages: dict)
Provide a factory to generate a standard set of CLI commands for a SAST tool.
Attributes:
Name | Type | Description |
---|---|---|
cli |
typer.Typer
|
The |
sast |
SAST
|
The SAST tool instance for which the CLI is being built. |
help_messages |
dict
|
A dictionary of help messages for the standard commands. |
Initialize the CLIFactory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
SAST
|
An instance of the SAST tool's implementation class. |
required |
|
dict
|
A dictionary of custom help messages to override the defaults. |
required |
Methods:
Name | Description |
---|---|
build_cli |
Build and return the Typer CLI application for the SAST tool. |
add_main |
Add the main callback command to the CLI. |
add_install |
Add the 'install' command to the CLI. |
add_analyze |
Add the 'analyze' command to the CLI. |
add_benchmark |
Add the 'benchmark' command to the CLI. |
add_list |
Add the 'list' command to the CLI. |
add_plot |
Add the 'plot' command to the CLI. |
help_messages
instance-attribute
help_messages = {
"main": f"{sast.name}",
"install": "List instruction to install missing requirements.",
"analyze": f"Analyze a project using {sast.name}.",
"benchmark": f"Benchmark a dataset using {sast.name}.",
"list": "List existing analysis results.",
"plot": "Generate plot for results visualization.",
}
add_main
Add the main callback command to the CLI.
This function sets up the main callback that runs when the SAST-specific command is invoked without a subcommand.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str
|
The help string for the main command. |
''
|
add_install
Add the 'install' command to the CLI.
This command opens the tool's installation guide URL in a web browser.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str
|
The help string for the command. |
''
|
add_analyze
Add the 'analyze' command to the CLI.
This command runs the SAST tool on the current directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str
|
The help string for the command. |
''
|
add_benchmark
Add the 'benchmark' command to the CLI.
This command runs the SAST tool against a specified dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str
|
The help string for the command. |
''
|
add_list
Add the 'list' command to the CLI.
This command lists all available analysis results for the SAST tool.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str
|
The help string for the command. |
''
|
graphics
Provides classes for generating plots and visualizations from analysis results.
This module contains base and specific graphics classes that use Matplotlib to create visual representations of SAST analysis data, such as defect distributions and benchmark performance.
Graphics
Graphics(sast: SAST, project_name: str)
Base class for generating graphics from SAST results.
Attributes:
Name | Type | Description |
---|---|---|
sast |
SAST
|
The SAST tool instance. |
output_dir |
Path
|
The directory containing the analysis results. |
color_mapping |
dict
|
A mapping of categories to colors for plotting. |
plot_functions |
list
|
A list of methods that generate plots. |
limit |
int
|
The maximum number of items to show in top-N plots. |
has_latex |
bool
|
True if a LaTeX installation is found. |
Initialize the Graphics object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
SAST
|
The SAST tool instance. |
required |
|
str
|
The name of the project or dataset being visualized. |
required |
Methods:
Name | Description |
---|---|
export |
Generate, save, and optionally display all registered plots. |
export
Generate, save, and optionally display all registered plots.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
bool
|
If True, overwrite existing figure files. |
required |
|
bool
|
If True and LaTeX is available, export figures in PGF format. |
required |
|
bool
|
If True, open the generated figures using the default viewer. |
required |
ProjectGraphics
ProjectGraphics(sast: SAST, project_name: str)
Bases: Graphics
Generate graphics for a single project analysis result.
Attributes:
Name | Type | Description |
---|---|---|
result |
AnalysisResult
|
The loaded analysis result data. |
Initialize the ProjectGraphics object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
SAST
|
The SAST tool instance. |
required |
|
str
|
The name of the project. |
required |
Methods:
Name | Description |
---|---|
checker_to_category |
Map a checker name to its category. |
plot_overview |
Generate an overview plot with stats by files, checkers, and categories. |
checker_to_category
FileDatasetGraphics
FileDatasetGraphics(sast: SAST, dataset: FileDataset)
Bases: ProjectGraphics
Generate graphics for a file-based dataset benchmark result.
Attributes:
Name | Type | Description |
---|---|---|
dataset |
FileDataset
|
The dataset instance used for the benchmark. |
benchmark_data |
FileDatasetData
|
The validated benchmark data. |
Initialize the FileDatasetGraphics object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
SAST
|
The SAST tool instance. |
required |
|
FileDataset
|
The file-based dataset that was benchmarked. |
required |
Methods:
Name | Description |
---|---|
plot_top_cwes |
Generate a plot showing the top predicted CWEs. |
GitRepoDatasetGraphics
GitRepoDatasetGraphics(sast: SAST, dataset: GitRepoDataset)
Bases: Graphics
Generate graphics for a Git repository-based dataset benchmark result.
Attributes:
Name | Type | Description |
---|---|---|
dataset |
GitRepoDataset
|
The dataset instance used for the benchmark. |
results |
list[AnalysisResult]
|
A list of loaded analysis results. |
benchmark_data |
GitRepoDatasetData
|
The validated benchmark data. |
Initialize the GitRepoDatasetGraphics object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
SAST
|
The SAST tool instance. |
required |
|
GitRepoDataset
|
The Git repository-based dataset that was benchmarked. |
required |
Methods:
Name | Description |
---|---|
checker_to_category |
Map a checker name to its category. |
plot_overview |
Generate an overview plot classifying defects. |
plot_top_cwes |
Generate a plot showing the top predicted CWEs. |
plot_defects_per_loc |
Generate a scatter plot of defects found versus lines of code. |
plot_time_per_loc |
Generate a scatter plot of analysis time versus lines of code. |
checker_to_category
plot_overview
Generate an overview plot classifying defects.
Returns:
Type | Description |
---|---|
Figure
|
A Matplotlib Figure object containing the plot. |
plot_top_cwes
Generate a plot showing the top predicted CWEs.
Returns:
Type | Description |
---|---|
Figure
|
A Matplotlib Figure object containing the plot. |
plot_defects_per_loc
Generate a scatter plot of defects found versus lines of code.
Returns:
Type | Description |
---|---|
Figure
|
A Matplotlib Figure object containing the plot. |
parser
Defines the core abstract classes for parsing SAST tool results.
This module provides the Defect
and AnalysisResult
classes, which serve as
standardized data structures for holding information about vulnerabilities and
the overall analysis process. Each SAST integration must implement a concrete
subclass of AnalysisResult
to parse its specific output format.
Defect
Represent a single defect or finding reported by a SAST tool.
Attributes:
Name | Type | Description |
---|---|---|
file |
str
|
The name of the file where the defect was found. |
checker |
str
|
The name of the checker or rule that reported the defect. |
category |
str
|
The category of the checker (e.g., security, performance). |
cwe |
CWE
|
The CWE associated with the defect. |
data |
tuple[Any]
|
A tuple containing the raw data for the defect. |
Initialize a Defect instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str
|
The file path of the defect. |
required |
|
str
|
The name of the rule/checker. |
required |
|
str
|
The category of the checker. |
required |
|
CWE
|
The CWE associated with the defect. |
required |
|
tuple[Any]
|
Raw data from the SAST tool for this defect. |
required |
Methods:
Name | Description |
---|---|
__repr__ |
Return a developer-friendly string representation of the Defect. |
AnalysisResult
AnalysisResult(
name: str,
lang: str,
files: list[str],
defects: list[Defect],
time: float,
loc: int,
data: tuple[Any],
)
Bases: ABC
Abstract base class for holding the parsed results of a SAST analysis.
Attributes:
Name | Type | Description |
---|---|---|
name |
str
|
The name of the analyzed project or dataset. |
lang |
str
|
The primary programming language analyzed. |
files |
list[str]
|
A list of files that were analyzed. |
defects |
list[Defect]
|
A list of |
time |
float
|
The duration of the analysis in seconds. |
loc |
int
|
The number of lines of code analyzed. |
data |
tuple[Any]
|
A tuple containing raw data from the analysis. |
Initialize an AnalysisResult instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str
|
The name of the analyzed project/dataset. |
required |
|
str
|
The programming language of the code. |
required |
|
list[str]
|
A list of analyzed files. |
required |
|
list[Defect]
|
A list of |
required |
|
float
|
The analysis duration in seconds. |
required |
|
int
|
The lines of code analyzed. |
required |
|
tuple[Any]
|
Raw data from the SAST tool's output. |
required |
Methods:
Name | Description |
---|---|
__repr__ |
Return a developer-friendly string representation of the AnalysisResult. |
load_from_output_dir |
Load and parse analysis results from a specified directory. |
load_from_output_dirs |
Load and parse analysis results from multiple directories. |
checker_to_category |
Get the category for a given checker name. |
stats_by_checkers |
Calculate statistics on defects, grouped by checker. |
stats_by_categories |
Calculate statistics on defects, grouped by category. |
stats_by_files |
Calculate statistics on defects, grouped by file. |
stats_by_cwes |
Calculate statistics on defects, grouped by CWE ID. |
__repr__
__repr__() -> str
Return a developer-friendly string representation of the AnalysisResult.
Returns:
Type | Description |
---|---|
str
|
A string showing key metrics of the analysis. |
load_from_output_dir
abstractmethod
classmethod
load_from_output_dir(output_dir: Path) -> Self
Load and parse analysis results from a specified directory.
This method must be implemented by subclasses to handle the specific output files of a SAST tool.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Path
|
The directory containing the raw analysis output files. |
required |
Returns:
Type | Description |
---|---|
Self
|
An instance of the |
load_from_output_dirs
classmethod
load_from_output_dirs(output_dirs: list[str]) -> list[Self]
checker_to_category
stats_by_checkers
stats_by_checkers() -> dict
stats_by_categories
stats_by_categories() -> dict
stats_by_files
stats_by_files() -> dict