1
- from dataclasses import dataclass
2
- from typing import List
1
+ # generate_xlsx_report.py
3
2
4
- import openpyxl
3
+ from openpyxl import Workbook
5
4
from openpyxl .styles import Font , PatternFill , Alignment
6
5
from openpyxl .utils import get_column_letter
6
+ from typing import List , NamedTuple
7
+ import re
7
8
8
-
9
- @dataclass
10
- class ScanResult :
9
+ class ScanResult (NamedTuple ):
11
10
file_path : str
12
11
line_number : int
13
12
title : str
14
13
message : str
15
14
severity : str
16
15
17
16
17
+ def sanitize_for_excel (text ):
18
+ illegal_characters_pattern = re .compile (r'[\000-\010]|[\013-\014]|[\016-\037]' )
19
+ return illegal_characters_pattern .sub ('' , str (text ))
20
+
18
21
def severity_key (result : ScanResult ):
19
22
severity_order = {
20
23
"Critical" : 1 ,
@@ -25,14 +28,10 @@ def severity_key(result: ScanResult):
25
28
}
26
29
return severity_order .get (result .severity , 6 )
27
30
28
-
29
31
def generate_xlsx_report (results : List [ScanResult ], output_file : str ):
30
- # Sort results by severity
31
- results .sort (key = severity_key )
32
-
33
- wb = openpyxl .Workbook ()
32
+ wb = Workbook ()
34
33
ws = wb .active
35
- ws .title = "Scan Results"
34
+ ws .title = "Security Scan Results"
36
35
37
36
# Define styles
38
37
header_font = Font (bold = True , color = "FFFFFF" )
@@ -58,11 +57,11 @@ def generate_xlsx_report(results: List[ScanResult], output_file: str):
58
57
59
58
# Write data
60
59
for row , result in enumerate (results , start = 2 ):
61
- ws .cell (row = row , column = 1 , value = result .severity ).alignment = wrapped_alignment
60
+ ws .cell (row = row , column = 1 , value = sanitize_for_excel ( result .severity ) ).alignment = wrapped_alignment
62
61
ws .cell (row = row , column = 2 , value = result .title ).alignment = wrapped_alignment
63
- ws .cell (row = row , column = 3 , value = result .file_path ).alignment = wrapped_alignment
64
- ws .cell (row = row , column = 4 , value = result .line_number ).alignment = wrapped_alignment
65
- ws .cell (row = row , column = 5 , value = result .message ).alignment = wrapped_alignment
62
+ ws .cell (row = row , column = 3 , value = sanitize_for_excel ( result .file_path ) ).alignment = wrapped_alignment
63
+ ws .cell (row = row , column = 4 , value = sanitize_for_excel ( result .line_number ) ).alignment = wrapped_alignment
64
+ ws .cell (row = row , column = 5 , value = sanitize_for_excel ( result .message ) ).alignment = wrapped_alignment
66
65
67
66
# Apply color to severity cell
68
67
severity_cell = ws .cell (row = row , column = 1 )
@@ -93,17 +92,12 @@ def generate_xlsx_report(results: List[ScanResult], output_file: str):
93
92
# Save the workbook
94
93
wb .save (output_file )
95
94
96
-
97
- # Example usage
98
95
if __name__ == "__main__" :
99
- # Sample data
96
+ # Example usage
100
97
sample_results = [
101
- ScanResult ("file1.abap" , 10 , "CheckCrossSiteScripting" , "Potential XSS vulnerability" , "High" ),
102
- ScanResult ("file2.abap" , 25 , "CheckHardcodedCredentials" , "Hardcoded password detected" , "Critical" ),
103
- ScanResult ("file1.abap" , 50 , "CheckOSCommandInjection" , "Potential OS command injection" , "High" ),
104
- ScanResult ("file3.abap" , 100 , "CheckWeakCrypto" , "Use of weak cryptographic algorithm" , "Medium" ),
105
- ScanResult ("file4.abap" , 75 , "CheckInfoDisclosure" , "Potential information disclosure" , "Low" ),
98
+ ScanResult ("file1.abap" , 10 , "Potential XSS" , "Unsanitized input" , "High" ),
99
+ ScanResult ("file2.abap" , 25 , "SQL Injection" , "Dynamic SQL query" , "Critical" ),
100
+ # Add more sample results as needed
106
101
]
107
-
108
- generate_xlsx_report (sample_results , "security_scan_report.xlsx" )
109
- print ("XLSX report generated successfully." )
102
+ generate_xlsx_report (sample_results , "sample_security_scan_report.xlsx" )
103
+ print ("Sample report generated: sample_security_scan_report.xlsx" )
0 commit comments