Skip to content

Commit 8d42af1

Browse files
committed
add vulntotal utils
Signed-off-by: Keshav Priyadarshi <[email protected]>
1 parent 1724d91 commit 8d42af1

File tree

1 file changed

+134
-0
lines changed

1 file changed

+134
-0
lines changed

vulntotal/vulntotal_utils.py

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#
2+
# Copyright (c) nexB Inc. and others. All rights reserved.
3+
# http://nexb.com and https://github.com/nexB/vulnerablecode/
4+
# The VulnTotal software is licensed under the Apache License version 2.0.
5+
# Data generated with VulnTotal require an acknowledgment.
6+
#
7+
# You may not use this software except in compliance with the License.
8+
# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0
9+
# Unless required by applicable law or agreed to in writing, software distributed
10+
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
11+
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
12+
# specific language governing permissions and limitations under the License.
13+
#
14+
# When you publish or redistribute any data created with VulnTotal or any VulnTotal
15+
# derivative work, you must accompany this data with the following acknowledgment:
16+
#
17+
# Generated with VulnTotal and provided on an "AS IS" BASIS, WITHOUT WARRANTIES
18+
# OR CONDITIONS OF ANY KIND, either express or implied. No content created from
19+
# VulnTotal should be considered or used as legal advice. Consult an Attorney
20+
# for any legal advice.
21+
# VulnTotal is a free software tool from nexB Inc. and others.
22+
# Visit https://github.com/nexB/vulnerablecode/ for support and download.
23+
24+
import operator
25+
26+
27+
class GenericVersion:
28+
def __init__(self, version):
29+
self.value = version.replace(" ", "").lstrip("v")
30+
31+
self.decomposed = tuple(
32+
[int(com) if com.isnumeric() else com for com in self.value.split(".")]
33+
)
34+
35+
def __str__(self):
36+
return str(self.value)
37+
38+
def __eq__(self, other):
39+
if not isinstance(other, self.__class__):
40+
return NotImplemented
41+
return self.value.__eq__(other.value)
42+
43+
def __lt__(self, other):
44+
if not isinstance(other, self.__class__):
45+
return NotImplemented
46+
for i, j in zip(self.decomposed, other.decomposed):
47+
if not isinstance(i, type(j)):
48+
continue
49+
if i.__gt__(j):
50+
return False
51+
return True
52+
53+
def __le__(self, other):
54+
if not isinstance(other, self.__class__):
55+
return NotImplemented
56+
return self.__lt__(other) or self.__eq__(other)
57+
58+
59+
def compare(version, package_comparator, package_version):
60+
operator_comparator = {
61+
"<": operator.lt,
62+
">": operator.gt,
63+
"=": operator.eq,
64+
"<=": operator.le,
65+
">=": operator.ge,
66+
"==": operator.eq,
67+
"!=": operator.ne,
68+
")": operator.lt,
69+
"]": operator.le,
70+
"(": operator.gt,
71+
"[": operator.ge,
72+
}
73+
compare = operator_comparator[package_comparator]
74+
return compare(version, package_version)
75+
76+
77+
def parse_constraint(constraint):
78+
if constraint.startswith(("<=", ">=", "==", "!=")):
79+
return constraint[:2], constraint[2:]
80+
81+
if constraint.startswith(("<", ">", "=", "[", "]", "(", ")")):
82+
return constraint[0], constraint[1:]
83+
84+
if constraint.endswith(("[", "]", "(", ")")):
85+
return constraint[-1], constraint[:-1]
86+
87+
88+
def github_constraints_satisfied(github_constrain, version):
89+
gh_constraints = github_constrain.strip().replace(" ", "")
90+
constraints = gh_constraints.split(",")
91+
for constraint in constraints:
92+
gh_comparator, gh_version = parse_constraint(constraint)
93+
if not gh_version:
94+
continue
95+
# TODO: Replace the GenericVersion with ecosystem specific from univers
96+
if not compare(GenericVersion(version), gh_comparator, GenericVersion(gh_version)):
97+
return False
98+
return True
99+
100+
101+
def snky_constraints_satisfied(snyk_constrain, version):
102+
snyk_constraints = snyk_constrain.strip().replace(" ", "")
103+
constraints = snyk_constraints.split(",")
104+
for constraint in constraints:
105+
snyk_comparator, snyk_version = parse_constraint(constraint)
106+
if not snyk_version:
107+
continue
108+
# TODO: Replace the GenericVersion with ecosystem specific from univers or maybe not if snyk is normalizing versions to semver
109+
if not compare(GenericVersion(version), snyk_comparator, GenericVersion(snyk_version)):
110+
return False
111+
return True
112+
113+
114+
def gitlab_constraints_satisfied(gitlab_constrain, version):
115+
gitlab_constraints = gitlab_constrain.strip()
116+
constraints = gitlab_constraints.split("||")
117+
118+
for constraint in constraints:
119+
is_constraint_satisfied = True
120+
121+
for subcontraint in constraint.strip().split(" "):
122+
123+
gitlab_comparator, gitlab_version = parse_constraint(subcontraint.strip())
124+
if not gitlab_version:
125+
continue
126+
# TODO: Replace the GenericVersion with ecosystem specific from univers
127+
if not compare(
128+
GenericVersion(version), gitlab_comparator, GenericVersion(gitlab_version)
129+
):
130+
is_constraint_satisfied = False
131+
break
132+
133+
if is_constraint_satisfied:
134+
return True

0 commit comments

Comments
 (0)