Skip to content

Instantly share code, notes, and snippets.

@chezou
Created December 25, 2019 09:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chezou/998c1bb19a6bb6f292ba5012d02d815a to your computer and use it in GitHub Desktop.
Save chezou/998c1bb19a6bb6f292ba5012d02d815a to your computer and use it in GitHub Desktop.
GitHub SecurityVulnerabilities
import requests
class GitHubClient:
def __init__(self, apikey, session=None):
self.headers = {"Authorization": f"Bearer {apikey}"}
if not session:
self.sess = requests.Session()
else:
self.sess = session
def query(self, query):
r = self.sess.post(
"https://api.github.com/graphql",
json={"query": query},
headers=self.headers,
)
if r.status_code == 200:
return r.json()
else:
raise Exception(
f"Query failed: status code {r.status_code}\n query: {query}"
)
def format_vulnerabilities(self, vuls):
return [
{
"name": v["package"]["name"],
"severity": v["severity"],
"vulnerableVersionRange": v["vulnerableVersionRange"],
"advisory": v["advisory"],
}
for v in vuls["data"]["securityVulnerabilities"]["nodes"]
]
def fetch_vulnerabilities(self, first=50, ecosystem="PIP"):
query = f"""
{{
securityVulnerabilities(first: {first}, ecosystem: {ecosystem}, orderBy: {{field: UPDATED_AT, direction: DESC}}) {{
nodes {{
package {{
ecosystem
name
}}
severity
vulnerableVersionRange
updatedAt
advisory {{
ghsaId
publishedAt
}}
}}
}}
}}
"""
vuls = self.query(query)
return self.format_vulnerabilities(vuls)
apikey = "your api key"
cli = GitHubClient(apikey)
vuls = cli.fetch_vulnerabilities()
print(vuls)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment