Skip to content

Commit cf24427

Browse files
authored
Allow empty introduced commit fields. (#99)
* Allow empty introduced commit fields. Assume all commits prior to the fix are vulnerable. * fix comment
1 parent cd4e934 commit cf24427

5 files changed

Lines changed: 145 additions & 7 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
id: BLAH-127
2+
package:
3+
name: blah.com/package
4+
ecosystem: golang
5+
summary: A vulnerability
6+
details: |
7+
Blah blah blah
8+
Blah
9+
severity: HIGH
10+
affects:
11+
ranges:
12+
- type: GIT
13+
repo: https://osv-test/repo/url
14+
fixed: 8d8242f545e9cec3e6d0d2e3f5bde8be1c659735
15+
references:
16+
- https://ref.com/ref
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
diff --git a/BLAH-127.yaml b/BLAH-127.yaml
2+
index 915f291..c6a4223 100644
3+
--- a/BLAH-127.yaml
4+
+++ b/BLAH-127.yaml
5+
@@ -12,5 +12,14 @@ affects:
6+
- type: GIT
7+
repo: https://osv-test/repo/url
8+
fixed: 8d8242f545e9cec3e6d0d2e3f5bde8be1c659735
9+
+ - type: GIT
10+
+ repo: https://osv-test/repo/url
11+
+ fixed: b9b3fd4732695b83c3068b7b6a14bb372ec31f98
12+
+ versions:
13+
+ - branch-v0.1.1
14+
+ - branch_1_cherrypick_regress
15+
+ - v0.1
16+
+ - v0.1.1
17+
references:
18+
- https://ref.com/ref
19+
+modified: '2021-01-01T00:00:00Z'

docker/worker/worker.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,10 @@ def _do_update(self, source_repo, repo, vulnerability, yaml_path,
358358
if affected_range.type != vulnerability_pb2.AffectedRange.GIT:
359359
continue
360360

361-
range_collectors[affected_range.repo].add(affected_range.introduced,
362-
affected_range.fixed)
361+
# Convert empty values ('') to None.
362+
introduced = affected_range.introduced or None
363+
fixed = affected_range.fixed or None
364+
range_collectors[affected_range.repo].add(introduced, fixed)
363365

364366
for affected_range in vulnerability.affects.ranges:
365367
# Go through existing provided ranges to find additional ranges (via

docker/worker/worker_test.py

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,9 @@ def setUp(self):
776776
self.mock_repo.add_file(
777777
'BLAH-125.yaml',
778778
self._load_test_data(os.path.join(TEST_DATA_DIR, 'BLAH-125.yaml')))
779+
self.mock_repo.add_file(
780+
'BLAH-127.yaml',
781+
self._load_test_data(os.path.join(TEST_DATA_DIR, 'BLAH-127.yaml')))
779782
self.mock_repo.commit('User', 'user@email')
780783

781784
osv.SourceRepository(
@@ -805,6 +808,12 @@ def setUp(self):
805808
ecosystem='golang',
806809
source_id='source:BLAH-125.yaml',
807810
source_of_truth=osv.SourceOfTruth.SOURCE_REPO).put()
811+
osv.Bug(
812+
id='BLAH-127',
813+
project='blah.com/package',
814+
ecosystem='golang',
815+
source_id='source:BLAH-127.yaml',
816+
source_of_truth=osv.SourceOfTruth.SOURCE_REPO).put()
808817

809818
def tearDown(self):
810819
self.tmp_dir.cleanup()
@@ -945,6 +954,80 @@ def test_update_add_fix(self):
945954
'ff8cc32ba60ad9cbb3b23f0a82aad96ebe9ff76b',
946955
], [commit.commit for commit in affected_commits])
947956

957+
def test_update_no_introduced(self):
958+
"""Test update vulnerability with no introduced commit."""
959+
task_runner = worker.TaskRunner(ndb_client, None, self.tmp_dir.name, None,
960+
None)
961+
962+
message = mock.Mock()
963+
message.attributes = {
964+
'source': 'source',
965+
'path': 'BLAH-127.yaml',
966+
'original_sha256': ('484f6d8659f0c01e2f08a6fba9791fb2'
967+
'9b5df09530e5d8307fc1f368b01d7dcb'),
968+
'deleted': 'false',
969+
}
970+
task_runner._source_update(message)
971+
972+
repo = pygit2.Repository(self.remote_source_repo_path)
973+
commit = repo.head.peel()
974+
975+
self.assertEqual('infra@osv.dev', commit.author.email)
976+
self.assertEqual('OSV', commit.author.name)
977+
self.assertEqual('Update BLAH-127', commit.message)
978+
diff = repo.diff(commit.parents[0], commit)
979+
self.assertEqual(self._load_test_data('expected_127.diff'), diff.patch)
980+
981+
self.assertDictEqual(
982+
{
983+
'additional_commit_ranges': [{
984+
'fixed_in': 'b9b3fd4732695b83c3068b7b6a14bb372ec31f98',
985+
'introduced_in': ''
986+
},],
987+
'affected': [],
988+
'affected_fuzzy': [],
989+
'confidence': None,
990+
'details': 'Blah blah blah\nBlah\n',
991+
'ecosystem': 'golang',
992+
'fixed': '8d8242f545e9cec3e6d0d2e3f5bde8be1c659735',
993+
'has_affected': False,
994+
'issue_id': None,
995+
'last_modified': datetime.datetime(2021, 1, 1, 0, 0),
996+
'project': 'blah.com/package',
997+
'public': None,
998+
'reference_urls': ['https://ref.com/ref'],
999+
'regressed': '',
1000+
'repo_url': 'https://osv-test/repo/url',
1001+
'search_indices': ['blah.com/package', 'BLAH-127', 'BLAH', '127'],
1002+
'severity': 'HIGH',
1003+
'sort_key': 'BLAH-0000127',
1004+
'source_id': 'source:BLAH-127.yaml',
1005+
'source_of_truth': osv.SourceOfTruth.SOURCE_REPO,
1006+
'status': None,
1007+
'summary': 'A vulnerability',
1008+
'timestamp': None
1009+
},
1010+
osv.Bug.get_by_id('BLAH-127')._to_dict())
1011+
1012+
affected_commits = list(osv.AffectedCommit.query())
1013+
self.assertCountEqual([
1014+
'b1c95a196f22d06fcf80df8c6691cd113d8fefff',
1015+
'eefe8ec3f1f90d0e684890e810f3f21e8500a4cd',
1016+
'a2ba949290915d445d34d0e8e9de2e7ce38198fc',
1017+
'e1b045257bc5ca2a11d0476474f45ef77a0366c7',
1018+
'00514d6f244f696e750a37083163992c6a50cfd3',
1019+
'25147a74d8aeb27b43665530ee121a2a1b19dc58',
1020+
'3c5dcf6a5bec14baab3b247d369a7270232e1b83',
1021+
'4c155795426727ea05575bd5904321def23c03f4',
1022+
'57e58a5d7c2bb3ce0f04f17ec0648b92ee82531f',
1023+
'90aa4127295b2c37b5f7fcf6a9772b12c99a5212',
1024+
'949f182716f037e25394bbb98d39b3295d230a29',
1025+
'b1fa81a5d59e9b4d6e276d82fc17058f3cf139d9',
1026+
'f0cc40d8c3dabb27c2cfe26f1764305abc91a0b9',
1027+
'febfac1940086bc1f6d3dc33fda0a1d1ba336209',
1028+
'ff8cc32ba60ad9cbb3b23f0a82aad96ebe9ff76b',
1029+
], [commit.commit for commit in affected_commits])
1030+
9481031
def test_update_new(self):
9491032
"""Test update with new vulnerability added."""
9501033
self.mock_repo.add_file(
@@ -1032,7 +1115,7 @@ def test_update_no_changes(self):
10321115
message.attributes = {
10331116
'source': 'source',
10341117
'path': 'BLAH-125.yaml',
1035-
'original_sha256': ('b5ecb05106faef7fc5bd07f86e089783',
1118+
'original_sha256': ('b5ecb05106faef7fc5bd07f86e089783'
10361119
'4354608c5bb59d3b6317491874198a3a'),
10371120
'deleted': 'false',
10381121
}

lib/osv/impact.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,12 @@ def get_affected(repo, regress_commit_or_range, fix_commit_or_range):
123123
for commit in regress_commits:
124124
tags_with_bug.update(get_tags_with_commits(repo, [commit]))
125125

126-
tags_with_fix = get_tags_with_commits(repo, fix_commits)
126+
if not regress_commits:
127+
# If no introduced commit provided, assume all commits prior to fix are
128+
# vulnerable.
129+
tags_with_bug.update(get_all_tags(repo))
127130

131+
tags_with_fix = get_tags_with_commits(repo, fix_commits)
128132
affected_commits, affected_ranges = get_affected_range(
129133
repo, regress_commits, fix_commits)
130134

@@ -163,7 +167,8 @@ def get_affected_range(repo, regress_commits, fix_commits):
163167
if equivalent_regress_commit:
164168
break
165169

166-
if not equivalent_regress_commit:
170+
# If regress_commits is provided, then we should find an equivalent.
171+
if not equivalent_regress_commit and regress_commits:
167172
continue
168173

169174
# Get the latest equivalent commit in the fix range.
@@ -186,7 +191,9 @@ def get_affected_range(repo, regress_commits, fix_commits):
186191
# Not fixed in this branch. Everything is still vulnerabile.
187192
last_affected_commits.append(repo.revparse_single(ref).id)
188193

189-
commits.add(equivalent_regress_commit)
194+
if equivalent_regress_commit:
195+
commits.add(equivalent_regress_commit)
196+
190197
for last_affected_commit in last_affected_commits:
191198
if (equivalent_regress_commit, last_affected_commit) in seen_commits:
192199
continue
@@ -216,6 +223,15 @@ def get_commit_range(repo, commit_or_range):
216223
return get_commit_list(repo, start_commit, end_commit)
217224

218225

226+
def get_all_tags(repo):
227+
"""Get all tags."""
228+
return [
229+
ref[len(TAG_PREFIX):]
230+
for ref in repo.listall_references()
231+
if ref.startswith(TAG_PREFIX)
232+
]
233+
234+
219235
def get_tags_with_commits(repo, commits):
220236
"""Get tags with a given commit."""
221237
if not commits:
@@ -244,7 +260,9 @@ def get_commit_list(repo, start_commit, end_commit):
244260
except KeyError as e:
245261
raise ImpactError('Invalid commit.') from e
246262

247-
walker.hide(start_commit)
263+
if start_commit:
264+
walker.hide(start_commit)
265+
248266
return [str(commit.id) for commit in walker]
249267

250268

0 commit comments

Comments
 (0)