secguard-cli 1.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- secguard/__init__.py +0 -0
- secguard/ai_engine.py +1556 -0
- secguard/cli.py +875 -0
- secguard/config.py +172 -0
- secguard/dependency_scanner.py +513 -0
- secguard/patcher.py +707 -0
- secguard/reporter.py +276 -0
- secguard/scanner.py +5009 -0
- secguard/test.py +5 -0
- secguard_cli-1.1.0.dist-info/METADATA +106 -0
- secguard_cli-1.1.0.dist-info/RECORD +15 -0
- secguard_cli-1.1.0.dist-info/WHEEL +5 -0
- secguard_cli-1.1.0.dist-info/entry_points.txt +2 -0
- secguard_cli-1.1.0.dist-info/licenses/LICENSE +21 -0
- secguard_cli-1.1.0.dist-info/top_level.txt +1 -0
secguard/reporter.py
ADDED
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
import difflib
|
|
2
|
+
import json
|
|
3
|
+
import re
|
|
4
|
+
from datetime import datetime, timezone
|
|
5
|
+
|
|
6
|
+
SARIF_SCHEMA = "https://json.schemastore.org/sarif-2.1.0.json"
|
|
7
|
+
SEVERITIES = ("CRITICAL", "HIGH", "MEDIUM", "LOW")
|
|
8
|
+
SARIF_LEVELS = {
|
|
9
|
+
"CRITICAL": "error",
|
|
10
|
+
"HIGH": "error",
|
|
11
|
+
"MEDIUM": "warning",
|
|
12
|
+
"LOW": "note",
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def _severity_counts(findings: list[dict]) -> dict[str, int]:
|
|
17
|
+
return {
|
|
18
|
+
severity: sum(
|
|
19
|
+
str(finding.get("severity", "")).upper() == severity
|
|
20
|
+
for finding in findings
|
|
21
|
+
)
|
|
22
|
+
for severity in SEVERITIES
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _patch_diff(finding: dict) -> str:
|
|
27
|
+
vulnerable = str(finding.get("vulnerable_code", ""))
|
|
28
|
+
fixed = str(finding.get("fixed_code", ""))
|
|
29
|
+
if not vulnerable or not fixed or vulnerable == fixed:
|
|
30
|
+
return "No recommended patch diff."
|
|
31
|
+
|
|
32
|
+
file_path = str(finding.get("file", "file"))
|
|
33
|
+
diff = difflib.unified_diff(
|
|
34
|
+
vulnerable.splitlines(),
|
|
35
|
+
fixed.splitlines(),
|
|
36
|
+
fromfile=f"a/{file_path}",
|
|
37
|
+
tofile=f"b/{file_path}",
|
|
38
|
+
lineterm="",
|
|
39
|
+
)
|
|
40
|
+
return "\n".join(diff)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def render_markdown_report(findings: list[dict]) -> str:
|
|
44
|
+
counts = _severity_counts(findings)
|
|
45
|
+
unknown_count = len(findings) - sum(counts.values())
|
|
46
|
+
lines = [
|
|
47
|
+
"# SecGuard Security Report",
|
|
48
|
+
"",
|
|
49
|
+
f"Total verified findings: **{len(findings)}**",
|
|
50
|
+
"",
|
|
51
|
+
(
|
|
52
|
+
"| Total | Critical | High | Medium | Low | Unknown |"
|
|
53
|
+
),
|
|
54
|
+
"| ---: | ---: | ---: | ---: | ---: | ---: |",
|
|
55
|
+
(
|
|
56
|
+
f"| {len(findings)} | {counts['CRITICAL']} | {counts['HIGH']} | "
|
|
57
|
+
f"{counts['MEDIUM']} | {counts['LOW']} | {unknown_count} |"
|
|
58
|
+
),
|
|
59
|
+
]
|
|
60
|
+
|
|
61
|
+
if not findings:
|
|
62
|
+
lines.extend(["", "No verified findings."])
|
|
63
|
+
|
|
64
|
+
for index, finding in enumerate(findings, start=1):
|
|
65
|
+
file_path = str(finding.get("file", "Unknown file"))
|
|
66
|
+
line_number = finding.get("line", "?")
|
|
67
|
+
lines.extend([
|
|
68
|
+
"",
|
|
69
|
+
f"## {index}. {finding.get('issue', 'Security finding')}",
|
|
70
|
+
"",
|
|
71
|
+
f"- **File:** `{file_path}:{line_number}`",
|
|
72
|
+
f"- **Severity:** {str(finding.get('severity', 'UNKNOWN')).upper()}",
|
|
73
|
+
f"- **CWE:** {finding.get('cwe') or 'Not specified'}",
|
|
74
|
+
f"- **Description:** {finding.get('explanation', '')}",
|
|
75
|
+
])
|
|
76
|
+
if finding.get("commit"):
|
|
77
|
+
lines.append(f"- **Commit:** `{finding['commit']}`")
|
|
78
|
+
if finding.get("suppressed"):
|
|
79
|
+
lines.append(
|
|
80
|
+
"- **Status:** Suppressed (`secguard: ignore`)"
|
|
81
|
+
)
|
|
82
|
+
lines.extend([
|
|
83
|
+
"",
|
|
84
|
+
"### Recommended patch diff",
|
|
85
|
+
"",
|
|
86
|
+
"```diff",
|
|
87
|
+
_patch_diff(finding),
|
|
88
|
+
"```",
|
|
89
|
+
])
|
|
90
|
+
|
|
91
|
+
return "\n".join(lines) + "\n"
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def _rule_id(finding: dict) -> str:
|
|
95
|
+
cwe = finding.get("cwe")
|
|
96
|
+
if isinstance(cwe, str) and re.fullmatch(r"CWE-\d+", cwe, re.IGNORECASE):
|
|
97
|
+
return cwe.upper()
|
|
98
|
+
issue = str(finding.get("issue", "Security finding"))
|
|
99
|
+
return re.sub(r"[^A-Za-z0-9._-]+", "-", issue).strip("-") or "security-finding"
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def _sarif_rule(finding: dict, rule_id: str) -> dict:
|
|
103
|
+
issue = str(finding.get("issue", "Security finding"))
|
|
104
|
+
severity = str(finding.get("severity", "LOW")).upper()
|
|
105
|
+
rule = {
|
|
106
|
+
"id": rule_id,
|
|
107
|
+
"name": issue,
|
|
108
|
+
"shortDescription": {"text": issue},
|
|
109
|
+
"help": {"text": "Review the finding and apply a secure fix."},
|
|
110
|
+
"defaultConfiguration": {"level": SARIF_LEVELS.get(severity, "warning")},
|
|
111
|
+
}
|
|
112
|
+
cwe = finding.get("cwe")
|
|
113
|
+
if isinstance(cwe, str) and re.fullmatch(r"CWE-\d+", cwe, re.IGNORECASE):
|
|
114
|
+
cwe_number = cwe.split("-", 1)[1]
|
|
115
|
+
rule["helpUri"] = f"https://cwe.mitre.org/data/definitions/{cwe_number}.html"
|
|
116
|
+
rule["properties"] = {"tags": ["security", f"external/cwe/{cwe.lower()}"]}
|
|
117
|
+
return rule
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def _sarif_result(finding: dict, rule_id: str) -> dict:
|
|
121
|
+
severity = str(finding.get("severity", "LOW")).upper()
|
|
122
|
+
file_path = str(finding.get("file", "unknown"))
|
|
123
|
+
issue = str(finding.get("issue", ""))
|
|
124
|
+
explanation = str(finding.get("explanation") or "No explanation provided.")
|
|
125
|
+
line_number = finding.get("line", 1)
|
|
126
|
+
try:
|
|
127
|
+
line_number = max(1, int(line_number))
|
|
128
|
+
except (TypeError, ValueError):
|
|
129
|
+
line_number = 1
|
|
130
|
+
|
|
131
|
+
result = {
|
|
132
|
+
"ruleId": rule_id,
|
|
133
|
+
"level": SARIF_LEVELS.get(severity, "warning"),
|
|
134
|
+
"message": {
|
|
135
|
+
"text": (
|
|
136
|
+
". ".join(part for part in (issue, explanation) if part)
|
|
137
|
+
if finding.get("is_dependency")
|
|
138
|
+
else explanation
|
|
139
|
+
)
|
|
140
|
+
},
|
|
141
|
+
"locations": [{
|
|
142
|
+
"physicalLocation": {
|
|
143
|
+
"artifactLocation": {"uri": file_path.replace("\\", "/")},
|
|
144
|
+
"region": {"startLine": line_number},
|
|
145
|
+
},
|
|
146
|
+
}],
|
|
147
|
+
}
|
|
148
|
+
properties = {}
|
|
149
|
+
if finding.get("commit"):
|
|
150
|
+
properties["commit"] = str(finding["commit"])
|
|
151
|
+
properties["isHistoryFinding"] = True
|
|
152
|
+
cwe = finding.get("cwe")
|
|
153
|
+
if cwe:
|
|
154
|
+
properties["cwe"] = str(cwe)
|
|
155
|
+
vulnerability_ids = finding.get("vulnerability_ids")
|
|
156
|
+
if isinstance(vulnerability_ids, list) and vulnerability_ids:
|
|
157
|
+
properties["vulnerabilityIds"] = vulnerability_ids
|
|
158
|
+
if properties:
|
|
159
|
+
result["properties"] = properties
|
|
160
|
+
if finding.get("suppressed"):
|
|
161
|
+
result["suppressions"] = [{
|
|
162
|
+
"kind": "inSource",
|
|
163
|
+
"justification": "Suppressed by a secguard: ignore comment",
|
|
164
|
+
}]
|
|
165
|
+
|
|
166
|
+
vulnerable = str(finding.get("vulnerable_code", ""))
|
|
167
|
+
fixed = str(finding.get("fixed_code", ""))
|
|
168
|
+
if vulnerable and fixed and vulnerable != fixed:
|
|
169
|
+
result["fixes"] = [{
|
|
170
|
+
"description": {"text": "AI-recommended secure patch"},
|
|
171
|
+
"artifactChanges": [{
|
|
172
|
+
"artifactLocation": {"uri": file_path.replace("\\", "/")},
|
|
173
|
+
"replacements": [{
|
|
174
|
+
"deletedRegion": {
|
|
175
|
+
"startLine": line_number,
|
|
176
|
+
"endLine": line_number + max(1, len(vulnerable.splitlines())) - 1,
|
|
177
|
+
},
|
|
178
|
+
"insertedContent": {"text": fixed},
|
|
179
|
+
}],
|
|
180
|
+
}],
|
|
181
|
+
}]
|
|
182
|
+
return result
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def render_sarif_report(findings: list[dict]) -> str:
|
|
186
|
+
rules: dict[str, dict] = {}
|
|
187
|
+
results = []
|
|
188
|
+
for finding in findings:
|
|
189
|
+
rule_id = _rule_id(finding)
|
|
190
|
+
rules.setdefault(rule_id, _sarif_rule(finding, rule_id))
|
|
191
|
+
results.append(_sarif_result(finding, rule_id))
|
|
192
|
+
|
|
193
|
+
report = {
|
|
194
|
+
"$schema": SARIF_SCHEMA,
|
|
195
|
+
"version": "2.1.0",
|
|
196
|
+
"runs": [{
|
|
197
|
+
"tool": {"driver": {"name": "SecGuard", "rules": list(rules.values())}},
|
|
198
|
+
"results": results,
|
|
199
|
+
}],
|
|
200
|
+
}
|
|
201
|
+
return json.dumps(report, ensure_ascii=False, indent=2) + "\n"
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def render_text_report(findings: list[dict]) -> str:
|
|
205
|
+
lines = [
|
|
206
|
+
"SecGuard Security Report",
|
|
207
|
+
f"Total verified findings: {len(findings)}",
|
|
208
|
+
]
|
|
209
|
+
for finding in findings:
|
|
210
|
+
location = (
|
|
211
|
+
f"{finding.get('file', 'Unknown file')}:"
|
|
212
|
+
f"{finding.get('line', '?')}"
|
|
213
|
+
)
|
|
214
|
+
if finding.get("commit"):
|
|
215
|
+
location += f" (commit {finding['commit']})"
|
|
216
|
+
lines.extend([
|
|
217
|
+
"",
|
|
218
|
+
f"[{str(finding.get('severity', 'UNKNOWN')).upper()}] {location}",
|
|
219
|
+
f"CWE: {finding.get('cwe') or 'Not specified'}",
|
|
220
|
+
f"Description: {finding.get('explanation', '')}",
|
|
221
|
+
_patch_diff(finding),
|
|
222
|
+
])
|
|
223
|
+
return "\n".join(lines) + "\n"
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
def render_table_report(findings: list[dict]) -> str:
|
|
227
|
+
return render_text_report(findings)
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
def _json_finding(finding: dict) -> dict:
|
|
231
|
+
line = finding.get("line")
|
|
232
|
+
try:
|
|
233
|
+
line = int(line)
|
|
234
|
+
except (TypeError, ValueError):
|
|
235
|
+
line = None
|
|
236
|
+
|
|
237
|
+
fix = finding.get("fix") or finding.get("fixed_code")
|
|
238
|
+
if not fix and finding.get("fixed_version"):
|
|
239
|
+
fix = f"Upgrade to version {finding['fixed_version']}"
|
|
240
|
+
|
|
241
|
+
result = {
|
|
242
|
+
"file": str(finding.get("file", "unknown")),
|
|
243
|
+
"line": line,
|
|
244
|
+
"severity": str(finding.get("severity", "UNKNOWN")).upper(),
|
|
245
|
+
"title": str(finding.get("title") or finding.get("issue", "Security finding")),
|
|
246
|
+
"cwe": finding.get("cwe"),
|
|
247
|
+
"description": str(
|
|
248
|
+
finding.get("description") or finding.get("explanation", "")
|
|
249
|
+
),
|
|
250
|
+
"fix": str(fix or ""),
|
|
251
|
+
"suppressed": bool(finding.get("suppressed", False)),
|
|
252
|
+
}
|
|
253
|
+
if finding.get("commit"):
|
|
254
|
+
result["commit"] = str(finding["commit"])
|
|
255
|
+
result["is_history"] = True
|
|
256
|
+
return result
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
def render_json_report(
|
|
260
|
+
findings: list[dict],
|
|
261
|
+
target: str,
|
|
262
|
+
scan_time: datetime | None = None,
|
|
263
|
+
) -> str:
|
|
264
|
+
timestamp = scan_time or datetime.now(timezone.utc)
|
|
265
|
+
if timestamp.tzinfo is None:
|
|
266
|
+
timestamp = timestamp.replace(tzinfo=timezone.utc)
|
|
267
|
+
report = {
|
|
268
|
+
"target": str(target),
|
|
269
|
+
"scan_time": timestamp.isoformat(),
|
|
270
|
+
"summary": {
|
|
271
|
+
"total_findings": len(findings),
|
|
272
|
+
"severity_distribution": _severity_counts(findings),
|
|
273
|
+
},
|
|
274
|
+
"findings": [_json_finding(finding) for finding in findings],
|
|
275
|
+
}
|
|
276
|
+
return json.dumps(report, ensure_ascii=False, indent=2) + "\n"
|