Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

service/osv: guard against schema changes #288

Merged
merged 2 commits into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ All versions prior to 0.0.9 are untracked.
connection failures to vulnerability sources was improved
([#287](https://github.com/trailofbits/pip-audit/pull/287))

* Vulnerability sources: the OSV service is now more resilient
to schema changes ([#288](https://github.com/trailofbits/pip-audit/pull/288))

### Fixed

* Vulnerability sources: a bug stemming from an incorrect assumption
Expand Down
9 changes: 9 additions & 0 deletions pip_audit/_service/osv.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ def query(self, spec: Dependency) -> Tuple[Dependency, List[VulnerabilityResult]
return spec, results

for vuln in response_json["vulns"]:
# Sanity check: only the v1 schema is specified at the moment,
# and the code below probably won't work with future incompatible
# schemas without additional changes.
# The absence of a schema is treated as 1.0.0, per the OSV spec.
schema_version = Version(vuln.get("schema_version", "1.0.0"))
if schema_version.major != 1:
logger.warning(f"Unsupported OSV schema version: {schema_version}")
continue

id = vuln["id"]

# The summary is intended to be shorter, so we prefer it over
Expand Down
29 changes: 29 additions & 0 deletions test/service/test_osv.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,35 @@ def test_osv_skipped_dep():
assert len(vulns) == 0


@pytest.mark.parametrize("version", ["0.0.0", "2.0.0", "2.3.4"])
def test_osv_unsupported_schema_version(monkeypatch, version):
logger = pretend.stub(warning=pretend.call_recorder(lambda s: None))
monkeypatch.setattr(service.osv, "logger", logger)

payload = {
"vulns": [
{"schema_version": version},
]
}

response = pretend.stub(raise_for_status=lambda: None, json=lambda: payload)
post = pretend.call_recorder(lambda *a, **kw: response)

osv = service.OsvService()
monkeypatch.setattr(osv.session, "post", post)

dep = service.ResolvedDependency("foo", Version("1.0.0"))
results = dict(osv.query_all(iter([dep])))

assert logger.warning.calls == [pretend.call(f"Unsupported OSV schema version: {version}")]

assert len(results) == 1
assert dep in results

vulns = results[dep]
assert len(vulns) == 0


@pytest.mark.parametrize(
["summary", "details", "description"],
[
Expand Down