|
10 | 10 | import json |
11 | 11 | import logging |
12 | 12 | from typing import Iterable |
| 13 | +from typing import Iterator |
13 | 14 | from typing import List |
14 | 15 | from typing import Optional |
15 | 16 | from typing import Tuple |
|
18 | 19 | from cvss.exceptions import CVSS3MalformedError |
19 | 20 | from cvss.exceptions import CVSS4MalformedError |
20 | 21 | from packageurl import PackageURL |
| 22 | +from packageurl.contrib.url2purl import url2purl |
21 | 23 | from univers.version_range import RANGE_CLASS_BY_SCHEMES |
22 | 24 | from univers.versions import InvalidVersion |
23 | 25 | from univers.versions import SemverVersion |
24 | 26 | from univers.versions import Version |
| 27 | +from websockets.version import commit |
25 | 28 |
|
26 | 29 | from vulnerabilities.importer import AdvisoryData |
27 | 30 | from vulnerabilities.importer import AffectedPackage |
|
49 | 52 | "cargo": "cargo", |
50 | 53 | } |
51 | 54 |
|
| 55 | +SUPPORTED_VCS_TYPES = {"github", "bitbucket", "gitlab"} |
| 56 | + |
52 | 57 |
|
53 | 58 | def parse_advisory_data( |
54 | 59 | raw_data: dict, supported_ecosystems, advisory_url: str |
@@ -164,11 +169,30 @@ def parse_advisory_data_v2( |
164 | 169 | get_fixed_version_range(fixed_versions, purl.type) if fixed_versions else None |
165 | 170 | ) |
166 | 171 |
|
| 172 | + if not purl and (affected_by_commits or fixed_by_commits): |
| 173 | + for code_commit in affected_by_commits + fixed_by_commits: |
| 174 | + purl = url2purl(code_commit.vcs_url) |
| 175 | + if purl.type not in SUPPORTED_VCS_TYPES: |
| 176 | + logger.error( |
| 177 | + f"Storing commit_hash:{code_commit.commit_hash}, code_commit:{code_commit.vcs_url} as reference, not creating CodeCommits." |
| 178 | + ) |
| 179 | + ref = ReferenceV2( |
| 180 | + reference_id=code_commit.commit_hash, |
| 181 | + reference_type="commit", |
| 182 | + url=code_commit.vcs_url, |
| 183 | + ) |
| 184 | + references.append(ref) |
| 185 | + |
| 186 | + affected_by_commits, fixed_by_commits = [], [] |
| 187 | + |
167 | 188 | if not purl or purl.type not in supported_ecosystems: |
168 | 189 | logger.error(f"Unsupported package type: {purl!r} in OSV: {advisory_id!r}") |
169 | | - continue |
| 190 | + # Ignore version ranges not associated with the same supported_ecosystems. |
| 191 | + fixed_version_range, affected_version_range = None, None |
170 | 192 |
|
171 | | - if fixed_version_range or affected_version_range or fixed_by_commits or affected_by_commits: |
| 193 | + if purl and ( |
| 194 | + fixed_version_range or affected_version_range or fixed_by_commits or affected_by_commits |
| 195 | + ): |
172 | 196 | affected_packages.append( |
173 | 197 | AffectedPackageV2( |
174 | 198 | package=purl, |
@@ -200,7 +224,7 @@ def parse_advisory_data_v2( |
200 | 224 | ) |
201 | 225 |
|
202 | 226 |
|
203 | | -def extract_introduced_and_fixed(ranges) -> Tuple[List[str], List[str]]: |
| 227 | +def extract_introduced_and_fixed(ranges) -> Iterator[Tuple[Optional[str], Optional[str]]]: |
204 | 228 | """ |
205 | 229 | Return pairs of introduced and fixed versions or commit hashes given a ``ranges`` |
206 | 230 | mapping of OSV data. |
|
0 commit comments