|
| 1 | +# |
| 2 | +# Copyright (c) nexB Inc. and others. All rights reserved. |
| 3 | +# VulnerableCode is a trademark of nexB Inc. |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | +# See http://www.apache.org/licenses/LICENSE-2.0 for the license text. |
| 6 | +# See https://github.com/aboutcode-org/vulnerablecode for support or download. |
| 7 | +# See https://aboutcode.org for more information about nexB OSS projects. |
| 8 | +# |
| 9 | + |
| 10 | +import re |
| 11 | +from collections import defaultdict |
| 12 | + |
| 13 | +from github import Github |
| 14 | + |
| 15 | +from vulnerabilities.importer import AdvisoryData |
| 16 | +from vulnerabilities.importer import ReferenceV2 |
| 17 | +from vulnerabilities.pipelines import VulnerableCodeBaseImporterPipelineV2 |
| 18 | +from vulnerablecode.settings import env |
| 19 | + |
| 20 | +GITHUB_TOKEN = env.str("GITHUB_TOKEN") |
| 21 | + |
| 22 | + |
| 23 | +class GithubPipelineIssuePR(VulnerableCodeBaseImporterPipelineV2): |
| 24 | + """ |
| 25 | + Pipeline to collect GitHub issues and PRs related to vulnerabilities. |
| 26 | + """ |
| 27 | + |
| 28 | + pipeline_id = "collect_issues_pr" |
| 29 | + |
| 30 | + @classmethod |
| 31 | + def steps(cls): |
| 32 | + return ( |
| 33 | + cls.fetch_entries, |
| 34 | + cls.collect_and_store_advisories, |
| 35 | + ) |
| 36 | + |
| 37 | + def fetch_entries(self): |
| 38 | + """Clone the repository.""" |
| 39 | + self.repo_url = "https://github.com/torvalds/linux" |
| 40 | + repo_name = "django/django" |
| 41 | + |
| 42 | + g = Github(login_or_token=GITHUB_TOKEN) |
| 43 | + |
| 44 | + base_query = f"repo:{repo_name} (CVE OR PYSEC OR GHSA)" |
| 45 | + self.issues = g.search_issues(f"{base_query} is:issue") |
| 46 | + self.pull_requestes = g.search_issues(f"{base_query} is:pr") |
| 47 | + |
| 48 | + def advisories_count(self) -> int: |
| 49 | + """ |
| 50 | + Return total number of advisories discovered (issues + PRs). |
| 51 | + """ |
| 52 | + return self.issues.totalCount + self.pull_requestes.totalCount |
| 53 | + |
| 54 | + def collect_issues_and_prs(self): |
| 55 | + """ |
| 56 | + Group issues and PRs by vulnerability identifiers (like CVE-xxxx-yyyy). |
| 57 | + Returns a dict mapping vuln_id -> [(type, html_url)]. |
| 58 | + """ |
| 59 | + self.log("Grouping GitHub issues and PRs by vulnerability identifiers.") |
| 60 | + |
| 61 | + grouped_items = defaultdict(list) |
| 62 | + pattern = re.compile(r"(CVE-\d{4}-\d+|PYSEC-\d{4}-\d+|GHSA-[\w-]+)", re.IGNORECASE) |
| 63 | + |
| 64 | + for issue in self.issues: |
| 65 | + matches = pattern.findall(issue.title + " " + (issue.body or "")) |
| 66 | + for match in matches: |
| 67 | + grouped_items[match].append(("Issue", issue.html_url)) |
| 68 | + |
| 69 | + for pr in self.pull_requestes: |
| 70 | + matches = pattern.findall(pr.title + " " + (pr.body or "")) |
| 71 | + for match in matches: |
| 72 | + grouped_items[match].append(("PR", pr.html_url)) |
| 73 | + |
| 74 | + self.log(f"Grouped {len(grouped_items)} unique vulnerability identifiers.") |
| 75 | + return grouped_items |
| 76 | + |
| 77 | + def collect_advisories(self): |
| 78 | + """ |
| 79 | + Generate AdvisoryData objects for each vulnerability ID grouped with its related GitHub issues and PRs. |
| 80 | + """ |
| 81 | + self.log("Generating AdvisoryData objects from GitHub issues and PRs.") |
| 82 | + grouped_data = self.collect_issues_and_prs() |
| 83 | + |
| 84 | + for vuln_id, refs in grouped_data.items(): |
| 85 | + references = [ReferenceV2(reference_id=ref_id, url=url) for ref_id, url in refs] |
| 86 | + |
| 87 | + yield AdvisoryData( |
| 88 | + advisory_id=vuln_id, |
| 89 | + aliases=[vuln_id], |
| 90 | + references_v2=references, |
| 91 | + url=self.repo_url, |
| 92 | + ) |
0 commit comments