Skip to content

Commit

Permalink
Fix bug with signature matching (#33)
Browse files Browse the repository at this point in the history
* Fix bug with signature matching

* Fix indent

* Add some tests, don't expone on non-existent config keys

* Fix CI
  • Loading branch information
inverse authored Dec 28, 2020
1 parent 364e96e commit 39f695f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
2 changes: 1 addition & 1 deletion tell_me_your_secrets/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '2.1.0'
__version__ = '2.1.1'
7 changes: 4 additions & 3 deletions tell_me_your_secrets/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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', [])
Expand Down Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions test/test_main.py
Original file line number Diff line number Diff line change
@@ -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])

0 comments on commit 39f695f

Please sign in to comment.