|
| 1 | +# |
| 2 | +# Copyright (c) nexB Inc. and others. All rights reserved. |
| 3 | +# http://nexb.com and https://github.com/nexB/vulnerablecode/ |
| 4 | +# The VulnerableCode software is licensed under the Apache License version 2.0. |
| 5 | +# Data generated with VulnerableCode require an acknowledgment. |
| 6 | +# |
| 7 | +# You may not use this software except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0 |
| 9 | +# Unless required by applicable law or agreed to in writing, software distributed |
| 10 | +# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 11 | +# CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 12 | +# specific language governing permissions and limitations under the License. |
| 13 | +# |
| 14 | +# When you publish or redistribute any data created with VulnerableCode or any VulnerableCode |
| 15 | +# derivative work, you must accompany this data with the following acknowledgment: |
| 16 | +# |
| 17 | +# Generated with VulnerableCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES |
| 18 | +# OR CONDITIONS OF ANY KIND, either express or implied. No content created from |
| 19 | +# VulnerableCode should be considered or used as legal advice. Consult an Attorney |
| 20 | +# for any legal advice. |
| 21 | +# VulnerableCode is a free software tool from nexB Inc. and others. |
| 22 | +# Visit https://github.com/nexB/vulnerablecode/ for support and download. |
| 23 | + |
| 24 | +import re |
| 25 | +from typing import Iterable |
| 26 | + |
| 27 | +from django.db.models import Q |
| 28 | +from django.db.models.query import QuerySet |
| 29 | + |
| 30 | +from vulnerabilities.importer import AdvisoryData |
| 31 | +from vulnerabilities.improver import Improver |
| 32 | +from vulnerabilities.improver import Inference |
| 33 | +from vulnerabilities.models import VulnerabilityReference |
| 34 | + |
| 35 | +""" |
| 36 | +Improvers that look for References without an id and tries to set one. |
| 37 | +""" |
| 38 | + |
| 39 | + |
| 40 | +class CveIdImprover(Improver): |
| 41 | + """ |
| 42 | + Add a CVE reference id when missing. |
| 43 | + Note that we only look for uppercase CVE for now |
| 44 | + """ |
| 45 | + |
| 46 | + @property |
| 47 | + def interesting_advisories(self) -> QuerySet: |
| 48 | + return VulnerabilityReference.objects.filter( |
| 49 | + Q(reference_id__isnull=True) | Q(reference_id__exact=""), |
| 50 | + url__contains="nvd.nist.gov/vuln/detail/CVE-", |
| 51 | + ) |
| 52 | + |
| 53 | + def get_inferences(self, advisory_data: AdvisoryData) -> Iterable[Inference]: |
| 54 | + cve_pattern = re.compile(r"(CVE-\d{4}-\d{4,7})").search |
| 55 | + for ref in self.interesting_advisories: |
| 56 | + cve_match = cve_pattern(ref.url) |
| 57 | + if cve_match: |
| 58 | + cve = cve_match.group() |
| 59 | + ref.reference_id = cve |
| 60 | + ref.save() |
0 commit comments