ds-reporting-lib 6.0.0b2__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.
@@ -0,0 +1,2584 @@
1
+ import json
2
+ import html
3
+ import re
4
+
5
+ from reporting_lib.template import (
6
+ PRIORITIZED_VULNERABILITIES,
7
+ VDR,
8
+ PROACTIVE_MEASURES,
9
+ SERVICE_ENDPOINTS,
10
+ REACHABLE_FLOWS,
11
+ NON_REACHABLE_FLOWS,
12
+ SECURE_DESIGN_TIPS,
13
+ HTML_REPORT,
14
+ MALWARE_ALERT,
15
+ RECOMMENDATION,
16
+ INFO,
17
+ )
18
+
19
+ NEWLINE = "\n"
20
+ SEPARATOR = "✦"
21
+
22
+
23
+ class ReportGenerator:
24
+ VULNERABILITIES_COUNT = "Vulnerabilities count"
25
+ SUMMARY = "Summary"
26
+ DATA = "Data"
27
+ TABLE_HEADERS = "Table headers"
28
+ RECOMMENDATION = "Recommendation"
29
+ ACTION_REQUIRED = "Action Required"
30
+ INFO = "INFO"
31
+
32
+ VULNERABILITY_DISCLOSURE_REPORT = "Vulnerability Disclosure Report"
33
+
34
+ DEPENDENCY_SCAN_RESULTS_BOM = "Dependency Scan Results (BOM)"
35
+ DEPENDENCY_SCAN_RESULTS_BOM_SUMMARY = DEPENDENCY_SCAN_RESULTS_BOM + " " + SUMMARY
36
+ DEPENDENCY_SCAN_RESULTS_BOM_DATA_COLUMNS = [
37
+ "Dependency Tree",
38
+ "Insights",
39
+ "Fix Version",
40
+ "Severity",
41
+ "Score",
42
+ ]
43
+ DEPENDENCY_SCAN_RESULTS_BOM_DATA = DEPENDENCY_SCAN_RESULTS_BOM + " " + DATA
44
+ DEPENDENCY_SCAN_RESULTS_BOM_RECOMMENDATION = (
45
+ DEPENDENCY_SCAN_RESULTS_BOM + " " + RECOMMENDATION
46
+ )
47
+ DEPENDENCY_SCAN_RESULTS_BOM_ACTION_REQUIRED = (
48
+ DEPENDENCY_SCAN_RESULTS_BOM + " " + ACTION_REQUIRED
49
+ )
50
+
51
+ PRIORITIZED_VULNERABILITIES = "Prioritized Vulnerabilities"
52
+ PRIORITIZED_VULNERABILITIES_ALTERNATIVE = "Next Steps"
53
+ TOP_PRIORITY_BOM = "Top Priority (BOM)"
54
+ TOP_PRIORITY_BOM_SUMMARY = TOP_PRIORITY_BOM + " " + SUMMARY
55
+ TOP_PRIORITY_BOM_DATA_COLUMNS = [
56
+ "Package",
57
+ "Prioritized CVEs",
58
+ "Fix Version",
59
+ "Next Steps",
60
+ ]
61
+ TOP_PRIORITY_BOM_DATA = TOP_PRIORITY_BOM + " " + DATA
62
+ PRIORITIZED_COUNT = "Prioritized count"
63
+
64
+ PROACTIVE_MEASURES = "Proactive Measures"
65
+ TOP_REACHABLE_PACKAGES = "Top Reachable Packages"
66
+ TOP_REACHABLE_PACKAGES_ALTERNATIVE_01 = "💥 Top Endpoint-Reachable Packages"
67
+ TOP_REACHABLE_PACKAGES_ALTERNATIVE_02 = "🕸 Top Endpoint-Reachable Packages"
68
+ TOP_REACHABLE_PACKAGES_SUMMARY = TOP_REACHABLE_PACKAGES + " " + SUMMARY
69
+ TOP_REACHABLE_PACKAGES_DATA_COLUMNS = ["Package", "Reachable Flows"]
70
+ TOP_REACHABLE_PACKAGES_DATA = TOP_REACHABLE_PACKAGES + " " + DATA
71
+
72
+ SERVICE_ENDPOINTS = "Service Endpoints"
73
+ ENDPOINTS = "Endpoints"
74
+ ENDPOINTS_SUMMARY = ENDPOINTS + " " + SUMMARY
75
+ ENDPOINTS_DATA_COLUMNS = ["URL Pattern", "HTTP Methods", "Code Hotspots"]
76
+ ENDPOINTS_DATA = ENDPOINTS + " " + DATA
77
+ IDENTIFIED_ENDPOINTS = "Identified Endpoints"
78
+
79
+ REACHABLE_FLOWS = "Reachable Flows"
80
+ REACHABLE_PACKAGES = "Reachable Packages"
81
+ REACHABLE_FLOWS_SUMMARY = REACHABLE_FLOWS + " " + SUMMARY
82
+ REACHABLE_FLOWS_DATA = REACHABLE_FLOWS + " " + DATA
83
+ REACHABLE_FLOWS_RECOMMENDATION = REACHABLE_FLOWS + " " + RECOMMENDATION
84
+ REACHABLE_FLOWS_DATA_COLUMNS = [
85
+ "Summary",
86
+ "Flows",
87
+ "Reachable Packages",
88
+ "Recommendation",
89
+ ]
90
+ REACHABLE_FLOWS_REACHABLE_PACKAGES = REACHABLE_FLOWS + " " + REACHABLE_PACKAGES
91
+
92
+ NON_REACHABLE_FLOWS = "Non-Reachable Flows"
93
+ NON_REACHABLE_PACKAGES = "Non-Reachable Packages"
94
+ NON_REACHABLE_FLOWS_SUMMARY = NON_REACHABLE_FLOWS + " " + SUMMARY
95
+ NON_REACHABLE_FLOWS_DATA = NON_REACHABLE_FLOWS + " " + DATA
96
+ NON_REACHABLE_FLOWS_RECOMMENDATION = NON_REACHABLE_FLOWS + " " + RECOMMENDATION
97
+ NON_REACHABLE_FLOWS_DATA_COLUMNS = [
98
+ "Summary",
99
+ "Flows",
100
+ "Reachable Packages",
101
+ "Recommendation",
102
+ ]
103
+ NON_REACHABLE_FLOWS_REACHABLE_PACKAGES = (
104
+ NON_REACHABLE_FLOWS + " " + REACHABLE_PACKAGES
105
+ )
106
+
107
+ SECURE_DESIGN_TIPS = "Secure Design Tips"
108
+ MALWARE_ALERT = "Malware Alert"
109
+
110
+ def __init__(
111
+ self,
112
+ report_output_path,
113
+ input_vdr_json_path=None,
114
+ input_rich_html_path=None,
115
+ input_txt_path=None,
116
+ raw_content=False,
117
+ ):
118
+ input_counter = 0
119
+
120
+ if input_vdr_json_path is not None:
121
+ input_counter += 1
122
+
123
+ if input_rich_html_path is not None:
124
+ input_counter += 1
125
+
126
+ if input_txt_path is not None:
127
+ input_counter += 1
128
+
129
+ if input_counter != 1:
130
+ raise ValueError(
131
+ "At least and at most one between 'input_vdr_json_path', 'input_rich_html_path', 'input_txt_path' should be not None"
132
+ )
133
+ self.report_output_path = report_output_path
134
+ self.input_vdr_json_path = input_vdr_json_path
135
+ self.input_rich_html_path = input_rich_html_path
136
+ self.input_txt_path = input_txt_path
137
+ self.raw_content = raw_content
138
+
139
+ def extract_depscan_reports_from_vdr_json(self):
140
+ depscan_reports = set()
141
+ with open(self.input_vdr_json_path, "r", encoding="utf-8") as file:
142
+ data = json.load(file)
143
+ if "annotations" not in data:
144
+ return []
145
+ for annotation in data["annotations"]:
146
+ if "annotator" not in annotation or "text" not in annotation:
147
+ continue
148
+ annotator = annotation["annotator"]
149
+ if "component" not in annotator:
150
+ continue
151
+ component = annotator["component"]
152
+ if "name" not in component:
153
+ continue
154
+ if component["name"] != "owasp-depscan":
155
+ continue
156
+ depscan_reports.add(annotation["text"])
157
+
158
+ return list(depscan_reports)
159
+
160
+ def extract_depscan_report_from_rich_html(self):
161
+ with open(self.input_rich_html_path, "r", encoding="utf-8") as file:
162
+ data = file.read()
163
+
164
+ styles = data.split("<style>", 1)[1].split("</style>", 1)[0]
165
+ if "body {" in styles:
166
+ styles = styles.split("body {", 1)[0]
167
+
168
+ depscan_report = data.split("<body>", 1)[1].split("</body>", 1)[0]
169
+ if depscan_report.strip().startswith(
170
+ """<pre style="font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"><code style="font-family:inherit">"""
171
+ ):
172
+ depscan_report = depscan_report.split(
173
+ """<pre style="font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"><code style="font-family:inherit">""",
174
+ 1,
175
+ )[1]
176
+ if depscan_report.strip().endswith("</code></pre>"):
177
+ depscan_report = depscan_report.rsplit("</code></pre>", 1)[0]
178
+
179
+ return depscan_report, styles
180
+
181
+ def extract_depscan_report_from_txt(self):
182
+ with open(self.input_txt_path, "r", encoding="utf-8") as file:
183
+ data = file.read()
184
+ return data
185
+
186
+ def string_matches_span_pattern(self, text, pattern_content):
187
+ pattern_content = pattern_content.replace("(", r"\(")
188
+ pattern_content = pattern_content.replace(")", r"\)")
189
+
190
+ pattern = rf'<span class="r\d\d?">[ \t]*{pattern_content}[ \t]*<\/span>'
191
+ match = re.fullmatch(pattern, text)
192
+ if match:
193
+ return True
194
+ else: # try with additional empty spans around the one with the string, it may happen
195
+ pattern = rf'<span class="r\d\d?">[ \t]*<\/span>{pattern}<span class="r\d\d?">[ \t]*<\/span>'
196
+ match = re.fullmatch(pattern, text)
197
+ if match:
198
+ return True
199
+ else:
200
+ return False
201
+
202
+ def string_matches_regex(self, text, pattern):
203
+ match = re.fullmatch(pattern, text)
204
+ if match:
205
+ return True
206
+ else:
207
+ return False
208
+
209
+ def array_matches_span_pattern(self, text_array, pattern_content_array):
210
+ if len(text_array) != len(pattern_content_array):
211
+ return False
212
+
213
+ for idx in range(0, len(text_array)):
214
+ if not self.string_matches_span_pattern(
215
+ text_array[idx], pattern_content_array[idx]
216
+ ):
217
+ return False
218
+
219
+ return True
220
+
221
+ def separate_report_by_language_explanations(self, depscan_report):
222
+ pieces = {}
223
+
224
+ current_piece = None
225
+
226
+ marker_01 = None # ┏ ━ ┓
227
+ marker_02 = None # ┃ <span class="rY">Explanations for XXX</span> ┃
228
+ marker_03 = None # ┗ ━ ┛
229
+
230
+ for current_line in depscan_report.splitlines():
231
+ line = current_line.strip()
232
+
233
+ if self.string_matches_regex(line, r"""┏━+┓"""):
234
+ marker_01 = line
235
+ marker_02 = None
236
+ marker_03 = None
237
+ elif marker_01 is not None and self.string_matches_regex(
238
+ line,
239
+ r"""┃\s+<span class=\"r\d\d?\d?">Explanations for [^<]*?<\/span>\s+┃""",
240
+ ):
241
+ marker_02 = line
242
+ marker_03 = None
243
+ elif (
244
+ marker_01 is not None
245
+ and marker_02 is not None
246
+ and self.string_matches_regex(line, r"""┗━+┛""")
247
+ ):
248
+ marker_03 = line
249
+ else:
250
+ marker_01 = None
251
+ marker_02 = None
252
+ marker_03 = None
253
+
254
+ if (
255
+ marker_01 is not None
256
+ and marker_02 is not None
257
+ and marker_03 is not None
258
+ ):
259
+ pieces[current_piece] = pieces[current_piece][:-2]
260
+ current_piece = (
261
+ marker_02.split("Explanations for", 1)[1].split("<", 1)[0].strip()
262
+ )
263
+ continue
264
+
265
+ if current_piece not in pieces:
266
+ pieces[current_piece] = []
267
+
268
+ pieces[current_piece].append(current_line)
269
+
270
+ for key in pieces.keys():
271
+ pieces[key] = "\n".join(pieces[key])
272
+
273
+ return pieces
274
+
275
+ def parse_depscan_report(self, depscan_report):
276
+ last_seen_reachable_flows = None
277
+ current_location = None
278
+ current_table_row = None
279
+ current_columns_count = None
280
+ summary_column = ""
281
+ reachable_flow_column = ""
282
+ reachable_packages_column = ""
283
+ recommendation_column = ""
284
+
285
+ sections_tree = {
286
+ self.VULNERABILITY_DISCLOSURE_REPORT: {
287
+ self.DEPENDENCY_SCAN_RESULTS_BOM: {
288
+ self.VULNERABILITIES_COUNT: "-1",
289
+ self.SUMMARY: "",
290
+ self.TABLE_HEADERS: [],
291
+ self.DATA: [],
292
+ },
293
+ self.RECOMMENDATION: "",
294
+ self.ACTION_REQUIRED: "",
295
+ },
296
+ self.PRIORITIZED_VULNERABILITIES: {
297
+ self.TOP_PRIORITY_BOM: {
298
+ self.PRIORITIZED_COUNT: "-1",
299
+ self.SUMMARY: "",
300
+ self.TABLE_HEADERS: [],
301
+ self.DATA: [],
302
+ }
303
+ },
304
+ self.PROACTIVE_MEASURES: {
305
+ self.TOP_REACHABLE_PACKAGES: {
306
+ self.SUMMARY: "",
307
+ self.TABLE_HEADERS: [],
308
+ self.DATA: [],
309
+ }
310
+ },
311
+ self.SERVICE_ENDPOINTS: {
312
+ self.ENDPOINTS: {
313
+ self.IDENTIFIED_ENDPOINTS: "-1",
314
+ self.SUMMARY: "",
315
+ self.TABLE_HEADERS: [],
316
+ self.DATA: [],
317
+ }
318
+ },
319
+ self.REACHABLE_FLOWS: {self.SUMMARY: "", self.DATA: []},
320
+ self.NON_REACHABLE_FLOWS: {self.SUMMARY: "", self.DATA: []},
321
+ self.SECURE_DESIGN_TIPS: {self.SUMMARY: ""},
322
+ self.MALWARE_ALERT: {self.SUMMARY: ""},
323
+ self.RECOMMENDATION: {self.SUMMARY: ""},
324
+ self.INFO: {self.SUMMARY: ""},
325
+ }
326
+
327
+ previous_line = None
328
+ line = None
329
+ for current_line in depscan_report.splitlines():
330
+ previous_line = line
331
+ line = current_line.strip()
332
+
333
+ # ---- Dirty fix for unusually structured sections ----
334
+
335
+ if (
336
+ current_location != self.REACHABLE_FLOWS_DATA
337
+ and last_seen_reachable_flows in [self.REACHABLE_FLOWS, None]
338
+ ):
339
+ if line.startswith("#") or self.string_matches_regex(
340
+ line,
341
+ r'<span class="r\d+">#\d+<\/span><span class="r\d+">.*<\/span>',
342
+ ):
343
+ current_location = self.REACHABLE_FLOWS_DATA
344
+
345
+ if (
346
+ current_location != self.NON_REACHABLE_FLOWS_DATA
347
+ and last_seen_reachable_flows == self.NON_REACHABLE_FLOWS
348
+ ):
349
+ if line.startswith("#") or self.string_matches_regex(
350
+ line,
351
+ r'<span class="r\d+">#\d+<\/span><span class="r\d+">.*<\/span>',
352
+ ):
353
+ current_location = self.NON_REACHABLE_FLOWS_DATA
354
+
355
+ # ---- Location identification: Secure Design Tips ----
356
+
357
+ if line == self.SECURE_DESIGN_TIPS or self.string_matches_span_pattern(
358
+ line, self.SECURE_DESIGN_TIPS
359
+ ):
360
+ current_location = self.SECURE_DESIGN_TIPS
361
+ continue
362
+
363
+ if previous_line == "" and self.string_matches_regex(
364
+ line, r"""<span class="r\d\d?\d?"> • <\/span>[^<]*"""
365
+ ):
366
+ current_location = self.SECURE_DESIGN_TIPS
367
+
368
+ # ---- Data population: Secure Design Tips ----
369
+
370
+ if current_location == self.SECURE_DESIGN_TIPS:
371
+ if line not in sections_tree[self.SECURE_DESIGN_TIPS][self.SUMMARY]:
372
+ if sections_tree[self.SECURE_DESIGN_TIPS][self.SUMMARY] != "":
373
+ sections_tree[self.SECURE_DESIGN_TIPS][self.SUMMARY] += "\n"
374
+ sections_tree[self.SECURE_DESIGN_TIPS][self.SUMMARY] += line
375
+ continue
376
+
377
+ # ---- Location identification: INFO ----
378
+
379
+ if current_location is None and (
380
+ self.string_matches_regex(
381
+ line,
382
+ r'<span class="r\d\d?\d?">\s*<\/span><span class="r\d\d?\d?">INFO\s*<\/span>.*<\/span>\!\s*',
383
+ )
384
+ or self.string_matches_regex(
385
+ line,
386
+ r'<span class="r\d\d?\d?">\s*<\/span><span class="r\d\d?\d?">INFO\s*<\/span> No vulnerabilities found!',
387
+ )
388
+ ):
389
+ current_location = self.INFO
390
+
391
+ if current_location == self.INFO and line == "":
392
+ current_location = None
393
+
394
+ # ---- Data population: INFO ----
395
+
396
+ if current_location == self.INFO:
397
+ if sections_tree[self.INFO][self.SUMMARY] != "":
398
+ sections_tree[self.INFO][self.SUMMARY] += "\n"
399
+ if sections_tree[self.INFO][self.SUMMARY] == "":
400
+ sections_tree[self.INFO][self.SUMMARY] += line.split("INFO", 1)[
401
+ 1
402
+ ].strip()
403
+ else:
404
+ sections_tree[self.INFO][self.SUMMARY] += line
405
+ continue
406
+
407
+ # ---- Location identification: Recommendation ----
408
+ if (
409
+ current_location is None
410
+ and line.endswith("╮")
411
+ and "─ Recommendation ─" in line
412
+ and line.startswith("╭")
413
+ ):
414
+ current_location = self.RECOMMENDATION
415
+ continue
416
+
417
+ if (
418
+ current_location == self.RECOMMENDATION
419
+ and line.startswith("╰")
420
+ and line.endswith("╯")
421
+ ):
422
+ current_location = None
423
+ continue
424
+
425
+ # ---- Data population: Recommendation ----
426
+
427
+ if (
428
+ current_location == self.RECOMMENDATION
429
+ and line.startswith("│")
430
+ and line.endswith("│")
431
+ ):
432
+ if sections_tree[self.RECOMMENDATION][self.SUMMARY] != "":
433
+ sections_tree[self.RECOMMENDATION][self.SUMMARY] += "\n"
434
+ sections_tree[self.RECOMMENDATION][self.SUMMARY] += line[1:-1]
435
+ continue
436
+
437
+ # ---- Location identification: Vulnerability Disclosure Report ----
438
+
439
+ if (
440
+ line == self.VULNERABILITY_DISCLOSURE_REPORT
441
+ or self.string_matches_span_pattern(
442
+ line, self.VULNERABILITY_DISCLOSURE_REPORT
443
+ )
444
+ ):
445
+ current_location = self.DEPENDENCY_SCAN_RESULTS_BOM_SUMMARY
446
+ continue
447
+
448
+ if (
449
+ line == self.DEPENDENCY_SCAN_RESULTS_BOM
450
+ or self.string_matches_span_pattern(
451
+ line, self.DEPENDENCY_SCAN_RESULTS_BOM
452
+ )
453
+ ):
454
+ current_location = self.DEPENDENCY_SCAN_RESULTS_BOM
455
+ continue
456
+
457
+ if (
458
+ current_location == self.DEPENDENCY_SCAN_RESULTS_BOM
459
+ and line.startswith("╔")
460
+ and line.endswith("╗")
461
+ ):
462
+ current_location = self.DEPENDENCY_SCAN_RESULTS_BOM_DATA
463
+ current_columns_count = len(line[1:-1].split("╤"))
464
+ continue
465
+
466
+ if (
467
+ current_location == self.DEPENDENCY_SCAN_RESULTS_BOM_DATA
468
+ and line.startswith("╚")
469
+ and line.endswith("╝")
470
+ ):
471
+ current_location = self.DEPENDENCY_SCAN_RESULTS_BOM
472
+ if len(current_table_row) == 0: # should never happen:
473
+ continue
474
+
475
+ cells = ["" for index in range(0, len(current_table_row[0]))]
476
+
477
+ for row_piece in current_table_row:
478
+ for index, row_piece_column in enumerate(row_piece):
479
+ cells[index] += row_piece_column + "\n"
480
+
481
+ sections_tree[self.VULNERABILITY_DISCLOSURE_REPORT][
482
+ self.DEPENDENCY_SCAN_RESULTS_BOM
483
+ ][self.DATA].append(cells)
484
+ current_table_row = []
485
+
486
+ continue
487
+
488
+ if (
489
+ current_location == self.DEPENDENCY_SCAN_RESULTS_BOM
490
+ and line.startswith("╭")
491
+ and line.endswith("╮")
492
+ and "─ Recommendation ─" in line
493
+ ):
494
+ current_location = self.DEPENDENCY_SCAN_RESULTS_BOM_RECOMMENDATION
495
+ continue
496
+
497
+ if (
498
+ current_location == self.DEPENDENCY_SCAN_RESULTS_BOM_RECOMMENDATION
499
+ and line.startswith("╰")
500
+ and line.endswith("╯")
501
+ ):
502
+ current_location = None
503
+ continue
504
+
505
+ if (
506
+ current_location == self.DEPENDENCY_SCAN_RESULTS_BOM
507
+ and line.startswith("╭")
508
+ and line.endswith("╮")
509
+ and "─ Action Required ─" in line
510
+ ):
511
+ current_location = self.DEPENDENCY_SCAN_RESULTS_BOM_ACTION_REQUIRED
512
+ continue
513
+
514
+ if (
515
+ current_location == self.DEPENDENCY_SCAN_RESULTS_BOM_ACTION_REQUIRED
516
+ and line.startswith("╰")
517
+ and line.endswith("╯")
518
+ ):
519
+ current_location = None
520
+ continue
521
+
522
+ # ---- Data population: Vulnerability Disclosure Report ----
523
+
524
+ if current_location == self.DEPENDENCY_SCAN_RESULTS_BOM_SUMMARY:
525
+ if (
526
+ sections_tree[self.VULNERABILITY_DISCLOSURE_REPORT][
527
+ self.DEPENDENCY_SCAN_RESULTS_BOM
528
+ ][self.SUMMARY]
529
+ != ""
530
+ ):
531
+ sections_tree[self.VULNERABILITY_DISCLOSURE_REPORT][
532
+ self.DEPENDENCY_SCAN_RESULTS_BOM
533
+ ][self.SUMMARY] += "\n"
534
+ sections_tree[self.VULNERABILITY_DISCLOSURE_REPORT][
535
+ self.DEPENDENCY_SCAN_RESULTS_BOM
536
+ ][self.SUMMARY] += line
537
+ continue
538
+
539
+ if current_location == self.DEPENDENCY_SCAN_RESULTS_BOM_DATA:
540
+ if line.startswith("║") and line.endswith("║"): # in table row
541
+ columns = line[1:-1].rsplit("│", maxsplit=current_columns_count - 1)
542
+
543
+ stripped_columns = [column.strip() for column in columns]
544
+ if (
545
+ stripped_columns
546
+ == self.DEPENDENCY_SCAN_RESULTS_BOM_DATA_COLUMNS
547
+ or self.array_matches_span_pattern(
548
+ stripped_columns,
549
+ self.DEPENDENCY_SCAN_RESULTS_BOM_DATA_COLUMNS,
550
+ )
551
+ ):
552
+ sections_tree[self.VULNERABILITY_DISCLOSURE_REPORT][
553
+ self.DEPENDENCY_SCAN_RESULTS_BOM
554
+ ][self.TABLE_HEADERS] = stripped_columns
555
+ current_table_row = []
556
+ continue
557
+ else:
558
+ current_table_row.append(columns)
559
+
560
+ elif line.startswith("╟") and line.endswith("╢"): # row separator
561
+ if len(current_table_row) == 0: # should never happen:
562
+ continue
563
+
564
+ cells = ["" for index in range(0, len(current_table_row[0]))]
565
+
566
+ for row_piece in current_table_row:
567
+ for index, row_piece_column in enumerate(row_piece):
568
+ cells[index] += row_piece_column + "\n"
569
+
570
+ sections_tree[self.VULNERABILITY_DISCLOSURE_REPORT][
571
+ self.DEPENDENCY_SCAN_RESULTS_BOM
572
+ ][self.DATA].append(cells)
573
+ current_table_row = []
574
+
575
+ continue
576
+
577
+ if current_location == self.DEPENDENCY_SCAN_RESULTS_BOM and (
578
+ line.startswith("Vulnerabilities count:")
579
+ or self.string_matches_span_pattern(line, r"Vulnerabilities count: \d+")
580
+ ):
581
+ sections_tree[self.VULNERABILITY_DISCLOSURE_REPORT][
582
+ self.DEPENDENCY_SCAN_RESULTS_BOM
583
+ ][self.VULNERABILITIES_COUNT] = (
584
+ line.split("Vulnerabilities count:", 1)[1].split("<", 1)[0].strip()
585
+ )
586
+ continue
587
+
588
+ if (
589
+ current_location == self.DEPENDENCY_SCAN_RESULTS_BOM_RECOMMENDATION
590
+ and line.startswith("│")
591
+ and line.endswith("│")
592
+ ):
593
+ if (
594
+ sections_tree[self.VULNERABILITY_DISCLOSURE_REPORT][
595
+ self.RECOMMENDATION
596
+ ]
597
+ != ""
598
+ ):
599
+ sections_tree[self.VULNERABILITY_DISCLOSURE_REPORT][
600
+ self.RECOMMENDATION
601
+ ] += "\n"
602
+ sections_tree[self.VULNERABILITY_DISCLOSURE_REPORT][
603
+ self.RECOMMENDATION
604
+ ] += line[1:-1]
605
+ continue
606
+
607
+ if (
608
+ current_location == self.DEPENDENCY_SCAN_RESULTS_BOM_ACTION_REQUIRED
609
+ and line.startswith("│")
610
+ and line.endswith("│")
611
+ ):
612
+ if (
613
+ sections_tree[self.VULNERABILITY_DISCLOSURE_REPORT][
614
+ self.ACTION_REQUIRED
615
+ ]
616
+ != ""
617
+ ):
618
+ sections_tree[self.VULNERABILITY_DISCLOSURE_REPORT][
619
+ self.ACTION_REQUIRED
620
+ ] += "\n"
621
+ sections_tree[self.VULNERABILITY_DISCLOSURE_REPORT][
622
+ self.ACTION_REQUIRED
623
+ ] += line[1:-1]
624
+ continue
625
+
626
+ # ---- Location identification: Proactive Measures ----
627
+
628
+ if line == self.PROACTIVE_MEASURES or self.string_matches_span_pattern(
629
+ line, self.PROACTIVE_MEASURES
630
+ ):
631
+ current_location = self.TOP_REACHABLE_PACKAGES_SUMMARY
632
+ continue
633
+
634
+ if (
635
+ line
636
+ in [
637
+ self.TOP_REACHABLE_PACKAGES,
638
+ self.TOP_REACHABLE_PACKAGES_ALTERNATIVE_01,
639
+ self.TOP_REACHABLE_PACKAGES_ALTERNATIVE_02,
640
+ ]
641
+ or self.string_matches_span_pattern(line, self.TOP_REACHABLE_PACKAGES)
642
+ or self.string_matches_span_pattern(
643
+ line, self.TOP_REACHABLE_PACKAGES_ALTERNATIVE_01
644
+ )
645
+ or self.string_matches_span_pattern(
646
+ line, self.TOP_REACHABLE_PACKAGES_ALTERNATIVE_02
647
+ )
648
+ ):
649
+ current_location = self.TOP_REACHABLE_PACKAGES
650
+ continue
651
+
652
+ if (
653
+ current_location == self.TOP_REACHABLE_PACKAGES
654
+ and line.startswith("╔")
655
+ and line.endswith("╗")
656
+ ):
657
+ current_location = self.TOP_REACHABLE_PACKAGES_DATA
658
+ current_columns_count = len(line[1:-1].split("╤"))
659
+ continue
660
+
661
+ if (
662
+ current_location == self.TOP_REACHABLE_PACKAGES_DATA
663
+ and line.startswith("╚")
664
+ and line.endswith("╝")
665
+ ):
666
+ current_location = self.TOP_REACHABLE_PACKAGES
667
+ if len(current_table_row) == 0: # should never happen:
668
+ continue
669
+
670
+ cells = ["" for index in range(0, len(current_table_row[0]))]
671
+
672
+ for row_piece in current_table_row:
673
+ for index, row_piece_column in enumerate(row_piece):
674
+ cells[index] += row_piece_column + "\n"
675
+ for index, cell in enumerate(
676
+ cells
677
+ ): # dirty fix to eliminate the useless \n added at the end of each cell
678
+ cells[index] = cell[:-1]
679
+
680
+ sections_tree[self.PROACTIVE_MEASURES][self.TOP_REACHABLE_PACKAGES][
681
+ self.DATA
682
+ ].append(cells)
683
+ current_table_row = []
684
+ continue
685
+
686
+ # ---- Data population: Proactive Measures ----
687
+
688
+ if current_location == self.TOP_REACHABLE_PACKAGES_SUMMARY:
689
+ if (
690
+ sections_tree[self.PROACTIVE_MEASURES][self.TOP_REACHABLE_PACKAGES][
691
+ self.SUMMARY
692
+ ]
693
+ != ""
694
+ ):
695
+ sections_tree[self.PROACTIVE_MEASURES][self.TOP_REACHABLE_PACKAGES][
696
+ self.SUMMARY
697
+ ] += "\n"
698
+ sections_tree[self.PROACTIVE_MEASURES][self.TOP_REACHABLE_PACKAGES][
699
+ self.SUMMARY
700
+ ] += line
701
+ continue
702
+
703
+ if current_location == self.TOP_REACHABLE_PACKAGES_DATA:
704
+ if line.startswith("║") and line.endswith("║"): # in table row
705
+ columns = line[1:-1].rsplit("│", maxsplit=current_columns_count - 1)
706
+
707
+ stripped_columns = [column.strip() for column in columns]
708
+ if (
709
+ stripped_columns == self.TOP_REACHABLE_PACKAGES_DATA_COLUMNS
710
+ or self.array_matches_span_pattern(
711
+ stripped_columns, self.TOP_REACHABLE_PACKAGES_DATA_COLUMNS
712
+ )
713
+ ):
714
+ sections_tree[self.PROACTIVE_MEASURES][
715
+ self.TOP_REACHABLE_PACKAGES
716
+ ][self.TABLE_HEADERS] = stripped_columns
717
+ current_table_row = []
718
+ continue
719
+ else:
720
+ current_table_row.append(columns)
721
+
722
+ elif line.startswith("╟") and line.endswith("╢"): # row separator
723
+ if len(current_table_row) == 0: # should never happen:
724
+ continue
725
+
726
+ cells = ["" for index in range(0, len(current_table_row[0]))]
727
+
728
+ for row_piece in current_table_row:
729
+ for index, row_piece_column in enumerate(row_piece):
730
+ cells[index] += row_piece_column + "\n"
731
+ for index, cell in enumerate(
732
+ cells
733
+ ): # dirty fix to eliminate the useless \n added at the end of each cell
734
+ cells[index] = cell[:-1]
735
+
736
+ sections_tree[self.PROACTIVE_MEASURES][self.TOP_REACHABLE_PACKAGES][
737
+ self.DATA
738
+ ].append(cells)
739
+ current_table_row = []
740
+
741
+ continue
742
+
743
+ # ---- Location identification: Reachable Flows ----
744
+
745
+ if line == self.REACHABLE_FLOWS or self.string_matches_span_pattern(
746
+ line, self.REACHABLE_FLOWS
747
+ ):
748
+ current_location = self.REACHABLE_FLOWS_SUMMARY
749
+ last_seen_reachable_flows = self.REACHABLE_FLOWS
750
+ continue
751
+
752
+ if (
753
+ current_location == self.REACHABLE_FLOWS_SUMMARY
754
+ and line == ""
755
+ and sections_tree[self.REACHABLE_FLOWS][self.SUMMARY] != ""
756
+ ):
757
+ current_location = self.REACHABLE_FLOWS_DATA
758
+ last_seen_reachable_flows = self.REACHABLE_FLOWS
759
+ continue
760
+
761
+ if (
762
+ current_location
763
+ in [
764
+ self.REACHABLE_FLOWS_REACHABLE_PACKAGES,
765
+ self.REACHABLE_FLOWS_DATA,
766
+ self.REACHABLE_FLOWS_RECOMMENDATION,
767
+ ]
768
+ and line == ""
769
+ ):
770
+ cells = [
771
+ summary_column,
772
+ reachable_flow_column,
773
+ reachable_packages_column,
774
+ recommendation_column,
775
+ ]
776
+ sections_tree[self.REACHABLE_FLOWS][self.DATA].append(cells)
777
+ summary_column = ""
778
+ reachable_flow_column = ""
779
+ reachable_packages_column = ""
780
+ recommendation_column = ""
781
+ current_location = self.REACHABLE_FLOWS_DATA
782
+ last_seen_reachable_flows = self.REACHABLE_FLOWS
783
+ continue
784
+
785
+ if (
786
+ current_location == self.REACHABLE_FLOWS_DATA
787
+ and line != "Reachable Packages:"
788
+ and not self.string_matches_span_pattern(line, r"Reachable Packages:")
789
+ and not line.strip().startswith("╚")
790
+ and not line.strip().endswith("╝")
791
+ and not line.strip().startswith("╔")
792
+ and not line.strip().endswith("╗")
793
+ and not line.strip().startswith("║")
794
+ and not line.strip().endswith("║")
795
+ and not line.strip().startswith("#")
796
+ and not self.string_matches_regex(
797
+ line,
798
+ r'<span class="r\d+">#\d+<\/span><span class="r\d+">.*<\/span>',
799
+ )
800
+ ):
801
+ current_location = self.REACHABLE_FLOWS_RECOMMENDATION
802
+ last_seen_reachable_flows = self.REACHABLE_FLOWS
803
+
804
+ if current_location in [
805
+ self.REACHABLE_FLOWS_DATA,
806
+ self.REACHABLE_FLOWS_RECOMMENDATION,
807
+ ] and (
808
+ line == "Reachable Packages:"
809
+ or self.string_matches_span_pattern(line, r"Reachable Packages:")
810
+ ):
811
+ current_location = self.REACHABLE_FLOWS_REACHABLE_PACKAGES
812
+ last_seen_reachable_flows = self.REACHABLE_FLOWS
813
+ continue
814
+
815
+ # ---- Data population: Reachable Flows ----
816
+
817
+ if current_location == self.REACHABLE_FLOWS_RECOMMENDATION:
818
+ if recommendation_column != "":
819
+ recommendation_column += "\n"
820
+ recommendation_column += line
821
+ continue
822
+
823
+ if current_location == self.REACHABLE_FLOWS_SUMMARY:
824
+ if sections_tree[self.REACHABLE_FLOWS][self.SUMMARY] != "":
825
+ sections_tree[self.REACHABLE_FLOWS][self.SUMMARY] += "\n"
826
+ sections_tree[self.REACHABLE_FLOWS][self.SUMMARY] += line
827
+ continue
828
+
829
+ if current_location == self.REACHABLE_FLOWS_DATA:
830
+ if line.startswith("#"):
831
+ summary_column = line.split(" ", 1)[1]
832
+ reachable_flow_column = ""
833
+ reachable_packages_column = ""
834
+ recommendation_column = ""
835
+ continue
836
+
837
+ if self.string_matches_regex(
838
+ line,
839
+ r'<span class="r\d+">#\d+<\/span><span class="r\d+">.*<\/span>',
840
+ ):
841
+ summary_column = "</span>".join(line.split("</span>")[1:])
842
+ reachable_flow_column = ""
843
+ reachable_packages_column = ""
844
+ recommendation_column = ""
845
+ continue
846
+
847
+ if line.startswith("║") and line.endswith("║"): # in table row
848
+ if reachable_flow_column != "":
849
+ reachable_flow_column += "\n"
850
+ reachable_flow_column += line[1:-1]
851
+ continue
852
+
853
+ if current_location == self.REACHABLE_FLOWS_REACHABLE_PACKAGES:
854
+ if reachable_packages_column != "":
855
+ reachable_packages_column += "\n"
856
+ reachable_packages_column += line
857
+ continue
858
+
859
+ # ---- Location identification: Non-Reachable Flows ----
860
+
861
+ if (
862
+ current_location == self.NON_REACHABLE_FLOWS_DATA
863
+ and line != "Reachable Packages:"
864
+ and not self.string_matches_span_pattern(line, r"Reachable Packages:")
865
+ and not line.strip().startswith("╚")
866
+ and not line.strip().endswith("╝")
867
+ and not line.strip().startswith("╔")
868
+ and not line.strip().endswith("╗")
869
+ and not line.strip().startswith("║")
870
+ and not line.strip().endswith("║")
871
+ and not line.strip().startswith("#")
872
+ and not self.string_matches_regex(
873
+ line,
874
+ r'<span class="r\d+">#\d+<\/span><span class="r\d+">.*<\/span>',
875
+ )
876
+ ):
877
+ current_location = self.NON_REACHABLE_FLOWS_RECOMMENDATION
878
+ last_seen_reachable_flows = self.NON_REACHABLE_FLOWS
879
+
880
+ if line == self.NON_REACHABLE_FLOWS or self.string_matches_span_pattern(
881
+ line, self.NON_REACHABLE_FLOWS
882
+ ):
883
+ current_location = self.NON_REACHABLE_FLOWS_SUMMARY
884
+ last_seen_reachable_flows = self.NON_REACHABLE_FLOWS
885
+ continue
886
+
887
+ if (
888
+ current_location == self.NON_REACHABLE_FLOWS_SUMMARY
889
+ and line == ""
890
+ and sections_tree[self.NON_REACHABLE_FLOWS][self.SUMMARY] != ""
891
+ ):
892
+ current_location = self.NON_REACHABLE_FLOWS_DATA
893
+ last_seen_reachable_flows = self.NON_REACHABLE_FLOWS
894
+ continue
895
+
896
+ if (
897
+ current_location
898
+ in [
899
+ self.NON_REACHABLE_FLOWS_REACHABLE_PACKAGES,
900
+ self.NON_REACHABLE_FLOWS_DATA,
901
+ self.NON_REACHABLE_FLOWS_RECOMMENDATION,
902
+ ]
903
+ and line == ""
904
+ ):
905
+ cells = [
906
+ summary_column,
907
+ reachable_flow_column,
908
+ reachable_packages_column,
909
+ recommendation_column,
910
+ ]
911
+ sections_tree[self.NON_REACHABLE_FLOWS][self.DATA].append(cells)
912
+ summary_column = ""
913
+ reachable_flow_column = ""
914
+ reachable_packages_column = ""
915
+ recommendation_column = ""
916
+ current_location = self.NON_REACHABLE_FLOWS_DATA
917
+ continue
918
+
919
+ if current_location in [
920
+ self.NON_REACHABLE_FLOWS_DATA,
921
+ self.NON_REACHABLE_FLOWS_RECOMMENDATION,
922
+ ] and (
923
+ line == "Reachable Packages:"
924
+ or self.string_matches_span_pattern(line, r"Reachable Packages:")
925
+ ):
926
+ current_location = self.NON_REACHABLE_FLOWS_REACHABLE_PACKAGES
927
+ last_seen_reachable_flows = self.NON_REACHABLE_FLOWS
928
+ continue
929
+
930
+ # ---- Data population: Non-Reachable Flows ----
931
+
932
+ if current_location == self.NON_REACHABLE_FLOWS_RECOMMENDATION:
933
+ if recommendation_column != "":
934
+ recommendation_column += "\n"
935
+ recommendation_column += line
936
+ continue
937
+
938
+ if current_location == self.NON_REACHABLE_FLOWS_SUMMARY:
939
+ if sections_tree[self.NON_REACHABLE_FLOWS][self.SUMMARY] != "":
940
+ sections_tree[self.NON_REACHABLE_FLOWS][self.SUMMARY] += "\n"
941
+ sections_tree[self.NON_REACHABLE_FLOWS][self.SUMMARY] += line
942
+ continue
943
+
944
+ if current_location == self.NON_REACHABLE_FLOWS_DATA:
945
+ if line.startswith("#"):
946
+ summary_column = line.split(" ", 1)[1]
947
+ reachable_flow_column = ""
948
+ reachable_packages_column = ""
949
+ recommendation_column = ""
950
+ continue
951
+
952
+ if self.string_matches_regex(
953
+ line,
954
+ r'<span class="r\d+">#\d+<\/span><span class="r\d+">.*<\/span>',
955
+ ):
956
+ summary_column = "</span>".join(line.split("</span>")[1:])
957
+ reachable_flow_column = ""
958
+ reachable_packages_column = ""
959
+ recommendation_column = ""
960
+ continue
961
+
962
+ if line.startswith("║") and line.endswith("║"): # in table row
963
+ if reachable_flow_column != "":
964
+ reachable_flow_column += "\n"
965
+ reachable_flow_column += line[1:-1]
966
+ continue
967
+
968
+ if current_location == self.NON_REACHABLE_FLOWS_REACHABLE_PACKAGES:
969
+ if reachable_packages_column != "":
970
+ reachable_packages_column += "\n"
971
+ reachable_packages_column += line
972
+ continue
973
+
974
+ # ---- Location identification: Malware Alert ----
975
+
976
+ if line == self.MALWARE_ALERT or self.string_matches_span_pattern(
977
+ line, self.MALWARE_ALERT
978
+ ):
979
+ current_location = self.MALWARE_ALERT
980
+ continue
981
+
982
+ if (
983
+ current_location == self.MALWARE_ALERT
984
+ and line == ""
985
+ and sections_tree[self.MALWARE_ALERT][self.SUMMARY] != ""
986
+ ):
987
+ current_location = None
988
+ continue
989
+
990
+ # ---- Data population: Malware Alert ----
991
+
992
+ if current_location == self.MALWARE_ALERT:
993
+ if sections_tree[self.MALWARE_ALERT][self.SUMMARY] != "":
994
+ sections_tree[self.MALWARE_ALERT][self.SUMMARY] += "\n"
995
+ sections_tree[self.MALWARE_ALERT][self.SUMMARY] += line
996
+ continue
997
+
998
+ # ---- Location identification: Service Endpoints ----
999
+
1000
+ if line == self.SERVICE_ENDPOINTS or self.string_matches_span_pattern(
1001
+ line, self.SERVICE_ENDPOINTS
1002
+ ):
1003
+ current_location = self.ENDPOINTS_SUMMARY
1004
+ continue
1005
+
1006
+ if line == self.ENDPOINTS or self.string_matches_span_pattern(
1007
+ line, self.ENDPOINTS
1008
+ ):
1009
+ current_location = self.ENDPOINTS
1010
+ continue
1011
+
1012
+ if (
1013
+ current_location == self.ENDPOINTS
1014
+ and line.startswith("╔")
1015
+ and line.endswith("╗")
1016
+ ):
1017
+ current_location = self.ENDPOINTS_DATA
1018
+ current_columns_count = len(line[1:-1].split("╤"))
1019
+ continue
1020
+
1021
+ if (
1022
+ current_location == self.ENDPOINTS_DATA
1023
+ and line.startswith("╚")
1024
+ and line.endswith("╝")
1025
+ ):
1026
+ current_location = self.ENDPOINTS
1027
+ if len(current_table_row) == 0: # should never happen:
1028
+ continue
1029
+
1030
+ cells = ["" for index in range(0, len(current_table_row[0]))]
1031
+
1032
+ for row_piece in current_table_row:
1033
+ for index, row_piece_column in enumerate(row_piece):
1034
+ cells[index] += row_piece_column + "\n"
1035
+
1036
+ sections_tree[self.SERVICE_ENDPOINTS][self.ENDPOINTS][self.DATA].append(
1037
+ cells
1038
+ )
1039
+ current_table_row = []
1040
+
1041
+ continue
1042
+
1043
+ # ---- Data population: Service Endpoints ----
1044
+
1045
+ if current_location == self.ENDPOINTS_SUMMARY:
1046
+ if (
1047
+ sections_tree[self.SERVICE_ENDPOINTS][self.ENDPOINTS][self.SUMMARY]
1048
+ != ""
1049
+ ):
1050
+ sections_tree[self.SERVICE_ENDPOINTS][self.ENDPOINTS][
1051
+ self.SUMMARY
1052
+ ] += "\n"
1053
+ sections_tree[self.SERVICE_ENDPOINTS][self.ENDPOINTS][self.SUMMARY] += (
1054
+ line
1055
+ )
1056
+ continue
1057
+
1058
+ if current_location == self.ENDPOINTS_DATA:
1059
+ if line.startswith("║") and line.endswith("║"): # in table row
1060
+ columns = line[1:-1].rsplit("│", maxsplit=current_columns_count - 1)
1061
+
1062
+ stripped_columns = [column.strip() for column in columns]
1063
+ if (
1064
+ stripped_columns == self.ENDPOINTS_DATA_COLUMNS
1065
+ or self.array_matches_span_pattern(
1066
+ stripped_columns, self.ENDPOINTS_DATA_COLUMNS
1067
+ )
1068
+ ):
1069
+ sections_tree[self.SERVICE_ENDPOINTS][self.ENDPOINTS][
1070
+ self.TABLE_HEADERS
1071
+ ] = stripped_columns
1072
+ current_table_row = []
1073
+ continue
1074
+ else:
1075
+ current_table_row.append(columns)
1076
+
1077
+ elif line.startswith("╟") and line.endswith("╢"): # row separator
1078
+ if len(current_table_row) == 0: # should never happen:
1079
+ continue
1080
+
1081
+ cells = ["" for index in range(0, len(current_table_row[0]))]
1082
+
1083
+ for row_piece in current_table_row:
1084
+ for index, row_piece_column in enumerate(row_piece):
1085
+ cells[index] += row_piece_column + "\n"
1086
+
1087
+ sections_tree[self.SERVICE_ENDPOINTS][self.ENDPOINTS][
1088
+ self.DATA
1089
+ ].append(cells)
1090
+ current_table_row = []
1091
+
1092
+ continue
1093
+
1094
+ if current_location == self.ENDPOINTS and (
1095
+ line.startswith("Identified Endpoints:")
1096
+ or self.string_matches_span_pattern(line, r"Identified Endpoints: \d+")
1097
+ ):
1098
+ sections_tree[self.SERVICE_ENDPOINTS][self.ENDPOINTS][
1099
+ self.IDENTIFIED_ENDPOINTS
1100
+ ] = line.split("Identified Endpoints:", 1)[1].split("<", 1)[0].strip()
1101
+ current_location = None
1102
+ continue
1103
+
1104
+ # ---- Location identification: Prioritized Vulnerabilities ----
1105
+
1106
+ if (
1107
+ line == self.PRIORITIZED_VULNERABILITIES
1108
+ or self.string_matches_span_pattern(
1109
+ line, self.PRIORITIZED_VULNERABILITIES
1110
+ )
1111
+ ):
1112
+ current_location = self.TOP_PRIORITY_BOM_SUMMARY
1113
+ continue
1114
+
1115
+ if (
1116
+ line == self.PRIORITIZED_VULNERABILITIES_ALTERNATIVE
1117
+ or self.string_matches_span_pattern(
1118
+ line, self.PRIORITIZED_VULNERABILITIES_ALTERNATIVE
1119
+ )
1120
+ ):
1121
+ current_location = self.TOP_PRIORITY_BOM_SUMMARY
1122
+ continue
1123
+
1124
+ if line == self.TOP_PRIORITY_BOM or self.string_matches_span_pattern(
1125
+ line, self.TOP_PRIORITY_BOM
1126
+ ):
1127
+ current_location = self.TOP_PRIORITY_BOM
1128
+ continue
1129
+
1130
+ if (
1131
+ current_location == self.TOP_PRIORITY_BOM
1132
+ and line.startswith("╔")
1133
+ and line.endswith("╗")
1134
+ ):
1135
+ current_location = self.TOP_PRIORITY_BOM_DATA
1136
+ current_columns_count = len(line[1:-1].split("╤"))
1137
+ continue
1138
+
1139
+ if (
1140
+ current_location == self.TOP_PRIORITY_BOM_DATA
1141
+ and line.startswith("╚")
1142
+ and line.endswith("╝")
1143
+ ):
1144
+ current_location = self.TOP_PRIORITY_BOM
1145
+ if len(current_table_row) == 0: # should never happen:
1146
+ continue
1147
+
1148
+ cells = ["" for index in range(0, len(current_table_row[0]))]
1149
+
1150
+ for row_piece in current_table_row:
1151
+ for index, row_piece_column in enumerate(row_piece):
1152
+ cells[index] += row_piece_column + "\n"
1153
+ for index, cell in enumerate(
1154
+ cells
1155
+ ): # dirty fix to eliminate the useless \n added at the end of each cell
1156
+ cells[index] = cell[:-1]
1157
+
1158
+ sections_tree[self.PRIORITIZED_VULNERABILITIES][self.TOP_PRIORITY_BOM][
1159
+ self.DATA
1160
+ ].append(cells)
1161
+ current_table_row = []
1162
+ continue
1163
+
1164
+ # ---- Data population: Prioritized Vulnerabilities ----
1165
+
1166
+ if current_location == self.TOP_PRIORITY_BOM_SUMMARY:
1167
+ if (
1168
+ sections_tree[self.PRIORITIZED_VULNERABILITIES][
1169
+ self.TOP_PRIORITY_BOM
1170
+ ][self.SUMMARY]
1171
+ != ""
1172
+ ):
1173
+ sections_tree[self.PRIORITIZED_VULNERABILITIES][
1174
+ self.TOP_PRIORITY_BOM
1175
+ ][self.SUMMARY] += "\n"
1176
+ sections_tree[self.PRIORITIZED_VULNERABILITIES][self.TOP_PRIORITY_BOM][
1177
+ self.SUMMARY
1178
+ ] += line
1179
+ continue
1180
+
1181
+ if current_location == self.TOP_PRIORITY_BOM_DATA:
1182
+ if line.startswith("║") and line.endswith("║"): # in table row
1183
+ columns = line[1:-1].rsplit("│", maxsplit=current_columns_count - 1)
1184
+
1185
+ stripped_columns = [column.strip() for column in columns]
1186
+ if (
1187
+ stripped_columns == self.TOP_PRIORITY_BOM_DATA_COLUMNS
1188
+ or self.array_matches_span_pattern(
1189
+ stripped_columns, self.TOP_PRIORITY_BOM_DATA_COLUMNS
1190
+ )
1191
+ ):
1192
+ sections_tree[self.PRIORITIZED_VULNERABILITIES][
1193
+ self.TOP_PRIORITY_BOM
1194
+ ][self.TABLE_HEADERS] = stripped_columns
1195
+ current_table_row = []
1196
+ continue
1197
+ else:
1198
+ current_table_row.append(columns)
1199
+
1200
+ elif line.startswith("╟") and line.endswith("╢"): # row separator
1201
+ if len(current_table_row) == 0: # should never happen:
1202
+ continue
1203
+
1204
+ cells = ["" for index in range(0, len(current_table_row[0]))]
1205
+
1206
+ for row_piece in current_table_row:
1207
+ for index, row_piece_column in enumerate(row_piece):
1208
+ cells[index] += row_piece_column + "\n"
1209
+ for index, cell in enumerate(
1210
+ cells
1211
+ ): # dirty fix to eliminate the useless \n added at the end of each cell
1212
+ cells[index] = cell[:-1]
1213
+
1214
+ sections_tree[self.PRIORITIZED_VULNERABILITIES][
1215
+ self.TOP_PRIORITY_BOM
1216
+ ][self.DATA].append(cells)
1217
+ current_table_row = []
1218
+
1219
+ continue
1220
+
1221
+ if (
1222
+ current_location == self.TOP_PRIORITY_BOM
1223
+ and line.startswith("Prioritized count:")
1224
+ or self.string_matches_span_pattern(line, r"Prioritized count: \d+")
1225
+ ):
1226
+ sections_tree[self.PRIORITIZED_VULNERABILITIES][self.TOP_PRIORITY_BOM][
1227
+ self.PRIORITIZED_COUNT
1228
+ ] = line.split("Prioritized count:", 1)[1]
1229
+ current_location = None
1230
+ continue
1231
+
1232
+ return sections_tree
1233
+
1234
+ def format_as_html_ul(self, input_string):
1235
+ formatted_content = ["<ul>"]
1236
+
1237
+ for content_element in input_string.strip().splitlines():
1238
+ content_element = content_element.strip()
1239
+ if content_element.startswith("•"):
1240
+ content_element = content_element[1:]
1241
+ elif " • " in content_element:
1242
+ content_element = content_element.replace(" • ", "")
1243
+ if content_element.strip() != "":
1244
+ formatted_content.append(f"<li>{content_element}</li>")
1245
+
1246
+ formatted_content.append("</ul>")
1247
+
1248
+ return "\n".join(formatted_content)
1249
+
1250
+ def identify_table_id(self, table_inits, prefix):
1251
+ table_init = f'initTable("#{prefix}");'
1252
+ if table_init not in table_inits:
1253
+ return prefix
1254
+
1255
+ counter = 0
1256
+ while True:
1257
+ table_id = f"{prefix}-{counter}"
1258
+ table_init = f'initTable("#{table_id}");'
1259
+ if table_init not in table_inits:
1260
+ return table_id
1261
+ else:
1262
+ counter += 1
1263
+
1264
+ def generate_section_prioritized_vulnerabilities(
1265
+ self, sections_tree, report_content, table_inits, tree_is_from_html, piece_id
1266
+ ):
1267
+ prioritized_vulnerabilities = PRIORITIZED_VULNERABILITIES
1268
+
1269
+ if piece_id is None:
1270
+ prioritized_vulnerabilities = prioritized_vulnerabilities.replace(
1271
+ "<PIECE_ID_PLACEHOLDER>", "", 1
1272
+ )
1273
+ else:
1274
+ if tree_is_from_html is False:
1275
+ piece_id = html.escape(piece_id)
1276
+ prioritized_vulnerabilities = prioritized_vulnerabilities.replace(
1277
+ "<PIECE_ID_PLACEHOLDER>", f"{piece_id} {SEPARATOR} ", 1
1278
+ )
1279
+
1280
+ current_content = sections_tree[self.PRIORITIZED_VULNERABILITIES][
1281
+ self.TOP_PRIORITY_BOM
1282
+ ][self.PRIORITIZED_COUNT]
1283
+ if tree_is_from_html is False:
1284
+ current_content = html.escape(current_content)
1285
+
1286
+ try:
1287
+ prioritized_count = int(current_content)
1288
+ except ValueError:
1289
+ prioritized_count = -1
1290
+ pass
1291
+ if prioritized_count == -1:
1292
+ all_cves = set()
1293
+ for row_data in sections_tree[self.PRIORITIZED_VULNERABILITIES][
1294
+ self.TOP_PRIORITY_BOM
1295
+ ][self.DATA]:
1296
+ current_data = (
1297
+ row_data[
1298
+ self.TOP_PRIORITY_BOM_DATA_COLUMNS.index("Prioritized CVEs")
1299
+ ]
1300
+ if len(row_data)
1301
+ >= self.TOP_PRIORITY_BOM_DATA_COLUMNS.index("Prioritized CVEs") + 1
1302
+ else "-"
1303
+ )
1304
+ if current_data == "-":
1305
+ continue
1306
+ for data_element in current_data.strip().splitlines():
1307
+ all_cves.add(data_element)
1308
+ current_content = f"{len(all_cves)}"
1309
+
1310
+ prioritized_vulnerabilities = prioritized_vulnerabilities.replace(
1311
+ "<PRIORITIZED_COUNT_PLACEHOLDER>", current_content, 1
1312
+ )
1313
+
1314
+ current_content = sections_tree[self.PRIORITIZED_VULNERABILITIES][
1315
+ self.TOP_PRIORITY_BOM
1316
+ ][self.SUMMARY]
1317
+ if tree_is_from_html is False:
1318
+ current_content = html.escape(current_content)
1319
+ prioritized_vulnerabilities = prioritized_vulnerabilities.replace(
1320
+ "<SUMMARY_PLACEHOLDER>", current_content, 1
1321
+ )
1322
+
1323
+ html_table = []
1324
+ for row_data in sections_tree[self.PRIORITIZED_VULNERABILITIES][
1325
+ self.TOP_PRIORITY_BOM
1326
+ ][self.DATA]:
1327
+ if self.raw_content:
1328
+ html_row = ["<tr>"]
1329
+
1330
+ current_content = (
1331
+ row_data[self.TOP_PRIORITY_BOM_DATA_COLUMNS.index("Package")]
1332
+ if len(row_data)
1333
+ >= self.TOP_PRIORITY_BOM_DATA_COLUMNS.index("Package") + 1
1334
+ else "-"
1335
+ )
1336
+ if tree_is_from_html is False:
1337
+ current_content = html.escape(current_content)
1338
+ html_row.append(f"<td><pre>{current_content}</pre></td>")
1339
+
1340
+ current_content = (
1341
+ row_data[
1342
+ self.TOP_PRIORITY_BOM_DATA_COLUMNS.index("Prioritized CVEs")
1343
+ ]
1344
+ if len(row_data)
1345
+ >= self.TOP_PRIORITY_BOM_DATA_COLUMNS.index("Prioritized CVEs") + 1
1346
+ else "-"
1347
+ )
1348
+ if tree_is_from_html is False:
1349
+ current_content = html.escape(current_content)
1350
+ html_row.append(f"<td><pre>{current_content}</pre></td>")
1351
+
1352
+ current_content = (
1353
+ row_data[self.TOP_PRIORITY_BOM_DATA_COLUMNS.index("Fix Version")]
1354
+ if len(row_data)
1355
+ >= self.TOP_PRIORITY_BOM_DATA_COLUMNS.index("Fix Version") + 1
1356
+ else "-"
1357
+ )
1358
+ if tree_is_from_html is False:
1359
+ current_content = html.escape(current_content)
1360
+ html_row.append(f"<td><pre>{current_content}</pre></td>")
1361
+
1362
+ current_content = (
1363
+ row_data[self.TOP_PRIORITY_BOM_DATA_COLUMNS.index("Next Steps")]
1364
+ if len(row_data)
1365
+ >= self.TOP_PRIORITY_BOM_DATA_COLUMNS.index("Next Steps") + 1
1366
+ else "-"
1367
+ )
1368
+ if tree_is_from_html is False:
1369
+ current_content = html.escape(current_content)
1370
+ html_row.append(f"<td><pre>{current_content}</pre></td>")
1371
+
1372
+ html_row.append("</tr>")
1373
+
1374
+ html_table.append("\n".join(html_row))
1375
+ else:
1376
+ html_row = ["<tr>"]
1377
+
1378
+ current_content = (
1379
+ row_data[self.TOP_PRIORITY_BOM_DATA_COLUMNS.index("Package")]
1380
+ if len(row_data)
1381
+ >= self.TOP_PRIORITY_BOM_DATA_COLUMNS.index("Package") + 1
1382
+ else "-"
1383
+ )
1384
+ if tree_is_from_html is False:
1385
+ current_content = html.escape(current_content)
1386
+ html_row.append(f"<td>{current_content}</td>")
1387
+
1388
+ current_content = (
1389
+ row_data[
1390
+ self.TOP_PRIORITY_BOM_DATA_COLUMNS.index("Prioritized CVEs")
1391
+ ]
1392
+ if len(row_data)
1393
+ >= self.TOP_PRIORITY_BOM_DATA_COLUMNS.index("Prioritized CVEs") + 1
1394
+ else "-"
1395
+ )
1396
+ if tree_is_from_html is False:
1397
+ current_content = html.escape(current_content)
1398
+ cves = []
1399
+ for content_element in current_content.strip().splitlines():
1400
+ cves.append(
1401
+ f'<span class="badge bg-secondary">{content_element}</span>'
1402
+ )
1403
+ html_row.append(f"<td>{'<br>'.join(cves)}</td>")
1404
+
1405
+ current_content = (
1406
+ row_data[self.TOP_PRIORITY_BOM_DATA_COLUMNS.index("Fix Version")]
1407
+ if len(row_data)
1408
+ >= self.TOP_PRIORITY_BOM_DATA_COLUMNS.index("Fix Version") + 1
1409
+ else "-"
1410
+ )
1411
+ if tree_is_from_html is False:
1412
+ current_content = html.escape(current_content)
1413
+ html_row.append(f"<td>{current_content}</td>")
1414
+
1415
+ current_content = (
1416
+ row_data[self.TOP_PRIORITY_BOM_DATA_COLUMNS.index("Next Steps")]
1417
+ if len(row_data)
1418
+ >= self.TOP_PRIORITY_BOM_DATA_COLUMNS.index("Next Steps") + 1
1419
+ else "-"
1420
+ )
1421
+ if tree_is_from_html is False:
1422
+ current_content = html.escape(current_content)
1423
+ html_row.append(f"<td>{current_content.replace(NEWLINE, '<br>')}</td>")
1424
+
1425
+ html_row.append("</tr>")
1426
+
1427
+ html_table.append("\n".join(html_row))
1428
+
1429
+ prioritized_vulnerabilities = prioritized_vulnerabilities.replace(
1430
+ "<TABLE_PLACEHOLDER>", "\n".join(html_table), 1
1431
+ )
1432
+
1433
+ table_id = self.identify_table_id(
1434
+ table_inits, "prioritized-vulnerabilities-table"
1435
+ )
1436
+ table_inits.append(f'initTable("#{table_id}");')
1437
+ prioritized_vulnerabilities = prioritized_vulnerabilities.replace(
1438
+ "<TABLE_ID_PLACEHOLDER>", table_id, 1
1439
+ )
1440
+
1441
+ report_content.append(prioritized_vulnerabilities)
1442
+
1443
+ def generate_section_vulnerability_disclosure_report(
1444
+ self, sections_tree, report_content, table_inits, tree_is_from_html, piece_id
1445
+ ):
1446
+ vulnerability_disclosure_report = VDR
1447
+
1448
+ if piece_id is None:
1449
+ vulnerability_disclosure_report = vulnerability_disclosure_report.replace(
1450
+ "<PIECE_ID_PLACEHOLDER>", "", 1
1451
+ )
1452
+ else:
1453
+ if tree_is_from_html is False:
1454
+ piece_id = html.escape(piece_id)
1455
+ vulnerability_disclosure_report = vulnerability_disclosure_report.replace(
1456
+ "<PIECE_ID_PLACEHOLDER>", f"{piece_id} {SEPARATOR} ", 1
1457
+ )
1458
+
1459
+ current_content = sections_tree[self.VULNERABILITY_DISCLOSURE_REPORT][
1460
+ self.DEPENDENCY_SCAN_RESULTS_BOM
1461
+ ][self.VULNERABILITIES_COUNT]
1462
+ if tree_is_from_html is False:
1463
+ current_content = html.escape(current_content)
1464
+
1465
+ try:
1466
+ vulnerabilities_count = int(current_content)
1467
+ except ValueError:
1468
+ vulnerabilities_count = -1
1469
+ pass
1470
+ if vulnerabilities_count == -1:
1471
+ all_cves = set()
1472
+ for row_data in sections_tree[self.VULNERABILITY_DISCLOSURE_REPORT][
1473
+ self.DEPENDENCY_SCAN_RESULTS_BOM
1474
+ ][self.DATA]:
1475
+ current_data = (
1476
+ row_data[
1477
+ self.DEPENDENCY_SCAN_RESULTS_BOM_DATA_COLUMNS.index(
1478
+ "Dependency Tree"
1479
+ )
1480
+ ]
1481
+ if len(row_data)
1482
+ >= self.DEPENDENCY_SCAN_RESULTS_BOM_DATA_COLUMNS.index(
1483
+ "Dependency Tree"
1484
+ )
1485
+ + 1
1486
+ else "-"
1487
+ )
1488
+ if current_data == "-":
1489
+ continue
1490
+ for data_element in current_data.strip().splitlines():
1491
+ if "⬅" in data_element:
1492
+ all_cves.add(data_element)
1493
+ current_content = f"{len(all_cves)}"
1494
+
1495
+ vulnerability_disclosure_report = vulnerability_disclosure_report.replace(
1496
+ "<VULNERABILITIES_COUNT_PLACEHOLDER>", current_content, 1
1497
+ )
1498
+
1499
+ current_content = sections_tree[self.VULNERABILITY_DISCLOSURE_REPORT][
1500
+ self.DEPENDENCY_SCAN_RESULTS_BOM
1501
+ ][self.SUMMARY]
1502
+ if tree_is_from_html is False:
1503
+ current_content = html.escape(current_content)
1504
+
1505
+ if current_content == "":
1506
+ current_content = "The table below lists all vulnerabilities identified in this project. Review and triage the information to identify any critical vulnerabilities."
1507
+ vulnerability_disclosure_report = vulnerability_disclosure_report.replace(
1508
+ "<SUMMARY_PLACEHOLDER>", current_content, 1
1509
+ )
1510
+
1511
+ if (
1512
+ sections_tree[self.VULNERABILITY_DISCLOSURE_REPORT][self.RECOMMENDATION]
1513
+ != ""
1514
+ ):
1515
+ current_content = sections_tree[self.VULNERABILITY_DISCLOSURE_REPORT][
1516
+ self.RECOMMENDATION
1517
+ ]
1518
+ if tree_is_from_html is False:
1519
+ current_content = html.escape(current_content)
1520
+ current_content = current_content.replace("\n", "<br>")
1521
+ current_content = f"""
1522
+ <br><br>
1523
+ <div class="alert alert-info" role="alert">
1524
+ <h4>Recommendation</h4>
1525
+ <br>
1526
+ <span>{current_content}</span>
1527
+ </div>"""
1528
+ vulnerability_disclosure_report = vulnerability_disclosure_report.replace(
1529
+ "<RECOMMENDATION_PLACEHOLDER>", current_content, 1
1530
+ )
1531
+ else:
1532
+ vulnerability_disclosure_report = vulnerability_disclosure_report.replace(
1533
+ "<RECOMMENDATION_PLACEHOLDER>", "", 1
1534
+ )
1535
+
1536
+ if (
1537
+ sections_tree[self.VULNERABILITY_DISCLOSURE_REPORT][self.ACTION_REQUIRED]
1538
+ != ""
1539
+ ):
1540
+ current_content = sections_tree[self.VULNERABILITY_DISCLOSURE_REPORT][
1541
+ self.ACTION_REQUIRED
1542
+ ]
1543
+ if tree_is_from_html is False:
1544
+ current_content = html.escape(current_content)
1545
+ current_content = current_content.replace("\n", "<br>")
1546
+ current_content = f"""
1547
+ <br><br>
1548
+ <div class="alert alert-danger" role="alert">
1549
+ <h4>Action Required</h4>
1550
+ <br>
1551
+ <span>{current_content}</span>
1552
+ </div>"""
1553
+ vulnerability_disclosure_report = vulnerability_disclosure_report.replace(
1554
+ "<ACTION_REQUIRED_PLACEHOLDER>", current_content, 1
1555
+ )
1556
+ else:
1557
+ vulnerability_disclosure_report = vulnerability_disclosure_report.replace(
1558
+ "<ACTION_REQUIRED_PLACEHOLDER>", "", 1
1559
+ )
1560
+
1561
+ html_table = []
1562
+ for row_data in sections_tree[self.VULNERABILITY_DISCLOSURE_REPORT][
1563
+ self.DEPENDENCY_SCAN_RESULTS_BOM
1564
+ ][self.DATA]:
1565
+ if self.raw_content:
1566
+ html_row = ["<tr>"]
1567
+
1568
+ current_content = (
1569
+ row_data[
1570
+ self.DEPENDENCY_SCAN_RESULTS_BOM_DATA_COLUMNS.index(
1571
+ "Dependency Tree"
1572
+ )
1573
+ ]
1574
+ if len(row_data)
1575
+ >= self.DEPENDENCY_SCAN_RESULTS_BOM_DATA_COLUMNS.index(
1576
+ "Dependency Tree"
1577
+ )
1578
+ + 1
1579
+ else "-"
1580
+ )
1581
+ if tree_is_from_html is False:
1582
+ current_content = html.escape(current_content)
1583
+ html_row.append(f"<td><pre>{current_content}</pre></td>")
1584
+
1585
+ current_content = (
1586
+ row_data[
1587
+ self.DEPENDENCY_SCAN_RESULTS_BOM_DATA_COLUMNS.index("Insights")
1588
+ ]
1589
+ if len(row_data)
1590
+ >= self.DEPENDENCY_SCAN_RESULTS_BOM_DATA_COLUMNS.index("Insights")
1591
+ + 1
1592
+ else "-"
1593
+ )
1594
+ if tree_is_from_html is False:
1595
+ current_content = html.escape(current_content)
1596
+ html_row.append(f"<td><pre>{current_content}</pre></td>")
1597
+
1598
+ current_content = (
1599
+ row_data[
1600
+ self.DEPENDENCY_SCAN_RESULTS_BOM_DATA_COLUMNS.index(
1601
+ "Fix Version"
1602
+ )
1603
+ ]
1604
+ if len(row_data)
1605
+ >= self.DEPENDENCY_SCAN_RESULTS_BOM_DATA_COLUMNS.index(
1606
+ "Fix Version"
1607
+ )
1608
+ + 1
1609
+ else "-"
1610
+ )
1611
+ if tree_is_from_html is False:
1612
+ current_content = html.escape(current_content)
1613
+ html_row.append(f"<td><pre>{current_content}</pre></td>")
1614
+
1615
+ current_content = (
1616
+ row_data[
1617
+ self.DEPENDENCY_SCAN_RESULTS_BOM_DATA_COLUMNS.index("Severity")
1618
+ ]
1619
+ if len(row_data)
1620
+ >= self.DEPENDENCY_SCAN_RESULTS_BOM_DATA_COLUMNS.index("Severity")
1621
+ + 1
1622
+ else "-"
1623
+ )
1624
+ if tree_is_from_html is False:
1625
+ current_content = html.escape(current_content)
1626
+ html_row.append(f"<td><pre>{current_content}</pre></td>")
1627
+
1628
+ current_content = (
1629
+ row_data[
1630
+ self.DEPENDENCY_SCAN_RESULTS_BOM_DATA_COLUMNS.index("Score")
1631
+ ]
1632
+ if len(row_data)
1633
+ >= self.DEPENDENCY_SCAN_RESULTS_BOM_DATA_COLUMNS.index("Score") + 1
1634
+ else "-"
1635
+ )
1636
+ if tree_is_from_html is False:
1637
+ current_content = html.escape(current_content)
1638
+ html_row.append(f"<td><pre>{current_content}</pre></td>")
1639
+
1640
+ html_row.append("</tr>")
1641
+
1642
+ html_table.append("\n".join(html_row))
1643
+ else:
1644
+ html_row = ["<tr>"]
1645
+
1646
+ current_content = (
1647
+ row_data[
1648
+ self.DEPENDENCY_SCAN_RESULTS_BOM_DATA_COLUMNS.index(
1649
+ "Dependency Tree"
1650
+ )
1651
+ ]
1652
+ if len(row_data)
1653
+ >= self.DEPENDENCY_SCAN_RESULTS_BOM_DATA_COLUMNS.index(
1654
+ "Dependency Tree"
1655
+ )
1656
+ + 1
1657
+ else "-"
1658
+ )
1659
+ if tree_is_from_html is False:
1660
+ current_content = html.escape(current_content)
1661
+ html_row.append(f"<td><pre>{current_content}</pre></td>")
1662
+
1663
+ current_content = (
1664
+ row_data[
1665
+ self.DEPENDENCY_SCAN_RESULTS_BOM_DATA_COLUMNS.index("Insights")
1666
+ ]
1667
+ if len(row_data)
1668
+ >= self.DEPENDENCY_SCAN_RESULTS_BOM_DATA_COLUMNS.index("Insights")
1669
+ + 1
1670
+ else "-"
1671
+ )
1672
+ if tree_is_from_html is False:
1673
+ current_content = html.escape(current_content)
1674
+ html_row.append(f"<td><pre>{current_content}</pre></td>")
1675
+
1676
+ current_content = (
1677
+ row_data[
1678
+ self.DEPENDENCY_SCAN_RESULTS_BOM_DATA_COLUMNS.index(
1679
+ "Fix Version"
1680
+ )
1681
+ ]
1682
+ if len(row_data)
1683
+ >= self.DEPENDENCY_SCAN_RESULTS_BOM_DATA_COLUMNS.index(
1684
+ "Fix Version"
1685
+ )
1686
+ + 1
1687
+ else "-"
1688
+ )
1689
+ if tree_is_from_html is False:
1690
+ current_content = html.escape(current_content)
1691
+ html_row.append(f"<td><pre>{current_content}</pre></td>")
1692
+
1693
+ current_content = (
1694
+ row_data[
1695
+ self.DEPENDENCY_SCAN_RESULTS_BOM_DATA_COLUMNS.index("Severity")
1696
+ ]
1697
+ if len(row_data)
1698
+ >= self.DEPENDENCY_SCAN_RESULTS_BOM_DATA_COLUMNS.index("Severity")
1699
+ + 1
1700
+ else "-"
1701
+ )
1702
+ if tree_is_from_html is False:
1703
+ current_content = html.escape(current_content)
1704
+ html_row.append(f"<td><pre>{current_content}</pre></td>")
1705
+
1706
+ current_content = (
1707
+ row_data[
1708
+ self.DEPENDENCY_SCAN_RESULTS_BOM_DATA_COLUMNS.index("Score")
1709
+ ]
1710
+ if len(row_data)
1711
+ >= self.DEPENDENCY_SCAN_RESULTS_BOM_DATA_COLUMNS.index("Score") + 1
1712
+ else "-"
1713
+ )
1714
+ if tree_is_from_html is False:
1715
+ current_content = html.escape(current_content)
1716
+ html_row.append(f"<td><pre>{current_content}</pre></td>")
1717
+
1718
+ html_row.append("</tr>")
1719
+
1720
+ html_table.append("\n".join(html_row))
1721
+
1722
+ vulnerability_disclosure_report = vulnerability_disclosure_report.replace(
1723
+ "<TABLE_PLACEHOLDER>", "\n".join(html_table), 1
1724
+ )
1725
+
1726
+ table_id = self.identify_table_id(
1727
+ table_inits, "vulnerability-disclosure-report-table"
1728
+ )
1729
+ table_inits.append(f'initTable("#{table_id}");')
1730
+ vulnerability_disclosure_report = vulnerability_disclosure_report.replace(
1731
+ "<TABLE_ID_PLACEHOLDER>", table_id, 1
1732
+ )
1733
+
1734
+ report_content.append(vulnerability_disclosure_report)
1735
+
1736
+ def generate_section_proactive_measures(
1737
+ self, sections_tree, report_content, table_inits, tree_is_from_html, piece_id
1738
+ ):
1739
+ proactive_measures = PROACTIVE_MEASURES
1740
+
1741
+ if piece_id is None:
1742
+ proactive_measures = proactive_measures.replace(
1743
+ "<PIECE_ID_PLACEHOLDER>", "", 1
1744
+ )
1745
+ else:
1746
+ if tree_is_from_html is False:
1747
+ piece_id = html.escape(piece_id)
1748
+ proactive_measures = proactive_measures.replace(
1749
+ "<PIECE_ID_PLACEHOLDER>", f"{piece_id} {SEPARATOR} ", 1
1750
+ )
1751
+
1752
+ current_content = sections_tree[self.PROACTIVE_MEASURES][
1753
+ self.TOP_REACHABLE_PACKAGES
1754
+ ][self.SUMMARY]
1755
+ if tree_is_from_html is False:
1756
+ current_content = html.escape(current_content)
1757
+ proactive_measures = proactive_measures.replace(
1758
+ "<SUMMARY_PLACEHOLDER>", current_content, 1
1759
+ )
1760
+
1761
+ html_table = []
1762
+ for row_data in sections_tree[self.PROACTIVE_MEASURES][
1763
+ self.TOP_REACHABLE_PACKAGES
1764
+ ][self.DATA]:
1765
+ if self.raw_content:
1766
+ html_row = ["<tr>"]
1767
+
1768
+ current_content = (
1769
+ row_data[self.TOP_REACHABLE_PACKAGES_DATA_COLUMNS.index("Package")]
1770
+ if len(row_data)
1771
+ >= self.TOP_REACHABLE_PACKAGES_DATA_COLUMNS.index("Package") + 1
1772
+ else "-"
1773
+ )
1774
+ if tree_is_from_html is False:
1775
+ current_content = html.escape(current_content)
1776
+ html_row.append(f"<td><pre>{current_content}</pre></td>")
1777
+
1778
+ current_content = (
1779
+ row_data[
1780
+ self.TOP_REACHABLE_PACKAGES_DATA_COLUMNS.index(
1781
+ "Reachable Flows"
1782
+ )
1783
+ ]
1784
+ if len(row_data)
1785
+ >= self.TOP_REACHABLE_PACKAGES_DATA_COLUMNS.index("Reachable Flows")
1786
+ + 1
1787
+ else "-"
1788
+ )
1789
+ if tree_is_from_html is False:
1790
+ current_content = html.escape(current_content)
1791
+ html_row.append(f"<td><pre>{current_content}</pre></td>")
1792
+
1793
+ html_row.append("</tr>")
1794
+
1795
+ html_table.append("\n".join(html_row))
1796
+
1797
+ else:
1798
+ html_row = ["<tr>"]
1799
+
1800
+ current_content = (
1801
+ row_data[self.TOP_REACHABLE_PACKAGES_DATA_COLUMNS.index("Package")]
1802
+ if len(row_data)
1803
+ >= self.TOP_REACHABLE_PACKAGES_DATA_COLUMNS.index("Package") + 1
1804
+ else "-"
1805
+ )
1806
+ if tree_is_from_html is False:
1807
+ current_content = html.escape(current_content)
1808
+ html_row.append(f"<td>{current_content}</td>")
1809
+ reachable_flows_index = self.TOP_REACHABLE_PACKAGES_DATA_COLUMNS.index(
1810
+ "Reachable Flows"
1811
+ )
1812
+ current_content = (
1813
+ row_data[
1814
+ self.TOP_REACHABLE_PACKAGES_DATA_COLUMNS.index(
1815
+ "Reachable Flows"
1816
+ )
1817
+ ]
1818
+ if len(row_data)
1819
+ >= self.TOP_REACHABLE_PACKAGES_DATA_COLUMNS.index("Reachable Flows")
1820
+ + 1
1821
+ else "-"
1822
+ )
1823
+ if tree_is_from_html is False:
1824
+ current_content = html.escape(current_content)
1825
+ if current_content:
1826
+ html_row.append(f"<td>{current_content}</td>")
1827
+
1828
+ html_row.append("</tr>")
1829
+
1830
+ html_table.append("\n".join(html_row))
1831
+
1832
+ proactive_measures = proactive_measures.replace(
1833
+ "<TABLE_PLACEHOLDER>", "\n".join(html_table), 1
1834
+ )
1835
+
1836
+ table_id = self.identify_table_id(table_inits, "proactive-measures-table")
1837
+ table_inits.append(f'initTable("#{table_id}");')
1838
+ proactive_measures = proactive_measures.replace(
1839
+ "<TABLE_ID_PLACEHOLDER>", table_id, 1
1840
+ )
1841
+
1842
+ report_content.append(proactive_measures)
1843
+
1844
+ def generate_section_service_endpoints(
1845
+ self, sections_tree, report_content, table_inits, tree_is_from_html, piece_id
1846
+ ):
1847
+ service_endpoints = SERVICE_ENDPOINTS
1848
+
1849
+ if piece_id is None:
1850
+ service_endpoints = service_endpoints.replace(
1851
+ "<PIECE_ID_PLACEHOLDER>", "", 1
1852
+ )
1853
+ else:
1854
+ if tree_is_from_html is False:
1855
+ piece_id = html.escape(piece_id)
1856
+ service_endpoints = service_endpoints.replace(
1857
+ "<PIECE_ID_PLACEHOLDER>", f"{piece_id} {SEPARATOR} ", 1
1858
+ )
1859
+
1860
+ current_content = sections_tree[self.SERVICE_ENDPOINTS][self.ENDPOINTS][
1861
+ self.IDENTIFIED_ENDPOINTS
1862
+ ]
1863
+ if tree_is_from_html is False:
1864
+ current_content = html.escape(current_content)
1865
+
1866
+ try:
1867
+ identified_endpoints = int(current_content)
1868
+ except ValueError:
1869
+ identified_endpoints = -1
1870
+ pass
1871
+ if identified_endpoints == -1:
1872
+ current_content = f"{len(sections_tree[self.SERVICE_ENDPOINTS][self.ENDPOINTS][self.DATA])}"
1873
+
1874
+ service_endpoints = service_endpoints.replace(
1875
+ "<IDENTIFIED_ENDPOINTS_PLACEHOLDER>", current_content, 1
1876
+ )
1877
+
1878
+ current_content = sections_tree[self.SERVICE_ENDPOINTS][self.ENDPOINTS][
1879
+ self.SUMMARY
1880
+ ]
1881
+ if tree_is_from_html is False:
1882
+ current_content = html.escape(current_content)
1883
+ service_endpoints = service_endpoints.replace(
1884
+ "<SUMMARY_PLACEHOLDER>", current_content, 1
1885
+ )
1886
+
1887
+ html_table = []
1888
+ for row_data in sections_tree[self.SERVICE_ENDPOINTS][self.ENDPOINTS][
1889
+ self.DATA
1890
+ ]:
1891
+ if self.raw_content:
1892
+ html_row = ["<tr>"]
1893
+
1894
+ current_content = (
1895
+ row_data[self.ENDPOINTS_DATA_COLUMNS.index("URL Pattern")]
1896
+ if len(row_data)
1897
+ >= self.ENDPOINTS_DATA_COLUMNS.index("URL Pattern") + 1
1898
+ else "-"
1899
+ )
1900
+ if tree_is_from_html is False:
1901
+ current_content = html.escape(current_content)
1902
+ html_row.append(f"<td><pre>{current_content}</pre></td>")
1903
+
1904
+ current_content = (
1905
+ row_data[self.ENDPOINTS_DATA_COLUMNS.index("HTTP Methods")]
1906
+ if len(row_data)
1907
+ >= self.ENDPOINTS_DATA_COLUMNS.index("HTTP Methods") + 1
1908
+ else "-"
1909
+ )
1910
+ if tree_is_from_html is False:
1911
+ current_content = html.escape(current_content)
1912
+ html_row.append(f"<td><pre>{current_content}</pre></td>")
1913
+
1914
+ current_content = (
1915
+ row_data[self.ENDPOINTS_DATA_COLUMNS.index("Code Hotspots")]
1916
+ if len(row_data)
1917
+ >= self.ENDPOINTS_DATA_COLUMNS.index("Code Hotspots") + 1
1918
+ else "-"
1919
+ )
1920
+ if tree_is_from_html is False:
1921
+ current_content = html.escape(current_content)
1922
+ html_row.append(f"<td><pre>{current_content}</pre></td>")
1923
+
1924
+ html_row.append("</tr>")
1925
+
1926
+ html_table.append("\n".join(html_row))
1927
+ else:
1928
+ html_row = ["<tr>"]
1929
+
1930
+ current_content = (
1931
+ row_data[self.ENDPOINTS_DATA_COLUMNS.index("URL Pattern")]
1932
+ if len(row_data)
1933
+ >= self.ENDPOINTS_DATA_COLUMNS.index("URL Pattern") + 1
1934
+ else "-"
1935
+ )
1936
+ if tree_is_from_html is False:
1937
+ current_content = html.escape(current_content)
1938
+ html_row.append(f"<td>{current_content}</td>")
1939
+
1940
+ current_content = (
1941
+ row_data[self.ENDPOINTS_DATA_COLUMNS.index("HTTP Methods")]
1942
+ if len(row_data)
1943
+ >= self.ENDPOINTS_DATA_COLUMNS.index("HTTP Methods") + 1
1944
+ else "-"
1945
+ )
1946
+ if tree_is_from_html is False:
1947
+ current_content = html.escape(current_content)
1948
+ current_content = current_content.replace("\n", "<br>")
1949
+ html_row.append(f"<td>{current_content}</td>")
1950
+
1951
+ current_content = (
1952
+ row_data[self.ENDPOINTS_DATA_COLUMNS.index("Code Hotspots")]
1953
+ if len(row_data)
1954
+ >= self.ENDPOINTS_DATA_COLUMNS.index("Code Hotspots") + 1
1955
+ else "-"
1956
+ )
1957
+ if tree_is_from_html is False:
1958
+ current_content = html.escape(current_content)
1959
+ current_content = current_content.replace("\n", "<br>")
1960
+ html_row.append(f"<td>{current_content}</td>")
1961
+
1962
+ html_row.append("</tr>")
1963
+
1964
+ html_table.append("\n".join(html_row))
1965
+
1966
+ service_endpoints = service_endpoints.replace(
1967
+ "<TABLE_PLACEHOLDER>", "\n".join(html_table), 1
1968
+ )
1969
+
1970
+ table_id = self.identify_table_id(table_inits, "service-endpoints-table")
1971
+ table_inits.append(f'initTable("#{table_id}");')
1972
+ service_endpoints = service_endpoints.replace(
1973
+ "<TABLE_ID_PLACEHOLDER>", table_id, 1
1974
+ )
1975
+
1976
+ report_content.append(service_endpoints)
1977
+
1978
+ def generate_section_reachable_flows(
1979
+ self, sections_tree, report_content, table_inits, tree_is_from_html, piece_id
1980
+ ):
1981
+ reachable_flows = REACHABLE_FLOWS
1982
+
1983
+ if piece_id is None:
1984
+ reachable_flows = reachable_flows.replace("<PIECE_ID_PLACEHOLDER>", "", 1)
1985
+ else:
1986
+ if tree_is_from_html is False:
1987
+ piece_id = html.escape(piece_id)
1988
+ reachable_flows = reachable_flows.replace(
1989
+ "<PIECE_ID_PLACEHOLDER>", f"{piece_id} {SEPARATOR} ", 1
1990
+ )
1991
+
1992
+ current_content = sections_tree[self.REACHABLE_FLOWS][self.SUMMARY]
1993
+ if tree_is_from_html is False:
1994
+ current_content = html.escape(current_content)
1995
+
1996
+ if current_content == "":
1997
+ current_content = "Below are some reachable flows identified by depscan."
1998
+
1999
+ reachable_flows = reachable_flows.replace(
2000
+ "<SUMMARY_PLACEHOLDER>", current_content, 1
2001
+ )
2002
+
2003
+ html_table = []
2004
+ for row_data in sections_tree[self.REACHABLE_FLOWS][self.DATA]:
2005
+ if self.raw_content:
2006
+ html_row = ["<tr>"]
2007
+
2008
+ summary = (
2009
+ row_data[self.REACHABLE_FLOWS_DATA_COLUMNS.index("Summary")]
2010
+ if len(row_data)
2011
+ >= self.REACHABLE_FLOWS_DATA_COLUMNS.index("Summary") + 1
2012
+ else "-"
2013
+ )
2014
+
2015
+ if tree_is_from_html is False:
2016
+ summary = html.escape(summary)
2017
+
2018
+ recommendation = (
2019
+ row_data[
2020
+ self.REACHABLE_FLOWS_DATA_COLUMNS.index("Recommendation")
2021
+ ].strip()
2022
+ if len(row_data)
2023
+ >= self.REACHABLE_FLOWS_DATA_COLUMNS.index("Recommendation") + 1
2024
+ else ""
2025
+ )
2026
+
2027
+ if tree_is_from_html is False:
2028
+ recommendation = html.escape(recommendation)
2029
+
2030
+ if recommendation != "":
2031
+ current_content = f"{summary}<br><br>{recommendation}"
2032
+ else:
2033
+ current_content = summary
2034
+
2035
+ if tree_is_from_html is False:
2036
+ current_content = html.escape(current_content)
2037
+ html_row.append(
2038
+ f'<td><pre class="breakable">{current_content}</pre></td>'
2039
+ )
2040
+
2041
+ current_content = (
2042
+ row_data[self.REACHABLE_FLOWS_DATA_COLUMNS.index("Flows")]
2043
+ if len(row_data)
2044
+ >= self.REACHABLE_FLOWS_DATA_COLUMNS.index("Flows") + 1
2045
+ else "-"
2046
+ )
2047
+ current_content = "\n".join(
2048
+ line.rstrip() for line in current_content.splitlines()
2049
+ )
2050
+ if tree_is_from_html is False:
2051
+ current_content = html.escape(current_content)
2052
+ html_row.append(
2053
+ f'<td><pre class="breakable">{current_content}</pre></td>'
2054
+ )
2055
+
2056
+ current_content = (
2057
+ row_data[
2058
+ self.REACHABLE_FLOWS_DATA_COLUMNS.index("Reachable Packages")
2059
+ ]
2060
+ if len(row_data)
2061
+ >= self.REACHABLE_FLOWS_DATA_COLUMNS.index("Reachable Packages") + 1
2062
+ else "-"
2063
+ )
2064
+ if tree_is_from_html is False:
2065
+ current_content = html.escape(current_content)
2066
+ html_row.append(
2067
+ f'<td><pre class="breakable">{current_content}</pre></td>'
2068
+ )
2069
+
2070
+ html_row.append("</tr>")
2071
+
2072
+ html_table.append("\n".join(html_row))
2073
+
2074
+ else:
2075
+ html_row = ["<tr>"]
2076
+
2077
+ summary = (
2078
+ row_data[self.REACHABLE_FLOWS_DATA_COLUMNS.index("Summary")]
2079
+ if len(row_data)
2080
+ >= self.REACHABLE_FLOWS_DATA_COLUMNS.index("Summary") + 1
2081
+ else "-"
2082
+ )
2083
+
2084
+ if tree_is_from_html is False:
2085
+ summary = html.escape(summary)
2086
+
2087
+ recommendation = (
2088
+ row_data[
2089
+ self.REACHABLE_FLOWS_DATA_COLUMNS.index("Recommendation")
2090
+ ].strip()
2091
+ if len(row_data)
2092
+ >= self.REACHABLE_FLOWS_DATA_COLUMNS.index("Recommendation") + 1
2093
+ else ""
2094
+ )
2095
+
2096
+ if tree_is_from_html is False:
2097
+ recommendation = html.escape(recommendation)
2098
+
2099
+ if recommendation != "":
2100
+ current_content = f"""{summary}<br><br><div class="alert alert-info" role="alert">{recommendation}</div>"""
2101
+ else:
2102
+ current_content = summary
2103
+
2104
+ if tree_is_from_html is False:
2105
+ current_content = html.escape(current_content)
2106
+ html_row.append(f"<td>{current_content}</td>")
2107
+
2108
+ current_content = (
2109
+ row_data[self.REACHABLE_FLOWS_DATA_COLUMNS.index("Flows")]
2110
+ if len(row_data)
2111
+ >= self.REACHABLE_FLOWS_DATA_COLUMNS.index("Flows") + 1
2112
+ else "-"
2113
+ )
2114
+ current_content = "\n".join(
2115
+ line.rstrip() for line in current_content.splitlines()
2116
+ )
2117
+ if tree_is_from_html is False:
2118
+ current_content = html.escape(current_content)
2119
+ html_row.append(
2120
+ f'<td><pre class="breakable">{current_content}</pre></td>'
2121
+ )
2122
+
2123
+ current_content = (
2124
+ row_data[
2125
+ self.REACHABLE_FLOWS_DATA_COLUMNS.index("Reachable Packages")
2126
+ ]
2127
+ if len(row_data)
2128
+ >= self.REACHABLE_FLOWS_DATA_COLUMNS.index("Reachable Packages") + 1
2129
+ else "-"
2130
+ )
2131
+ if tree_is_from_html is False:
2132
+ current_content = html.escape(current_content)
2133
+ formatted_content = self.format_as_html_ul(current_content)
2134
+ html_row.append(f"<td>{formatted_content}</td>")
2135
+
2136
+ html_row.append("</tr>")
2137
+
2138
+ html_table.append("\n".join(html_row))
2139
+
2140
+ reachable_flows = reachable_flows.replace(
2141
+ "<TABLE_PLACEHOLDER>", "\n".join(html_table), 1
2142
+ )
2143
+
2144
+ table_id = self.identify_table_id(table_inits, "reachable-flows-table")
2145
+ table_inits.append(f'initTable("#{table_id}");')
2146
+ reachable_flows = reachable_flows.replace("<TABLE_ID_PLACEHOLDER>", table_id, 1)
2147
+
2148
+ report_content.append(reachable_flows)
2149
+
2150
+ def generate_section_non_reachable_flows(
2151
+ self, sections_tree, report_content, table_inits, tree_is_from_html, piece_id
2152
+ ):
2153
+ non_reachable_flows = NON_REACHABLE_FLOWS
2154
+
2155
+ if piece_id is None:
2156
+ non_reachable_flows = non_reachable_flows.replace(
2157
+ "<PIECE_ID_PLACEHOLDER>", "", 1
2158
+ )
2159
+ else:
2160
+ if tree_is_from_html is False:
2161
+ piece_id = html.escape(piece_id)
2162
+ non_reachable_flows = non_reachable_flows.replace(
2163
+ "<PIECE_ID_PLACEHOLDER>", f"{piece_id} {SEPARATOR} ", 1
2164
+ )
2165
+
2166
+ current_content = sections_tree[self.NON_REACHABLE_FLOWS][self.SUMMARY]
2167
+ if tree_is_from_html is False:
2168
+ current_content = html.escape(current_content)
2169
+ non_reachable_flows = non_reachable_flows.replace(
2170
+ "<SUMMARY_PLACEHOLDER>", current_content, 1
2171
+ )
2172
+
2173
+ html_table = []
2174
+ for row_data in sections_tree[self.NON_REACHABLE_FLOWS][self.DATA]:
2175
+ if self.raw_content:
2176
+ html_row = ["<tr>"]
2177
+
2178
+ current_content = (
2179
+ row_data[self.NON_REACHABLE_FLOWS_DATA_COLUMNS.index("Summary")]
2180
+ if len(row_data)
2181
+ >= self.NON_REACHABLE_FLOWS_DATA_COLUMNS.index("Summary") + 1
2182
+ else "-"
2183
+ )
2184
+ if tree_is_from_html is False:
2185
+ current_content = html.escape(current_content)
2186
+ html_row.append(
2187
+ f'<td><pre class="breakable">{current_content}</pre></td>'
2188
+ )
2189
+
2190
+ current_content = (
2191
+ row_data[self.NON_REACHABLE_FLOWS_DATA_COLUMNS.index("Flows")]
2192
+ if len(row_data)
2193
+ >= self.NON_REACHABLE_FLOWS_DATA_COLUMNS.index("Flows") + 1
2194
+ else "-"
2195
+ )
2196
+ current_content = "\n".join(
2197
+ line.rstrip() for line in current_content.splitlines()
2198
+ )
2199
+ if tree_is_from_html is False:
2200
+ current_content = html.escape(current_content)
2201
+ html_row.append(
2202
+ f'<td><pre class="breakable">{current_content}</pre></td>'
2203
+ )
2204
+
2205
+ current_content = (
2206
+ row_data[
2207
+ self.NON_REACHABLE_FLOWS_DATA_COLUMNS.index(
2208
+ "Reachable Packages"
2209
+ )
2210
+ ]
2211
+ if len(row_data)
2212
+ >= self.NON_REACHABLE_FLOWS_DATA_COLUMNS.index("Reachable Packages")
2213
+ + 1
2214
+ else "-"
2215
+ )
2216
+ if tree_is_from_html is False:
2217
+ current_content = html.escape(current_content)
2218
+ html_row.append(
2219
+ f'<td><pre class="breakable">{current_content}</pre></td>'
2220
+ )
2221
+
2222
+ html_row.append("</tr>")
2223
+
2224
+ html_table.append("\n".join(html_row))
2225
+
2226
+ else:
2227
+ html_row = ["<tr>"]
2228
+
2229
+ current_content = (
2230
+ row_data[self.NON_REACHABLE_FLOWS_DATA_COLUMNS.index("Summary")]
2231
+ if len(row_data)
2232
+ >= self.NON_REACHABLE_FLOWS_DATA_COLUMNS.index("Summary") + 1
2233
+ else "-"
2234
+ )
2235
+ if tree_is_from_html is False:
2236
+ current_content = html.escape(current_content)
2237
+ html_row.append(f"<td>{current_content}</td>")
2238
+
2239
+ current_content = (
2240
+ row_data[self.NON_REACHABLE_FLOWS_DATA_COLUMNS.index("Flows")]
2241
+ if len(row_data)
2242
+ >= self.NON_REACHABLE_FLOWS_DATA_COLUMNS.index("Flows") + 1
2243
+ else "-"
2244
+ )
2245
+ current_content = "\n".join(
2246
+ line.rstrip() for line in current_content.splitlines()
2247
+ )
2248
+ if tree_is_from_html is False:
2249
+ current_content = html.escape(current_content)
2250
+ html_row.append(
2251
+ f'<td><pre class="breakable">{current_content}</pre></td>'
2252
+ )
2253
+
2254
+ current_content = (
2255
+ row_data[
2256
+ self.NON_REACHABLE_FLOWS_DATA_COLUMNS.index(
2257
+ "Reachable Packages"
2258
+ )
2259
+ ]
2260
+ if len(row_data)
2261
+ >= self.NON_REACHABLE_FLOWS_DATA_COLUMNS.index("Reachable Packages")
2262
+ + 1
2263
+ else "-"
2264
+ )
2265
+ if tree_is_from_html is False:
2266
+ current_content = html.escape(current_content)
2267
+ formatted_content = self.format_as_html_ul(current_content)
2268
+ html_row.append(f"<td>{formatted_content}</td>")
2269
+
2270
+ html_row.append("</tr>")
2271
+
2272
+ html_table.append("\n".join(html_row))
2273
+
2274
+ non_reachable_flows = non_reachable_flows.replace(
2275
+ "<TABLE_PLACEHOLDER>", "\n".join(html_table), 1
2276
+ )
2277
+
2278
+ table_id = self.identify_table_id(table_inits, "non-reachable-flows-table")
2279
+ table_inits.append(f'initTable("#{table_id}");')
2280
+ non_reachable_flows = non_reachable_flows.replace(
2281
+ "<TABLE_ID_PLACEHOLDER>", table_id, 1
2282
+ )
2283
+
2284
+ report_content.append(non_reachable_flows)
2285
+
2286
+ def generate_section_secure_design_tips(
2287
+ self, sections_tree, report_content, table_inits, tree_is_from_html, piece_id
2288
+ ):
2289
+ secure_design_tips = SECURE_DESIGN_TIPS
2290
+
2291
+ if piece_id is None:
2292
+ secure_design_tips = secure_design_tips.replace(
2293
+ "<SECURE_DESIGN_TIPS>", "", 1
2294
+ )
2295
+ else:
2296
+ if tree_is_from_html is False:
2297
+ piece_id = html.escape(piece_id)
2298
+ secure_design_tips = secure_design_tips.replace(
2299
+ "<PIECE_ID_PLACEHOLDER>", f"{piece_id} {SEPARATOR} ", 1
2300
+ )
2301
+
2302
+ current_content = sections_tree[self.SECURE_DESIGN_TIPS][self.SUMMARY]
2303
+ if tree_is_from_html is False:
2304
+ current_content = html.escape(current_content)
2305
+
2306
+ if self.raw_content:
2307
+ secure_design_tips = secure_design_tips.replace(
2308
+ "<SUMMARY_PLACEHOLDER>", f"<pre>{current_content}</pre>", 1
2309
+ )
2310
+ else:
2311
+ formatted_content = self.format_as_html_ul(current_content)
2312
+
2313
+ secure_design_tips = secure_design_tips.replace(
2314
+ "<SUMMARY_PLACEHOLDER>", formatted_content, 1
2315
+ )
2316
+
2317
+ report_content.append(secure_design_tips)
2318
+
2319
+ def generate_section_malware_alert(
2320
+ self, sections_tree, report_content, table_inits, tree_is_from_html, piece_id
2321
+ ):
2322
+ malware_alert = MALWARE_ALERT
2323
+
2324
+ if piece_id is None:
2325
+ malware_alert = malware_alert.replace("<PIECE_ID_PLACEHOLDER>", "", 1)
2326
+ else:
2327
+ if tree_is_from_html is False:
2328
+ piece_id = html.escape(piece_id)
2329
+ malware_alert = malware_alert.replace(
2330
+ "<PIECE_ID_PLACEHOLDER>", f"{piece_id} {SEPARATOR} ", 1
2331
+ )
2332
+
2333
+ current_content = sections_tree[self.MALWARE_ALERT][self.SUMMARY]
2334
+ if tree_is_from_html is False:
2335
+ current_content = html.escape(current_content)
2336
+
2337
+ if self.raw_content:
2338
+ malware_alert = malware_alert.replace(
2339
+ "<SUMMARY_PLACEHOLDER>", f"<pre>{current_content}</pre>", 1
2340
+ )
2341
+ else:
2342
+ malware_alert = malware_alert.replace(
2343
+ "<SUMMARY_PLACEHOLDER>",
2344
+ current_content.strip().replace("\n", "<br>"),
2345
+ 1,
2346
+ )
2347
+
2348
+ report_content.append(malware_alert)
2349
+
2350
+ def generate_section_recommendation(
2351
+ self, sections_tree, report_content, table_inits, tree_is_from_html, piece_id
2352
+ ):
2353
+ recommendation = RECOMMENDATION
2354
+
2355
+ if piece_id is None:
2356
+ recommendation = recommendation.replace("<PIECE_ID_PLACEHOLDER>", "", 1)
2357
+ else:
2358
+ if tree_is_from_html is False:
2359
+ piece_id = html.escape(piece_id)
2360
+ recommendation = recommendation.replace(
2361
+ "<PIECE_ID_PLACEHOLDER>", f"{piece_id} {SEPARATOR} ", 1
2362
+ )
2363
+
2364
+ current_content = sections_tree[self.RECOMMENDATION][self.SUMMARY]
2365
+ if tree_is_from_html is False:
2366
+ current_content = html.escape(current_content)
2367
+
2368
+ if self.raw_content:
2369
+ recommendation = recommendation.replace(
2370
+ "<SUMMARY_PLACEHOLDER>", f"<pre>{current_content}</pre>", 1
2371
+ )
2372
+ else:
2373
+ recommendation = recommendation.replace(
2374
+ "<SUMMARY_PLACEHOLDER>",
2375
+ current_content.strip().replace("\n", "<br>"),
2376
+ 1,
2377
+ )
2378
+
2379
+ report_content.append(recommendation)
2380
+
2381
+ def generate_section_info(
2382
+ self, sections_tree, report_content, table_inits, tree_is_from_html, piece_id
2383
+ ):
2384
+ info = INFO
2385
+
2386
+ if piece_id is None:
2387
+ info = info.replace("<PIECE_ID_PLACEHOLDER>", "", 1)
2388
+ else:
2389
+ if tree_is_from_html is False:
2390
+ piece_id = html.escape(piece_id)
2391
+ info = malware_alert.info(
2392
+ "<PIECE_ID_PLACEHOLDER>", f"{piece_id} {SEPARATOR} ", 1
2393
+ )
2394
+
2395
+ current_content = sections_tree[self.INFO][self.SUMMARY]
2396
+ if tree_is_from_html is False:
2397
+ current_content = html.escape(current_content)
2398
+
2399
+ if self.raw_content:
2400
+ info = info.replace(
2401
+ "<SUMMARY_PLACEHOLDER>", f"<pre>{current_content}</pre>", 1
2402
+ )
2403
+ else:
2404
+ info = info.replace(
2405
+ "<SUMMARY_PLACEHOLDER>",
2406
+ current_content.strip().replace("\n", "<br>"),
2407
+ 1,
2408
+ )
2409
+
2410
+ report_content.append(info)
2411
+
2412
+ def generate_html(
2413
+ self,
2414
+ sections_tree=None,
2415
+ sections_trees=None,
2416
+ tree_is_from_html=False,
2417
+ styles="",
2418
+ ):
2419
+ if sections_tree is not None:
2420
+ sections_trees = {None: sections_tree}
2421
+
2422
+ main_report = HTML_REPORT
2423
+
2424
+ main_report = main_report.replace("<ADDITIONAL_STYLES_PLACEHOLDER>", styles)
2425
+
2426
+ report_content = []
2427
+ table_inits = []
2428
+
2429
+ # if a summary is populated it means that the corresponding section was in the original data, so we put it also in the html report
2430
+
2431
+ for piece_id, sections_tree in sections_trees.items():
2432
+ if sections_tree[self.MALWARE_ALERT][self.SUMMARY] != "":
2433
+ self.generate_section_malware_alert(
2434
+ sections_tree,
2435
+ report_content,
2436
+ table_inits,
2437
+ tree_is_from_html,
2438
+ piece_id,
2439
+ )
2440
+
2441
+ if sections_tree[self.RECOMMENDATION][self.SUMMARY] != "":
2442
+ self.generate_section_recommendation(
2443
+ sections_tree,
2444
+ report_content,
2445
+ table_inits,
2446
+ tree_is_from_html,
2447
+ piece_id,
2448
+ )
2449
+
2450
+ if sections_tree[self.INFO][self.SUMMARY] != "":
2451
+ self.generate_section_info(
2452
+ sections_tree,
2453
+ report_content,
2454
+ table_inits,
2455
+ tree_is_from_html,
2456
+ piece_id,
2457
+ )
2458
+
2459
+ if (
2460
+ sections_tree[self.PRIORITIZED_VULNERABILITIES][self.TOP_PRIORITY_BOM][
2461
+ self.SUMMARY
2462
+ ]
2463
+ != ""
2464
+ ):
2465
+ self.generate_section_prioritized_vulnerabilities(
2466
+ sections_tree,
2467
+ report_content,
2468
+ table_inits,
2469
+ tree_is_from_html,
2470
+ piece_id,
2471
+ )
2472
+
2473
+ if (
2474
+ sections_tree[self.PROACTIVE_MEASURES][self.TOP_REACHABLE_PACKAGES][
2475
+ self.SUMMARY
2476
+ ]
2477
+ != ""
2478
+ ):
2479
+ self.generate_section_proactive_measures(
2480
+ sections_tree,
2481
+ report_content,
2482
+ table_inits,
2483
+ tree_is_from_html,
2484
+ piece_id,
2485
+ )
2486
+
2487
+ if (
2488
+ sections_tree[self.VULNERABILITY_DISCLOSURE_REPORT][
2489
+ self.DEPENDENCY_SCAN_RESULTS_BOM
2490
+ ][self.SUMMARY]
2491
+ != ""
2492
+ ) or len(
2493
+ sections_tree[self.VULNERABILITY_DISCLOSURE_REPORT][
2494
+ self.DEPENDENCY_SCAN_RESULTS_BOM
2495
+ ][self.DATA]
2496
+ ) > 0:
2497
+ self.generate_section_vulnerability_disclosure_report(
2498
+ sections_tree,
2499
+ report_content,
2500
+ table_inits,
2501
+ tree_is_from_html,
2502
+ piece_id,
2503
+ )
2504
+
2505
+ if (
2506
+ sections_tree[self.SERVICE_ENDPOINTS][self.ENDPOINTS][self.SUMMARY]
2507
+ != ""
2508
+ ):
2509
+ self.generate_section_service_endpoints(
2510
+ sections_tree,
2511
+ report_content,
2512
+ table_inits,
2513
+ tree_is_from_html,
2514
+ piece_id,
2515
+ )
2516
+
2517
+ if (
2518
+ sections_tree[self.REACHABLE_FLOWS][self.SUMMARY] != ""
2519
+ or len(sections_tree[self.REACHABLE_FLOWS][self.DATA]) > 0
2520
+ ):
2521
+ self.generate_section_reachable_flows(
2522
+ sections_tree,
2523
+ report_content,
2524
+ table_inits,
2525
+ tree_is_from_html,
2526
+ piece_id,
2527
+ )
2528
+
2529
+ if sections_tree[self.NON_REACHABLE_FLOWS][self.SUMMARY] != "":
2530
+ self.generate_section_non_reachable_flows(
2531
+ sections_tree,
2532
+ report_content,
2533
+ table_inits,
2534
+ tree_is_from_html,
2535
+ piece_id,
2536
+ )
2537
+
2538
+ if sections_tree[self.SECURE_DESIGN_TIPS][self.SUMMARY] != "":
2539
+ self.generate_section_secure_design_tips(
2540
+ sections_tree,
2541
+ report_content,
2542
+ table_inits,
2543
+ tree_is_from_html,
2544
+ piece_id,
2545
+ )
2546
+
2547
+ main_report = main_report.replace(
2548
+ "<CONTENT_PLACEHOLDER>", "\n<p>&nbsp;</p>".join(report_content), 1
2549
+ )
2550
+ main_report = main_report.replace(
2551
+ "<INIT_TABLE_PLACEHOLDER>", "\n".join(table_inits), 1
2552
+ )
2553
+
2554
+ with open(self.report_output_path, "w", encoding="utf-8") as f:
2555
+ f.write(main_report)
2556
+
2557
+ def parse_and_generate_report(self):
2558
+ if self.input_vdr_json_path is not None:
2559
+ depscan_reports = self.extract_depscan_reports_from_vdr_json()
2560
+ for depscan_report in depscan_reports:
2561
+ sections_tree = self.parse_depscan_report(depscan_report)
2562
+ self.generate_html(sections_tree=sections_tree, tree_is_from_html=False)
2563
+ break # we assume the VDR JSON file contains at most one Depscan report
2564
+ return
2565
+
2566
+ if self.input_rich_html_path is not None:
2567
+ depscan_report, styles = self.extract_depscan_report_from_rich_html()
2568
+ report_pieces = self.separate_report_by_language_explanations(
2569
+ depscan_report
2570
+ )
2571
+ sections_trees = {}
2572
+ for piece_id, report_piece in report_pieces.items():
2573
+ sections_tree = self.parse_depscan_report(report_piece)
2574
+ sections_trees[piece_id] = sections_tree
2575
+ self.generate_html(
2576
+ sections_trees=sections_trees, tree_is_from_html=True, styles=styles
2577
+ )
2578
+ return
2579
+
2580
+ if self.input_txt_path is not None:
2581
+ depscan_report = self.extract_depscan_report_from_txt()
2582
+ sections_tree = self.parse_depscan_report(depscan_report)
2583
+ self.generate_html(sections_tree=sections_tree, tree_is_from_html=False)
2584
+ return