test_patch_cve.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # Checks related to the patch's CVE lines
  2. #
  3. # Copyright (C) 2016 Intel Corporation
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License version 2 as
  7. # published by the Free Software Foundation.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License along
  15. # with this program; if not, write to the Free Software Foundation, Inc.,
  16. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. # SPDX-License-Identifier: GPL-2.0-or-later
  18. import base
  19. import os
  20. import pyparsing
  21. class CVE(base.Base):
  22. re_cve_pattern = pyparsing.Regex("CVE\-\d{4}\-\d+")
  23. re_cve_payload_tag = pyparsing.Regex("\+CVE:(\s+CVE\-\d{4}\-\d+)+")
  24. def setUp(self):
  25. if self.unidiff_parse_error:
  26. self.skip('Parse error %s' % self.unidiff_parse_error)
  27. # we are just interested in series that introduce CVE patches, thus discard other
  28. # possibilities: modification to current CVEs, patch directly introduced into the
  29. # recipe, upgrades already including the CVE, etc.
  30. new_cves = [p for p in self.patchset if p.path.endswith('.patch') and p.is_added_file]
  31. if not new_cves:
  32. self.skip('No new CVE patches introduced')
  33. def test_cve_tag_format(self):
  34. for commit in CVE.commits:
  35. if self.re_cve_pattern.search_string(commit.shortlog) or self.re_cve_pattern.search_string(commit.commit_message):
  36. tag_found = False
  37. for line in commit.payload.splitlines():
  38. if self.re_cve_payload_tag.search_string(line):
  39. tag_found = True
  40. break
  41. if not tag_found:
  42. 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"',
  43. commit=commit)