forked from PyGithub/PyGithub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AdvisoryVulnerability.py
173 lines (147 loc) · 7.61 KB
/
AdvisoryVulnerability.py
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
############################ Copyrights and license ############################
# #
# Copyright 2023 Enrico Minack <github@enrico.minack.dev> #
# Copyright 2023 Jirka Borovec <6035284+Borda@users.noreply.github.com> #
# Copyright 2023 Jonathan Leitschuh <jonathan.leitschuh@gmail.com> #
# Copyright 2023 Joseph Henrich <crimsonknave@gmail.com> #
# Copyright 2023 Trim21 <trim21.me@gmail.com> #
# #
# This file is part of PyGithub. #
# http://pygithub.readthedocs.io/ #
# #
# PyGithub is free software: you can redistribute it and/or modify it under #
# the terms of the GNU Lesser General Public License as published by the Free #
# Software Foundation, either version 3 of the License, or (at your option) #
# any later version. #
# #
# PyGithub is distributed in the hope that it will be useful, but WITHOUT ANY #
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more #
# details. #
# #
# You should have received a copy of the GNU Lesser General Public License #
# along with PyGithub. If not, see <http://www.gnu.org/licenses/>. #
# #
################################################################################
from __future__ import annotations
from typing import TYPE_CHECKING, Any, Union
from typing_extensions import TypedDict
import github.AdvisoryVulnerabilityPackage
from github.GithubObject import Attribute, NonCompletableGithubObject, NotSet
if TYPE_CHECKING:
from github.AdvisoryVulnerabilityPackage import AdvisoryVulnerabilityPackage
class SimpleAdvisoryVulnerabilityPackage(TypedDict):
"""
A simple package in an advisory.
"""
ecosystem: str
name: str | None
class SimpleAdvisoryVulnerability(TypedDict):
"""
A simple vulnerability in a security advisory.
"""
package: SimpleAdvisoryVulnerabilityPackage
patched_versions: str | None
vulnerable_functions: list[str] | None
vulnerable_version_range: str | None
AdvisoryVulnerabilityInput = Union[SimpleAdvisoryVulnerability, "AdvisoryVulnerability"]
class AdvisoryVulnerability(NonCompletableGithubObject):
"""
This class represents a package that is vulnerable to a parent SecurityAdvisory.
The reference can be found here
https://docs.github.com/en/rest/security-advisories/repository-advisories
"""
@property
def package(
self,
) -> AdvisoryVulnerabilityPackage:
"""
:type: :class:`github.AdvisoryVulnerability.AdvisoryVulnerability`
"""
return self._package.value
@property
def patched_versions(self) -> str:
"""
:type: string
"""
return self._patched_versions.value
@property
def vulnerable_functions(self) -> list[str] | None:
"""
:type: list of string
"""
return self._vulnerable_functions.value
@property
def vulnerable_version_range(self) -> str | None:
"""
:type: string
"""
return self._vulnerable_version_range.value
def _initAttributes(self) -> None:
self._package: Attribute[AdvisoryVulnerabilityPackage] = NotSet
self._patched_versions: Attribute[str] = NotSet
self._vulnerable_functions: Attribute[list[str]] = NotSet
self._vulnerable_version_range: Attribute[str] = NotSet
def _useAttributes(self, attributes: dict[str, Any]) -> None:
if "package" in attributes: # pragma no branch
self._package = self._makeClassAttribute(
github.AdvisoryVulnerabilityPackage.AdvisoryVulnerabilityPackage,
attributes["package"],
)
if "patched_versions" in attributes: # pragma no branch
self._patched_versions = self._makeStringAttribute(attributes["patched_versions"])
if "vulnerable_functions" in attributes: # pragma no branch
self._vulnerable_functions = self._makeListOfStringsAttribute(attributes["vulnerable_functions"])
if "vulnerable_version_range" in attributes: # pragma no branch
self._vulnerable_version_range = self._makeStringAttribute(attributes["vulnerable_version_range"])
@classmethod
def _validate_vulnerability(cls, vulnerability: AdvisoryVulnerabilityInput) -> None:
assert isinstance(vulnerability, (dict, cls)), vulnerability
if isinstance(vulnerability, dict):
assert "package" in vulnerability, vulnerability
package: SimpleAdvisoryVulnerabilityPackage = vulnerability["package"]
assert isinstance(package, dict), package
assert "ecosystem" in package, package
assert isinstance(package["ecosystem"], str), package
assert "name" in package, package
assert isinstance(package["name"], (str, type(None))), package
assert "patched_versions" in vulnerability, vulnerability
assert isinstance(vulnerability["patched_versions"], (str, type(None))), vulnerability
assert "vulnerable_functions" in vulnerability, vulnerability
assert isinstance(vulnerability["vulnerable_functions"], (list, type(None))), vulnerability
assert "vulnerable_functions" in vulnerability, vulnerability
assert (
all(isinstance(vf, str) for vf in vulnerability["vulnerable_functions"])
if vulnerability["vulnerable_functions"] is not None
else True
), vulnerability
assert "vulnerable_version_range" in vulnerability, vulnerability
assert isinstance(vulnerability["vulnerable_version_range"], (str, type(None))), vulnerability
else:
assert (
vulnerability.package is github.AdvisoryVulnerabilityPackage.AdvisoryVulnerabilityPackage
), vulnerability
@staticmethod
def _to_github_dict(
vulnerability: AdvisoryVulnerabilityInput,
) -> SimpleAdvisoryVulnerability:
if isinstance(vulnerability, dict):
vulnerability_package: SimpleAdvisoryVulnerabilityPackage = vulnerability["package"]
return {
"package": {
"ecosystem": vulnerability_package["ecosystem"],
"name": vulnerability_package["name"],
},
"patched_versions": vulnerability["patched_versions"],
"vulnerable_functions": vulnerability["vulnerable_functions"],
"vulnerable_version_range": vulnerability["vulnerable_version_range"],
}
return {
"package": {
"ecosystem": vulnerability.package.ecosystem,
"name": vulnerability.package.name,
},
"patched_versions": vulnerability.patched_versions,
"vulnerable_functions": vulnerability.vulnerable_functions,
"vulnerable_version_range": vulnerability.vulnerable_version_range,
}