Skip to content

Commit 5fe5a1e

Browse files
committed
follow-cvelist.py increase exception handling
1 parent 22fd0f0 commit 5fe5a1e

File tree

1 file changed

+40
-15
lines changed

1 file changed

+40
-15
lines changed

bin/follow-cvelist.py

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -119,18 +119,29 @@ def print_changes(current_commit: str, past_commit: str):
119119
if "delta" in path:
120120
continue
121121

122+
# Skip files outside cve
123+
if "cve" not in path:
124+
continue
125+
122126
if type == "D":
123127
print(f"Deleted: {Path(path).stem}", file=sys.stderr)
124128
else:
125-
current = json_at_commit(path, current_commit)
126-
modified = current["cveMetadata"]["dateUpdated"]
127-
modified = re.sub(r"\..*", "", modified)
128-
modified = re.sub(r"T", " ", modified)
129-
cve = current["cveMetadata"]["cveId"]
129+
try:
130+
current = json_at_commit(path, current_commit)
131+
modified = current["cveMetadata"]["dateUpdated"]
132+
modified = re.sub(r"\..*", "", modified)
133+
modified = re.sub(r"T", " ", modified)
134+
cve = current["cveMetadata"]["cveId"]
135+
except (KeyError, TypeError):
136+
continue
130137

131138
if type == "M":
132-
past = json_at_commit(path, past_commit)
133-
past_cvss = cvss31score(past)
139+
try:
140+
past = json_at_commit(path, past_commit)
141+
past_cvss = cvss31score(past)
142+
except TypeError:
143+
print(f"Unexpected structure in (past) {path}", file=sys.stderr)
144+
past_cvss = " "
134145
else:
135146
past_cvss = " "
136147

@@ -173,21 +184,33 @@ def print_changes_color(current_commit: str, past_commit: str):
173184
if "delta" in path:
174185
continue
175186

187+
# Skip files outside cve
188+
if "cve" not in path:
189+
continue
190+
176191
if type == "D":
177192
print(
178193
f"{ansi('red')}Deleted: {Path(path).stem}{ansi('end')}", file=sys.stderr
179194
)
180195
else:
181-
current = json_at_commit(path, current_commit)
182-
modified = current["cveMetadata"]["dateUpdated"]
183-
modified = re.sub(r"\..*", "", modified)
184-
modified = re.sub(r"T", " ", modified)
185-
cve = current["cveMetadata"]["cveId"]
196+
try:
197+
current = json_at_commit(path, current_commit)
198+
modified = current["cveMetadata"]["dateUpdated"]
199+
modified = re.sub(r"\..*", "", modified)
200+
modified = re.sub(r"T", " ", modified)
201+
cve = current["cveMetadata"]["cveId"]
202+
except (KeyError, TypeError):
203+
print(f"Unexpected structure in {path}", file=sys.stderr)
204+
continue
186205

187206
if type == "M":
188207
cve = f"{ansi('bright_blue')}{cve}{ansi('end')}"
189-
past = json_at_commit(path, past_commit)
190-
past_cvss = cvss31score(past)
208+
try:
209+
past = json_at_commit(path, past_commit)
210+
past_cvss = cvss31score(past)
211+
except TypeError:
212+
print(f"Unexpected structure in (past) {path}", file=sys.stderr)
213+
past_cvss = f" "
191214
else:
192215
cve = f"{ansi('bright_cyan')}{cve}{ansi('end')}"
193216
past_cvss = " "
@@ -300,7 +323,9 @@ def json_at_commit(path: Path, commit: str) -> dict:
300323
data = json.loads(result.stdout.decode("utf-8"))
301324
return data
302325
except IOError:
303-
print(f"Could not open {path}", file=sys.stderr)
326+
print(f"Could not open {commit}:{path}", file=sys.stderr)
327+
except json.decoder.JSONDecodeError:
328+
print(f"Could not parse {commit}:{path}", file=sys.stderr)
304329

305330

306331
def cvelist_repo():

0 commit comments

Comments
 (0)