Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add excel macro filter for csv output #1634

Merged
merged 1 commit into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cve_bin_tool/output_engine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,14 @@ def save_intermediate(

def output_csv(all_cve_data: Dict[ProductInfo, CVEData], outfile):
"""Output a CSV of CVEs"""

formatted_output = format_output(all_cve_data)

# Trim any leading -, =, + or @ to avoid excel macros
for cve_entry in formatted_output:
for key, value in cve_entry.items():
cve_entry[key] = value.strip("-=+@")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why only these special characters ?


writer = csv.DictWriter(
outfile,
fieldnames=[
Expand Down
40 changes: 40 additions & 0 deletions test/test_output_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -844,3 +844,43 @@ def test_output_file_incorrect_filename(self):

# assert
self.assertEqual(contains_sb, True)

def test_csv_macros(self):
"""tests that output engine will not output leading -, =, + or @
characters, used in spreadsheet macros"""

bad_input = {
ProductInfo("=vendor0", "+product0", "@1.0"): CVEData(
cves=[
CVE(
"-CVE-1234-1234",
"@-=+MEDIUM",
score=4.2,
cvss_version=2,
cvss_vector="C:H",
),
],
paths={"@@@@bad"},
),
}
expected_output = [
{
"vendor": "vendor0",
"product": "product0",
"version": "1.0",
"cve_number": "CVE-1234-1234",
"severity": "MEDIUM",
"score": "4.2",
"cvss_version": "2",
"cvss_vector": "C:H",
"paths": "bad",
"remarks": "NewFound",
"comments": "",
},
]

output_csv(bad_input, self.mock_file)
self.mock_file.seek(0) # reset file position
reader = csv.DictReader(self.mock_file)
actual_output = [dict(x) for x in reader]
self.assertEqual(actual_output, expected_output)