MCPcopy Create free account
hub / github.com/aboutcode-org/vulnerablecode / get_affected_purl

Function get_affected_purl

vulnerabilities/pipes/osv_v2.py:324–370  ·  view source on GitHub ↗

Return an affected PackageURL or None given a mapping of ``affected_pkg`` data and a ``raw_id``.

(affected_pkg, raw_id)

Source from the content-addressed store, hash-verified

322
323
324def get_affected_purl(affected_pkg, raw_id):
325 """
326 Return an affected PackageURL or None given a mapping of ``affected_pkg``
327 data and a ``raw_id``.
328 """
329 package = affected_pkg.get("package") or {}
330 if purl := package.get("purl"):
331 try:
332 purl_dict = PackageURL.from_string(purl).to_dict()
333 del purl_dict["version"]
334 purl = PackageURL(**purl_dict)
335 except ValueError:
336 logger.error(
337 f"Invalid PackageURL: {purl!r} for OSV "
338 f"affected_pkg {affected_pkg} and id: {raw_id}"
339 )
340 else:
341 ecosys = package.get("ecosystem")
342 name = package.get("name")
343 namespace = ""
344
345 if ecosys and name:
346 ecosys = ecosys.lower()
347 purl_type = PURL_TYPE_BY_OSV_ECOSYSTEM.get(ecosys)
348 if ecosys.startswith("ubuntu"):
349 purl_type = "deb"
350 namespace = "ubuntu"
351
352 if not purl_type:
353 return
354 if purl_type == "maven":
355 namespace, _, name = name.partition(":")
356
357 purl = PackageURL(type=purl_type, namespace=namespace, name=name)
358 else:
359 logger.error(
360 f"No PackageURL possible: {purl!r} for affected_pkg {affected_pkg} for OSV id: {raw_id}"
361 )
362 return
363 try:
364 package_url = PackageURL.from_string(str(purl))
365 return package_url
366 except:
367 logger.error(
368 f"Invalid PackageURL: {purl!r} for affected_pkg {affected_pkg} for OSV id: {raw_id}"
369 )
370 return None
371
372
373def get_explicit_affected_range(affected_pkg, raw_id, supported_ecosystem):

Callers 3

parse_advisory_data_v3Function · 0.70

Calls 2

getMethod · 0.45
to_dictMethod · 0.45

Tested by 2