@@ -130,7 +130,8 @@ def vuln_ref_exists(vulnerability, url, reference_id):
130130
131131def get_vuln_pkg_refs (vulnerability , package ):
132132 return models .PackageRelatedVulnerability .objects .filter (
133- vulnerability = vulnerability , package = package ,
133+ vulnerability = vulnerability ,
134+ package = package ,
134135 )
135136
136137
@@ -146,11 +147,14 @@ def process_advisories(data_source: DataSource) -> None:
146147 vuln , vuln_created = _get_or_create_vulnerability (advisory )
147148 for vuln_ref in advisory .vuln_references :
148149 ref = VulnerabilityReferenceInserter (
149- vulnerability = vuln , url = vuln_ref .url , reference_id = vuln_ref .reference_id ,
150+ vulnerability = vuln ,
151+ url = vuln_ref .url ,
152+ reference_id = vuln_ref .reference_id ,
150153 )
151154
152155 if vuln_created or not vuln_ref_exists (vuln , vuln_ref .url , vuln_ref .reference_id ):
153- # ref cant exist if vuln is just created so insert it
156+ # A vulnerability reference can't exist if the vulnerability is just created so
157+ # insert it
154158 bulk_create_vuln_refs .add (ref )
155159
156160 for purl in chain (advisory .impacted_package_urls , advisory .resolved_package_urls ):
@@ -162,16 +166,17 @@ def process_advisories(data_source: DataSource) -> None:
162166
163167 if vuln_created or pkg_created :
164168 bulk_create_vuln_pkg_refs .add (pkg_vuln_ref )
165- # insert all references of vuln + pkg
169+ # A vulnerability-package relationship does not exist already if either the
170+ # vulnerability or the package is just created.
166171
167172 else :
168- # insert only if it is new vuln-pkg references
173+ # insert only if it there is no existing vulnerability-package relationship.
169174 existing_ref = get_vuln_pkg_refs (vuln , pkg )
170175 if not existing_ref :
171176 bulk_create_vuln_pkg_refs .add (pkg_vuln_ref )
172177
173178 else :
174- # This handles conflicts between existing datq and obtained data
179+ # This handles conflicts between existing data and obtained data
175180 if existing_ref [0 ].is_vulnerable != pkg_vuln_ref .is_vulnerable :
176181 handle_conflicts ([existing_ref [0 ], pkg_vuln_ref .to_model_object ()])
177182 existing_ref .delete ()
@@ -190,15 +195,30 @@ def process_advisories(data_source: DataSource) -> None:
190195 handle_conflicts ([i .to_model_object () for i in conflicts ])
191196
192197
193- def find_conflicting_relations (relations ):
198+ def find_conflicting_relations (
199+ relations : Set [Set [PackageRelatedVulnerabilityInserter ]],
200+ ) -> Set [PackageRelatedVulnerabilityInserter ]:
201+
202+ # Chop off `is_vulnerable` flag from PackageRelatedVulnerabilityInserter and create a list of
203+ # tuples of format (rel.package, rel.vulnerability)
194204
195205 relation_tuples = [(rel .package , rel .vulnerability ) for rel in relations ]
196206 relation_counter = Counter (relation_tuples ).most_common ()
207+
208+ # If a (rel.package, rel.vulnerability) occurs twice then that means the
209+ # PackageRelatedVulnerabilityInserter objects
210+ # (rel.package, rel.vulnerability, is_vulnerable=True) and
211+ # (rel.package, rel.vulnerability, is_vulnerable=False) both existed which is conflicting data.
212+ # We detect and return these conflicts.
213+
197214 conflicts = set ()
198215 for rel , count in relation_counter :
199216 if count < 2 :
217+ # All the subsequent entries from here on would have count == 1 which is of no interest
218+ # since conflicts exist in pairs with `is_vulnerable=True` and `is_vulnerable=False`.
200219 break
201220
221+ # `rel` is of format (pkg, vuln)
202222 conflicts .add (
203223 PackageRelatedVulnerabilityInserter (
204224 vulnerability = rel [1 ], package = rel [0 ], is_vulnerable = True
@@ -219,7 +239,9 @@ def handle_conflicts(conflicts):
219239 models .ImportProblem .objects .create (conflicting_model = conflicts )
220240
221241
222- def _get_or_create_vulnerability (advisory : Advisory ,) -> Tuple [models .Vulnerability , bool ]:
242+ def _get_or_create_vulnerability (
243+ advisory : Advisory ,
244+ ) -> Tuple [models .Vulnerability , bool ]:
223245 if advisory .cve_id :
224246 query_kwargs = {"cve_id" : advisory .cve_id }
225247 elif advisory .summary :
0 commit comments