Skip to content

Commit 38b559c

Browse files
authored
↪️ Merge pull request #169 from dgzlopes/add-stripe-plugin
Add Stripe detector to plugins
2 parents c86214a + 9ae909f commit 38b559c

File tree

7 files changed

+82
-0
lines changed

7 files changed

+82
-0
lines changed

detect_secrets/core/usage.py

+5
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,11 @@ class PluginOptions(object):
295295
disable_flag_text='--no-artifactory-scan',
296296
disable_help_text='Disable scanning for Artifactory credentials',
297297
),
298+
PluginDescriptor(
299+
classname='StripeDetector',
300+
disable_flag_text='--no-stripe-scan',
301+
disable_help_text='Disable scanning for Stripe keys',
302+
),
298303
]
299304

300305
def __init__(self, parser):

detect_secrets/plugins/common/initialize.py

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from ..keyword import KeywordDetector # noqa: F401
1414
from ..private_key import PrivateKeyDetector # noqa: F401
1515
from ..slack import SlackDetector # noqa: F401
16+
from ..stripe import StripeDetector # noqa: F401
1617
from detect_secrets.core.log import log
1718
from detect_secrets.core.usage import PluginOptions
1819

detect_secrets/plugins/stripe.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""
2+
This plugin searches for Stripe keys
3+
"""
4+
from __future__ import absolute_import
5+
6+
import re
7+
8+
from .base import RegexBasedDetector
9+
10+
11+
class StripeDetector(RegexBasedDetector):
12+
13+
secret_type = 'Stripe Access Key'
14+
15+
blacklist = (
16+
# stripe standard keys begin with sk_live and restricted with rk_live
17+
re.compile(r'(r|s)k_live_[0-9a-zA-Z]{24}'),
18+
)

tests/core/usage_test.py

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def test_consolidates_output_basic(self):
4040
'AWSKeyDetector': {},
4141
'SlackDetector': {},
4242
'ArtifactoryDetector': {},
43+
'StripeDetector': {},
4344
}
4445
assert not hasattr(args, 'no_private_key_scan')
4546

tests/main_test.py

+14
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ def test_scan_string_basic(
9696
KeywordDetector : False
9797
PrivateKeyDetector : False
9898
SlackDetector : False
99+
StripeDetector : False
99100
""".format(
100101
expected_base64_result,
101102
expected_hex_result,
@@ -119,6 +120,7 @@ def test_scan_string_cli_overrides_stdin(self):
119120
KeywordDetector : False
120121
PrivateKeyDetector : False
121122
SlackDetector : False
123+
StripeDetector : False
122124
""")[1:]
123125

124126
def test_scan_with_all_files_flag(self, mock_baseline_initialize):
@@ -257,6 +259,9 @@ def test_old_baseline_ignored_with_update_flag(
257259
{
258260
"name": "SlackDetector",
259261
},
262+
{
263+
"name": "StripeDetector",
264+
},
260265
],
261266
),
262267
( # remove some plugins from all plugins
@@ -288,6 +293,9 @@ def test_old_baseline_ignored_with_update_flag(
288293
{
289294
"name": "SlackDetector",
290295
},
296+
{
297+
"name": "StripeDetector",
298+
},
291299
],
292300
),
293301
( # use same plugin list from baseline
@@ -375,6 +383,9 @@ def test_old_baseline_ignored_with_update_flag(
375383
{
376384
"name": "SlackDetector",
377385
},
386+
{
387+
"name": "StripeDetector",
388+
},
378389
],
379390
),
380391
( # use plugin limit from baseline when using --use-all-plugins and no input limit
@@ -408,6 +419,9 @@ def test_old_baseline_ignored_with_update_flag(
408419
{
409420
"name": "SlackDetector",
410421
},
422+
{
423+
"name": "StripeDetector",
424+
},
411425
],
412426
),
413427
],

tests/plugins/stripe_key_test.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from __future__ import absolute_import
2+
from __future__ import unicode_literals
3+
4+
import pytest
5+
6+
from detect_secrets.plugins.stripe import StripeDetector
7+
from testing.mocks import mock_file_object
8+
9+
10+
class TestStripeKeyDetector(object):
11+
12+
@pytest.mark.parametrize(
13+
'file_content,should_flag',
14+
[
15+
(
16+
'sk_live_ReTllpYQYfIZu2Jnf2lAPFjD',
17+
True,
18+
),
19+
(
20+
'rk_live_5TcWfjKmJgpql9hjpRnwRXbT',
21+
True,
22+
),
23+
(
24+
'pk_live_j5krY8XTgIcDaHDb3YrsAfCl',
25+
False,
26+
),
27+
(
28+
'sk_live_',
29+
False,
30+
),
31+
],
32+
)
33+
def test_analyze(self, file_content, should_flag):
34+
logic = StripeDetector()
35+
36+
f = mock_file_object(file_content)
37+
output = logic.analyze(f, 'mock_filename')
38+
assert len(output) == (1 if should_flag else 0)
39+
for potential_secret in output:
40+
assert 'mock_filename' == potential_secret.filename

tests/pre_commit_hook_test.py

+3
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ def test_that_baseline_gets_updated(
194194
{
195195
'name': 'SlackDetector',
196196
},
197+
{
198+
'name': 'StripeDetector',
199+
},
197200
]
198201

199202
def test_writes_new_baseline_if_modified(self):

0 commit comments

Comments
 (0)