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

Use different regexes in KeywordDetector to improve accuracy #86

Merged
merged 19 commits into from
Jan 3, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
b9c80f9
Use different regexes in KeywordDetector to improve accuracy
KevinHock Oct 22, 2018
c4e7676
Trim regexes down, include secret 'foo'; as well.
KevinHock Oct 22, 2018
fce2837
Do not alert if whitespace is in secret
KevinHock Oct 22, 2018
3167cb1
[Keyword Plugin] Add 3 re's and their secret groups in dict
KevinHock Oct 31, 2018
b6dc9cc
Merge branch 'master' into upgrade_keyword_detector
KevinHock Oct 31, 2018
b89209f
Add a few more Keyword negative test-cases
KevinHock Oct 31, 2018
431ad12
Added a variety of accuracy improvements to Keyword Plugin (see tests)
KevinHock Nov 1, 2018
a27659a
:telescope:[Keyword Plugin] Filter false-positives
KevinHock Dec 14, 2018
a4c0432
Merge branch 'master' into upgrade_keyword_detector
KevinHock Dec 14, 2018
d8f4e29
:snake: Make tests pass
KevinHock Dec 14, 2018
ed6a374
:snake: Improve test coverage
KevinHock Dec 14, 2018
3fd9e87
:telescope:[Keyword Plugin] Precision improvements
KevinHock Dec 14, 2018
f15366e
:zap: Remove unnecessary wrapping parens
KevinHock Dec 21, 2018
e01d818
:telescope:[Keyword Plugin] Precision improvements
KevinHock Dec 28, 2018
4581aa8
:mortar_board: Eg. -> E.g.
KevinHock Dec 28, 2018
b7e48ab
:telescope:[Keyword Plugin] Handle dict['keyword']
KevinHock Dec 28, 2018
a37a9c9
:arrows_counterclockwise: Merge branch 'master' into upgrade_keyword_…
KevinHock Dec 28, 2018
d314550
:snake:[consistency] 2 spaces before pragma comment
KevinHock Dec 29, 2018
a29108b
:telescope:[Keyword Plugin] Precision improvements
KevinHock Jan 3, 2019
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
25 changes: 20 additions & 5 deletions detect_secrets/core/audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from ..plugins.core import initialize
from ..plugins.high_entropy_strings import HighEntropyStringsPlugin
from ..plugins.keyword import KeywordDetector
from .baseline import format_baseline_for_output
from .baseline import merge_results
from .bidirectional_iterator import BidirectionalIterator
Expand Down Expand Up @@ -518,7 +519,13 @@ def _get_secret_with_context(
)


def _highlight_secret(secret_line, secret_lineno, secret, filename, plugin_settings):
def _highlight_secret(
secret_line,
secret_lineno,
secret,
filename,
plugin_settings,
):
"""
:type secret_line: str
:param secret_line: the line on which the secret is found
Expand All @@ -544,7 +551,11 @@ def _highlight_secret(secret_line, secret_lineno, secret, filename, plugin_setti
plugin_settings,
)

for raw_secret in _raw_secret_generator(plugin, secret_line):
for raw_secret in _raw_secret_generator(
plugin,
secret_line,
is_php_file=filename.endswith('.php'),
):
secret_obj = PotentialSecret(
plugin.secret_type,
filename,
Expand Down Expand Up @@ -572,10 +583,14 @@ def _highlight_secret(secret_line, secret_lineno, secret, filename, plugin_setti
)


def _raw_secret_generator(plugin, secret_line):
def _raw_secret_generator(plugin, secret_line, is_php_file):
"""Generates raw secrets by re-scanning the line, with the specified plugin"""
for raw_secret in plugin.secret_generator(secret_line):
yield raw_secret
if isinstance(plugin, KeywordDetector):
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I felt :/ about writing it like this, but didn't see a better way.

Just wanted to put neon lights on it for the review 😅

for raw_secret in plugin.secret_generator(secret_line, is_php_file):
yield raw_secret
else:
for raw_secret in plugin.secret_generator(secret_line):
yield raw_secret

if issubclass(plugin.__class__, HighEntropyStringsPlugin):
with plugin.non_quoted_string_regex(strict=False):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
secret = 'BEEF0123456789a'
seecret = 'BEEF0123456789a'
skipped_sequential_false_positive = '0123456789a'
print('second line')
var = 'third line'
4 changes: 2 additions & 2 deletions tests/core/audit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ def test_compare(self, mock_printer):
# These files come after, because filenames are sorted first
assert headers[2] == textwrap.dedent("""
Secret: 3 of 4
Filename: test_data/short_files/first_line.py
Filename: test_data/short_files/first_line.php
Secret Type: Hex High Entropy String
Status: >> REMOVED <<
""")[1:]
Expand Down Expand Up @@ -406,7 +406,7 @@ def old_baseline(self):
],

# This entire file will be "removed"
'test_data/short_files/first_line.py': [
'test_data/short_files/first_line.php': [
{
'hashed_secret': '0de9a11b3f37872868ca49ecd726c955e25b6e21',
'line_number': 1,
Expand Down
4 changes: 2 additions & 2 deletions tests/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,9 @@ def test_old_baseline_ignored_with_update_flag(
'filename, expected_output',
[
(
'test_data/short_files/first_line.py',
'test_data/short_files/first_line.php',
textwrap.dedent("""
1:secret = 'BEEF0123456789a'
1:seecret = 'BEEF0123456789a'
2:skipped_sequential_false_positive = '0123456789a'
3:print('second line')
4:var = 'third line'
Expand Down
3 changes: 3 additions & 0 deletions tests/pre_commit_hook_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ def test_that_baseline_gets_updated(
'hex_limit': 3,
'name': 'HexHighEntropyString',
},
{
'name': 'KeywordDetector',
},
{
'name': 'PrivateKeyDetector',
},
Expand Down