|
| 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 | +from vulnerabilities.models import PackageCommitPatch, Patch |
| 11 | +from vulnerabilities.pipelines import VulnerableCodePipeline |
| 12 | +from vulnerabilities.utils import fetch_response |
| 13 | + |
| 14 | + |
| 15 | +class FetchPatchURLImproverPipeline(VulnerableCodePipeline): |
| 16 | + """FetchPatchURL Improver Pipeline""" |
| 17 | + |
| 18 | + pipeline_id = "fetch_patch_url" |
| 19 | + precedence = 200 |
| 20 | + |
| 21 | + @classmethod |
| 22 | + def steps(cls): |
| 23 | + return ( |
| 24 | + cls.collect_patch_text, |
| 25 | + ) |
| 26 | + |
| 27 | + def fetch_patch_content(self, url): |
| 28 | + """ |
| 29 | + Fetches the text content of a patch from a URL. |
| 30 | + """ |
| 31 | + if not url: |
| 32 | + return None |
| 33 | + |
| 34 | + self.log(f"Fetching `{url}`") |
| 35 | + |
| 36 | + response = fetch_response(url) |
| 37 | + if response: |
| 38 | + return response.text.replace("\x00", "") |
| 39 | + |
| 40 | + self.log(f"Skipping {url} due to fetch failure.") |
| 41 | + return None |
| 42 | + |
| 43 | + def advisories_count(self) -> int: |
| 44 | + return ( |
| 45 | + PackageCommitPatch.objects.filter(patch_text__isnull=True).count() + |
| 46 | + Patch.objects.filter(patch_text__isnull=True).count() |
| 47 | + ) |
| 48 | + |
| 49 | + def collect_patch_text(self): |
| 50 | + for pcp in PackageCommitPatch.objects.filter(patch_text__isnull=True): |
| 51 | + patch_url = generate_patch_url(pcp.vcs_url, pcp.commit_hash) |
| 52 | + content = self.fetch_patch_content(patch_url) |
| 53 | + if not content: |
| 54 | + continue |
| 55 | + pcp.patch_text = content |
| 56 | + pcp.save() |
| 57 | + |
| 58 | + for patch in Patch.objects.filter(patch_text__isnull=True): |
| 59 | + content = self.fetch_patch_content(patch.patch_url) |
| 60 | + if not content: |
| 61 | + continue |
| 62 | + |
| 63 | + patch.patch_text = content |
| 64 | + patch.save() |
| 65 | + |
| 66 | +def generate_patch_url(vcs_url, commit_hash): |
| 67 | + """ |
| 68 | + Generate patch URL from VCS URL and commit hash. |
| 69 | + """ |
| 70 | + if not vcs_url or not commit_hash: |
| 71 | + return None |
| 72 | + |
| 73 | + vcs_url = vcs_url.rstrip("/") |
| 74 | + |
| 75 | + if vcs_url.startswith("https://github.com"): |
| 76 | + return f"{vcs_url}/commit/{commit_hash}.patch" |
| 77 | + elif vcs_url.startswith("https://gitlab.com"): |
| 78 | + return f"{vcs_url}/-/commit/{commit_hash}.patch" |
| 79 | + elif vcs_url.startswith("https://bitbucket.org"): |
| 80 | + return f"{vcs_url}/-/commit/{commit_hash}/raw" |
| 81 | + elif vcs_url.startswith("https://git.kernel.org"): |
| 82 | + return f"{vcs_url}.git/patch/?id={commit_hash}" |
| 83 | + return |
0 commit comments