Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for audit #83

Merged
merged 5 commits into from
Oct 10, 2018
Merged
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
15 changes: 11 additions & 4 deletions detect_secrets/core/audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,12 @@ def _get_secret_with_context(
if len(output) < end_line - start_line + 1:
# This handles the case of a short file.
num_lines_in_file = int(subprocess.check_output([
'wc',
'-l',
# https://stackoverflow.com/a/38870057
# - 'wc -l' cannot be used here because if the last char
# of the file isn't \n, then the last line isn't counted
'grep',
'-c',
'',
filename,
]).decode('utf-8').split()[0])

Expand Down Expand Up @@ -324,11 +328,14 @@ def _highlight_secret(secret_line, secret_lineno, secret, filename, plugin_setti
else:
raise SecretNotFoundOnSpecifiedLineError(secret_lineno)

index_of_secret = secret_line.index(raw_secret)
index_of_secret = secret_line.lower().index(raw_secret.lower())
end_of_secret = index_of_secret + len(raw_secret)
return '{}{}{}'.format(
secret_line[:index_of_secret],
BashColor.color(
raw_secret,
# copy the secret out of the line because .lower() from secret
# generator may be different from the original value:
secret_line[index_of_secret:end_of_secret],
Color.RED,
),
secret_line[index_of_secret + len(raw_secret):],
Expand Down
13 changes: 9 additions & 4 deletions detect_secrets/plugins/keyword.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@


BLACKLIST = (
'PASS =',
# NOTE all values here should be lowercase,
# otherwise _secret_generator can fail to match them
'pass =',
'password',
'passwd',
'pwd',
Expand All @@ -55,7 +57,7 @@ def analyze_string(self, string, line_num, filename):
if WHITELIST_REGEX.search(string):
return output

for identifier in self.secret_generator(string.lower()):
for identifier in self.secret_generator(string):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably more performant to make the string lowered once here, and passed into secret_generator, than to make it .lower() every time.

secret = PotentialSecret(
self.secret_type,
filename,
Expand All @@ -66,7 +68,10 @@ def analyze_string(self, string, line_num, filename):

return output

def secret_generator(self, string):
def _secret_generator(self, lowercase_string):
for line in BLACKLIST:
if line in string:
if line in lowercase_string:
yield line

def secret_generator(self, string):
return self._secret_generator(string.lower())
5 changes: 5 additions & 0 deletions tests/plugins/keyword_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class TestKeywordDetector(object):
(
'token = "noentropy"'
),
(
'PASSWORD = "verysimple"'
),
],
)
def test_analyze(self, file_content):
Expand All @@ -28,3 +31,5 @@ def test_analyze(self, file_content):
assert len(output) == 1
for potential_secret in output:
assert 'mock_filename' == potential_secret.filename
generated = list(logic.secret_generator(file_content))
assert len(generated) == len(output)