-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path__init__.py
More file actions
223 lines (196 loc) · 4.43 KB
/
__init__.py
File metadata and controls
223 lines (196 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
"""
Combine multiple popular python security tools and generate reports or output
into different formats
Plugins (these require the plugin executable in the system path. e.g. bandit
requires bandit to be in the system path...)
- bandit
- safety
- dodgy
- dlint
- semgrep
Formats
- ansi (for terminal)
- json
- markdown
- csv
- sarif
"""
from __future__ import annotations
import argparse
from sys import exit as sysexit
from sys import stdout
from typing import Any, Callable, TextIO
from simplesecurity import filter as secfilter
from simplesecurity import formatter, plugins
stdout.reconfigure(encoding="utf-8") # type:ignore
FORMAT_HELP = "Output format. One of ansi, json, markdown, csv. default=ansi"
PLUGIN_HELP = "Plugin to use. One of bandit, safety, dodgy, dlint, semgrep, all, default=all"
def _processFile(file: str | None) -> TextIO:
return (
stdout
if file is None
else open(file, "w", encoding="utf-8") # pylint: disable=consider-using-with
)
def _processColour(noColour: bool, highContrast: bool) -> int:
colourMode = 1
if noColour:
colourMode = 0
if highContrast:
colourMode = 2
return colourMode
def _processFormat(formatin: str | None) -> Callable:
formatMap = {
"json": formatter.json,
"markdown": formatter.markdown,
"csv": formatter.csv,
"ansi": formatter.ansi,
"sarif": formatter.sarif,
}
if formatin is None:
formatt = formatter.ansi
elif formatin in formatMap:
formatt = formatMap[formatin]
else:
print(FORMAT_HELP)
sysexit(1)
return formatt
def _processPlugin(args) -> list[Callable]:
pluginMap = {
"bandit": {
"func": plugins.bandit,
"max_severity": 3,
"max_confidence": 3,
"fast": True,
},
"safety": {
"func": plugins.safety,
"max_severity": 4,
"max_confidence": 3,
"fast": True,
},
"dodgy": {
"func": plugins.dodgy,
"max_severity": 2,
"max_confidence": 2,
"fast": True,
},
"dlint": {
"func": plugins.dlint,
"max_severity": 4,
"max_confidence": 2,
"fast": True,
},
"semgrep": {
"func": plugins.semgrep,
"max_severity": 3,
"max_confidence": 3,
"fast": False,
},
}
plugin = args.plugin
filtered = {
k: v["func"]
for k, v in pluginMap.items()
if (
v["max_severity"] >= args.level
and v["max_confidence"] >= args.confidence
and (not args.fast or v["fast"])
)
}
if plugin in (None, "all"):
return [v for _, v in filtered.items()]
if plugin in filtered:
return [filtered[plugin]]
print(PLUGIN_HELP)
sysexit(2)
def cli():
"""Cli entry point."""
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawTextHelpFormatter
)
parser.add_argument(
"--scan-dir",
"-s",
help="Pass a path to the scan directory (optional)",
)
parser.add_argument(
"--format",
"-f",
help=FORMAT_HELP,
)
parser.add_argument(
"--plugin",
"-p",
help=PLUGIN_HELP,
)
parser.add_argument(
"--file",
"-o",
help="Filename to write to (omit for stdout)",
)
# Let's use a low level and medium confidence by default
parser.add_argument(
"--level",
"-l",
help="Minimum severity/ level to show",
type=int,
default=1,
)
parser.add_argument(
"--confidence",
"-c",
help="Minimum confidence to show",
type=int,
default=2,
)
parser.add_argument(
"--no-colour",
"-z",
help="No ANSI colours",
action="store_true",
)
parser.add_argument(
"--high-contrast",
"-Z",
help="High contrast colours",
action="store_true",
)
parser.add_argument(
"--fast",
"--skip",
action="store_true",
help="Skip long running jobs. Will omit plugins with long run time (applies to -p all only)",
)
parser.add_argument(
"--zero",
"-0",
action="store_true",
help="Return non zero exit code if any security vulnerabilities are found",
)
args = parser.parse_args()
scanDir = args.scan_dir or "."
filename = _processFile(args.file)
colourMode = _processColour(args.no_colour, args.high_contrast)
formatt = _processFormat(args.format)
filteredPlugins = _processPlugin(args)
findings = []
for plugin in filteredPlugins:
finding = []
try:
finding = plugin(scanDir=scanDir)
except BaseException as e:
print(f"! SimpleSecurity encountered an error: {e}")
findings.extend(finding)
filteredFindings = secfilter.filterSeverityAndConfidence(
secfilter.deduplicate(findings), args.level, args.confidence
)
print(
formatt(
filteredFindings,
colourMode=colourMode,
),
file=filename,
)
if len(filteredFindings) > 0 and args.zero:
sysexit(1)
sysexit(0)