Skip to content

Commit 0e99640

Browse files
ms_defender: skip empty files from zip (#12780)
1 parent c481048 commit 0e99640

1 file changed

Lines changed: 46 additions & 10 deletions

File tree

dojo/tools/ms_defender/parser.py

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,16 @@ def get_description_for_scan_types(self, scan_type):
2323
def get_findings(self, file, test):
2424
findings = []
2525
if str(file.name).endswith(".json"):
26-
vulnerabilityfile = json.load(file)
27-
vulnerabilitydata = vulnerabilityfile["value"]
28-
findings.extend(self.process_json(vulnerability) for vulnerability in vulnerabilitydata)
26+
try:
27+
vulnerabilityfile = json.load(file)
28+
if "value" not in vulnerabilityfile:
29+
logger.debug("JSON file missing 'value' key: %s", file.name)
30+
return []
31+
vulnerabilitydata = vulnerabilityfile["value"]
32+
findings.extend(self.process_json(vulnerability) for vulnerability in vulnerabilitydata)
33+
except (json.JSONDecodeError, KeyError) as e:
34+
logger.warning("Error parsing JSON file %s: %s", file.name, str(e))
35+
return []
2936
elif str(file.name).endswith(".zip"):
3037
if str(file.__class__) == "<class '_io.TextIOWrapper'>":
3138
input_zip = zipfile.ZipFile(file.name, "r")
@@ -49,14 +56,43 @@ def get_findings(self, file, test):
4956
machines = {}
5057
for vulnerabilityfile in vulnerabilityfiles:
5158
logger.debug("Loading vulnerabilitiy file: %s", vulnerabilityfile)
52-
output = json.loads(zipdata[vulnerabilityfile].decode("ascii"))["value"]
53-
for data in output:
54-
vulnerabilities.append(data)
59+
try:
60+
file_content = zipdata[vulnerabilityfile].decode("ascii")
61+
if not file_content.strip():
62+
logger.debug("Skipping empty vulnerability file: %s", vulnerabilityfile)
63+
continue
64+
65+
parsed_json = json.loads(file_content)
66+
if "value" not in parsed_json:
67+
logger.debug("Skipping vulnerability file without 'value' key: %s", vulnerabilityfile)
68+
continue
69+
70+
output = parsed_json["value"]
71+
for data in output:
72+
vulnerabilities.append(data)
73+
except (json.JSONDecodeError, KeyError, UnicodeDecodeError) as e:
74+
logger.warning("Error parsing vulnerability file %s: %s", vulnerabilityfile, str(e))
75+
continue
76+
5577
for machinefile in machinefiles:
56-
logger.debug("Loading machine file: %s", vulnerabilityfile)
57-
output = json.loads(zipdata[machinefile].decode("ascii"))["value"]
58-
for data in output:
59-
machines[data.get("id")] = data
78+
logger.debug("Loading machine file: %s", machinefile)
79+
try:
80+
file_content = zipdata[machinefile].decode("ascii")
81+
if not file_content.strip():
82+
logger.debug("Skipping empty machine file: %s", machinefile)
83+
continue
84+
85+
parsed_json = json.loads(file_content)
86+
if "value" not in parsed_json:
87+
logger.debug("Skipping machine file without 'value' key: %s", machinefile)
88+
continue
89+
90+
output = parsed_json["value"]
91+
for data in output:
92+
machines[data.get("id")] = data
93+
except (json.JSONDecodeError, KeyError, UnicodeDecodeError) as e:
94+
logger.warning("Error parsing machine file %s: %s", machinefile, str(e))
95+
continue
6096
for vulnerability in vulnerabilities:
6197
try:
6298
machine = machines.get(vulnerability["machineId"], None)

0 commit comments

Comments
 (0)