1- from dataclasses import dataclass
2- from typing import List
1+ # generate_xlsx_report.py
32
4- import openpyxl
3+ from openpyxl import Workbook
54from openpyxl .styles import Font , PatternFill , Alignment
65from openpyxl .utils import get_column_letter
6+ from typing import List , NamedTuple
7+ import re
78
8-
9- @dataclass
10- class ScanResult :
9+ class ScanResult (NamedTuple ):
1110 file_path : str
1211 line_number : int
1312 title : str
1413 message : str
1514 severity : str
1615
1716
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+
1821def severity_key (result : ScanResult ):
1922 severity_order = {
2023 "Critical" : 1 ,
@@ -25,14 +28,10 @@ def severity_key(result: ScanResult):
2528 }
2629 return severity_order .get (result .severity , 6 )
2730
28-
2931def 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 ()
3433 ws = wb .active
35- ws .title = "Scan Results"
34+ ws .title = "Security Scan Results"
3635
3736 # Define styles
3837 header_font = Font (bold = True , color = "FFFFFF" )
@@ -58,11 +57,11 @@ def generate_xlsx_report(results: List[ScanResult], output_file: str):
5857
5958 # Write data
6059 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
6261 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
6665
6766 # Apply color to severity cell
6867 severity_cell = ws .cell (row = row , column = 1 )
@@ -93,17 +92,12 @@ def generate_xlsx_report(results: List[ScanResult], output_file: str):
9392 # Save the workbook
9493 wb .save (output_file )
9594
96-
97- # Example usage
9895if __name__ == "__main__" :
99- # Sample data
96+ # Example usage
10097 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
106101 ]
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