Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions detect_secrets/core/usage/scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,11 @@ def parse_args(args: argparse.Namespace) -> None:
# NOTE: This is assumed to run *after* the baseline argument processor, and before
# the plugin argument processor.
if args.baseline is not None and args.force_use_all_plugins:
# Only the plugins should be replaced by the latest defaults. The filters
# configured from the baseline (including the exclusion of the baseline file
# itself) must be kept, otherwise the baseline file gets scanned and the
# results change on every run (issue #502).
filters = get_settings().filters
get_settings().plugins.clear()
initialize_plugin_settings(args)
get_settings().filters = filters
24 changes: 24 additions & 0 deletions tests/core/usage/scan_usage_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,27 @@ def test_default_plugins_initialized(parser):
assert len(get_settings().plugins) == len(get_mapping_from_secret_type_to_class())
assert plugins.initialize.from_plugin_classname('HexHighEntropyString').entropy_limit == 2
assert plugins.initialize.from_plugin_classname('Base64HighEntropyString').entropy_limit == 4.5


def test_force_use_all_plugins_preserves_baseline_filters(parser):
with mock_named_temporary_file() as f:
f.write(
json.dumps({
'version': '0.0.1',
'plugins_used': [
{
'name': 'AWSKeyDetector',
},
],
'results': [],
}).encode(),
)
f.seek(0)

parser.parse_args(['scan', '--force-use-all-plugins', '--baseline', f.name])

# Forcing the latest plugins must not reset the filters loaded from the
# baseline, in particular the exclusion of the baseline file itself (#502).
assert get_settings().filters['detect_secrets.filters.common.is_baseline_file'] == {
'filename': f.name,
}
47 changes: 47 additions & 0 deletions tests/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,50 @@ def mock_stdin(response=None):
m.stdin.isatty.return_value = False
m.stdin.read.return_value = response
yield


class TestForceUseAllPlugins:
@staticmethod
def test_forcing_latest_plugins_keeps_baseline_stable():
with tempfile.TemporaryDirectory() as d:
sample = Path(d, 'sample.txt')
sample.write_text('password = "hunter2"\n')
baseline_path = Path(d, '.secrets.baseline')

with transient_settings({
'plugins_used': [{'name': 'KeywordDetector'}],
'filters_used': [],
}):
baseline.save_to_file(
baseline.format_for_output(SecretsCollection()),
str(baseline_path),
)

# Keep the test independent of the optional gibberish dependency:
# the baseline must not require that filter to be initialized.
content = json.loads(baseline_path.read_text())
content['filters_used'] = [
f for f in content['filters_used']
if 'gibberish' not in f['path']
]
baseline_path.write_text(json.dumps(content))

results = []
with disable_gibberish_filter():
old_cwd = os.getcwd()
os.chdir(d)
try:
for _ in range(2):
main_module.main([
'scan', '--all-files', '--no-verify',
'--baseline', '.secrets.baseline', '--force-use-all-plugins', '.',
])
results.append(json.loads(baseline_path.read_text())['results'])
finally:
os.chdir(old_cwd)

# Forcing the latest plugins must not make the baseline file get
# scanned, which used to change the results on every run (#502).
assert results[0] == results[1]
assert '.secrets.baseline' not in results[0]
assert 'sample.txt' in results[0]