1- #
2- # Copyright (c) nexB Inc. and others. All rights reserved.
3- # VulnerableCode is a trademark of nexB Inc.
4- # SPDX-License-Identifier: Apache-2.0
5- # See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
6- # See https://github.com/aboutcode-org/vulnerablecode for support or download.
7- # See https://aboutcode.org for more information about nexB OSS projects.
8- #
9-
101import json
112from pathlib import Path
123
@@ -42,8 +33,7 @@ def clone(self):
4233 self .vcs_response = fetch_via_vcs (self .repo_url )
4334
4435 def advisories_count (self ):
45- root = Path (self .vcs_response .dest_dir )
46- return sum (1 for _ in root .rglob ("*.json" ))
36+ return 0
4737
4838 def collect_detection_rules (self ):
4939 base_path = Path (self .vcs_response .dest_dir ) / "data"
@@ -65,20 +55,8 @@ def collect_detection_rules(self):
6555
6656 source_url = json_data .get ("source_url" )
6757 for rule in json_data .get ("rules" , []):
68- advisories = set ()
69- for vulnerability_id in rule .get ("vulnerabilities" , []):
70- try :
71- if alias := AdvisoryAlias .objects .get (alias = vulnerability_id ):
72- for adv in alias .advisories .all ():
73- advisories .add (adv )
74- else :
75- advs = AdvisoryV2 .objects .filter (
76- advisory_id = vulnerability_id
77- ).latest_per_avid ()
78- for adv in advs :
79- advisories .add (adv )
80- except AdvisoryAlias .DoesNotExist :
81- self .log (f"No advisory found for aliases { vulnerability_id } " )
58+ vulns_id = rule .get ("vulnerabilities" , [])
59+ advisories = get_related_advisories (vulns_id )
8260
8361 raw_text = rule .get ("rule_text" )
8462 rule_metadata = rule .get ("rule_metadata" )
@@ -102,3 +80,26 @@ def clean_downloads(self):
10280 def on_failure (self ):
10381 """Ensure cleanup is always performed on failure."""
10482 self .clean_downloads ()
83+
84+
85+ def get_related_advisories (vulnerability_ids , logger = print ):
86+ """
87+ Fetches related advisories for a list of vulnerability IDs.
88+ """
89+ advisories = set ()
90+
91+ for vulnerability_id in vulnerability_ids :
92+ try :
93+ alias = AdvisoryAlias .objects .get (alias = vulnerability_id )
94+ advs = alias .advisories .all ()
95+ advisories .update (advs )
96+
97+ except AdvisoryAlias .DoesNotExist :
98+ advs = AdvisoryV2 .objects .filter (advisory_id = vulnerability_id ).latest_per_avid ()
99+
100+ if advs :
101+ advisories .update (advs )
102+ else :
103+ logger (f"No advisory found for ID/alias: { vulnerability_id } " )
104+
105+ return advisories
0 commit comments