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

Add CWE support in multiple importers #1526

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

ambuj-1211
Copy link
Collaborator

Fixes: #1093
This adds cwe data to following importers:

  • debian
  • fireeye
  • apache_httpd

@ambuj-1211
Copy link
Collaborator Author

@ziadhany I don't know why some tests related to API are failing please check what should I do to resolve that.

@ambuj-1211 ambuj-1211 force-pushed the add-cwe-support-in-multiple-importers branch from 229b70a to 131f37c Compare September 15, 2024 18:45
@ziadhany
Copy link
Collaborator

ziadhany commented Sep 16, 2024

@ziadhany I don't know why some tests related to API are failing please check what should I do to resolve that.

@ambuj-1211 I don’t see any errors in the test. Please let it fail in the CI so I can investigate the issue.

@ambuj-1211
Copy link
Collaborator Author

@ziadhany please have a look on this I have resolved the issues which you indicated earlier.

@TG1999 TG1999 added this to the v36.0.0 - 3-next milestone Oct 15, 2024
Copy link
Collaborator

@ziadhany ziadhany left a comment

Choose a reason for hiding this comment

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

@ambuj-1211, you are doing a great job! Please make the required changes, and I hope we can merge this PR this week.

for problem in problemtype_data:
for desc in problem["description"]:
value = desc.get("value", "")
cwe_pattern = r"CWE-\d+"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please move the CWE regular expression pattern to utils.py file

if alias:
problemtype_data = get_item(cve_data, "problemtype", "problemtype_data") or []
for problem in problemtype_data:
for desc in problem["description"]:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
for desc in problem["description"]:
for desc in problem.get("description", []) :

try:
db.get(cwe)
weaknesses.append(cwe)
except Exception:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please avoid using a Exception general exception. Instead, use the InvalidCWEError exception.

Suggested change
except Exception:
except InvalidCWEError:

"""

alias = get_item(cve_data, "CVE_data_meta", "ID")
cwe_id = []
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
cwe_id = []
cwe_ids = []

descriptions = problemTypes[0].get("descriptions", []) if len(problemTypes) > 0 else []
for description in descriptions:
cwe_id_string = description.get("cweId", "")
cwe_id.append(get_cwe_id(cwe_id_string))
Copy link
Collaborator

Choose a reason for hiding this comment

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

For improved readability, assign the value to a variable, ensure it is validated, and then proceed:

Suggested change
cwe_id.append(get_cwe_id(cwe_id_string))
cwe_id = get_cwe_id(cwe_id_string)
cwe_ids.append(cwe_id)

Comment on lines +184 to +191
if cwe_string:
cwe_id = get_cwe_id(cwe_string)
try:
db.get(cwe_id)
weaknesses.append(cwe_id)
except Exception:
logger.error("Invalid CWE id")
return weaknesses
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please skip invalid or empty cwe_string/cwe_id immediately

Suggested change
if cwe_string:
cwe_id = get_cwe_id(cwe_string)
try:
db.get(cwe_id)
weaknesses.append(cwe_id)
except Exception:
logger.error("Invalid CWE id")
return weaknesses
if not cwe_string:
continue
cwe_id = get_cwe_id(cwe_string)
if not cwe_id:
logger.error("Invalid CWE id: No CWE ID found")
continue
try:
db.get(cwe_id)
weaknesses.append(cwe_id)
except InvalidCWEError:
logger.error(f"Invalid CWE id: {cwe_id}")
return weaknesses

Comment on lines +166 to +178
weaknesses = []
db = Database()

for cwe_string in cwe_list:

if cwe_string:
cwe_id = get_cwe_id(cwe_string)
try:
db.get(cwe_id)
weaknesses.append(cwe_id)
except Exception:
logger.error("Invalid CWE id")
return weaknesses
Copy link
Collaborator

Choose a reason for hiding this comment

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

This section is duplicated; please refactor it into a function, move it to utils.py, and reuse it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add CWE support in all importers
3 participants