forked from SocketDev/socket-python-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessages.py
More file actions
339 lines (322 loc) · 12.6 KB
/
messages.py
File metadata and controls
339 lines (322 loc) · 12.6 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import json
from mdutils import MdUtils
from socketsecurity.core.classes import Diff, Purl, Issue
from prettytable import PrettyTable
class Messages:
@staticmethod
def create_security_comment_json(diff: Diff) -> dict:
scan_failed = False
if len(diff.new_alerts) == 0:
for alert in diff.new_alerts:
alert: Issue
if alert.error:
scan_failed = True
break
output = {
"scan_failed": scan_failed,
"new_alerts": [],
"full_scan_id": diff.id
}
for alert in diff.new_alerts:
alert: Issue
output["new_alerts"].append(json.loads(str(alert)))
return output
@staticmethod
def security_comment_template(diff: Diff) -> str:
"""
Creates the security comment template
:param diff: Diff - Diff report with the data needed for the template
:return:
"""
md = MdUtils(file_name="markdown_security_temp.md")
md.new_line("<!-- socket-security-comment-actions -->")
md.new_header(level=1, title="Socket Security: Issues Report")
md.new_line("Potential security issues detected. Learn more about [socket.dev](https://socket.dev)")
md.new_line("To accept the risk, merge this PR and you will not be notified again.")
md.new_line()
md.new_line("<!-- start-socket-alerts-table -->")
md, ignore_commands, next_steps = Messages.create_security_alert_table(diff, md)
md.new_line("<!-- end-socket-alerts-table -->")
md.new_line()
md = Messages.create_next_steps(md, next_steps)
md = Messages.create_deeper_look(md)
md = Messages.create_remove_package(md)
md = Messages.create_acceptable_risk(md, ignore_commands)
md.create_md_file()
return md.file_data_text.lstrip()
@staticmethod
def create_next_steps(md: MdUtils, next_steps: dict):
"""
Creates the next steps section of the security comment template
:param md: MdUtils - Main markdown variable
:param next_steps: Dict - Contains the detected next steps to include
:return:
"""
for step in next_steps:
detail = next_steps[step]
md.new_line("<details>")
md.new_line(f"<summary>{step}</summary>")
for line in detail:
md.new_paragraph(line)
md.new_line("</details>")
return md
@staticmethod
def create_deeper_look(md: MdUtils) -> MdUtils:
"""
Creates the deeper look section for the Security Comment Template
:param md: MdUtils - Main markdown variable
:return:
"""
md.new_line("<details>")
md.new_line("<summary>Take a deeper look at the dependency</summary>")
md.new_paragraph(
"Take a moment to review the security alert above. Review the linked package source code to understand the "
"potential risk. Ensure the package is not malicious before proceeding. If you're unsure how to proceed, "
"reach out to your security team or ask the Socket team for help at support [AT] socket [DOT] dev."
)
md.new_line("</details>")
return md
@staticmethod
def create_remove_package(md: MdUtils) -> MdUtils:
"""
Creates the remove packages suggestion section for the Security Comment Template
:param md:
:return:
"""
md.new_line("<details>")
md.new_line("<summary>Remove the package</summary>")
md.new_paragraph(
"If you happen to install a dependency that Socket reports as "
"[https://socket.dev/npm/issue/malware](Known Malware) you should immediately remove it and select a "
"different dependency. For other alert types, you may may wish to investigate alternative packages or "
"consider if there are other ways to mitigate the specific risk posed by the dependency."
)
md.new_line("</details>")
return md
@staticmethod
def create_acceptable_risk(md: MdUtils, ignore_commands: list) -> MdUtils:
"""
Creates the comment on how to accept risk for the Security Comment Template
:param md: MdUtils - Main markdown variable
:param ignore_commands: List of detected ignore commands based on the alerts associated purls
:return:
"""
md.new_line("<details>")
md.new_line("<summary>Mark a package as acceptable risk</summary>")
md.new_paragraph(
"To ignore an alert, reply with a comment starting with `SocketSecurity ignore` followed by a space "
"separated list of `ecosystem/package-name@version` specifiers. e.g. `SocketSecurity ignore npm/foo@1.0.0`"
" or ignore all packages with `SocketSecurity ignore-all`"
)
md.new_list(ignore_commands)
md.new_line("</details>")
return md
@staticmethod
def create_security_alert_table(diff: Diff, md: MdUtils) -> (MdUtils, list, dict):
"""
Creates the detected issues table based on the Security Policy
:param diff: Diff - Diff report with the detected issues
:param md: MdUtils - Main markdown variable
:return:
"""
alert_table = [
"Alert",
"Package",
"Introduced by",
"Manifest File",
"CI"
]
num_of_alert_columns = len(alert_table)
next_steps = {}
ignore_commands = []
for alert in diff.new_alerts:
alert: Issue
if alert.next_step_title not in next_steps:
next_steps[alert.next_step_title] = [
alert.description,
alert.suggestion
]
ignore = f"`SocketSecurity ignore {alert.purl}`"
if ignore not in ignore_commands:
ignore_commands.append(ignore)
manifest_str, source_str = Messages.create_sources(alert)
purl_url = f"[{alert.purl}]({alert.url})"
if alert.error:
emoji = ':no_entry_sign:'
else:
emoji = ':warning:'
row = [
alert.title,
purl_url,
source_str,
manifest_str,
emoji
]
if row not in alert_table:
alert_table.extend(row)
num_of_alert_rows = len(diff.new_alerts) + 1
md.new_table(
columns=num_of_alert_columns,
rows=num_of_alert_rows,
text=alert_table,
text_align="left"
)
return md, ignore_commands, next_steps
@staticmethod
def dependency_overview_template(diff: Diff) -> str:
"""
Creates the dependency Overview comment and returns a dict of the results
:param diff: Diff - Diff report with the added & removed packages
:return:
"""
md = MdUtils(file_name="markdown_overview_temp.md")
md.new_line("<!-- socket-overview-comment-actions -->")
md.new_header(level=1, title="Socket Security: Dependency Overview")
md.new_line("New and removed dependencies detected. Learn more about [socket.dev](https://socket.dev)")
md.new_line()
md = Messages.create_added_table(diff, md)
if len(diff.removed_packages) > 0:
md = Messages.create_remove_line(diff, md)
md.create_md_file()
if len(md.file_data_text.lstrip()) >= 65500:
md = Messages.short_dependency_overview_comment(diff)
return md.file_data_text.lstrip()
@staticmethod
def short_dependency_overview_comment(diff: Diff) -> MdUtils:
md = MdUtils(file_name="markdown_overview_temp.md")
md.new_line("<!-- socket-overview-comment-actions -->")
md.new_header(level=1, title="Socket Security: Dependency Overview")
md.new_line("New and removed dependencies detected. Learn more about [socket.dev](https://socket.dev)")
md.new_line()
md.new_line("The amount of dependency changes were to long for this comment. Please check out the full report")
md.new_line(f"To view more information about this report checkout the [Full Report]({diff.diff_url})")
return md
@staticmethod
def create_remove_line(diff: Diff, md: MdUtils) -> MdUtils:
"""
Creates the removed packages line for the Dependency Overview template
:param diff: Diff - Diff report with the removed packages
:param md: MdUtils - Main markdown variable
:return:
"""
removed_line = "Removed packages:"
for removed in diff.removed_packages:
removed: Purl
package_url = Messages.create_purl_link(removed)
removed_line += f" {package_url},"
removed_line = removed_line.rstrip(",")
md.new_line(removed_line)
return md
@staticmethod
def create_added_table(diff: Diff, md: MdUtils) -> MdUtils:
"""
Create the Added packages table for the Dependency Overview template
:param diff: Diff - Diff report with the Added packages information
:param md: MdUtils - Main markdown variable
:return:
"""
overview_table = [
"Package",
"Direct",
"Capabilities",
"Transitives",
"Size",
"Author"
]
num_of_overview_columns = len(overview_table)
count = 0
for added in diff.new_packages:
added: Purl
package_url = Messages.create_purl_link(added)
capabilities = ", ".join(added.capabilities)
row = [
package_url,
added.direct,
capabilities,
added.transitives,
f"{added.size} KB",
added.author_url
]
overview_table.extend(row)
count += 1
num_of_overview_rows = count + 1
md.new_table(
columns=num_of_overview_columns,
rows=num_of_overview_rows,
text=overview_table,
text_align="left"
)
return md
@staticmethod
def create_purl_link(details: Purl) -> str:
"""
Creates the Purl link for the Dependency Overview Comment for the added packages
:param details: Purl - Details about the package needed to create the URLs
:return:
"""
package_url = f"[{details.purl}]({details.url})"
return package_url
@staticmethod
def create_console_security_alert_table(diff: Diff) -> PrettyTable:
"""
Creates the detected issues table based on the Security Policy
:param diff: Diff - Diff report with the detected issues
:return:
"""
alert_table = PrettyTable(
[
"Alert",
"Package",
"url",
"Introduced by",
"Manifest File",
"CI Status"
]
)
for alert in diff.new_alerts:
alert: Issue
manifest_str, source_str = Messages.create_sources(alert, "console")
if alert.error:
state = "block"
elif alert.warn:
state = "warn"
elif alert.monitor:
state = "monitor"
else:
state = "ignore"
row = [
alert.title,
alert.purl,
alert.url,
source_str,
manifest_str,
state
]
alert_table.add_row(row)
return alert_table
@staticmethod
def create_sources(alert: Issue, style="md") -> [str, str]:
sources = []
manifests = []
for source, manifest in alert.introduced_by:
if style == "md":
add_str = f"<li>{manifest}</li>"
source_str = f"<li>{source}</li>"
else:
add_str = f"{manifest};"
source_str = f"{source};"
if source_str not in sources:
sources.append(source_str)
if add_str not in manifests:
manifests.append(add_str)
manifest_list = "".join(manifests)
source_list = "".join(sources)
source_list = source_list.rstrip(";")
manifest_list = manifest_list.rstrip(";")
if style == "md":
manifest_str = f"<ul>{manifest_list}</ul>"
sources_str = f"<ul>{source_list}</ul>"
else:
manifest_str = manifest_list
sources_str = source_list
return manifest_str, sources_str