def choice_matcher(self, scan_paths, rule): """Run a Single Choice Matcher rule on all files.""" try: matches = set() all_matches = set() for sfile in scan_paths: ext = sfile.suffix.lower() if self.exts and ext not in self.exts: continue if sfile.stat().st_size / 1000 / 1000 > 5: # Skip scanning files greater than 5 MB continue data = sfile.read_text('utf-8', 'ignore') if ext in ('.html', '.xml'): data = strip_comments2(data) else: data = strip_comments(data) match = choices.find_choices(data, rule) if match: if isinstance(match, set): # all all_matches.update(match) elif isinstance(match, list): # or, and matches.add(match[0]) self.add_finding(rule, matches, all_matches) except Exception: raise exceptions.RuleProcessingError('Rule processing error.')
def pattern_matcher(self, data, file_path, ext): """Static Analysis Pattern Matcher.""" try: for rule in self.scan_rules: case = rule.get('input_case') if case == 'lower': tmp_data = data.lower() elif case == 'upper': tmp_data = data.upper() else: tmp_data = data if ext in ('.html', '.xml'): fmt_data = strip_comments2(tmp_data) else: fmt_data = strip_comments(tmp_data) matches = self.matcher._find_match(rule['type'], fmt_data, rule) if matches: self.add_finding(file_path, rule, matches) except Exception: raise exceptions.RuleProcessingException('Rule processing error.')