Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Gilles Peskine | 7dfcfce | 2019-07-04 19:31:02 +0200 | [diff] [blame] | 2 | |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 3 | # Copyright The Mbed TLS Contributors |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 4 | # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Gilles Peskine | 7dfcfce | 2019-07-04 19:31:02 +0200 | [diff] [blame] | 5 | |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 6 | """ |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 7 | This script checks the current state of the source code for minor issues, |
| 8 | including incorrect file permissions, presence of tabs, non-Unix line endings, |
Gilles Peskine | 55b49ee | 2019-07-04 19:31:33 +0200 | [diff] [blame] | 9 | trailing whitespace, and presence of UTF-8 BOM. |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 10 | Note: requires python 3, must be run from Mbed TLS root. |
| 11 | """ |
| 12 | |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 13 | import argparse |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 14 | import codecs |
Gilles Peskine | f2fb9f6 | 2023-11-03 14:13:55 +0100 | [diff] [blame] | 15 | import inspect |
Gilles Peskine | 990030b | 2023-11-03 13:55:00 +0100 | [diff] [blame] | 16 | import logging |
| 17 | import os |
Gilles Peskine | 0598db8 | 2020-05-10 16:57:16 +0200 | [diff] [blame] | 18 | import re |
Gilles Peskine | 3e2ee3c | 2020-05-10 17:18:06 +0200 | [diff] [blame] | 19 | import subprocess |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 20 | import sys |
Gilles Peskine | ac9e7c0 | 2020-08-11 15:11:50 +0200 | [diff] [blame] | 21 | try: |
| 22 | from typing import FrozenSet, Optional, Pattern # pylint: disable=unused-import |
| 23 | except ImportError: |
| 24 | pass |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 25 | |
Gilles Peskine | d9071e7 | 2022-09-18 21:17:09 +0200 | [diff] [blame] | 26 | import scripts_path # pylint: disable=unused-import |
| 27 | from mbedtls_dev import build_tree |
| 28 | |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 29 | |
Gilles Peskine | 184c096 | 2020-03-24 18:25:17 +0100 | [diff] [blame] | 30 | class FileIssueTracker: |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 31 | """Base class for file-wide issue tracking. |
| 32 | |
| 33 | To implement a checker that processes a file as a whole, inherit from |
Gilles Peskine | 1e9698a | 2019-02-25 21:10:04 +0100 | [diff] [blame] | 34 | this class and implement `check_file_for_issue` and define ``heading``. |
| 35 | |
Gilles Peskine | 05a51a8 | 2020-05-10 16:52:44 +0200 | [diff] [blame] | 36 | ``suffix_exemptions``: files whose name ends with a string in this set |
Gilles Peskine | 1e9698a | 2019-02-25 21:10:04 +0100 | [diff] [blame] | 37 | will not be checked. |
| 38 | |
Gilles Peskine | 0598db8 | 2020-05-10 16:57:16 +0200 | [diff] [blame] | 39 | ``path_exemptions``: files whose path (relative to the root of the source |
| 40 | tree) matches this regular expression will not be checked. This can be |
| 41 | ``None`` to match no path. Paths are normalized and converted to ``/`` |
| 42 | separators before matching. |
| 43 | |
Gilles Peskine | 1e9698a | 2019-02-25 21:10:04 +0100 | [diff] [blame] | 44 | ``heading``: human-readable description of the issue |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 45 | """ |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 46 | |
Gilles Peskine | ac9e7c0 | 2020-08-11 15:11:50 +0200 | [diff] [blame] | 47 | suffix_exemptions = frozenset() #type: FrozenSet[str] |
| 48 | path_exemptions = None #type: Optional[Pattern[str]] |
Gilles Peskine | 1e9698a | 2019-02-25 21:10:04 +0100 | [diff] [blame] | 49 | # heading must be defined in derived classes. |
| 50 | # pylint: disable=no-member |
| 51 | |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 52 | def __init__(self): |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 53 | self.files_with_issues = {} |
| 54 | |
Gilles Peskine | 0598db8 | 2020-05-10 16:57:16 +0200 | [diff] [blame] | 55 | @staticmethod |
| 56 | def normalize_path(filepath): |
Gilles Peskine | eca95db | 2020-05-28 18:19:20 +0200 | [diff] [blame] | 57 | """Normalize ``filepath`` with / as the directory separator.""" |
Gilles Peskine | 0598db8 | 2020-05-10 16:57:16 +0200 | [diff] [blame] | 58 | filepath = os.path.normpath(filepath) |
Gilles Peskine | eca95db | 2020-05-28 18:19:20 +0200 | [diff] [blame] | 59 | # On Windows, we may have backslashes to separate directories. |
| 60 | # We need slashes to match exemption lists. |
Gilles Peskine | 0598db8 | 2020-05-10 16:57:16 +0200 | [diff] [blame] | 61 | seps = os.path.sep |
| 62 | if os.path.altsep is not None: |
| 63 | seps += os.path.altsep |
| 64 | return '/'.join(filepath.split(seps)) |
| 65 | |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 66 | def should_check_file(self, filepath): |
Gilles Peskine | aaee444 | 2020-03-24 16:49:21 +0100 | [diff] [blame] | 67 | """Whether the given file name should be checked. |
| 68 | |
Gilles Peskine | 05a51a8 | 2020-05-10 16:52:44 +0200 | [diff] [blame] | 69 | Files whose name ends with a string listed in ``self.suffix_exemptions`` |
| 70 | or whose path matches ``self.path_exemptions`` will not be checked. |
Gilles Peskine | aaee444 | 2020-03-24 16:49:21 +0100 | [diff] [blame] | 71 | """ |
Gilles Peskine | 05a51a8 | 2020-05-10 16:52:44 +0200 | [diff] [blame] | 72 | for files_exemption in self.suffix_exemptions: |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 73 | if filepath.endswith(files_exemption): |
| 74 | return False |
Gilles Peskine | 0598db8 | 2020-05-10 16:57:16 +0200 | [diff] [blame] | 75 | if self.path_exemptions and \ |
| 76 | re.match(self.path_exemptions, self.normalize_path(filepath)): |
| 77 | return False |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 78 | return True |
| 79 | |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 80 | def check_file_for_issue(self, filepath): |
Gilles Peskine | aaee444 | 2020-03-24 16:49:21 +0100 | [diff] [blame] | 81 | """Check the specified file for the issue that this class is for. |
| 82 | |
| 83 | Subclasses must implement this method. |
| 84 | """ |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 85 | raise NotImplementedError |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 86 | |
Gilles Peskine | 0439805 | 2018-11-23 21:11:30 +0100 | [diff] [blame] | 87 | def record_issue(self, filepath, line_number): |
Gilles Peskine | aaee444 | 2020-03-24 16:49:21 +0100 | [diff] [blame] | 88 | """Record that an issue was found at the specified location.""" |
Gilles Peskine | 0439805 | 2018-11-23 21:11:30 +0100 | [diff] [blame] | 89 | if filepath not in self.files_with_issues.keys(): |
| 90 | self.files_with_issues[filepath] = [] |
| 91 | self.files_with_issues[filepath].append(line_number) |
| 92 | |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 93 | def output_file_issues(self, logger): |
Gilles Peskine | aaee444 | 2020-03-24 16:49:21 +0100 | [diff] [blame] | 94 | """Log all the locations where the issue was found.""" |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 95 | if self.files_with_issues.values(): |
| 96 | logger.info(self.heading) |
| 97 | for filename, lines in sorted(self.files_with_issues.items()): |
| 98 | if lines: |
| 99 | logger.info("{}: {}".format( |
| 100 | filename, ", ".join(str(x) for x in lines) |
| 101 | )) |
| 102 | else: |
| 103 | logger.info(filename) |
| 104 | logger.info("") |
| 105 | |
Gilles Peskine | d4a853d | 2020-05-10 16:57:59 +0200 | [diff] [blame] | 106 | BINARY_FILE_PATH_RE_LIST = [ |
| 107 | r'docs/.*\.pdf\Z', |
| 108 | r'programs/fuzz/corpuses/[^.]+\Z', |
| 109 | r'tests/data_files/[^.]+\Z', |
| 110 | r'tests/data_files/.*\.(crt|csr|db|der|key|pubkey)\Z', |
| 111 | r'tests/data_files/.*\.req\.[^/]+\Z', |
| 112 | r'tests/data_files/.*malformed[^/]+\Z', |
| 113 | r'tests/data_files/format_pkcs12\.fmt\Z', |
Gilles Peskine | 0ed9e78 | 2023-01-05 20:27:18 +0100 | [diff] [blame] | 114 | r'tests/data_files/.*\.bin\Z', |
Gilles Peskine | d4a853d | 2020-05-10 16:57:59 +0200 | [diff] [blame] | 115 | ] |
| 116 | BINARY_FILE_PATH_RE = re.compile('|'.join(BINARY_FILE_PATH_RE_LIST)) |
| 117 | |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 118 | class LineIssueTracker(FileIssueTracker): |
| 119 | """Base class for line-by-line issue tracking. |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 120 | |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 121 | To implement a checker that processes files line by line, inherit from |
| 122 | this class and implement `line_with_issue`. |
| 123 | """ |
| 124 | |
Gilles Peskine | d4a853d | 2020-05-10 16:57:59 +0200 | [diff] [blame] | 125 | # Exclude binary files. |
| 126 | path_exemptions = BINARY_FILE_PATH_RE |
| 127 | |
Gilles Peskine | b389743 | 2023-01-05 20:28:30 +0100 | [diff] [blame] | 128 | def issue_with_line(self, line, filepath, line_number): |
Gilles Peskine | aaee444 | 2020-03-24 16:49:21 +0100 | [diff] [blame] | 129 | """Check the specified line for the issue that this class is for. |
| 130 | |
| 131 | Subclasses must implement this method. |
| 132 | """ |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 133 | raise NotImplementedError |
| 134 | |
| 135 | def check_file_line(self, filepath, line, line_number): |
Gilles Peskine | b389743 | 2023-01-05 20:28:30 +0100 | [diff] [blame] | 136 | if self.issue_with_line(line, filepath, line_number): |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 137 | self.record_issue(filepath, line_number) |
| 138 | |
| 139 | def check_file_for_issue(self, filepath): |
Gilles Peskine | aaee444 | 2020-03-24 16:49:21 +0100 | [diff] [blame] | 140 | """Check the lines of the specified file. |
| 141 | |
| 142 | Subclasses must implement the ``issue_with_line`` method. |
| 143 | """ |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 144 | with open(filepath, "rb") as f: |
| 145 | for i, line in enumerate(iter(f.readline, b"")): |
| 146 | self.check_file_line(filepath, line, i + 1) |
| 147 | |
Gilles Peskine | 2c61873 | 2020-03-24 22:26:01 +0100 | [diff] [blame] | 148 | |
| 149 | def is_windows_file(filepath): |
| 150 | _root, ext = os.path.splitext(filepath) |
Gilles Peskine | d2df86f | 2020-05-10 17:36:51 +0200 | [diff] [blame] | 151 | return ext in ('.bat', '.dsp', '.dsw', '.sln', '.vcxproj') |
Gilles Peskine | 2c61873 | 2020-03-24 22:26:01 +0100 | [diff] [blame] | 152 | |
| 153 | |
Gilles Peskine | 4aebb8d | 2020-08-08 23:15:18 +0200 | [diff] [blame] | 154 | class ShebangIssueTracker(FileIssueTracker): |
| 155 | """Track files with a bad, missing or extraneous shebang line. |
| 156 | |
| 157 | Executable scripts must start with a valid shebang (#!) line. |
| 158 | """ |
| 159 | |
| 160 | heading = "Invalid shebang line:" |
| 161 | |
| 162 | # Allow either /bin/sh, /bin/bash, or /usr/bin/env. |
| 163 | # Allow at most one argument (this is a Linux limitation). |
| 164 | # For sh and bash, the argument if present must be options. |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 165 | # For env, the argument must be the base name of the interpreter. |
Gilles Peskine | 4aebb8d | 2020-08-08 23:15:18 +0200 | [diff] [blame] | 166 | _shebang_re = re.compile(rb'^#! ?(?:/bin/(bash|sh)(?: -[^\n ]*)?' |
| 167 | rb'|/usr/bin/env ([^\n /]+))$') |
| 168 | _extensions = { |
| 169 | b'bash': 'sh', |
| 170 | b'perl': 'pl', |
| 171 | b'python3': 'py', |
| 172 | b'sh': 'sh', |
| 173 | } |
| 174 | |
| 175 | def is_valid_shebang(self, first_line, filepath): |
| 176 | m = re.match(self._shebang_re, first_line) |
| 177 | if not m: |
| 178 | return False |
| 179 | interpreter = m.group(1) or m.group(2) |
| 180 | if interpreter not in self._extensions: |
| 181 | return False |
| 182 | if not filepath.endswith('.' + self._extensions[interpreter]): |
| 183 | return False |
| 184 | return True |
| 185 | |
| 186 | def check_file_for_issue(self, filepath): |
| 187 | is_executable = os.access(filepath, os.X_OK) |
| 188 | with open(filepath, "rb") as f: |
| 189 | first_line = f.readline() |
| 190 | if first_line.startswith(b'#!'): |
| 191 | if not is_executable: |
| 192 | # Shebang on a non-executable file |
| 193 | self.files_with_issues[filepath] = None |
| 194 | elif not self.is_valid_shebang(first_line, filepath): |
| 195 | self.files_with_issues[filepath] = [1] |
| 196 | elif is_executable: |
| 197 | # Executable without a shebang |
| 198 | self.files_with_issues[filepath] = None |
| 199 | |
| 200 | |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 201 | class EndOfFileNewlineIssueTracker(FileIssueTracker): |
Gilles Peskine | 0d060ef | 2019-02-25 20:35:31 +0100 | [diff] [blame] | 202 | """Track files that end with an incomplete line |
| 203 | (no newline character at the end of the last line).""" |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 204 | |
Gilles Peskine | 1e9698a | 2019-02-25 21:10:04 +0100 | [diff] [blame] | 205 | heading = "Missing newline at end of file:" |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 206 | |
Gilles Peskine | d4a853d | 2020-05-10 16:57:59 +0200 | [diff] [blame] | 207 | path_exemptions = BINARY_FILE_PATH_RE |
| 208 | |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 209 | def check_file_for_issue(self, filepath): |
| 210 | with open(filepath, "rb") as f: |
Gilles Peskine | 12b180a | 2020-05-10 17:36:42 +0200 | [diff] [blame] | 211 | try: |
| 212 | f.seek(-1, 2) |
| 213 | except OSError: |
| 214 | # This script only works on regular files. If we can't seek |
| 215 | # 1 before the end, it means that this position is before |
| 216 | # the beginning of the file, i.e. that the file is empty. |
| 217 | return |
| 218 | if f.read(1) != b"\n": |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 219 | self.files_with_issues[filepath] = None |
| 220 | |
| 221 | |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 222 | class Utf8BomIssueTracker(FileIssueTracker): |
Gilles Peskine | 0d060ef | 2019-02-25 20:35:31 +0100 | [diff] [blame] | 223 | """Track files that start with a UTF-8 BOM. |
| 224 | Files should be ASCII or UTF-8. Valid UTF-8 does not start with a BOM.""" |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 225 | |
Gilles Peskine | 1e9698a | 2019-02-25 21:10:04 +0100 | [diff] [blame] | 226 | heading = "UTF-8 BOM present:" |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 227 | |
Gilles Peskine | 05a51a8 | 2020-05-10 16:52:44 +0200 | [diff] [blame] | 228 | suffix_exemptions = frozenset([".vcxproj", ".sln"]) |
Gilles Peskine | d4a853d | 2020-05-10 16:57:59 +0200 | [diff] [blame] | 229 | path_exemptions = BINARY_FILE_PATH_RE |
Gilles Peskine | 2c61873 | 2020-03-24 22:26:01 +0100 | [diff] [blame] | 230 | |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 231 | def check_file_for_issue(self, filepath): |
| 232 | with open(filepath, "rb") as f: |
| 233 | if f.read().startswith(codecs.BOM_UTF8): |
| 234 | self.files_with_issues[filepath] = None |
| 235 | |
| 236 | |
Gilles Peskine | d11bb47 | 2023-01-05 20:28:57 +0100 | [diff] [blame] | 237 | class UnicodeIssueTracker(LineIssueTracker): |
| 238 | """Track lines with invalid characters or invalid text encoding.""" |
| 239 | |
| 240 | heading = "Invalid UTF-8 or forbidden character:" |
| 241 | |
Aditya Deshpande | 15b6dd0 | 2023-01-30 13:46:58 +0000 | [diff] [blame] | 242 | # Only allow valid UTF-8, and only other explicitly allowed characters. |
Gilles Peskine | d11bb47 | 2023-01-05 20:28:57 +0100 | [diff] [blame] | 243 | # We deliberately exclude all characters that aren't a simple non-blank, |
| 244 | # non-zero-width glyph, apart from a very small set (tab, ordinary space, |
| 245 | # line breaks, "basic" no-break space and soft hyphen). In particular, |
| 246 | # non-ASCII control characters, combinig characters, and Unicode state |
| 247 | # changes (e.g. right-to-left text) are forbidden. |
| 248 | # Note that we do allow some characters with a risk of visual confusion, |
| 249 | # for example '-' (U+002D HYPHEN-MINUS) vs '' (U+00AD SOFT HYPHEN) vs |
| 250 | # '‐' (U+2010 HYPHEN), or 'A' (U+0041 LATIN CAPITAL LETTER A) vs |
| 251 | # 'Α' (U+0391 GREEK CAPITAL LETTER ALPHA). |
| 252 | GOOD_CHARACTERS = ''.join([ |
| 253 | '\t\n\r -~', # ASCII (tabs and line endings are checked separately) |
| 254 | '\u00A0-\u00FF', # Latin-1 Supplement (for NO-BREAK SPACE and punctuation) |
| 255 | '\u2010-\u2027\u2030-\u205E', # General Punctuation (printable) |
| 256 | '\u2070\u2071\u2074-\u208E\u2090-\u209C', # Superscripts and Subscripts |
| 257 | '\u2190-\u21FF', # Arrows |
| 258 | '\u2200-\u22FF', # Mathematical Symbols |
Aditya Deshpande | ebb2269 | 2023-02-01 13:30:26 +0000 | [diff] [blame] | 259 | '\u2500-\u257F' # Box Drawings characters used in markdown trees |
Gilles Peskine | d11bb47 | 2023-01-05 20:28:57 +0100 | [diff] [blame] | 260 | ]) |
| 261 | # Allow any of the characters and ranges above, and anything classified |
| 262 | # as a word constituent. |
| 263 | GOOD_CHARACTERS_RE = re.compile(r'[\w{}]+\Z'.format(GOOD_CHARACTERS)) |
| 264 | |
| 265 | def issue_with_line(self, line, _filepath, line_number): |
| 266 | try: |
| 267 | text = line.decode('utf-8') |
| 268 | except UnicodeDecodeError: |
| 269 | return True |
| 270 | if line_number == 1 and text.startswith('\uFEFF'): |
| 271 | # Strip BOM (U+FEFF ZERO WIDTH NO-BREAK SPACE) at the beginning. |
| 272 | # Which files are allowed to have a BOM is handled in |
| 273 | # Utf8BomIssueTracker. |
| 274 | text = text[1:] |
| 275 | return not self.GOOD_CHARACTERS_RE.match(text) |
| 276 | |
Gilles Peskine | 2c61873 | 2020-03-24 22:26:01 +0100 | [diff] [blame] | 277 | class UnixLineEndingIssueTracker(LineIssueTracker): |
Gilles Peskine | 0d060ef | 2019-02-25 20:35:31 +0100 | [diff] [blame] | 278 | """Track files with non-Unix line endings (i.e. files with CR).""" |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 279 | |
Gilles Peskine | 2c61873 | 2020-03-24 22:26:01 +0100 | [diff] [blame] | 280 | heading = "Non-Unix line endings:" |
| 281 | |
| 282 | def should_check_file(self, filepath): |
Gilles Peskine | 0598db8 | 2020-05-10 16:57:16 +0200 | [diff] [blame] | 283 | if not super().should_check_file(filepath): |
| 284 | return False |
Gilles Peskine | 2c61873 | 2020-03-24 22:26:01 +0100 | [diff] [blame] | 285 | return not is_windows_file(filepath) |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 286 | |
Gilles Peskine | b389743 | 2023-01-05 20:28:30 +0100 | [diff] [blame] | 287 | def issue_with_line(self, line, _filepath, _line_number): |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 288 | return b"\r" in line |
| 289 | |
| 290 | |
Gilles Peskine | 545e13f | 2020-03-24 22:29:11 +0100 | [diff] [blame] | 291 | class WindowsLineEndingIssueTracker(LineIssueTracker): |
Gilles Peskine | d703a2e | 2020-04-01 13:35:46 +0200 | [diff] [blame] | 292 | """Track files with non-Windows line endings (i.e. CR or LF not in CRLF).""" |
Gilles Peskine | 545e13f | 2020-03-24 22:29:11 +0100 | [diff] [blame] | 293 | |
| 294 | heading = "Non-Windows line endings:" |
| 295 | |
| 296 | def should_check_file(self, filepath): |
Gilles Peskine | 0598db8 | 2020-05-10 16:57:16 +0200 | [diff] [blame] | 297 | if not super().should_check_file(filepath): |
| 298 | return False |
Gilles Peskine | 545e13f | 2020-03-24 22:29:11 +0100 | [diff] [blame] | 299 | return is_windows_file(filepath) |
| 300 | |
Gilles Peskine | b389743 | 2023-01-05 20:28:30 +0100 | [diff] [blame] | 301 | def issue_with_line(self, line, _filepath, _line_number): |
Gilles Peskine | d703a2e | 2020-04-01 13:35:46 +0200 | [diff] [blame] | 302 | return not line.endswith(b"\r\n") or b"\r" in line[:-2] |
Gilles Peskine | 545e13f | 2020-03-24 22:29:11 +0100 | [diff] [blame] | 303 | |
| 304 | |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 305 | class TrailingWhitespaceIssueTracker(LineIssueTracker): |
Gilles Peskine | 0d060ef | 2019-02-25 20:35:31 +0100 | [diff] [blame] | 306 | """Track lines with trailing whitespace.""" |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 307 | |
Gilles Peskine | 1e9698a | 2019-02-25 21:10:04 +0100 | [diff] [blame] | 308 | heading = "Trailing whitespace:" |
Gilles Peskine | 05a51a8 | 2020-05-10 16:52:44 +0200 | [diff] [blame] | 309 | suffix_exemptions = frozenset([".dsp", ".md"]) |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 310 | |
Gilles Peskine | b389743 | 2023-01-05 20:28:30 +0100 | [diff] [blame] | 311 | def issue_with_line(self, line, _filepath, _line_number): |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 312 | return line.rstrip(b"\r\n") != line.rstrip() |
| 313 | |
| 314 | |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 315 | class TabIssueTracker(LineIssueTracker): |
Gilles Peskine | 0d060ef | 2019-02-25 20:35:31 +0100 | [diff] [blame] | 316 | """Track lines with tabs.""" |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 317 | |
Gilles Peskine | 1e9698a | 2019-02-25 21:10:04 +0100 | [diff] [blame] | 318 | heading = "Tabs present:" |
Gilles Peskine | 05a51a8 | 2020-05-10 16:52:44 +0200 | [diff] [blame] | 319 | suffix_exemptions = frozenset([ |
Gilles Peskine | 344da1c | 2020-05-10 17:37:02 +0200 | [diff] [blame] | 320 | ".pem", # some openssl dumps have tabs |
Gilles Peskine | 2c61873 | 2020-03-24 22:26:01 +0100 | [diff] [blame] | 321 | ".sln", |
Gilles Peskine | 6e8d5a0 | 2020-03-24 22:01:28 +0100 | [diff] [blame] | 322 | "/Makefile", |
| 323 | "/Makefile.inc", |
| 324 | "/generate_visualc_files.pl", |
Gilles Peskine | 1e9698a | 2019-02-25 21:10:04 +0100 | [diff] [blame] | 325 | ]) |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 326 | |
Gilles Peskine | b389743 | 2023-01-05 20:28:30 +0100 | [diff] [blame] | 327 | def issue_with_line(self, line, _filepath, _line_number): |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 328 | return b"\t" in line |
| 329 | |
| 330 | |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 331 | class MergeArtifactIssueTracker(LineIssueTracker): |
Gilles Peskine | 0d060ef | 2019-02-25 20:35:31 +0100 | [diff] [blame] | 332 | """Track lines with merge artifacts. |
| 333 | These are leftovers from a ``git merge`` that wasn't fully edited.""" |
Gilles Peskine | c117d59 | 2018-11-23 21:11:52 +0100 | [diff] [blame] | 334 | |
Gilles Peskine | 1e9698a | 2019-02-25 21:10:04 +0100 | [diff] [blame] | 335 | heading = "Merge artifact:" |
Gilles Peskine | c117d59 | 2018-11-23 21:11:52 +0100 | [diff] [blame] | 336 | |
Gilles Peskine | b389743 | 2023-01-05 20:28:30 +0100 | [diff] [blame] | 337 | def issue_with_line(self, line, _filepath, _line_number): |
Gilles Peskine | c117d59 | 2018-11-23 21:11:52 +0100 | [diff] [blame] | 338 | # Detect leftover git conflict markers. |
| 339 | if line.startswith(b'<<<<<<< ') or line.startswith(b'>>>>>>> '): |
| 340 | return True |
| 341 | if line.startswith(b'||||||| '): # from merge.conflictStyle=diff3 |
| 342 | return True |
| 343 | if line.rstrip(b'\r\n') == b'=======' and \ |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 344 | not _filepath.endswith('.md'): |
Gilles Peskine | c117d59 | 2018-11-23 21:11:52 +0100 | [diff] [blame] | 345 | return True |
| 346 | return False |
| 347 | |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 348 | |
Gilles Peskine | ce78200 | 2023-11-03 14:49:12 +0100 | [diff] [blame] | 349 | def this_location(): |
| 350 | frame = inspect.currentframe() |
| 351 | assert frame is not None |
| 352 | info = inspect.getframeinfo(frame) |
| 353 | return os.path.basename(info.filename), info.lineno |
| 354 | THIS_FILE_BASE_NAME, LINE_NUMBER_BEFORE_LICENSE_ISSUE_TRACKER = this_location() |
| 355 | |
Gilles Peskine | f2fb9f6 | 2023-11-03 14:13:55 +0100 | [diff] [blame] | 356 | class LicenseIssueTracker(LineIssueTracker): |
| 357 | """Check copyright statements and license indications. |
| 358 | |
| 359 | This class only checks that statements are correct if present. It does |
| 360 | not enforce the presence of statements in each file. |
| 361 | """ |
| 362 | |
| 363 | heading = "License issue:" |
| 364 | |
| 365 | LICENSE_EXEMPTION_RE_LIST = [ |
| 366 | # Third-party code, other than whitelisted third-party modules, |
| 367 | # may be under a different license. |
| 368 | r'3rdparty/(?!(p256-m)/.*)', |
| 369 | # Documentation explaining the license may have accidental |
| 370 | # false positives. |
| 371 | r'(ChangeLog|LICENSE|[-0-9A-Z_a-z]+\.md)\Z', |
| 372 | # Files imported from TF-M, and not used except in test builds, |
| 373 | # may be under a different license. |
| 374 | r'configs/crypto_config_profile_medium\.h\Z', |
| 375 | r'configs/tfm_mbedcrypto_config_profile_medium\.h\Z', |
| 376 | # Third-party file. |
| 377 | r'dco\.txt\Z', |
| 378 | ] |
| 379 | path_exemptions = re.compile('|'.join(BINARY_FILE_PATH_RE_LIST + |
| 380 | LICENSE_EXEMPTION_RE_LIST)) |
| 381 | |
| 382 | COPYRIGHT_HOLDER = rb'The Mbed TLS Contributors' |
| 383 | # Catch "Copyright foo", "Copyright (C) foo", "Copyright © foo", etc. |
| 384 | COPYRIGHT_RE = re.compile(rb'.*\bcopyright\s+((?:\w|\s|[()]|[^ -~])*\w)', re.I) |
| 385 | |
| 386 | SPDX_HEADER_KEY = b'SPDX-License-Identifier' |
| 387 | LICENSE_IDENTIFIER = b'Apache-2.0 OR GPL-2.0-or-later' |
| 388 | SPDX_RE = re.compile(br'.*?(' + |
| 389 | re.escape(SPDX_HEADER_KEY) + |
| 390 | br')(:\s*(.*?)\W*\Z|.*)', re.I) |
| 391 | |
Gilles Peskine | 3b9facd | 2023-11-03 14:35:28 +0100 | [diff] [blame] | 392 | LICENSE_MENTION_RE = re.compile(rb'.*(?:' + rb'|'.join([ |
| 393 | rb'Apache License', |
| 394 | rb'General Public License', |
| 395 | ]) + rb')', re.I) |
| 396 | |
Gilles Peskine | f2fb9f6 | 2023-11-03 14:13:55 +0100 | [diff] [blame] | 397 | def __init__(self): |
| 398 | super().__init__() |
| 399 | # Record what problem was caused. We can't easily report it due to |
| 400 | # the structure of the script. To be fixed after |
| 401 | # https://github.com/Mbed-TLS/mbedtls/pull/2506 |
| 402 | self.problem = None |
| 403 | |
| 404 | def issue_with_line(self, line, filepath, line_number): |
Gilles Peskine | 3b9facd | 2023-11-03 14:35:28 +0100 | [diff] [blame] | 405 | #pylint: disable=too-many-return-statements |
| 406 | |
Gilles Peskine | f2fb9f6 | 2023-11-03 14:13:55 +0100 | [diff] [blame] | 407 | # Use endswith() rather than the more correct os.path.basename() |
| 408 | # because experimentally, it makes a significant difference to |
| 409 | # the running time. |
| 410 | if filepath.endswith(THIS_FILE_BASE_NAME) and \ |
| 411 | line_number > LINE_NUMBER_BEFORE_LICENSE_ISSUE_TRACKER: |
| 412 | # Avoid false positives from the code in this class. |
| 413 | # Also skip the rest of this file, which is highly unlikely to |
| 414 | # contain any problematic statements since we put those near the |
| 415 | # top of files. |
| 416 | return False |
| 417 | |
| 418 | m = self.COPYRIGHT_RE.match(line) |
| 419 | if m and m.group(1) != self.COPYRIGHT_HOLDER: |
| 420 | self.problem = 'Invalid copyright line' |
| 421 | return True |
| 422 | |
| 423 | m = self.SPDX_RE.match(line) |
| 424 | if m: |
| 425 | if m.group(1) != self.SPDX_HEADER_KEY: |
| 426 | self.problem = 'Misspelled ' + self.SPDX_HEADER_KEY.decode() |
| 427 | return True |
| 428 | if not m.group(3): |
| 429 | self.problem = 'Improperly formatted SPDX license identifier' |
| 430 | return True |
| 431 | if m.group(3) != self.LICENSE_IDENTIFIER: |
| 432 | self.problem = 'Wrong SPDX license identifier' |
| 433 | return True |
Gilles Peskine | 3b9facd | 2023-11-03 14:35:28 +0100 | [diff] [blame] | 434 | |
| 435 | m = self.LICENSE_MENTION_RE.match(line) |
| 436 | if m: |
| 437 | self.problem = 'Suspicious license mention' |
| 438 | return True |
| 439 | |
Gilles Peskine | f2fb9f6 | 2023-11-03 14:13:55 +0100 | [diff] [blame] | 440 | return False |
| 441 | |
| 442 | |
Gilles Peskine | 184c096 | 2020-03-24 18:25:17 +0100 | [diff] [blame] | 443 | class IntegrityChecker: |
Gilles Peskine | 0d060ef | 2019-02-25 20:35:31 +0100 | [diff] [blame] | 444 | """Sanity-check files under the current directory.""" |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 445 | |
| 446 | def __init__(self, log_file): |
Gilles Peskine | 0d060ef | 2019-02-25 20:35:31 +0100 | [diff] [blame] | 447 | """Instantiate the sanity checker. |
| 448 | Check files under the current directory. |
| 449 | Write a report of issues to log_file.""" |
Gilles Peskine | d9071e7 | 2022-09-18 21:17:09 +0200 | [diff] [blame] | 450 | build_tree.check_repo_path() |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 451 | self.logger = None |
| 452 | self.setup_logger(log_file) |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 453 | self.issues_to_check = [ |
Gilles Peskine | 4aebb8d | 2020-08-08 23:15:18 +0200 | [diff] [blame] | 454 | ShebangIssueTracker(), |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 455 | EndOfFileNewlineIssueTracker(), |
| 456 | Utf8BomIssueTracker(), |
Gilles Peskine | d11bb47 | 2023-01-05 20:28:57 +0100 | [diff] [blame] | 457 | UnicodeIssueTracker(), |
Gilles Peskine | 2c61873 | 2020-03-24 22:26:01 +0100 | [diff] [blame] | 458 | UnixLineEndingIssueTracker(), |
Gilles Peskine | 545e13f | 2020-03-24 22:29:11 +0100 | [diff] [blame] | 459 | WindowsLineEndingIssueTracker(), |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 460 | TrailingWhitespaceIssueTracker(), |
| 461 | TabIssueTracker(), |
Gilles Peskine | c117d59 | 2018-11-23 21:11:52 +0100 | [diff] [blame] | 462 | MergeArtifactIssueTracker(), |
Gilles Peskine | f2fb9f6 | 2023-11-03 14:13:55 +0100 | [diff] [blame] | 463 | LicenseIssueTracker(), |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 464 | ] |
| 465 | |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 466 | def setup_logger(self, log_file, level=logging.INFO): |
| 467 | self.logger = logging.getLogger() |
| 468 | self.logger.setLevel(level) |
| 469 | if log_file: |
| 470 | handler = logging.FileHandler(log_file) |
| 471 | self.logger.addHandler(handler) |
| 472 | else: |
| 473 | console = logging.StreamHandler() |
| 474 | self.logger.addHandler(console) |
| 475 | |
Gilles Peskine | 3e2ee3c | 2020-05-10 17:18:06 +0200 | [diff] [blame] | 476 | @staticmethod |
| 477 | def collect_files(): |
| 478 | bytes_output = subprocess.check_output(['git', 'ls-files', '-z']) |
| 479 | bytes_filepaths = bytes_output.split(b'\0')[:-1] |
| 480 | ascii_filepaths = map(lambda fp: fp.decode('ascii'), bytes_filepaths) |
| 481 | # Prepend './' to files in the top-level directory so that |
| 482 | # something like `'/Makefile' in fp` matches in the top-level |
| 483 | # directory as well as in subdirectories. |
| 484 | return [fp if os.path.dirname(fp) else os.path.join(os.curdir, fp) |
| 485 | for fp in ascii_filepaths] |
Gilles Peskine | 95c5575 | 2018-09-28 11:48:10 +0200 | [diff] [blame] | 486 | |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 487 | def check_files(self): |
Gilles Peskine | 3e2ee3c | 2020-05-10 17:18:06 +0200 | [diff] [blame] | 488 | for issue_to_check in self.issues_to_check: |
| 489 | for filepath in self.collect_files(): |
| 490 | if issue_to_check.should_check_file(filepath): |
| 491 | issue_to_check.check_file_for_issue(filepath) |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 492 | |
| 493 | def output_issues(self): |
| 494 | integrity_return_code = 0 |
| 495 | for issue_to_check in self.issues_to_check: |
| 496 | if issue_to_check.files_with_issues: |
| 497 | integrity_return_code = 1 |
| 498 | issue_to_check.output_file_issues(self.logger) |
| 499 | return integrity_return_code |
| 500 | |
| 501 | |
| 502 | def run_main(): |
Gilles Peskine | 7dfcfce | 2019-07-04 19:31:02 +0200 | [diff] [blame] | 503 | parser = argparse.ArgumentParser(description=__doc__) |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 504 | parser.add_argument( |
| 505 | "-l", "--log_file", type=str, help="path to optional output log", |
| 506 | ) |
| 507 | check_args = parser.parse_args() |
| 508 | integrity_check = IntegrityChecker(check_args.log_file) |
| 509 | integrity_check.check_files() |
| 510 | return_code = integrity_check.output_issues() |
| 511 | sys.exit(return_code) |
| 512 | |
| 513 | |
| 514 | if __name__ == "__main__": |
| 515 | run_main() |