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 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
|
############################ 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> #
# Copyright 2024 Enrico Minack <github@enrico.minack.dev> #
# Copyright 2024 Jirka Borovec <6035284+Borda@users.noreply.github.com> #
# Copyright 2024 Thomas Cooper <coopernetes@proton.me> #
# Copyright 2025 Enrico Minack <github@enrico.minack.dev> #
# #
# 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 datetime import datetime
from typing import TYPE_CHECKING, Any, Iterable
import github.AdvisoryCredit
import github.AdvisoryCreditDetailed
import github.AdvisoryVulnerability
import github.NamedUser
import github.Repository
import github.Team
from github.AdvisoryBase import AdvisoryBase
from github.GithubObject import Attribute, NotSet, Opt
if TYPE_CHECKING:
from github.AdvisoryCredit import AdvisoryCredit, Credit
from github.AdvisoryCreditDetailed import AdvisoryCreditDetailed
from github.AdvisoryVulnerability import AdvisoryVulnerability, AdvisoryVulnerabilityInput
from github.NamedUser import NamedUser
from github.Repository import Repository
from github.Team import Team
class RepositoryAdvisory(AdvisoryBase):
"""
This class represents a RepositoryAdvisory.
The reference can be found here
https://docs.github.com/en/rest/security-advisories/repository-advisories
The OpenAPI schema can be found at
- /components/schemas/repository-advisory
"""
def _initAttributes(self) -> None:
super()._initAttributes()
self._author: Attribute[NamedUser] = NotSet
self._closed_at: Attribute[datetime] = NotSet
self._collaborating_teams: Attribute[list[Team]] = NotSet
self._collaborating_users: Attribute[list[NamedUser]] = NotSet
self._created_at: Attribute[datetime] = NotSet
self._credits: Attribute[list[AdvisoryCredit]] = NotSet
self._credits_detailed: Attribute[list[AdvisoryCreditDetailed]] = NotSet
self._cwe_ids: Attribute[list[str]] = NotSet
self._private_fork: Attribute[Repository] = NotSet
self._publisher: Attribute[NamedUser] = NotSet
self._state: Attribute[str] = NotSet
self._submission: Attribute[dict[str, Any]] = NotSet
self._vulnerabilities: Attribute[list[AdvisoryVulnerability]] = NotSet
super()._initAttributes()
@property
def author(self) -> NamedUser:
return self._author.value
@property
def closed_at(self) -> datetime:
return self._closed_at.value
@property
def collaborating_teams(self) -> list[Team]:
return self._collaborating_teams.value
@property
def collaborating_users(self) -> list[NamedUser]:
return self._collaborating_users.value
@property
def created_at(self) -> datetime:
return self._created_at.value
@property
def credits(
self,
) -> list[AdvisoryCredit]:
return self._credits.value
@property
def credits_detailed(
self,
) -> list[AdvisoryCreditDetailed]:
return self._credits_detailed.value
@property
def cwe_ids(self) -> list[str]:
return self._cwe_ids.value
@property
def private_fork(self) -> Repository:
return self._private_fork.value
@property
def publisher(self) -> NamedUser:
return self._publisher.value
@property
def state(self) -> str:
return self._state.value
@property
def submission(self) -> dict[str, Any]:
return self._submission.value
@property
def vulnerabilities(self) -> list[AdvisoryVulnerability]:
return self._vulnerabilities.value
def add_vulnerability(
self,
ecosystem: str,
package_name: str | None = None,
vulnerable_version_range: str | None = None,
patched_versions: str | None = None,
vulnerable_functions: list[str] | None = None,
) -> None:
"""
:calls: `PATCH /repos/{owner}/{repo}/security-advisories/:advisory_id <https://docs.github.com/en/rest/security-advisories/repository-advisories>`\
"""
return self.add_vulnerabilities(
[
{
"package": {
"ecosystem": ecosystem,
"name": package_name,
},
"vulnerable_version_range": vulnerable_version_range,
"patched_versions": patched_versions,
"vulnerable_functions": vulnerable_functions,
}
]
)
def add_vulnerabilities(self, vulnerabilities: Iterable[AdvisoryVulnerabilityInput]) -> None:
"""
:calls: `PATCH /repos/{owner}/{repo}/security-advisories/:advisory_id <https://docs.github.com/en/rest/security-advisories/repository-advisories>`
"""
assert isinstance(vulnerabilities, Iterable), vulnerabilities
for vulnerability in vulnerabilities:
github.AdvisoryVulnerability.AdvisoryVulnerability._validate_vulnerability(vulnerability)
post_parameters = {
"vulnerabilities": [
github.AdvisoryVulnerability.AdvisoryVulnerability._to_github_dict(vulnerability)
for vulnerability in (self.vulnerabilities + list(vulnerabilities))
]
}
headers, data = self._requester.requestJsonAndCheck(
"PATCH",
self.url,
input=post_parameters,
)
self._useAttributes(data)
def offer_credit(
self,
login_or_user: str | github.NamedUser.NamedUser,
credit_type: str,
) -> None:
"""
Offers credit to a user for a vulnerability in a repository.
Unless you are giving credit to yourself, the user having credit offered will need to explicitly accept the credit.
:calls: `PATCH /repos/{owner}/{repo}/security-advisories/:advisory_id <https://docs.github.com/en/rest/security-advisories/repository-advisories>`
"""
self.offer_credits([{"login": login_or_user, "type": credit_type}])
def offer_credits(
self,
credited: Iterable[Credit],
) -> None:
"""
Offers credit to a list of users for a vulnerability in a repository.
Unless you are giving credit to yourself, the user having credit offered will need to explicitly accept the credit.
:calls: `PATCH /repos/{owner}/{repo}/security-advisories/:advisory_id <https://docs.github.com/en/rest/security-advisories/repository-advisories>`
:param credited: iterable of dict with keys "login" and "type"
"""
assert isinstance(credited, Iterable), credited
for credit in credited:
github.AdvisoryCredit.AdvisoryCredit._validate_credit(credit)
patch_parameters = {
"credits": [
github.AdvisoryCredit.AdvisoryCredit._to_github_dict(credit)
for credit in (self.credits + list(credited))
]
}
headers, data = self._requester.requestJsonAndCheck(
"PATCH",
self.url,
input=patch_parameters,
)
self._useAttributes(data)
def revoke_credit(self, login_or_user: str | github.NamedUser.NamedUser) -> None:
"""
:calls: `PATCH /repos/{owner}/{repo}/security-advisories/:advisory_id <https://docs.github.com/en/rest/security-advisories/repository-advisories>`_
"""
assert isinstance(login_or_user, (str, github.NamedUser.NamedUser)), login_or_user
if isinstance(login_or_user, github.NamedUser.NamedUser):
login_or_user = login_or_user.login
patch_parameters = {
"credits": [
dict(login=credit.login, type=credit.type) for credit in self.credits if credit.login != login_or_user
]
}
headers, data = self._requester.requestJsonAndCheck(
"PATCH",
self.url,
input=patch_parameters,
)
self._useAttributes(data)
def clear_credits(self) -> None:
"""
:calls: `PATCH /repos/{owner}/{repo}/security-advisories/:advisory_id <https://docs.github.com/en/rest/security-advisories/repository-advisories>`_
"""
patch_parameters: dict[str, Any] = {"credits": []}
headers, data = self._requester.requestJsonAndCheck(
"PATCH",
self.url,
input=patch_parameters,
)
self._useAttributes(data)
def edit(
self,
summary: Opt[str] = NotSet,
description: Opt[str] = NotSet,
severity_or_cvss_vector_string: Opt[str] = NotSet,
cve_id: Opt[str] = NotSet,
vulnerabilities: Opt[Iterable[AdvisoryVulnerabilityInput]] = NotSet,
cwe_ids: Opt[Iterable[str]] = NotSet,
credits: Opt[Iterable[Credit]] = NotSet,
state: Opt[str] = NotSet,
) -> RepositoryAdvisory:
"""
:calls: `PATCH /repos/{owner}/{repo}/security-advisories/:advisory_id <https://docs.github.com/en/rest/security-advisories/repository-advisories>`_
"""
assert summary is NotSet or isinstance(summary, str), summary
assert description is NotSet or isinstance(description, str), description
assert severity_or_cvss_vector_string is NotSet or isinstance(
severity_or_cvss_vector_string, str
), severity_or_cvss_vector_string
assert cve_id is NotSet or isinstance(cve_id, str), cve_id
assert vulnerabilities is NotSet or isinstance(vulnerabilities, Iterable), vulnerabilities
if isinstance(vulnerabilities, Iterable):
for vulnerability in vulnerabilities:
github.AdvisoryVulnerability.AdvisoryVulnerability._validate_vulnerability(vulnerability)
assert cwe_ids is NotSet or (
isinstance(cwe_ids, Iterable) and all(isinstance(element, str) for element in cwe_ids)
), cwe_ids
if isinstance(credits, Iterable):
for credit in credits:
github.AdvisoryCredit.AdvisoryCredit._validate_credit(credit)
assert state is NotSet or isinstance(state, str), state
patch_parameters: dict[str, Any] = {}
if summary is not NotSet:
patch_parameters["summary"] = summary
if description is not NotSet:
patch_parameters["description"] = description
if isinstance(severity_or_cvss_vector_string, str):
if severity_or_cvss_vector_string.startswith("CVSS:"):
patch_parameters["cvss_vector_string"] = severity_or_cvss_vector_string
else:
patch_parameters["severity"] = severity_or_cvss_vector_string
if cve_id is not NotSet:
patch_parameters["cve_id"] = cve_id
if isinstance(vulnerabilities, Iterable):
patch_parameters["vulnerabilities"] = [
github.AdvisoryVulnerability.AdvisoryVulnerability._to_github_dict(vulnerability)
for vulnerability in vulnerabilities
]
if isinstance(cwe_ids, Iterable):
patch_parameters["cwe_ids"] = list(cwe_ids)
if isinstance(credits, Iterable):
patch_parameters["credits"] = [
github.AdvisoryCredit.AdvisoryCredit._to_github_dict(credit) for credit in credits
]
if state is not NotSet:
patch_parameters["state"] = state
headers, data = self._requester.requestJsonAndCheck(
"PATCH",
self.url,
input=patch_parameters,
)
self._useAttributes(data)
return self
def accept_report(self) -> None:
"""
Accepts the advisory reported from an external reporter via private vulnerability reporting.
:calls: `PATCH /repos/{owner}/{repo}/security-advisories/:advisory_id <https://docs.github.com/en/rest/security-advisories/repository-advisories>`
"""
patch_parameters = {"state": "draft"}
headers, data = self._requester.requestJsonAndCheck(
"PATCH",
self.url,
input=patch_parameters,
)
self._useAttributes(data)
def publish(self) -> None:
"""
Publishes the advisory.
:calls: `PATCH /repos/{owner}/{repo}/security-advisories/:advisory_id <https://docs.github.com/en/rest/security-advisories/repository-advisories>`
"""
patch_parameters = {"state": "published"}
headers, data = self._requester.requestJsonAndCheck(
"PATCH",
self.url,
input=patch_parameters,
)
self._useAttributes(data)
def request_cve(self) -> None:
"""
Requests a CVE for the advisory.
:calls: `POST /repos/{owner}/{repo}/security-advisories/{ghsa_id}/cve <https://docs.github.com/en/rest/security-advisories/repository-advisories#request-a-cve-for-a-repository-security-advisory>`_
"""
self._requester.requestJsonAndCheck(
"POST",
self.url + "/cve",
)
def close(self) -> None:
"""
Closes the advisory.
:calls: `PATCH /repos/{owner}/{repo}/security-advisories/:advisory_id <https://docs.github.com/en/rest/security-advisories/repository-advisories>`
"""
patch_parameters = {"state": "closed"}
headers, data = self._requester.requestJsonAndCheck(
"PATCH",
self.url,
input=patch_parameters,
)
self._useAttributes(data)
def _useAttributes(self, attributes: dict[str, Any]) -> None:
if "author" in attributes: # pragma no branch
self._author = self._makeClassAttribute(github.NamedUser.NamedUser, attributes["author"])
if "closed_at" in attributes: # pragma no branch
assert attributes["closed_at"] is None or isinstance(attributes["closed_at"], str), attributes["closed_at"]
self._closed_at = self._makeDatetimeAttribute(attributes["closed_at"])
if "collaborating_teams" in attributes: # pragma no branch
self._collaborating_teams = self._makeListOfClassesAttribute(
github.Team.Team, attributes["collaborating_teams"]
)
if "collaborating_users" in attributes: # pragma no branch
self._collaborating_users = self._makeListOfClassesAttribute(
github.NamedUser.NamedUser, attributes["collaborating_users"]
)
if "created_at" in attributes: # pragma no branch
assert attributes["created_at"] is None or isinstance(attributes["created_at"], str), attributes[
"created_at"
]
self._created_at = self._makeDatetimeAttribute(attributes["created_at"])
if "credits" in attributes: # pragma no branch
self._credits = self._makeListOfClassesAttribute(
github.AdvisoryCredit.AdvisoryCredit,
attributes["credits"],
)
if "credits_detailed" in attributes: # pragma no branch
self._credits_detailed = self._makeListOfClassesAttribute(
github.AdvisoryCreditDetailed.AdvisoryCreditDetailed,
attributes["credits_detailed"],
)
if "cwe_ids" in attributes: # pragma no branch
self._cwe_ids = self._makeListOfStringsAttribute(attributes["cwe_ids"])
if "private_fork" in attributes: # pragma no branch
self._private_fork = self._makeClassAttribute(github.Repository.Repository, attributes["private_fork"])
if "publisher" in attributes: # pragma no branch
self._publisher = self._makeClassAttribute(github.NamedUser.NamedUser, attributes["publisher"])
if "state" in attributes: # pragma no branch
self._state = self._makeStringAttribute(attributes["state"])
if "submission" in attributes: # pragma no branch
self._submission = self._makeDictAttribute(attributes["submission"])
if "vulnerabilities" in attributes:
self._vulnerabilities = self._makeListOfClassesAttribute(
github.AdvisoryVulnerability.AdvisoryVulnerability,
attributes["vulnerabilities"],
)
super()._useAttributes(attributes)
|