Skip to content

Commit

Permalink
Merge pull request #64 from Yelp/62_reduce_sequential_false_positives
Browse files Browse the repository at this point in the history
Reduce sequential string false positives
  • Loading branch information
KevinHock committed Jul 31, 2018
2 parents 11b8768 + b5135fc commit 8c2d022
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 8 deletions.
6 changes: 3 additions & 3 deletions detect_secrets/core/usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,20 +282,20 @@ def consolidate_args(args):
def _add_custom_limits(self):
high_entropy_help_text = (
'Sets the entropy limit for high entropy strings. '
'Value must be between 0.0 and 8.0.'
'Value must be between 0.0 and 8.0, '
)

self.parser.add_argument(
'--base64-limit',
type=self._argparse_minmax_type,
nargs='?',
help=high_entropy_help_text,
help=high_entropy_help_text + 'defaults to 4.5.',
)
self.parser.add_argument(
'--hex-limit',
type=self._argparse_minmax_type,
nargs='?',
help=high_entropy_help_text,
help=high_entropy_help_text + 'defaults to 3.0.',
)
return self

Expand Down
18 changes: 17 additions & 1 deletion detect_secrets/plugins/high_entropy_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@
from detect_secrets.plugins.core.yaml_file_parser import YamlFileParser


IGNORED_SEQUENTIAL_STRINGS = (
(
string.ascii_uppercase +
string.ascii_uppercase +
string.digits +
string.ascii_uppercase +
string.ascii_uppercase +
'+/'
),
string.hexdigits.upper() + string.hexdigits.upper(),
string.ascii_uppercase + '=/',
)
YAML_EXTENSIONS = (
'.yaml',
'.yml',
Expand Down Expand Up @@ -75,12 +87,16 @@ def analyze_string(self, string, line_num, filename):
"""Searches string for custom pattern, and captures all high entropy strings that
match self.regex, with a limit defined as self.entropy_limit.
"""

output = {}

if WHITELIST_REGEX.search(string):
return output

uppercased_string = string.upper()
for sequential_string in IGNORED_SEQUENTIAL_STRINGS:
if uppercased_string in sequential_string:
return output

for result in self.secret_generator(string):
secret = PotentialSecret(self.secret_type, filename, line_num, result)
output[secret] = secret
Expand Down
1 change: 1 addition & 0 deletions test_data/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ credentials:
some_value_here: not_a_secret
other_value_here: 1234567890a
nested:
value: AKIAabcdefghijklmnop
value: abcdefghijklmnop
list_of_keys:
- 123
Expand Down
2 changes: 1 addition & 1 deletion test_data/short_files/last_line.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
secrets_for_no_one_to_find =
hunter2
password123
0123456789a
BEEF0123456789a
2 changes: 1 addition & 1 deletion tests/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def test_old_baseline_ignored_with_update_flag(
2:secrets_for_no_one_to_find =
3: hunter2
4: password123
5: 0123456789a
5: BEEF0123456789a
""")[1:-1],
),
],
Expand Down
3 changes: 1 addition & 2 deletions tests/plugins/high_entropy_strings_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,11 @@ def test_yaml_file(self):
with open('test_data/config.yaml') as f:
secrets = plugin.analyze(f, 'test_data/config.yaml')

assert len(secrets.values()) == 2
assert len(secrets.values()) == 1
for secret in secrets.values():
location = str(secret).splitlines()[1]
assert location in (
'Location: test_data/config.yaml:3',
'Location: test_data/config.yaml:5',
)

def test_entropy_lower_limit(self):
Expand Down

0 comments on commit 8c2d022

Please sign in to comment.