Skip to content

Commit f0dcdf0

Browse files
committed
fix: ignore conflicts while bulk creating v2 packages
- This is a TOCTOU problem when multiple workers try to create the same PURL Signed-off-by: Keshav Priyadarshi <git@keshav.space>
1 parent 6ea0ced commit f0dcdf0

1 file changed

Lines changed: 2 additions & 4 deletions

File tree

vulnerabilities/models.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3406,7 +3406,6 @@ def bulk_get_or_create_from_purls(self, purls: List[Union[PackageURL, str]]):
34063406
existing_packages = PackageV2.objects.filter(package_url__in=purl_strings)
34073407
existing_purls = set(existing_packages.values_list("package_url", flat=True))
34083408

3409-
all_packages = list(existing_packages)
34103409
packages_to_create = []
34113410
for purl in purls:
34123411
if str(purl) in existing_purls:
@@ -3425,13 +3424,12 @@ def bulk_get_or_create_from_purls(self, purls: List[Union[PackageURL, str]]):
34253424
packages_to_create.append(PackageV2(**purl_dict))
34263425

34273426
try:
3428-
new_packages = PackageV2.objects.bulk_create(packages_to_create)
3427+
PackageV2.objects.bulk_create(packages_to_create, ignore_conflicts=True)
34293428
except Exception as e:
34303429
logging.error(f"Error creating PackageV2: {e} \n {traceback_format_exc()}")
34313430
return []
34323431

3433-
all_packages.extend(new_packages)
3434-
return all_packages
3432+
return PackageV2.objects.filter(package_url__in=purl_strings)
34353433

34363434
def only_vulnerable(self):
34373435
return self._vulnerable(True)

0 commit comments

Comments
 (0)