class PythonChecker(object): """Processes text lines for checking style.""" def __init__(self, file_path, handle_style_error): self._file_path = file_path self._handle_style_error = handle_style_error self._inclusive_language_checker = InclusiveLanguageChecker(handle_style_error) def check(self, lines): self._check_pycodestyle(lines) # FIXME: https://bugs.webkit.org/show_bug.cgi?id=204133 # Pylint can't live happily in python 2 and 3 world, we need to pick one if sys.version_info < (3, 0): self._check_pylint(lines) self._inclusive_language_checker.check(lines) def _check_pycodestyle(self, lines): # Initialize pycodestyle.options, which is necessary for # Checker.check_all() to execute. pycodestyle.process_options(arglist=[self._file_path]) pycodestyle_checker = pycodestyle.Checker(self._file_path) def _pycodestyle_handle_error(line_number, offset, text, check): # FIXME: Incorporate the character offset into the error output. # This will require updating the error handler __call__ # signature to include an optional "offset" parameter. pycodestyle_code = text[:4] pycodestyle_message = text[5:] category = "pep8/" + pycodestyle_code self._handle_style_error(line_number, category, 5, pycodestyle_message) pycodestyle_checker.report_error = _pycodestyle_handle_error pycodestyle_errors = pycodestyle_checker.check_all() def _check_pylint(self, lines): pylinter = Pylinter() # FIXME: for now, we only report pylint errors, but we should be catching and # filtering warnings using the rules in style/checker.py instead. output = pylinter.run(['-E', self._file_path]) lint_regex = re.compile(r'([^:]+):([^:]+): \[([^]]+)\] (.*)') for error in output.getvalue().splitlines(): match_obj = lint_regex.match(error) if not match_obj: continue line_number = int(match_obj.group(2)) category_and_method = match_obj.group(3).split(', ') category = 'pylint/' + (category_and_method[0]) if len(category_and_method) > 1: message = '[%s] %s' % (category_and_method[1], match_obj.group(4)) else: message = match_obj.group(4) self._handle_style_error(line_number, category, 5, message)
class TextChecker(object): """Processes text lines for checking style.""" def __init__(self, file_path, handle_style_error): self.file_path = file_path self.handle_style_error = handle_style_error self._tab_checker = TabChecker(file_path, handle_style_error) self._inclusive_language_checker = InclusiveLanguageChecker(handle_style_error) def check(self, lines): self._tab_checker.check(lines) self._inclusive_language_checker.check(lines)
class JSChecker(object): """Processes JavaScript lines for checking style.""" # FIXME: plug in a JavaScript parser to find syntax errors. categories = set(('js/syntax', )) def __init__(self, file_path, handle_style_error): self._handle_style_error = handle_style_error self._tab_checker = TabChecker(file_path, handle_style_error) self._single_quote_checker = SingleQuoteChecker( file_path, handle_style_error) self._inclusive_language_checker = InclusiveLanguageChecker( handle_style_error) def check(self, lines): self._tab_checker.check(lines) self._single_quote_checker.check(lines) self._inclusive_language_checker.check(lines)
class ChangeLogChecker(object): """Processes text lines for checking style.""" categories = set( ['changelog/bugnumber', 'changelog/filechangedescriptionwhitespace']) def __init__(self, file_path, handle_style_error, should_line_be_checked): self.file_path = file_path self.handle_style_error = handle_style_error self.should_line_be_checked = should_line_be_checked self._tab_checker = TabChecker(file_path, handle_style_error) self._inclusive_language_checker = InclusiveLanguageChecker( handle_style_error) def check_entry(self, first_line_checked, entry_lines): if not entry_lines: return for line in entry_lines: if parse_bug_id_from_changelog(line): break if searchIgnorecase("Unreviewed", line): break if searchIgnorecase("build", line) and searchIgnorecase( "fix", line): break else: self.handle_style_error(first_line_checked, "changelog/bugnumber", 5, "ChangeLog entry has no bug number") # check file change descriptions for style violations line_no = first_line_checked - 1 for line in entry_lines: line_no = line_no + 1 # filter file change descriptions if not match(r'\s*\*\s', line): continue if search(r':\s*$', line) or search(r':\s', line): continue self.handle_style_error( line_no, "changelog/filechangedescriptionwhitespace", 5, "Need whitespace between colon and description") # check for a lingering "No new tests (OOPS!)." left over from prepare-changeLog. line_no = first_line_checked - 1 for line in entry_lines: line_no = line_no + 1 if match(r'\s*No new tests \(OOPS!\)\.$', line): self.handle_style_error( line_no, "changelog/nonewtests", 5, "You should remove the 'No new tests' and either add and list tests, or explain why no new tests were possible." ) self.check_for_unwanted_security_phrases(first_line_checked, entry_lines) def check(self, lines): self._tab_checker.check(lines) self._inclusive_language_checker.check(lines) first_line_checked = 0 entry_lines = [] for line_index, line in enumerate(lines): if not self.should_line_be_checked(line_index + 1): # If we transitioned from finding changed lines to # unchanged lines, then we are done. if first_line_checked: break continue if not first_line_checked: first_line_checked = line_index + 1 entry_lines.append(line) self.check_entry(first_line_checked, entry_lines) def contains_phrase_in_first_line_or_across_two_lines( self, phrase, line1, line2): return searchIgnorecase(phrase, line1) or ( (not searchIgnorecase(phrase, line2)) and searchIgnorecase(phrase, line1 + " " + line2)) def check_for_unwanted_security_phrases(self, first_line_checked, lines): unwanted_security_phrases = [ "arbitrary code execution", "buffer overflow", "buffer overrun", "buffer underrun", "dangling pointer", "double free", "fuzzer", "fuzzing", "fuzz test", "invalid cast", "jsfunfuzz", "malicious", "memory corruption", "security bug", "security flaw", "use after free", "use-after-free", "UAF", "UXSS", "WTFCrashWithSecurityImplication", "spoof", # Captures spoof, spoofed, spoofing "vulnerab", # Captures vulnerable, vulnerability, vulnerabilities ] lines_with_single_spaces = [] for line in lines: lines_with_single_spaces.append(" ".join(line.split())) found_unwanted_security_phrases = [] last_index = len(lines_with_single_spaces) - 1 first_line_number_with_unwanted_phrase = maxsize for unwanted_phrase in unwanted_security_phrases: for line_index, line in enumerate(lines_with_single_spaces): next_line = "" if line_index >= last_index else lines_with_single_spaces[ line_index + 1] if self.contains_phrase_in_first_line_or_across_two_lines( unwanted_phrase, line, next_line): found_unwanted_security_phrases.append(unwanted_phrase) first_line_number_with_unwanted_phrase = min( first_line_number_with_unwanted_phrase, first_line_checked + line_index) if len(found_unwanted_security_phrases) > 0: self.handle_style_error( first_line_number_with_unwanted_phrase, "changelog/unwantedsecurityterms", 3, "Please consider whether the use of security-sensitive phrasing could help someone exploit WebKit: {}" .format(", ".join(found_unwanted_security_phrases)))