Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame^] | 1 | # Checks related to the patch's CVE lines |
| 2 | # |
| 3 | # Copyright (C) 2016 Intel Corporation |
| 4 | # |
| 5 | # SPDX-License-Identifier: GPL-2.0-only |
| 6 | # |
| 7 | |
| 8 | import base |
| 9 | import parse_signed_off_by |
| 10 | import parse_upstream_status |
| 11 | import pyparsing |
| 12 | |
| 13 | class TestPatch(base.Base): |
| 14 | |
| 15 | re_cve_pattern = pyparsing.Regex("CVE\-\d{4}\-\d+") |
| 16 | re_cve_payload_tag = pyparsing.Regex("\+CVE:(\s+CVE\-\d{4}\-\d+)+") |
| 17 | upstream_status_regex = pyparsing.AtLineStart("+" + "Upstream-Status") |
| 18 | |
| 19 | @classmethod |
| 20 | def setUpClassLocal(cls): |
| 21 | cls.newpatches = [] |
| 22 | # get just those relevant patches: new software patches |
| 23 | for patch in cls.patchset: |
| 24 | if patch.path.endswith('.patch') and patch.is_added_file: |
| 25 | cls.newpatches.append(patch) |
| 26 | |
| 27 | cls.mark = str(parse_signed_off_by.signed_off_by_mark).strip('"') |
| 28 | |
| 29 | # match PatchSignedOffBy.mark with '+' preceding it |
| 30 | cls.prog = parse_signed_off_by.patch_signed_off_by |
| 31 | |
| 32 | def setUp(self): |
| 33 | if self.unidiff_parse_error: |
| 34 | self.skip('Parse error %s' % self.unidiff_parse_error) |
| 35 | |
| 36 | self.valid_status = ', '.join(parse_upstream_status.upstream_status_nonliteral_valid_status) |
| 37 | self.standard_format = 'Upstream-Status: <Valid status>' |
| 38 | |
| 39 | # we are just interested in series that introduce CVE patches, thus discard other |
| 40 | # possibilities: modification to current CVEs, patch directly introduced into the |
| 41 | # recipe, upgrades already including the CVE, etc. |
| 42 | new_cves = [p for p in self.patchset if p.path.endswith('.patch') and p.is_added_file] |
| 43 | if not new_cves: |
| 44 | self.skip('No new CVE patches introduced') |
| 45 | |
| 46 | def test_upstream_status_presence_format(self): |
| 47 | if not TestPatch.newpatches: |
| 48 | self.skip("There are no new software patches, no reason to test Upstream-Status presence/format") |
| 49 | |
| 50 | for newpatch in TestPatch.newpatches: |
| 51 | payload = newpatch.__str__() |
| 52 | if not self.upstream_status_regex.search_string(payload): |
| 53 | self.fail('Added patch file is missing Upstream-Status: <Valid status> in the commit message', |
| 54 | data=[('Standard format', self.standard_format), ('Valid status', self.valid_status)]) |
| 55 | for line in payload.splitlines(): |
| 56 | if self.patchmetadata_regex.match(line): |
| 57 | continue |
| 58 | if self.upstream_status_regex.search_string(line): |
| 59 | if parse_upstream_status.inappropriate_status_mark.searchString(line): |
| 60 | try: |
| 61 | parse_upstream_status.upstream_status_inappropriate_info.parseString(line.lstrip('+')) |
| 62 | except pyparsing.ParseException as pe: |
| 63 | self.fail('Upstream-Status is Inappropriate, but no reason was provided', |
| 64 | data=[('Current', pe.pstr), ('Standard format', 'Upstream-Status: Inappropriate [reason]')]) |
| 65 | elif parse_upstream_status.submitted_status_mark.searchString(line): |
| 66 | try: |
| 67 | parse_upstream_status.upstream_status_submitted_info.parseString(line.lstrip('+')) |
| 68 | except pyparsing.ParseException as pe: |
| 69 | self.fail('Upstream-Status is Submitted, but it is not mentioned where', |
| 70 | data=[('Current', pe.pstr), ('Standard format', 'Upstream-Status: Submitted [where]')]) |
| 71 | else: |
| 72 | try: |
| 73 | parse_upstream_status.upstream_status.parseString(line.lstrip('+')) |
| 74 | except pyparsing.ParseException as pe: |
| 75 | self.fail('Upstream-Status is in incorrect format', |
| 76 | data=[('Current', pe.pstr), ('Standard format', self.standard_format), ('Valid status', self.valid_status)]) |
| 77 | |
| 78 | def test_signed_off_by_presence(self): |
| 79 | if not TestPatch.newpatches: |
| 80 | self.skip("There are no new software patches, no reason to test %s presence" % PatchSignedOffBy.mark) |
| 81 | |
| 82 | for newpatch in TestPatch.newpatches: |
| 83 | payload = newpatch.__str__() |
| 84 | for line in payload.splitlines(): |
| 85 | if self.patchmetadata_regex.match(line): |
| 86 | continue |
| 87 | if TestPatch.prog.search_string(payload): |
| 88 | break |
| 89 | else: |
| 90 | self.fail('A patch file has been added without a Signed-off-by tag. Sign off the added patch file (%s)' % newpatch.path) |
| 91 | |
| 92 | def test_cve_tag_format(self): |
| 93 | for commit in TestPatch.commits: |
| 94 | if self.re_cve_pattern.search_string(commit.shortlog) or self.re_cve_pattern.search_string(commit.commit_message): |
| 95 | tag_found = False |
| 96 | for line in commit.payload.splitlines(): |
| 97 | if self.re_cve_payload_tag.search_string(line): |
| 98 | tag_found = True |
| 99 | break |
| 100 | if not tag_found: |
| 101 | self.fail('Missing or incorrectly formatted CVE tag in patch file. Correct or include the CVE tag in the patch with format: "CVE: CVE-YYYY-XXXX"', |
| 102 | commit=commit) |