-
-
Notifications
You must be signed in to change notification settings - Fork 303
Expand file tree
/
Copy pathtest_osv_v2.py
More file actions
340 lines (311 loc) · 14.4 KB
/
test_osv_v2.py
File metadata and controls
340 lines (311 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# VulnerableCode is a trademark of nexB Inc.
# SPDX-License-Identifier: Apache-2.0
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
# See https://github.com/aboutcode-org/vulnerablecode for support or download.
# See https://aboutcode.org for more information about nexB OSS projects.
#
import json
import os
from pathlib import Path
from unittest import TestCase
import saneyaml
from univers.version_constraint import VersionConstraint
from univers.version_range import MavenVersionRange
from univers.version_range import PypiVersionRange
from univers.versions import MavenVersion
from univers.versions import PypiVersion
from vulnerabilities.pipes.osv_v2 import get_explicit_affected_range
from vulnerabilities.pipes.osv_v2 import get_version_ranges_constraints
from vulnerabilities.pipes.osv_v2 import parse_advisory_data_v3
from vulnerabilities.tests import util_tests
def test_parse_advisory_data_v3_withdrawn_returns_none():
raw_data = {
"id": "GHSA-w596-4wvx-j9j6",
"published": "2022-10-16T12:00:23Z",
"withdrawn": "2025-08-01T20:34:11Z",
"aliases": ["CVE-2022-42969"],
"summary": "Withdrawn Advisory: ReDoS in py library",
"affected": [
{
"package": {"ecosystem": "PyPI", "name": "py"},
"ranges": [
{
"type": "ECOSYSTEM",
"events": [{"introduced": "0"}, {"last_affected": "1.11.0"}],
}
],
}
],
}
result = parse_advisory_data_v3(
raw_data,
supported_ecosystems=["pypi"],
advisory_url="https://github.com/github/advisory-database/blob/main/advisories/GHSA-w596-4wvx-j9j6.json",
advisory_text="",
)
assert result is None
def test_parse_advisory_data_v3_not_withdrawn_returns_advisory():
raw_data = {
"id": "GHSA-j3f7-7rmc-6wqj",
"published": "2022-01-10T14:12:00Z",
"aliases": ["CVE-2022-0001"],
"summary": "Some valid advisory",
"affected": [],
}
result = parse_advisory_data_v3(
raw_data,
supported_ecosystems=["pypi"],
advisory_url="https://github.com/github/advisory-database/blob/main/advisories/GHSA-j3f7-7rmc-6wqj.json",
advisory_text="",
)
assert result is not None
TEST_DATA = Path(__file__).parent.parent / "test_data" / "osv_test"
def test_get_version_ranges_constraints():
assert get_version_ranges_constraints(
ranges={"type": "ECOSYSTEM", "events": [{"introduced": "0"}, {"fixed": "0.21.8"}]},
raw_id="GHSA-8hxh-r6f7-jf45",
supported_ecosystem="maven",
) == (
[VersionConstraint(comparator="<", version=MavenVersion(string="0.21.8"))],
[VersionConstraint(comparator="=", version=MavenVersion(string="0.21.8"))],
[],
[],
)
assert get_version_ranges_constraints(
ranges={
"events": [
{"introduced": "0"},
{"fixed": "91f4af7e6af53e1c6bf17ed36cb2161863eddae4"},
{"fixed": "18eea90ebb24a9c22248f0b7e18646cc6e3e3e0f"},
{"fixed": "a1991aeac19c3fec1fdd0d184c6760c90c9f9fc9"},
{"fixed": "31e41eea6c2322689826e6065ceba82551c565aa"},
{"fixed": "a40285c5a0288669b72f9d991508d4405885bffc"},
],
"repo": "https://fedoraproject.org/wiki/Infrastructure/Fedorahosted-retirement",
"type": "GIT",
},
raw_id="GHSA-8hxh-r6f7-jf45",
supported_ecosystem="maven",
) == (
[],
[],
[],
[
"91f4af7e6af53e1c6bf17ed36cb2161863eddae4",
"18eea90ebb24a9c22248f0b7e18646cc6e3e3e0f",
"a1991aeac19c3fec1fdd0d184c6760c90c9f9fc9",
"31e41eea6c2322689826e6065ceba82551c565aa",
"a40285c5a0288669b72f9d991508d4405885bffc",
],
)
assert get_version_ranges_constraints(
ranges={"type": "ECOSYSTEM", "events": []},
raw_id="GHSA-8hxh-r6f7-jf45",
supported_ecosystem="maven",
) == (
[],
[],
[],
[],
)
def test_get_explicit_affected_constraints():
assert get_explicit_affected_range(
affected_pkg={
"versions": [
"4.10.2",
"4.12.2",
"4.4.0.dev1",
"4.5.0",
"4.5.2",
"4.5.4",
"4.6.2",
"4.6.3",
"4.6.4",
"4.6.5",
"4.6.7",
"4.7.0",
"4.7.1",
"4.7.2",
"4.7.4",
"4.7.5",
"4.8.0",
"4.8.0rc1",
"4.8.1",
"4.8.2",
"4.8.3",
"4.8.5",
"4.8.6",
"4.8.7",
"4.8.9",
"4.9.12",
]
},
raw_id="GHSA-8hxh-r6f7-jf45",
supported_ecosystem="maven",
) == MavenVersionRange(
constraints=(
VersionConstraint(comparator="=", version=MavenVersion(string="4.4.0.dev1")),
VersionConstraint(comparator="=", version=MavenVersion(string="4.5.0")),
VersionConstraint(comparator="=", version=MavenVersion(string="4.5.2")),
VersionConstraint(comparator="=", version=MavenVersion(string="4.5.4")),
VersionConstraint(comparator="=", version=MavenVersion(string="4.6.2")),
VersionConstraint(comparator="=", version=MavenVersion(string="4.6.3")),
VersionConstraint(comparator="=", version=MavenVersion(string="4.6.4")),
VersionConstraint(comparator="=", version=MavenVersion(string="4.6.5")),
VersionConstraint(comparator="=", version=MavenVersion(string="4.6.7")),
VersionConstraint(comparator="=", version=MavenVersion(string="4.7.0")),
VersionConstraint(comparator="=", version=MavenVersion(string="4.7.1")),
VersionConstraint(comparator="=", version=MavenVersion(string="4.7.2")),
VersionConstraint(comparator="=", version=MavenVersion(string="4.7.4")),
VersionConstraint(comparator="=", version=MavenVersion(string="4.7.5")),
VersionConstraint(comparator="=", version=MavenVersion(string="4.8.0rc1")),
VersionConstraint(comparator="=", version=MavenVersion(string="4.8.0")),
VersionConstraint(comparator="=", version=MavenVersion(string="4.8.1")),
VersionConstraint(comparator="=", version=MavenVersion(string="4.8.2")),
VersionConstraint(comparator="=", version=MavenVersion(string="4.8.3")),
VersionConstraint(comparator="=", version=MavenVersion(string="4.8.5")),
VersionConstraint(comparator="=", version=MavenVersion(string="4.8.6")),
VersionConstraint(comparator="=", version=MavenVersion(string="4.8.7")),
VersionConstraint(comparator="=", version=MavenVersion(string="4.8.9")),
VersionConstraint(comparator="=", version=MavenVersion(string="4.9.12")),
VersionConstraint(comparator="=", version=MavenVersion(string="4.10.2")),
VersionConstraint(comparator="=", version=MavenVersion(string="4.12.2")),
)
)
# Invalid versions are skipped.
assert get_explicit_affected_range(
affected_pkg={"versions": ["qwqw4684", "4.10.2", "fhgj5449"]},
raw_id="GHSA-8hxh-r6f7-jf45",
supported_ecosystem="pypi",
) == PypiVersionRange(
constraints=(VersionConstraint(comparator="=", version=PypiVersion(string="4.10.2")),)
)
class TestOSVImporter(TestCase):
def test_to_advisories_github1(self):
with open(os.path.join(TEST_DATA, "github/github-1.json")) as f:
mock_response = json.load(f)
expected_file = os.path.join(TEST_DATA, "github/github-expected-1.json")
imported_data = parse_advisory_data_v3(
mock_response, "maven", advisory_url="https://test.com", advisory_text=""
)
result = imported_data.to_dict()
util_tests.check_results_against_json(result, expected_file)
def test_to_advisories_github2(self):
with open(os.path.join(TEST_DATA, "github/github-2.json")) as f:
mock_response = json.load(f)
expected_file = os.path.join(TEST_DATA, "github/github-expected-2.json")
imported_data = parse_advisory_data_v3(
mock_response, "composer", advisory_url="https://test.com", advisory_text=""
)
result = imported_data.to_dict()
util_tests.check_results_against_json(result, expected_file)
def test_to_advisories_github3(self):
with open(os.path.join(TEST_DATA, "github/github-3.json")) as f:
mock_response = json.load(f)
expected_file = os.path.join(TEST_DATA, "github/github-expected-3.json")
imported_data = parse_advisory_data_v3(
mock_response, "maven", advisory_url="https://test.com", advisory_text=""
)
result = imported_data.to_dict()
util_tests.check_results_against_json(result, expected_file)
def test_to_advisories_github4(self):
with open(os.path.join(TEST_DATA, "github/github-4.json")) as f:
mock_response = json.load(f)
expected_file = os.path.join(TEST_DATA, "github/github-expected-4.json")
imported_data = parse_advisory_data_v3(
mock_response, "cargo", advisory_url="https://test.com", advisory_text=""
)
result = imported_data.to_dict()
util_tests.check_results_against_json(result, expected_file)
def test_to_advisories_oss_fuzz1(self):
with open(os.path.join(TEST_DATA, "oss-fuzz/oss-fuzz-1.yaml")) as f:
mock_response = saneyaml.load(f)
expected_file = os.path.join(TEST_DATA, "oss-fuzz/oss-fuzz-expected-1.json")
imported_data = parse_advisory_data_v3(
mock_response, "generic", advisory_url="https://test.com", advisory_text=""
)
result = imported_data.to_dict()
util_tests.check_results_against_json(result, expected_file)
def test_to_advisories_oss_fuzz2(self):
with open(os.path.join(TEST_DATA, "oss-fuzz/oss-fuzz-2.yaml")) as f:
mock_response = saneyaml.load(f)
expected_file = os.path.join(TEST_DATA, "oss-fuzz/oss-fuzz-expected-2.json")
imported_data = parse_advisory_data_v3(
mock_response, "generic", advisory_url="https://test.com", advisory_text=""
)
result = imported_data.to_dict()
util_tests.check_results_against_json(result, expected_file)
def test_to_advisories_oss_fuzz3(self):
with open(os.path.join(TEST_DATA, "oss-fuzz/oss-fuzz-3.yaml")) as f:
mock_response = saneyaml.load(f)
expected_file = os.path.join(TEST_DATA, "oss-fuzz/oss-fuzz-expected-3.json")
imported_data = parse_advisory_data_v3(
mock_response, "generic", advisory_url="https://test.com", advisory_text=""
)
result = imported_data.to_dict()
util_tests.check_results_against_json(result, expected_file)
def test_to_advisories_pypa1(self):
with open(os.path.join(TEST_DATA, "pypa/pypa-1.yaml")) as f:
mock_response = saneyaml.load(f)
expected_file = os.path.join(TEST_DATA, "pypa/pypa-expected-1.json")
imported_data = parse_advisory_data_v3(
mock_response, "pypi", advisory_url="https://test.com", advisory_text=""
)
result = imported_data.to_dict()
util_tests.check_results_against_json(result, expected_file)
def test_to_advisories_pypa2(self):
with open(os.path.join(TEST_DATA, "pypa/pypa-2.yaml")) as f:
mock_response = saneyaml.load(f)
expected_file = os.path.join(TEST_DATA, "pypa/pypa-expected-2.json")
imported_data = parse_advisory_data_v3(
mock_response, "pypi", advisory_url="https://test.com", advisory_text=""
)
result = imported_data.to_dict()
util_tests.check_results_against_json(result, expected_file)
def test_to_advisories_pypa3(self):
with open(os.path.join(TEST_DATA, "pypa/pypa-3.yaml")) as f:
mock_response = saneyaml.load(f)
expected_file = os.path.join(TEST_DATA, "pypa/pypa-expected-3.json")
imported_data = parse_advisory_data_v3(
mock_response, "pypi", advisory_url="https://test.com", advisory_text=""
)
result = imported_data.to_dict()
util_tests.check_results_against_json(result, expected_file)
def test_to_advisories_pypa4(self):
with open(os.path.join(TEST_DATA, "pypa/pypa-4.yaml")) as f:
mock_response = saneyaml.load(f)
expected_file = os.path.join(TEST_DATA, "pypa/pypa-expected-4.json")
imported_data = parse_advisory_data_v3(
mock_response, "pypi", advisory_url="https://test.com", advisory_text=""
)
result = imported_data.to_dict()
util_tests.check_results_against_json(result, expected_file)
def test_to_advisories_pypa5(self):
with open(os.path.join(TEST_DATA, "pypa/pypa-5.yaml")) as f:
mock_response = saneyaml.load(f)
expected_file = os.path.join(TEST_DATA, "pypa/pypa-expected-5.json")
imported_data = parse_advisory_data_v3(
mock_response, "pypi", advisory_url="https://test.com", advisory_text=""
)
result = imported_data.to_dict()
util_tests.check_results_against_json(result, expected_file)
def test_to_advisories_pypa6(self):
with open(os.path.join(TEST_DATA, "pypa/pypa-6.yaml")) as f:
mock_response = saneyaml.load(f)
expected_file = os.path.join(TEST_DATA, "pypa/pypa-expected-6.json")
imported_data = parse_advisory_data_v3(
mock_response, "pypi", advisory_url="https://test.com", advisory_text=""
)
result = imported_data.to_dict()
util_tests.check_results_against_json(result, expected_file)
def test_to_advisories_pypa7(self):
with open(os.path.join(TEST_DATA, "pypa/pypa-7.yaml")) as f:
mock_response = saneyaml.load(f)
expected_file = os.path.join(TEST_DATA, "pypa/pypa-expected-7.json")
imported_data = parse_advisory_data_v3(
mock_response, "pypi", advisory_url="https://test.com", advisory_text=""
)
result = imported_data.to_dict()
util_tests.check_results_against_json(result, expected_file)