Skip to content

Commit

Permalink
Add firstPatchedVersion in github API
Browse files Browse the repository at this point in the history
Signed-off-by: Tushar Goel <[email protected]>
  • Loading branch information
TG1999 committed May 21, 2022
1 parent 6ec2e9e commit d07eabe
Show file tree
Hide file tree
Showing 14 changed files with 59 additions and 38 deletions.
63 changes: 33 additions & 30 deletions vulnerabilities/importers/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from dateutil import parser as dateparser
from django.db.models.query import QuerySet
from packageurl import PackageURL
from univers.version_range import RANGE_CLASS_BY_SCHEMES
from univers.version_range import build_range_from_github_advisory_constraint

from vulnerabilities import severity_systems
Expand Down Expand Up @@ -153,6 +154,9 @@
severity
publishedAt
}
firstPatchedVersion{
identifier
}
package {
name
}
Expand Down Expand Up @@ -236,43 +240,42 @@ def process_response(resp: dict, package_type: str) -> Iterable[AdvisoryData]:
return

for vulnerability in vulnerabilities:
affected_packages = []
aliases = set()
github_advisory = get_item(vulnerability, "node")
if not github_advisory:
logger.error(f"No node found in {vulnerability!r}")
continue

name = get_item(github_advisory, "package", "name")
if not name:
logger.error(f"No name found in {github_advisory!r}")
continue

purl = get_purl(pkg_type=package_type, github_name=name)
if not purl:
continue

vulnerable_range = get_item(github_advisory, "vulnerableVersionRange")
if not vulnerable_range:
logger.error(f"No affected range found in {github_advisory!r}")
continue

affected_range = None
try:
affected_range = build_range_from_github_advisory_constraint(
package_type, vulnerable_range
)
except InvalidVersionRange:
logger.error(f"Could not parse affected range {vulnerable_range!r}")
continue

if affected_range != NotImplementedError:
affected_packages.append(
AffectedPackage(
package=purl,
affected_version_range=affected_range,
if name:
purl = get_purl(pkg_type=package_type, github_name=name)
if purl:
affected_range = get_item(github_advisory, "vulnerableVersionRange")
fixed_version = get_item(github_advisory, "firstPatchedVersion", "identifier")
if affected_range:
try:
affected_range = build_range_from_github_advisory_constraint(
package_type, affected_range
)
except InvalidVersionRange as e:
logger.error(f"Could not parse affected range {affected_range!r} {e!r}")
affected_range = None
if fixed_version:
try:
fixed_version = RANGE_CLASS_BY_SCHEMES[package_type].version_class(
fixed_version
)
except Exception as e:
logger.error(f"Invalid fixed version {fixed_version!r} {e!r}")
fixed_version = None
affected_packages = []
if affected_range or fixed_version:
affected_packages.append(
AffectedPackage(
package=purl,
affected_version_range=affected_range,
fixed_version=fixed_version,
)
)
)

advisory = get_item(github_advisory, "advisory")
if not advisory:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@
"subpath": null
},
"affected_version_range": "vers:composer/<22.1.0",
"fixed_version": null
"fixed_version": "22.1.0"
}
],
"references": [
Expand Down
3 changes: 3 additions & 0 deletions vulnerabilities/tests/test_data/github_api/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@
"package": {
"name": "librenms/librenms"
},
"firstPatchedVersion": {
"identifier" :"22.1.0"
},
"vulnerableVersionRange": "< 22.1.0"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"subpath": null
},
"affected_version_range": "vers:gem/<=1.3.1",
"fixed_version": null
"fixed_version": "1.3.2"
}
],
"references": [
Expand Down
3 changes: 3 additions & 0 deletions vulnerabilities/tests/test_data/github_api/gem.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@
"package": {
"name": "webrick"
},
"firstPatchedVersion": {
"identifier" :"1.3.2"
},
"vulnerableVersionRange": "<= 1.3.1"
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"subpath": null
},
"affected_version_range": "vers:golang/<1.3.3",
"fixed_version": null
"fixed_version": "1.3.3"
}
],
"references": [
Expand Down
3 changes: 3 additions & 0 deletions vulnerabilities/tests/test_data/github_api/golang.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
"package": {
"name": "github.com/moby/moby"
},
"firstPatchedVersion": {
"identifier" :"1.3.3"
},
"vulnerableVersionRange": "< 1.3.3"
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
"subpath": null
},
"affected_version_range": "vers:maven/>=9.0.0|<9.0.31",
"fixed_version": null
"fixed_version": "9.0.1"
}
],
"references": [
Expand Down
3 changes: 3 additions & 0 deletions vulnerabilities/tests/test_data/github_api/maven.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@
"package": {
"name": "org.apache.tomcat.embed:tomcat-embed-core"
},
"firstPatchedVersion": {
"identifier" :"9.0.1"
},
"vulnerableVersionRange": ">= 9.0.0, < 9.0.31"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"subpath": null
},
"affected_version_range": "vers:nuget/<=4.5.1-alpha001",
"fixed_version": null
"fixed_version": "4.5.1"
}
],
"references": [
Expand Down
3 changes: 3 additions & 0 deletions vulnerabilities/tests/test_data/github_api/nuget.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
"package": {
"name": "RazorEngine"
},
"firstPatchedVersion": {
"identifier" :"4.5.1"
},
"vulnerableVersionRange": "<= 4.5.1-alpha001"
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"subpath": null
},
"affected_version_range": "vers:pypi/<9.0.0",
"fixed_version": null
"fixed_version": "9.0.0"
}
],
"references": [
Expand Down
3 changes: 3 additions & 0 deletions vulnerabilities/tests/test_data/github_api/pypi.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
"package": {
"name": "Pillow"
},
"firstPatchedVersion": {
"identifier" :"9.0.0"
},
"vulnerableVersionRange": "< 9.0.0"
}
},
Expand Down
4 changes: 2 additions & 2 deletions vulnerabilities/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,9 @@ def get_item(dictionary: dict, *attributes):
'd'
>>> assert(get_item({'a': {'b': {'c': 'd'}}}, 'a', 'b', 'e')) == None
"""
if not dictionary:
return
for attribute in attributes:
if not dictionary:
return
if attribute not in dictionary:
logger.error(f"Missing attribute {attribute} in {dictionary}")
return None
Expand Down

0 comments on commit d07eabe

Please sign in to comment.