diff --git a/tell_me_your_secrets/__init__.py b/tell_me_your_secrets/__init__.py index a33997d..55fa725 100644 --- a/tell_me_your_secrets/__init__.py +++ b/tell_me_your_secrets/__init__.py @@ -1 +1 @@ -__version__ = '2.1.0' +__version__ = '2.1.1' diff --git a/tell_me_your_secrets/__main__.py b/tell_me_your_secrets/__main__.py index 9891035..ebcca35 100644 --- a/tell_me_your_secrets/__main__.py +++ b/tell_me_your_secrets/__main__.py @@ -137,7 +137,7 @@ def __init__(self, config_object: dict, search_path: str, use_gitignore: bool, p module_logger.debug(f'Using gitignore file: {gitignore_file}') self.gitignore_matcher = parse_gitignore(gitignore_file) self.blacklisted_extensions = config_object.get('blacklisted_extensions', []) - self.blacklisted_paths = [path.format(sep=os.path.sep) for path in config_object['blacklisted_paths']] + self.blacklisted_paths = [path.format(sep=os.path.sep) for path in config_object.get('blacklisted_paths', [])] self.red_flag_extensions = config_object.get('red_flag_extensions', []) self.max_file_size = config_object.get('max_file_size', MAX_FILE_SIZE) self.whitelisted_strings = config_object.get('whitelisted_strings', []) @@ -219,9 +219,10 @@ def write_results_to_file(self): def run_signatures(self, file_path, content) -> Tuple[Optional[str], Optional[str]]: for signature in self.signatures: match_result = signature.match(file_path, content) - if match_result.matched_value: + if match_result.is_match: if match_result.matched_value in self.whitelisted_strings: - module_logger.debug(f'Signature {signature.name} matched {match_result.matched_value} but skipping since it is whitelisted') + module_logger.debug(f'Signature {signature.name} matched {match_result.matched_value} but skipping' + f' since it is whitelisted') continue # $ Return the first signature Match. return signature.name, signature.part diff --git a/test/test_main.py b/test/test_main.py new file mode 100644 index 0000000..1a41510 --- /dev/null +++ b/test/test_main.py @@ -0,0 +1,35 @@ +import unittest +from typing import Optional + +from tell_me_your_secrets.__main__ import (MatchResult, Signature, + SignatureRecognizer) + + +class MockSignature(Signature): + def match(self, file_path: str, file_content: str) -> MatchResult: + return MatchResult(self.is_fail, self.matched_value or '') + + def __init__(self, is_fail: bool, matched_value: Optional[str] = None): + super().__init__('file', 'Mock Signature', 'Mock Signature') + self.is_fail = is_fail + self.matched_value = matched_value + + +class RunSignaturesTest(unittest.TestCase): + + def test_run_signatures_matched(self): + signature_recognizer = SignatureRecognizer({}, '.', False) + signature_recognizer.signatures.append(MockSignature(True, 'matched-yada')) + + result = signature_recognizer.run_signatures('file/with/issues', 'dodgy-content') + self.assertEquals('Mock Signature', result[0]) + self.assertEquals('file', result[1]) + + def test_run_signatures_whitelisted(self): + signature_recognizer = SignatureRecognizer({}, '.', False) + signature_recognizer.whitelisted_strings.append('matched-yada') + signature_recognizer.signatures.append(MockSignature(True, 'matched-yada')) + + result = signature_recognizer.run_signatures('file/with/issues', 'dodgy-content') + self.assertIsNone(result[0]) + self.assertIsNone(result[1])