Skip to content

Commit c81fe57

Browse files
committed
Add support for SARIF file output
Signed-off-by: lelia <lelia@socket.dev>
1 parent ceb3572 commit c81fe57

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

socketsecurity/config.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class CliConfig:
4040
allow_unverified: bool = False
4141
enable_json: bool = False
4242
enable_sarif: bool = False
43+
sarif_file: Optional[str] = None
4344
enable_gitlab_security: bool = False
4445
gitlab_security_file: Optional[str] = None
4546
disable_overview: bool = False
@@ -103,6 +104,10 @@ def from_args(cls, args_list: Optional[List[str]] = None) -> 'CliConfig':
103104
args.api_token
104105
)
105106

107+
# --sarif-file implies --enable-sarif
108+
if args.sarif_file:
109+
args.enable_sarif = True
110+
106111
# Strip quotes from commit message if present
107112
commit_message = args.commit_message
108113
if commit_message and commit_message.startswith('"') and commit_message.endswith('"'):
@@ -126,6 +131,7 @@ def from_args(cls, args_list: Optional[List[str]] = None) -> 'CliConfig':
126131
'allow_unverified': args.allow_unverified,
127132
'enable_json': args.enable_json,
128133
'enable_sarif': args.enable_sarif,
134+
'sarif_file': args.sarif_file,
129135
'enable_gitlab_security': args.enable_gitlab_security,
130136
'gitlab_security_file': args.gitlab_security_file,
131137
'disable_overview': args.disable_overview,
@@ -471,6 +477,13 @@ def create_argument_parser() -> argparse.ArgumentParser:
471477
action="store_true",
472478
help="Enable SARIF output of results instead of table or JSON format"
473479
)
480+
output_group.add_argument(
481+
"--sarif-file",
482+
dest="sarif_file",
483+
metavar="<path>",
484+
default=None,
485+
help="Output file path for SARIF report (implies --enable-sarif)"
486+
)
474487
output_group.add_argument(
475488
"--enable-gitlab-security",
476489
dest="enable_gitlab_security",

socketsecurity/output.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ def output_console_json(self, diff_report: Diff, sbom_file_name: Optional[str] =
139139
def output_console_sarif(self, diff_report: Diff, sbom_file_name: Optional[str] = None) -> None:
140140
"""
141141
Generate SARIF output from the diff report and print to console.
142+
If --sarif-file is configured, also save to file.
142143
"""
143144
if diff_report.id != "NO_DIFF_RAN":
144145
# Generate the SARIF structure using Messages
@@ -147,6 +148,14 @@ def output_console_sarif(self, diff_report: Diff, sbom_file_name: Optional[str]
147148
# Print the SARIF output to the console in JSON format
148149
print(json.dumps(console_security_comment, indent=2))
149150

151+
# Save to file if --sarif-file is specified
152+
if self.config.sarif_file:
153+
sarif_path = Path(self.config.sarif_file)
154+
sarif_path.parent.mkdir(parents=True, exist_ok=True)
155+
with open(sarif_path, "w") as f:
156+
json.dump(console_security_comment, f, indent=2)
157+
self.logger.info(f"SARIF report saved to {self.config.sarif_file}")
158+
150159
def report_pass(self, diff_report: Diff) -> bool:
151160
"""Determines if the report passes security checks"""
152161
# Priority 1: --disable-blocking always passes

0 commit comments

Comments
 (0)