2023-02-04 23:24:31 +00:00
|
|
|
from typing import TypeVar, Generic, Optional
|
|
|
|
|
2023-02-04 23:35:47 +00:00
|
|
|
from fastapi import APIRouter, Depends
|
|
|
|
from fastapi.exceptions import HTTPException
|
2023-02-04 23:24:31 +00:00
|
|
|
from fastapi_pagination import create_page
|
|
|
|
from fastapi_pagination.links import Page
|
|
|
|
from pydantic import BaseModel
|
|
|
|
from slugify import slugify
|
|
|
|
|
|
|
|
from apollo.db import Advisory
|
|
|
|
from apollo.db.advisory import fetch_advisories
|
2023-02-05 01:14:58 +00:00
|
|
|
from apollo.rpmworker.repomd import EPOCH_RE, NEVRA_RE
|
2023-02-04 23:35:47 +00:00
|
|
|
from apollo.server.settings import UI_URL, get_setting
|
2023-02-04 23:24:31 +00:00
|
|
|
|
|
|
|
from common.fastapi import Params, to_rfc3339_date
|
|
|
|
|
|
|
|
router = APIRouter(tags=["osv"])
|
|
|
|
|
|
|
|
T = TypeVar("T")
|
|
|
|
|
|
|
|
|
|
|
|
class Pagination(Page[T], Generic[T]):
|
|
|
|
class Config:
|
|
|
|
allow_population_by_field_name = True
|
|
|
|
fields = {"items": {"alias": "advisories"}}
|
|
|
|
|
|
|
|
|
|
|
|
class OSVSeverity(BaseModel):
|
|
|
|
type: str
|
|
|
|
score: str
|
|
|
|
|
|
|
|
|
|
|
|
class OSVPackage(BaseModel):
|
|
|
|
ecosystem: str
|
|
|
|
name: str
|
|
|
|
purl: Optional[str] = None
|
|
|
|
|
|
|
|
|
|
|
|
class OSVEvent(BaseModel):
|
|
|
|
introduced: Optional[str] = None
|
|
|
|
fixed: Optional[str] = None
|
|
|
|
last_affected: Optional[str] = None
|
|
|
|
limit: Optional[str] = None
|
|
|
|
|
|
|
|
|
|
|
|
class OSVRangeDatabaseSpecific(BaseModel):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class OSVRange(BaseModel):
|
|
|
|
type: str
|
|
|
|
repo: str
|
|
|
|
events: list[OSVEvent]
|
|
|
|
database_specific: OSVRangeDatabaseSpecific
|
|
|
|
|
|
|
|
|
|
|
|
class OSVEcosystemSpecific(BaseModel):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class OSVAffectedDatabaseSpecific(BaseModel):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class OSVAffected(BaseModel):
|
|
|
|
package: OSVPackage
|
|
|
|
ranges: list[OSVRange]
|
|
|
|
versions: list[str]
|
|
|
|
ecosystem_specific: OSVEcosystemSpecific
|
|
|
|
database_specific: OSVAffectedDatabaseSpecific
|
|
|
|
|
|
|
|
|
|
|
|
class OSVReference(BaseModel):
|
|
|
|
type: str
|
|
|
|
url: str
|
|
|
|
|
|
|
|
|
|
|
|
class OSVCredit(BaseModel):
|
|
|
|
name: str
|
2023-02-04 23:35:47 +00:00
|
|
|
contact: list[str] = None
|
2023-02-04 23:24:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
class OSVDatabaseSpecific(BaseModel):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class OSVAdvisory(BaseModel):
|
|
|
|
schema_version: str = "1.3.1"
|
|
|
|
id: str
|
|
|
|
modified: str
|
|
|
|
published: str
|
|
|
|
withdrawn: Optional[str]
|
|
|
|
aliases: list[str]
|
|
|
|
related: list[str]
|
|
|
|
summary: str
|
|
|
|
details: str
|
|
|
|
severity: list[OSVSeverity]
|
|
|
|
affected: list[OSVAffected]
|
|
|
|
references: list[OSVReference]
|
|
|
|
credits: list[OSVCredit]
|
|
|
|
database_specific: OSVDatabaseSpecific
|
|
|
|
|
|
|
|
|
2023-02-04 23:35:47 +00:00
|
|
|
def to_osv_advisory(ui_url: str, advisory: Advisory) -> OSVAdvisory:
|
2023-02-04 23:24:31 +00:00
|
|
|
affected_pkgs = []
|
|
|
|
|
2023-02-05 01:14:58 +00:00
|
|
|
vendors = []
|
2023-02-04 23:24:31 +00:00
|
|
|
pkg_name_map = {}
|
|
|
|
for pkg in advisory.packages:
|
2023-02-05 01:14:58 +00:00
|
|
|
if pkg.supported_product.vendor not in vendors:
|
|
|
|
vendors.append(pkg.supported_product.vendor)
|
|
|
|
|
|
|
|
nevra = NEVRA_RE.search(pkg.nevra)
|
|
|
|
name = nevra.group(1)
|
|
|
|
arch = nevra.group(5).lower()
|
|
|
|
|
2023-02-05 00:18:46 +00:00
|
|
|
product_name = slugify(pkg.product_name)
|
2023-02-04 23:24:31 +00:00
|
|
|
if pkg.supported_products_rh_mirror:
|
2023-02-05 00:18:46 +00:00
|
|
|
product_name = f"{slugify(pkg.supported_product.variant)}:{pkg.supported_products_rh_mirror.match_major_version}"
|
|
|
|
|
|
|
|
if product_name not in pkg_name_map:
|
|
|
|
pkg_name_map[product_name] = {}
|
2023-02-05 01:14:58 +00:00
|
|
|
if arch not in pkg_name_map[product_name]:
|
|
|
|
pkg_name_map[product_name][arch] = {}
|
|
|
|
if name not in pkg_name_map[product_name][arch]:
|
|
|
|
pkg_name_map[product_name][arch][name] = []
|
2023-02-04 23:24:31 +00:00
|
|
|
|
2023-02-05 01:14:58 +00:00
|
|
|
pkg_name_map[product_name][arch][name].append((pkg, nevra))
|
2023-02-04 23:24:31 +00:00
|
|
|
|
2023-02-05 01:14:58 +00:00
|
|
|
for product_name, arches in pkg_name_map.items():
|
|
|
|
for arch, affected_arches in arches.items():
|
|
|
|
if not affected_arches:
|
2023-02-04 23:24:31 +00:00
|
|
|
continue
|
|
|
|
|
2023-02-05 01:14:58 +00:00
|
|
|
for pkg_name, affected_packages in affected_arches.items():
|
|
|
|
for pkg in affected_packages:
|
|
|
|
x = pkg[0]
|
|
|
|
nevra = pkg[1]
|
|
|
|
|
|
|
|
ver_rel = f"{nevra.group(3)}-{nevra.group(4)}"
|
|
|
|
slugified = slugify(x.supported_product.variant)
|
|
|
|
slugified_distro = slugify(x.product_name)
|
|
|
|
for arch_, _ in arches.items():
|
|
|
|
slugified_arch = f"-{slugify(arch_)}"
|
|
|
|
slugified_distro = slugified_distro.replace(
|
|
|
|
slugified_arch,
|
|
|
|
"",
|
|
|
|
)
|
|
|
|
epoch = nevra.group(2)
|
|
|
|
|
|
|
|
purl = f"pkg:rpm/{slugified}/{pkg_name}@{ver_rel}?arch={arch}&distro={slugified_distro}&epoch={epoch}"
|
|
|
|
|
|
|
|
affected = OSVAffected(
|
|
|
|
package=OSVPackage(
|
|
|
|
ecosystem=product_name,
|
|
|
|
name=pkg_name,
|
|
|
|
purl=purl,
|
|
|
|
),
|
|
|
|
ranges=[
|
|
|
|
OSVRange(
|
|
|
|
type="ECOSYSTEM",
|
|
|
|
repo=x.repo_name,
|
|
|
|
events=[
|
|
|
|
OSVEvent(introduced="0"),
|
|
|
|
OSVEvent(fixed=ver_rel),
|
|
|
|
],
|
|
|
|
database_specific=OSVRangeDatabaseSpecific(),
|
|
|
|
)
|
2023-02-04 23:24:31 +00:00
|
|
|
],
|
2023-02-05 01:14:58 +00:00
|
|
|
versions=[],
|
|
|
|
ecosystem_specific=OSVEcosystemSpecific(),
|
|
|
|
database_specific=OSVAffectedDatabaseSpecific(),
|
2023-02-04 23:24:31 +00:00
|
|
|
)
|
|
|
|
|
2023-02-05 01:14:58 +00:00
|
|
|
affected_pkgs.append(affected)
|
2023-02-04 23:24:31 +00:00
|
|
|
|
2023-02-04 23:35:47 +00:00
|
|
|
references = [
|
|
|
|
OSVReference(type="ADVISORY", url=f"{ui_url}/{advisory.name}"),
|
|
|
|
]
|
|
|
|
for fix in advisory.fixes:
|
|
|
|
references.append(OSVReference(type="REPORT", url=fix.source))
|
|
|
|
|
|
|
|
osv_credits = [OSVCredit(name=x) for x in vendors]
|
|
|
|
if advisory.red_hat_advisory:
|
|
|
|
osv_credits.append(OSVCredit(name="Red Hat"))
|
|
|
|
|
2023-02-04 23:24:31 +00:00
|
|
|
return OSVAdvisory(
|
|
|
|
id=advisory.name,
|
|
|
|
modified=to_rfc3339_date(advisory.updated_at),
|
|
|
|
published=to_rfc3339_date(advisory.published_at),
|
|
|
|
withdrawn=None,
|
|
|
|
aliases=[x.cve for x in advisory.cves],
|
|
|
|
related=[],
|
|
|
|
summary=advisory.synopsis,
|
|
|
|
details=advisory.description,
|
|
|
|
severity=[
|
|
|
|
OSVSeverity(type="CVSS_V3", score=x.cvss3_scoring_vector)
|
|
|
|
for x in advisory.cves
|
|
|
|
],
|
|
|
|
affected=affected_pkgs,
|
2023-02-04 23:35:47 +00:00
|
|
|
references=references,
|
|
|
|
credits=osv_credits,
|
2023-02-04 23:24:31 +00:00
|
|
|
database_specific=OSVDatabaseSpecific(),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/", response_model=Pagination[OSVAdvisory])
|
|
|
|
async def get_advisories_osv(
|
|
|
|
params: Params = Depends(),
|
|
|
|
product: Optional[str] = None,
|
|
|
|
before: Optional[str] = None,
|
|
|
|
after: Optional[str] = None,
|
|
|
|
cve: Optional[str] = None,
|
|
|
|
synopsis: Optional[str] = None,
|
|
|
|
keyword: Optional[str] = None,
|
|
|
|
severity: Optional[str] = None,
|
|
|
|
kind: Optional[str] = None,
|
|
|
|
):
|
|
|
|
fetch_adv = await fetch_advisories(
|
|
|
|
params.get_size(),
|
|
|
|
params.get_offset(),
|
|
|
|
keyword,
|
|
|
|
product,
|
|
|
|
before,
|
|
|
|
after,
|
|
|
|
cve,
|
|
|
|
synopsis,
|
|
|
|
severity,
|
|
|
|
kind,
|
|
|
|
fetch_related=True,
|
|
|
|
)
|
|
|
|
count = fetch_adv[0]
|
|
|
|
advisories = fetch_adv[1]
|
|
|
|
|
2023-02-04 23:35:47 +00:00
|
|
|
ui_url = await get_setting(UI_URL)
|
|
|
|
osv_advisories = [to_osv_advisory(ui_url, x) for x in advisories]
|
2023-02-04 23:24:31 +00:00
|
|
|
return create_page(osv_advisories, count, params)
|
2023-02-04 23:35:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
@router.get("/{advisory_id}", response_model=OSVAdvisory)
|
|
|
|
async def get_advisory_osv(advisory_id: str):
|
|
|
|
advisory = await Advisory.filter(name=advisory_id).prefetch_related(
|
|
|
|
"packages",
|
|
|
|
"cves",
|
|
|
|
"fixes",
|
|
|
|
"affected_products",
|
|
|
|
"packages",
|
|
|
|
"packages__supported_product",
|
|
|
|
"packages__supported_products_rh_mirror",
|
|
|
|
).get_or_none()
|
|
|
|
|
|
|
|
if not advisory:
|
|
|
|
raise HTTPException(404)
|
|
|
|
|
|
|
|
ui_url = await get_setting(UI_URL)
|
|
|
|
return to_osv_advisory(ui_url, advisory)
|