Skip to content

Commit 7efca91

Browse files
authored
feat: Support querying by tags for GIT affected. (google#3489)
Also smoothed out some pain points in testing Changes made: - Edited preput to also include repo name in project field if it contains a GIT affected field - Added special matching rules to match package name against repo_urls, and GIT ecosystems - Removed footgun in the hard coded GOOGLE_CLOUD_PROJECT being oss-vdb, so you can edit it only in the make file to change projects. Added run-api-server-test to run api server in test instance.
1 parent 6b36e9e commit 7efca91

10 files changed

Lines changed: 39 additions & 12 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ build/lib
1111
**/.history/*
1212
*/datastore-helper/
1313
hurl-scripts/
14+
temp/*

Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ run-website-emulator:
6161
run-api-server:
6262
test -f $(HOME)/.config/gcloud/application_default_credentials.json || (echo "GCP Application Default Credentials not set, try 'gcloud auth login --update-adc'"; exit 1)
6363
cd gcp/api && docker build -f Dockerfile.esp -t osv/esp:latest .
64-
cd gcp/api && $(install-cmd) && GOOGLE_CLOUD_PROJECT=oss-vdb $(run-cmd) python test_server.py $(HOME)/.config/gcloud/application_default_credentials.json $(ARGS)
64+
cd gcp/api && $(install-cmd) && GOOGLE_CLOUD_PROJECT=oss-vdb $(run-cmd) python test_server.py $(HOME)/.config/gcloud/application_default_credentials.json $(ARGS)# Run with `make run-api-server ARGS=--no-backend` to launch esp without backend.
65+
66+
run-api-server-test:
67+
test -f $(HOME)/.config/gcloud/application_default_credentials.json || (echo "GCP Application Default Credentials not set, try 'gcloud auth login --update-adc'"; exit 1)
68+
cd gcp/api && docker build -f Dockerfile.esp -t osv/esp:latest .
69+
cd gcp/api && $(install-cmd) && GOOGLE_CLOUD_PROJECT=oss-vdb-test $(run-cmd) python test_server.py $(HOME)/.config/gcloud/application_default_credentials.json $(ARGS)
6570

6671
# TODO: API integration tests.
6772
all-tests: lib-tests worker-tests importer-tests alias-tests website-tests vulnfeed-tests

gcp/api/integration_tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@ def test_get_vuln_by_alias_not_in_db(self):
886886
{
887887
'code': 5,
888888
'message': 'Bug not found, but the following aliases were: '
889-
'GHSA-q97m-8853-pq76'
889+
'GHSA-q97m-8853-pq76 GO-2025-3690'
890890
}, response.json())
891891

892892
def test_query_batch(self):

gcp/api/server.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,15 +1024,27 @@ def _is_version_affected(affected_packages,
10241024
range.
10251025
"""
10261026
for affected_package in affected_packages:
1027-
if package_name and package_name != affected_package.package.name:
1028-
continue
1029-
10301027
if ecosystem:
10311028
# If package ecosystem has a :, also try ignoring parts after it.
10321029
if not is_matching_package_ecosystem(affected_package.package.ecosystem,
10331030
ecosystem):
10341031
continue
10351032

1033+
if package_name:
1034+
if ecosystem == 'GIT':
1035+
# Special case for git ecosystems to match repo urls
1036+
repo_url = ''
1037+
for rg in affected_package.ranges:
1038+
if rg.repo_url != '':
1039+
repo_url = rg.repo_url
1040+
break
1041+
1042+
if package_name != repo_url:
1043+
continue
1044+
else:
1045+
if package_name != affected_package.package.name:
1046+
continue
1047+
10361048
if normalize:
10371049
if any(
10381050
osv.normalize_tag(version) == osv.normalize_tag(v)
@@ -1212,7 +1224,6 @@ def query_by_version(
12121224
query = query.filter(osv.Bug.ecosystem == ecosystem)
12131225
ecosystem_info = ecosystems.get(ecosystem)
12141226

1215-
ecosystem_info = ecosystems.get(ecosystem)
12161227
is_semver = ecosystem_info and ecosystem_info.is_semver
12171228
supports_comparing = ecosystem_info and ecosystem_info.supports_comparing
12181229

@@ -1450,6 +1461,12 @@ def is_matching_package_ecosystem(package_ecosystem: str,
14501461
"""Checks if the queried ecosystem matches the affected package's ecosystem,
14511462
considering potential variations in the package's ecosystem.
14521463
"""
1464+
1465+
# Special case for GIT queries,
1466+
# for which osv entries will have an empty ecosystem for.
1467+
if ecosystem == 'GIT' and package_ecosystem == '':
1468+
return True
1469+
14531470
return any(eco == ecosystem for eco in (
14541471
package_ecosystem,
14551472
ecosystems.normalize(package_ecosystem),

gcp/api/test_server.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
_ESP_PORT = 8080
2222
_BACKEND_PORT = 8000
23-
_GCP_PROJECT = 'oss-vdb'
2423

2524

2625
class ServerInstance:
@@ -49,7 +48,6 @@ def start_backend(port, log_path):
4948
"""Start backend server."""
5049
log_handle = open(log_path, 'w')
5150
env = os.environ.copy()
52-
env['GOOGLE_CLOUD_PROJECT'] = _GCP_PROJECT
5351

5452
backend_proc = subprocess.Popen(
5553
[sys.executable, 'server.py', f'--port={port}'],

gcp/workers/worker/testdata/UpdateTest_dont_index_too_many_git_versions.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5025,7 +5025,7 @@
50255025
'is_fixed': False,
50265026
'issue_id': None,
50275027
'last_modified': DatetimeWithNanoseconds(2021, 1, 1, 0, 0, tzinfo=datetime.timezone.utc),
5028-
'project': [],
5028+
'project': ['https://osv-test/repo/url'],
50295029
'public': True,
50305030
'purl': [],
50315031
'reference_url_types': {'https://ref.com/ref': 'WEB'},
@@ -5035,6 +5035,7 @@
50355035
'affected',
50365036
'affected-01',
50375037
'git',
5038+
'https',
50385039
'https://osv-test/repo/url',
50395040
'last',
50405041
'last-affected',

osv/ecosystems/_ecosystems.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
'Linux': OrderingUnsupportedEcosystem(),
7171
'OSS-Fuzz': OrderingUnsupportedEcosystem(),
7272
'Photon OS': OrderingUnsupportedEcosystem(),
73+
'GIT': OrderingUnsupportedEcosystem(),
7374
}
7475

7576
# Semver-based ecosystems, should correspond to _ecosystems above.

osv/models.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ def id(self):
336336

337337
@property
338338
def repo_url(self):
339-
"""Repo URL."""
339+
"""Repo URL. Assumes there is only ever one repo URL per advisory."""
340340
for affected_package in self.affected_packages:
341341
for affected_range in affected_package.ranges:
342342
if affected_range.repo_url:
@@ -399,6 +399,10 @@ def _pre_put_hook(self): # pylint: disable=arguments-differ
399399
self.project = list({
400400
pkg.package.name for pkg in self.affected_packages if pkg.package.name
401401
})
402+
403+
if self.repo_url and 'GIT' in self.ecosystem:
404+
self.project.append(self.repo_url)
405+
402406
self.project.sort()
403407

404408
ecosystems_set = {

poetry.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tools/datafix/reput_bugs.py

100644100755
File mode changed.

0 commit comments

Comments
 (0)