Skip to content

Commit f20c953

Browse files
committed
feat: add JSON output and severity filtering
1 parent 9878c72 commit f20c953

4 files changed

Lines changed: 48 additions & 15 deletions

File tree

cli.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,17 @@
1717
help="Path to the directory to scan"
1818
)
1919

20+
parser.add_argument("--json", action="store_true")
21+
22+
parser.add_argument("--severity", choices=["LOW", "MEDIUM", "HIGH"])
23+
2024
# Parse command-line arguments
2125
args = parser.parse_args()
2226

2327
# Extract the input path for use in the application
2428
input_path = args.path
2529

30+
use_json = args.json
31+
32+
chosen_severity = args.severity
33+

detectors/find_secrets.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def detect_ast_secrets(code):
179179
if vulnerabilities:
180180
for pattern_name, severity, value in vulnerabilities:
181181
findings.append((line_number, pattern_name, severity, value))
182-
182+
183183
return findings
184184

185185

main.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
Errors related to invalid input paths are handled gracefully.
1212
"""
1313

14-
from cli import input_path
15-
from scanner import check_path, scan, list_python_files, output
14+
from cli import input_path, chosen_severity, use_json
15+
from scanner import check_path, scan, list_python_files, output, filter_results
1616

1717

1818
if __name__ == "__main__":
@@ -26,8 +26,10 @@
2626
# Run the scanning engine and collect findings
2727
results = scan(files)
2828

29+
filtered_findings = filter_results(results, chosen_severity)
30+
2931
# Output results to the CLI
30-
output(results)
32+
output(filtered_findings, use_json, files)
3133

3234
except FileNotFoundError as e:
3335
# Display a user-friendly error message for invalid paths

scanner.py

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from pathlib import Path
22
from detectors.find_secrets import detect_ast_secrets
3-
3+
import json
44

55
def check_path(input_path):
66
"""
@@ -64,8 +64,6 @@ def scan(files):
6464
print("No Python files found.")
6565
return []
6666

67-
print(f"Scanning {len(files)} Python files...")
68-
6967
files = sorted(files) # Ensure deterministic output order
7068
findings = []
7169

@@ -76,13 +74,32 @@ def scan(files):
7674
content = f.read()
7775

7876
ast_results = detect_ast_secrets(content)
79-
for line_number, var_name, severity, value in ast_results:
80-
findings.append((line_number, file, var_name, severity, value))
77+
for line_number, rule_name, severity, value in ast_results:
78+
findings.append((line_number, file, rule_name, severity, value))
8179

8280
return findings
8381

84-
85-
def output(results):
82+
def filter_results(results, chosen_severity):
83+
filtered_findings = []
84+
for line_number, file, rule_name, severity, value in results:
85+
if severity == chosen_severity or chosen_severity is None:
86+
filtered_findings.append((line_number, file, rule_name, severity, value))
87+
return filtered_findings
88+
89+
def output_json(filtered_findings):
90+
json_results = []
91+
for line_number, file, rule_name, severity, value in filtered_findings:
92+
finding = {
93+
"line": line_number,
94+
"file": str(file),
95+
"rule": rule_name,
96+
"severity": severity,
97+
"value": value,
98+
}
99+
json_results.append(finding)
100+
print(json.dumps(json_results, indent=2))
101+
102+
def output(filtered_findings, use_json, files):
86103
"""
87104
Display scan results in a structured CLI format.
88105
@@ -96,16 +113,22 @@ def output(results):
96113
Returns:
97114
None
98115
"""
99-
if not results:
116+
if use_json:
117+
output_json(filtered_findings)
118+
return
119+
120+
print(f"Scanning {len(files)} Python files...")
121+
122+
if not filtered_findings:
100123
print("\nNo vulnerabilities found.")
101124
else:
102125
print("\n--- Findings ---\n")
103126

104-
for line_number, file, rule_name, severity, match in results:
127+
for line_number, file, rule_name, severity, value in filtered_findings:
105128
print(
106129
f"[{severity}] "
107130
f"{file}:{line_number} "
108-
f"{rule_name}{match}"
131+
f"{rule_name}{value}"
109132
)
110133

111-
print(f"\nTotal findings: {len(results)}")
134+
print(f"\nTotal findings: {len(filtered_findings)}")

0 commit comments

Comments
 (0)