Skip to content
Closed
Changes from 1 commit
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
17 changes: 16 additions & 1 deletion collectors/nvd/collectors.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Union
from typing import List, Optional, Union

import nvdlib
from celery.utils.log import get_task_logger
Expand Down Expand Up @@ -65,6 +65,20 @@ def response2result(self, vulnerabilities: list) -> list:
filtering out everything unnecessary and simplifying
"""

def get_cpe_list(data: CVE) -> Optional[List[str]]:
"""
Return a list of CPEs from the CVE `data`
"""
cpe_list = []
if "cpe" in data and len(data.cpe) > 0:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This can be reduced to hasattr(data, "cpe") since we are checking if the field exists. An empty data.cpe will just mean that the following loop wouldn't run.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Oh good call. Changed that in fa056a0

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

cpe doesn't seem to be a valid top-level attribute per NVD's Vulnerability API schema, I would add a test or two as Jin suggested.

for entry in data.cpe:
cpe_list.append(entry.criteria)

if len(cpe_list) > 0:
return cpe_list
else:
return None
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think it should be fine with just returning the cpe_list as is (potentially an empty list). The new field will likely have a default of an empty list and a None value might produce an unexpected result when writing to it. Will make adjustments later if that is no longer the case.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fair enough. Updated in bb2d409


def get_cvss_metric(data: CVE, version: str) -> Union[dict, None]:
"""
Return CVSS metric from `data` for the given `version`.
Expand Down Expand Up @@ -104,6 +118,7 @@ def get_cvss_metric(data: CVE, version: str) -> Union[dict, None]:
],
)
),
"nvd_cpes": get_cpe_list(vulnerability),
}
)

Expand Down
Loading