Skip to content

Commit 2982083

Browse files
committed
Fix some issues and add limits for scanning
1 parent 1bf7461 commit 2982083

File tree

2 files changed

+29
-28
lines changed

2 files changed

+29
-28
lines changed

generate_xlsx_report.py

+21-27
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
1-
from dataclasses import dataclass
2-
from typing import List
1+
# generate_xlsx_report.py
32

4-
import openpyxl
3+
from openpyxl import Workbook
54
from openpyxl.styles import Font, PatternFill, Alignment
65
from 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+
1821
def 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-
2931
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()
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
9895
if __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")

scanner.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def _load_checks(self):
2727
checks.append(check_class())
2828
return checks
2929

30-
def scan(self, path: str) -> List[ScanResult]:
30+
def scan(self, path: str, limit: int = 40000) -> List[ScanResult]:
3131
results = []
3232
files_to_scan = []
3333

@@ -39,6 +39,13 @@ def scan(self, path: str) -> List[ScanResult]:
3939
for file in files:
4040
if any(file.endswith(ext) for ext in self.config.get_file_extensions()):
4141
files_to_scan.append(os.path.join(root, file))
42+
if len(files_to_scan) >= limit:
43+
break
44+
if len(files_to_scan) >= limit:
45+
break
46+
47+
# Limit the number of files to scan
48+
files_to_scan = files_to_scan[:limit]
4249

4350
# Scan files with progress bar
4451
for file_path in tqdm(files_to_scan, desc="Scanning files", unit="file"):

0 commit comments

Comments
 (0)