Skip to content

Commit 192504d

Browse files
authored
Merge pull request #173 from GeekMasher/depbot-alerts2
Fix CWE issues with advisories and Dependabot GraphQL issue
2 parents 9a73baa + 4fc2cfb commit 192504d

File tree

6 files changed

+28
-9
lines changed

6 files changed

+28
-9
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "ghastoolkit"
3-
version = "0.11.0"
3+
version = "0.11.1"
44
authors = [{ name = "GeekMasher" }]
55
description = "GitHub Advanced Security Python Toolkit"
66
readme = "README.md"

src/ghastoolkit/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
__name__ = "ghastoolkit"
33
__title__ = "GHAS Toolkit"
44

5-
__version__ = "0.11.0"
5+
__version__ = "0.11.1"
66

77
__description__ = "GitHub Advanced Security Python Toolkit"
88
__summary__ = """\

src/ghastoolkit/octokit/dependabot.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,14 @@ def getAlertsGraphQL(self) -> list[DependencyAlert]:
105105
"GetDependencyAlerts",
106106
options={"owner": self.repository.owner, "repo": self.repository.repo},
107107
)
108-
alerts = (
109-
data.get("data", {})
110-
.get("repository", {})
111-
.get("vulnerabilityAlerts", {})
112-
)
108+
repo = data.get("data", {}).get("repository", {})
109+
if not repo:
110+
logger.error(f"Failed to get GraphQL repository")
111+
logger.error(
112+
"This could be due to a lack of permissions or access token"
113+
)
114+
raise Exception(f"Failed to get GraphQL repository alerts")
115+
alerts = repo.get("vulnerabilityAlerts", {})
113116

114117
for alert in alerts.get("edges", []):
115118
data = alert.get("node", {})

src/ghastoolkit/octokit/octokit.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def get(
175175
self,
176176
path: str,
177177
parameters: dict = {},
178-
expected: int = 200,
178+
expected: Optional[int] = 200,
179179
authenticated: bool = False,
180180
display_errors: bool = True,
181181
) -> Union[dict, list[dict]]:
@@ -210,7 +210,7 @@ def get(
210210
response = self.session.get(url, params=params)
211211
response_json = response.json()
212212

213-
if response.status_code != expected:
213+
if expected and response.status_code != expected:
214214
if display_errors:
215215
logger.error(f"Error code from server :: {response.status_code}")
216216
logger.error(f"Content :: {response_json}")

src/ghastoolkit/supplychain/advisories.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,15 @@ def __post_init__(self):
160160
self.ghsa_id = self.ghsa_id.lower()
161161
self.severity = self.severity.lower()
162162

163+
# cwes checking and processing
164+
cwes = []
165+
for cwe in self.cwes:
166+
if isinstance(cwe, dict):
167+
cwes.append(cwe.get("cwe_id"))
168+
else:
169+
cwes.append(cwe)
170+
self.cwes = cwes
171+
163172
@staticmethod
164173
def load(path: str) -> "Advisory":
165174
"""Load Advisory from path using GitHub Advisory Spec."""

tests/test_advisories.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ def test_advisory_check(self):
3333
alert = self.advisories.check(dep)
3434
self.assertEquals(alert, [ad])
3535

36+
def test_advisory_cwes(self):
37+
ad = Advisory("rand", "high", cwes=["CWE-1234"])
38+
self.assertEquals(ad.cwes, ["CWE-1234"])
39+
40+
ad = Advisory("rand", "high", cwes=[{"cwe_id": "CWE-1234"}])
41+
self.assertEquals(ad.cwes, ["CWE-1234"])
42+
3643
def test_affect_check(self):
3744
dep = Dependency("ghastoolkit", "com.geekmasher", "0.8", "maven")
3845
affect = AdvisoryAffect(

0 commit comments

Comments
 (0)