|
| 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/nexB/vulnerablecode for support or download. |
| 7 | +# See https://aboutcode.org for more information about nexB OSS projects. |
| 8 | +# |
| 9 | + |
| 10 | +import logging |
| 11 | +import re |
| 12 | + |
| 13 | +from django.db import transaction |
| 14 | +from django.db.models.query import QuerySet |
| 15 | + |
| 16 | +from vulnerabilities.improver import Improver |
| 17 | +from vulnerabilities.models import Commit |
| 18 | +from vulnerabilities.models import VulnerabilityReference |
| 19 | + |
| 20 | +logger = logging.getLogger(__name__) |
| 21 | + |
| 22 | +""" |
| 23 | +Improver that looks for commits related to a vulnerability |
| 24 | +""" |
| 25 | + |
| 26 | + |
| 27 | +class CommitRelationImprover(Improver): |
| 28 | + """ |
| 29 | + Detect related commits to an advisory by applying a REGEX. |
| 30 | + """ |
| 31 | + |
| 32 | + def __init__(self): |
| 33 | + # using cached insertion for memory efficiency |
| 34 | + self.insert_chunk_size = 500 |
| 35 | + self.commit_instances = [] |
| 36 | + |
| 37 | + @property |
| 38 | + def is_custom_improver(cls): |
| 39 | + return True |
| 40 | + |
| 41 | + @property |
| 42 | + def interesting_references(self) -> QuerySet: |
| 43 | + # Regex base coming from: https://github.com/secureIT-project/CVEfixes/ |
| 44 | + # Below regex is the compatible form for Postgresql |
| 45 | + # For now, we are only interested in Bitbucket, Github and Gitlab sources |
| 46 | + # TODO: Add other sources such as Apache related sources, Linux kernel, etc. |
| 47 | + git_url = r"((https|http)://(bitbucket|github|gitlab)\.(org|com)/([^/]+)/([^/]*))/(commit|commits)/(\w+)#?" |
| 48 | + return VulnerabilityReference.objects.filter( |
| 49 | + url__iregex=git_url, |
| 50 | + ) |
| 51 | + |
| 52 | + def __generate_instance(self): |
| 53 | + commit_pattern = r"(((?P<repo>(https|http):\/\/(bitbucket|github|gitlab)\.(org|com)\/(?P<owner>[^\/]+)\/(?P<project>[^\/]*))\/(commit|commits)\/(?P<hash>\w+)#?)+)" |
| 54 | + for ref in self.interesting_references: |
| 55 | + commit_groups = re.search(commit_pattern, ref.url) |
| 56 | + yield Commit( |
| 57 | + reference=ref, |
| 58 | + hash=commit_groups.group("hash"), |
| 59 | + ) |
| 60 | + |
| 61 | + def __insert_bulk(self) -> None: |
| 62 | + if len(self.commit_instances) == 0: |
| 63 | + return |
| 64 | + |
| 65 | + with transaction.atomic(): |
| 66 | + # Ignore_conflicts allows mass |
| 67 | + Commit.objects.bulk_create(self.commit_instances, ignore_conflicts=True) |
| 68 | + |
| 69 | + # Empty the cache buffer further inserts |
| 70 | + self.commit_instances.clear() |
| 71 | + |
| 72 | + def run(self) -> None: |
| 73 | + for i, commit in enumerate(self.__generate_instance()): |
| 74 | + self.commit_instances.append(commit) |
| 75 | + if len(self.commit_instances) >= self.insert_chunk_size: |
| 76 | + self.__insert_bulk() |
| 77 | + # Add remaining commits |
| 78 | + self.__insert_bulk() |
0 commit comments