|
| 1 | +import os |
| 2 | +import re |
| 3 | +import shutil |
| 4 | +import subprocess |
| 5 | +import tempfile |
| 6 | +from collections import defaultdict |
| 7 | + |
| 8 | +from git import Repo |
| 9 | + |
| 10 | +from vulnerabilities.importer import AdvisoryData |
| 11 | +from vulnerabilities.importer import ReferenceV2 |
| 12 | +from vulnerabilities.pipelines import VulnerableCodeBaseImporterPipelineV2 |
| 13 | + |
| 14 | +SECURITY_PATTERNS = [ |
| 15 | + r"\bCVE-\d{4}-\d{4,19}\b", |
| 16 | + r"\bGHSA-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}\b", |
| 17 | + r"\bPYSEC-\d{4}-\d{1,6}\b", |
| 18 | + r"\bXSA-\d{1,4}\b", |
| 19 | +] |
| 20 | + |
| 21 | + |
| 22 | +class CollectRepoFixCommitPipeline(VulnerableCodeBaseImporterPipelineV2): |
| 23 | + """ |
| 24 | + Pipeline to collect fix commits from any git repository. |
| 25 | + """ |
| 26 | + |
| 27 | + pipeline_id = "repo_fix_commit" |
| 28 | + |
| 29 | + @classmethod |
| 30 | + def steps(cls): |
| 31 | + return ( |
| 32 | + cls.clone, |
| 33 | + cls.collect_and_store_advisories, |
| 34 | + cls.clean_downloads, |
| 35 | + ) |
| 36 | + |
| 37 | + def clone(self): |
| 38 | + """Clone the repository.""" |
| 39 | + self.repo_url = "https://github.com/torvalds/linux" |
| 40 | + repo_path = tempfile.mkdtemp() |
| 41 | + cmd = [ |
| 42 | + "git", |
| 43 | + "clone", |
| 44 | + "--bare", |
| 45 | + "--filter=blob:none", |
| 46 | + "--no-checkout", |
| 47 | + self.repo_url, |
| 48 | + repo_path, |
| 49 | + ] |
| 50 | + subprocess.run(cmd, check=True) |
| 51 | + self.repo = Repo(repo_path) |
| 52 | + |
| 53 | + def advisories_count(self) -> int: |
| 54 | + return int(self.repo.git.rev_list("--count", "HEAD")) |
| 55 | + |
| 56 | + def classify_commit_type(self, commit) -> list[str]: |
| 57 | + """ |
| 58 | + Extract vulnerability identifiers from a commit message. |
| 59 | + Returns a list of matched vulnerability IDs (normalized to uppercase). |
| 60 | + """ |
| 61 | + matches = [] |
| 62 | + for pattern in SECURITY_PATTERNS: |
| 63 | + found = re.findall(pattern, commit.message, flags=re.IGNORECASE) |
| 64 | + matches.extend(found) |
| 65 | + return matches |
| 66 | + |
| 67 | + def collect_fix_commits(self): |
| 68 | + """ |
| 69 | + Iterate through repository commits and group them by vulnerability identifiers. |
| 70 | + return a list with (vuln_id, [(commit_id, commit_message)]). |
| 71 | + """ |
| 72 | + self.log("Processing git repository fix commits (grouped by vulnerability IDs).") |
| 73 | + |
| 74 | + grouped_commits = defaultdict(list) |
| 75 | + for commit in self.repo.iter_commits("--all"): |
| 76 | + matched_ids = self.classify_commit_type(commit) |
| 77 | + if not matched_ids: |
| 78 | + continue |
| 79 | + |
| 80 | + commit_id = commit.hexsha |
| 81 | + commit_message = commit.message.strip() |
| 82 | + |
| 83 | + for vuln_id in matched_ids: |
| 84 | + grouped_commits[vuln_id].append((commit_id, commit_message)) |
| 85 | + |
| 86 | + self.log(f"Found {len(grouped_commits)} vulnerabilities with related commits.") |
| 87 | + self.log("Finished processing all commits.") |
| 88 | + return grouped_commits |
| 89 | + |
| 90 | + def collect_advisories(self): |
| 91 | + """ |
| 92 | + Generate AdvisoryData objects for each vulnerability ID grouped with its related commits. |
| 93 | + """ |
| 94 | + self.log("Generating AdvisoryData objects from grouped commits.") |
| 95 | + grouped_commits = self.collect_fix_commits() |
| 96 | + for vuln_id, commits in grouped_commits.items(): |
| 97 | + references = [ReferenceV2(url=f"{self.repo_url}/commit/{cid}") for cid, _ in commits] |
| 98 | + |
| 99 | + summary_lines = [f"- {cid}: {msg}" for cid, msg in commits] |
| 100 | + summary = f"Commits fixing {vuln_id}:\n" + "\n".join(summary_lines) |
| 101 | + yield AdvisoryData( |
| 102 | + advisory_id=vuln_id, |
| 103 | + aliases=[vuln_id], |
| 104 | + summary=summary, |
| 105 | + references_v2=references, |
| 106 | + url=self.repo_url, |
| 107 | + ) |
| 108 | + |
| 109 | + def clean_downloads(self): |
| 110 | + """Cleanup any temporary repository data.""" |
| 111 | + self.log("Cleaning up local repository resources.") |
| 112 | + if os.path.isdir(self.repo.working_tree_dir): |
| 113 | + shutil.rmtree(path=self.repo.working_tree_dir) |
| 114 | + |
| 115 | + def on_failure(self): |
| 116 | + """Ensure cleanup is always performed on failure.""" |
| 117 | + self.clean_downloads() |
0 commit comments