-
Notifications
You must be signed in to change notification settings - Fork 89
Fix CWE related weakness parsing and add regression tests #756
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |||||||||||||||||||||||||
| import os | ||||||||||||||||||||||||||
| import tempfile | ||||||||||||||||||||||||||
| import requests | ||||||||||||||||||||||||||
| from typing import Dict | ||||||||||||||||||||||||||
| from typing import Any, Dict | ||||||||||||||||||||||||||
| from application.database import db | ||||||||||||||||||||||||||
| from application.defs import cre_defs as defs | ||||||||||||||||||||||||||
| import shutil | ||||||||||||||||||||||||||
|
|
@@ -161,9 +161,19 @@ def register_cwe(self, cache: db.Node_collection, xml_file: str): | |||||||||||||||||||||||||
| return entries | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def parse_related_weakness( | ||||||||||||||||||||||||||
| self, cache: db.Node_collection, rw: Dict[str, Dict], cwe: defs.Standard | ||||||||||||||||||||||||||
| self, cache: db.Node_collection, rw: Dict[str, Any], cwe: defs.Standard | ||||||||||||||||||||||||||
| ) -> defs.Standard: | ||||||||||||||||||||||||||
| cwe_entry = rw.get("Related_Weakness") | ||||||||||||||||||||||||||
| if isinstance(cwe_entry, Dict): | ||||||||||||||||||||||||||
| id = cwe_entry["@CWE_ID"] | ||||||||||||||||||||||||||
| return self.link_to_related_cwe(cwe=cwe, cache=cache, related_id=id) | ||||||||||||||||||||||||||
| cwe_entries = rw.get("Related_Weakness") | ||||||||||||||||||||||||||
| if isinstance(cwe_entries, Dict): | ||||||||||||||||||||||||||
| cwe_entries = [cwe_entries] | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if isinstance(cwe_entries, list): | ||||||||||||||||||||||||||
| for cwe_entry in cwe_entries: | ||||||||||||||||||||||||||
| if isinstance(cwe_entry, Dict): | ||||||||||||||||||||||||||
|
Comment on lines
+167
to
+172
|
||||||||||||||||||||||||||
| if isinstance(cwe_entries, Dict): | |
| cwe_entries = [cwe_entries] | |
| if isinstance(cwe_entries, list): | |
| for cwe_entry in cwe_entries: | |
| if isinstance(cwe_entry, Dict): | |
| if isinstance(cwe_entries, dict): | |
| cwe_entries = [cwe_entries] | |
| if isinstance(cwe_entries, list): | |
| for cwe_entry in cwe_entries: | |
| if isinstance(cwe_entry, dict): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implemented and verified.
This PR specifically fixes parse_related_weakness() in application/utils/external_project_parsers/parsers/cwe.py to:
- normalize
Related_Weaknessdict/list shapes - iterate all related CWE entries safely
- always return a
Standardnode (noNonepropagation)
Added regression tests in application/tests/cwe_parser_test.py:
test_parse_related_weakness_handles_listtest_parse_related_weakness_returns_original_on_empty_input
Validation run:
python -m unittest application.tests.cwe_parser_test -v
Result: Ran 3 tests ... OK
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typing.Dictshould not be used withisinstance()(it raisesTypeErroron Python 3.11/3.12). Use the runtime type (dict) orcollections.abc.Mappingfor this check, otherwise this code path can crash during parsing (and the new tests will hit it).