Skip to content

Commit 169a6a1

Browse files
committed
Add support for version range from snyk advisory
Signed-off-by: Keshav Priyadarshi <git@keshav.space>
1 parent d109a1b commit 169a6a1

1 file changed

Lines changed: 85 additions & 5 deletions

File tree

src/univers/version_range.py

Lines changed: 85 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
#
55
# Visit https://aboutcode.org and https://github.com/nexB/univers for support and download.
66

7+
from typing import List
8+
from typing import Union
9+
710
import attr
811
import semantic_version
912
from packaging.specifiers import InvalidSpecifier
@@ -1222,7 +1225,7 @@ def build_constraint_from_github_advisory_string(scheme: str, string: str):
12221225
return VersionConstraint(comparator=comparator, version=version)
12231226

12241227

1225-
def build_range_from_github_advisory_constraint(scheme: str, string: str):
1228+
def build_range_from_github_advisory_constraint(scheme: str, string: Union[str, List]):
12261229
"""
12271230
Github has a special syntax for version ranges.
12281231
For example:
@@ -1231,7 +1234,7 @@ def build_range_from_github_advisory_constraint(scheme: str, string: str):
12311234
Github native version range looks like:
12321235
``>= 1.0.0, < 1.0.1``
12331236
1234-
Return a VersionRange built from a ``string`` single github-native
1237+
Return a VersionRange built from a ``string`` single or multiple github-native
12351238
version relationship string.
12361239
12371240
For example::
@@ -1245,14 +1248,91 @@ def build_range_from_github_advisory_constraint(scheme: str, string: str):
12451248
>>> vr = build_range_from_github_advisory_constraint("pypi","= 9.0")
12461249
>>> assert str(vr) == "vers:pypi/9.0"
12471250
"""
1248-
constraint_strings = string.split(",")
1251+
if isinstance(string, str):
1252+
string = [string]
1253+
12491254
constraints = []
12501255
vrc = RANGE_CLASS_BY_SCHEMES[scheme]
1251-
for constraint in constraint_strings:
1252-
constraints.append(build_constraint_from_github_advisory_string(scheme, constraint))
1256+
for item in string:
1257+
constraint_strings = item.split(",")
1258+
1259+
for constraint in constraint_strings:
1260+
constraints.append(build_constraint_from_github_advisory_string(scheme, constraint))
12531261
return vrc(constraints=constraints)
12541262

12551263

1264+
vers_by_snyk_native_comparators = {
1265+
"==": "=",
1266+
"=": "=",
1267+
"!=": "!=",
1268+
"<=": "<=",
1269+
">=": ">=",
1270+
"<": "<",
1271+
">": ">",
1272+
"(": ">",
1273+
"[": ">=",
1274+
}
1275+
1276+
vers_by_snyk_native_comparators_rear = {
1277+
")": "<",
1278+
"]": "<=",
1279+
}
1280+
1281+
1282+
def build_range_from_snyk_advisory_string(scheme: str, string: Union[str, List]):
1283+
"""
1284+
Return a VersionRange built from a ``string`` single or multiple snyk
1285+
version relationship string.
1286+
Snyk version range looks like:
1287+
">=4.0.0, <4.0.10.16"
1288+
">=4.1.0 <4.4.15.7"
1289+
"[3.0.0,3.1.25)"
1290+
"(,9.21]"
1291+
"[1.4.5,)"
1292+
1293+
For example::
1294+
1295+
>>> vr = build_range_from_snyk_advisory_string("pypi", ">=4.0.0, <4.0.10")
1296+
>>> assert str(vr) == "vers:pypi/>=4.0.0|<4.0.10"
1297+
>>> vr = build_range_from_snyk_advisory_string("composer", ">=4.1.0 <4.4.15.7")
1298+
>>> assert str(vr) == "vers:composer/>=4.1.0|<4.4.15.7"
1299+
>>> vr = build_range_from_snyk_advisory_string("pypi", "(,9.21]")
1300+
>>> assert str(vr) == "vers:pypi/<=9.21"
1301+
"""
1302+
# https://security.snyk.io/package/golang/github.com%2Fmattermost%2Fmattermost%2Fserver%2Fpublic%2Fmodel
1303+
# >=9.6.0-rc1 <9.8.1-rc1
1304+
version_constraints = []
1305+
vrc = RANGE_CLASS_BY_SCHEMES[scheme]
1306+
1307+
if isinstance(string, str):
1308+
string = [string]
1309+
1310+
for item in string:
1311+
delimiter = "," if "," in item else " "
1312+
if delimiter == ",":
1313+
snyk_constraints = item.strip().replace(" ", "")
1314+
constraints = snyk_constraints.split(",")
1315+
else:
1316+
snyk_constraints = item.strip()
1317+
constraints = snyk_constraints.split(" ")
1318+
1319+
for constraint in constraints:
1320+
comparator, version = split_req(
1321+
string=constraint,
1322+
comparators=vers_by_snyk_native_comparators,
1323+
comparators_rear=vers_by_snyk_native_comparators_rear,
1324+
)
1325+
if comparator and version:
1326+
version = vrc.version_class(version)
1327+
version_constraints.append(
1328+
VersionConstraint(
1329+
comparator=comparator,
1330+
version=version,
1331+
)
1332+
)
1333+
return vrc(constraints=version_constraints)
1334+
1335+
12561336
RANGE_CLASS_BY_SCHEMES = {
12571337
"npm": NpmVersionRange,
12581338
"deb": DebianVersionRange,

0 commit comments

Comments
 (0)