Skip to content

Commit

Permalink
fix: unicode error during audit
Browse files Browse the repository at this point in the history
Fix an issue when there is unicode in a file containing secret,
the audit would fail with UnicodeEncodeError
  • Loading branch information
Xianjun Zhu committed Feb 12, 2019
1 parent c2c3697 commit 7b83715
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
1 change: 1 addition & 0 deletions detect_secrets/core/audit.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from __future__ import print_function
from __future__ import unicode_literals

import codecs
import itertools
Expand Down
6 changes: 5 additions & 1 deletion testing/mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,11 @@ def __init__(self):
self.clear()

def add(self, message, *args, **kwargs):
self.message += str(message) + '\n'
try:
# For python 2.x compatible
self.message += unicode(message) + '\n'
except NameError:
self.message += str(message) + '\n'

def clear(self):
self.message = ''
Expand Down
34 changes: 34 additions & 0 deletions tests/core/audit_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from __future__ import absolute_import
from __future__ import unicode_literals

import string
import textwrap
Expand Down Expand Up @@ -685,6 +686,39 @@ def test_keyword_secret_in_yaml_file(self, mock_printer):
""")[1:-1]

def test_unicode_in_output(self, mock_printer):
# Instead of mocking open, read from file with
# unicode in it to mimic the audit error
self.run_logic(
secret=potential_secret_factory(
type_='Base64 High Entropy String',
filename="test_data/config.md",
secret='ToCynx5Se4e2PtoZxEhW7lUJcOX15c54',
lineno=10,
).json(),
settings=[
{
"base64_limit": 4.5,
"name": "Base64HighEntropyString",
},
],
)

assert uncolor(mock_printer.message) == textwrap.dedent("""
Secret: 1 of 2
Filename: test_data/config.md
Secret Type: Base64 High Entropy String
----------
5:Test Unicode in non ini file would not fail on python 2.7.
6:
7:\u256D\u2500 diagnose
8:\u2570\u00BB ssh to server x:22324241234423414
9:
10:key="ToCynx5Se4e2PtoZxEhW7lUJcOX15c54"
----------
""")[1:-1]


class TestGetUserDecision(object):

Expand Down

0 comments on commit 7b83715

Please sign in to comment.