Skip to content

Commit

Permalink
↪️ Merge pull request #169 from dgzlopes/add-stripe-plugin
Browse files Browse the repository at this point in the history
Add Stripe detector to plugins
  • Loading branch information
KevinHock committed May 13, 2019
2 parents c86214a + 9ae909f commit 38b559c
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 0 deletions.
5 changes: 5 additions & 0 deletions detect_secrets/core/usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,11 @@ class PluginOptions(object):
disable_flag_text='--no-artifactory-scan',
disable_help_text='Disable scanning for Artifactory credentials',
),
PluginDescriptor(
classname='StripeDetector',
disable_flag_text='--no-stripe-scan',
disable_help_text='Disable scanning for Stripe keys',
),
]

def __init__(self, parser):
Expand Down
1 change: 1 addition & 0 deletions detect_secrets/plugins/common/initialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from ..keyword import KeywordDetector # noqa: F401
from ..private_key import PrivateKeyDetector # noqa: F401
from ..slack import SlackDetector # noqa: F401
from ..stripe import StripeDetector # noqa: F401
from detect_secrets.core.log import log
from detect_secrets.core.usage import PluginOptions

Expand Down
18 changes: 18 additions & 0 deletions detect_secrets/plugins/stripe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""
This plugin searches for Stripe keys
"""
from __future__ import absolute_import

import re

from .base import RegexBasedDetector


class StripeDetector(RegexBasedDetector):

secret_type = 'Stripe Access Key'

blacklist = (
# stripe standard keys begin with sk_live and restricted with rk_live
re.compile(r'(r|s)k_live_[0-9a-zA-Z]{24}'),
)
1 change: 1 addition & 0 deletions tests/core/usage_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def test_consolidates_output_basic(self):
'AWSKeyDetector': {},
'SlackDetector': {},
'ArtifactoryDetector': {},
'StripeDetector': {},
}
assert not hasattr(args, 'no_private_key_scan')

Expand Down
14 changes: 14 additions & 0 deletions tests/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def test_scan_string_basic(
KeywordDetector : False
PrivateKeyDetector : False
SlackDetector : False
StripeDetector : False
""".format(
expected_base64_result,
expected_hex_result,
Expand All @@ -119,6 +120,7 @@ def test_scan_string_cli_overrides_stdin(self):
KeywordDetector : False
PrivateKeyDetector : False
SlackDetector : False
StripeDetector : False
""")[1:]

def test_scan_with_all_files_flag(self, mock_baseline_initialize):
Expand Down Expand Up @@ -257,6 +259,9 @@ def test_old_baseline_ignored_with_update_flag(
{
"name": "SlackDetector",
},
{
"name": "StripeDetector",
},
],
),
( # remove some plugins from all plugins
Expand Down Expand Up @@ -288,6 +293,9 @@ def test_old_baseline_ignored_with_update_flag(
{
"name": "SlackDetector",
},
{
"name": "StripeDetector",
},
],
),
( # use same plugin list from baseline
Expand Down Expand Up @@ -375,6 +383,9 @@ def test_old_baseline_ignored_with_update_flag(
{
"name": "SlackDetector",
},
{
"name": "StripeDetector",
},
],
),
( # use plugin limit from baseline when using --use-all-plugins and no input limit
Expand Down Expand Up @@ -408,6 +419,9 @@ def test_old_baseline_ignored_with_update_flag(
{
"name": "SlackDetector",
},
{
"name": "StripeDetector",
},
],
),
],
Expand Down
40 changes: 40 additions & 0 deletions tests/plugins/stripe_key_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from __future__ import absolute_import
from __future__ import unicode_literals

import pytest

from detect_secrets.plugins.stripe import StripeDetector
from testing.mocks import mock_file_object


class TestStripeKeyDetector(object):

@pytest.mark.parametrize(
'file_content,should_flag',
[
(
'sk_live_ReTllpYQYfIZu2Jnf2lAPFjD',
True,
),
(
'rk_live_5TcWfjKmJgpql9hjpRnwRXbT',
True,
),
(
'pk_live_j5krY8XTgIcDaHDb3YrsAfCl',
False,
),
(
'sk_live_',
False,
),
],
)
def test_analyze(self, file_content, should_flag):
logic = StripeDetector()

f = mock_file_object(file_content)
output = logic.analyze(f, 'mock_filename')
assert len(output) == (1 if should_flag else 0)
for potential_secret in output:
assert 'mock_filename' == potential_secret.filename
3 changes: 3 additions & 0 deletions tests/pre_commit_hook_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ def test_that_baseline_gets_updated(
{
'name': 'SlackDetector',
},
{
'name': 'StripeDetector',
},
]

def test_writes_new_baseline_if_modified(self):
Expand Down

0 comments on commit 38b559c

Please sign in to comment.