Skip to content

Commit

Permalink
bug fix: preventing infinite add for exclude regex
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Loo committed Sep 28, 2018
1 parent 658ad0d commit 0a6f767
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/.coverage
/.pytest_cache
/.tox
/venv
/venv**
/tmp

.*ignore
Expand Down
8 changes: 6 additions & 2 deletions detect_secrets/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,12 @@ def _perform_scan(args, plugins):

# If we have knowledge of an existing baseline file, we should use
# that knowledge and *not* scan that file.
if args.import_filename and args.exclude:
args.exclude += r'|^{}$'.format(args.import_filename[0])
if args.import_filename:
payload = '^{}$'.format(args.import_filename[0])
if args.exclude and payload not in args.exclude:
args.exclude += r'|{}'.format(payload)
elif not args.exclude:
args.exclude = payload

new_baseline = baseline.initialize(
plugins,
Expand Down
26 changes: 20 additions & 6 deletions tests/main_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import shlex
import textwrap
from contextlib import contextmanager

Expand All @@ -16,11 +17,15 @@

@pytest.fixture
def mock_baseline_initialize():
secrets = secrets_collection_factory()
def mock_initialize_function(plugins, exclude_regex, *args, **kwargs):
return secrets_collection_factory(
plugins=plugins,
exclude_regex=exclude_regex,
)

with mock.patch(
'detect_secrets.main.baseline.initialize',
return_value=secrets,
side_effect=mock_initialize_function,
) as mock_initialize:
yield mock_initialize

Expand Down Expand Up @@ -154,6 +159,10 @@ def test_reads_old_baseline_from_file(self, mock_merge_baseline):
'--exclude "secrets/.*"',
'secrets/.*|^old_baseline_file$',
),
(
'--exclude "^old_baseline_file$"',
'^old_baseline_file$',
),
],
)
def test_old_baseline_ignored_with_update_flag(
Expand All @@ -168,13 +177,18 @@ def test_old_baseline_ignored_with_update_flag(
), mock.patch(
# We don't want to be creating a file during test
'detect_secrets.main._write_to_file',
):
) as file_writer:
assert main(
'scan --update old_baseline_file {}'.format(
exclude_param,
).split(),
shlex.split(
'scan --update old_baseline_file {}'.format(
exclude_param,
),
),
) == 0

assert json.loads(file_writer.call_args[0][1])['exclude_regex'] == \
expected_regex

@pytest.mark.parametrize(
'filename, expected_output',
[
Expand Down

0 comments on commit 0a6f767

Please sign in to comment.