Skip to content

Commit 66bfb4c

Browse files
authored
Merge pull request #103 from JoshuaRLi/refactor-detectors
refactor various detectors to use RegexBasedDetector
2 parents 5f4a055 + 2f24180 commit 66bfb4c

File tree

3 files changed

+27
-62
lines changed

3 files changed

+27
-62
lines changed

detect_secrets/plugins/base.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,5 @@ def analyze_string(self, string, line_num, filename):
126126

127127
def secret_generator(self, string):
128128
for regex in self.blacklist:
129-
if regex.search(string):
130-
yield regex.pattern
129+
for match in regex.findall(string):
130+
yield match

detect_secrets/plugins/basic_auth.py

+10-28
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,20 @@
22

33
import re
44

5-
from .base import BasePlugin
6-
from detect_secrets.core.potential_secret import PotentialSecret
5+
from .base import RegexBasedDetector
76

87

98
SPECIAL_URL_CHARACTERS = ':/?#[]@'
10-
BASIC_AUTH_REGEX = re.compile(
11-
r'://[^{}\s]+:([^{}\s]+)@'.format(
12-
re.escape(SPECIAL_URL_CHARACTERS),
13-
re.escape(SPECIAL_URL_CHARACTERS),
14-
),
15-
)
169

1710

18-
class BasicAuthDetector(BasePlugin):
11+
class BasicAuthDetector(RegexBasedDetector):
1912

2013
secret_type = 'Basic Auth Credentials'
21-
22-
def analyze_string(self, string, line_num, filename):
23-
output = {}
24-
25-
for result in self.secret_generator(string):
26-
secret = PotentialSecret(
27-
self.secret_type,
28-
filename,
29-
result,
30-
line_num,
31-
)
32-
output[secret] = secret
33-
34-
return output
35-
36-
def secret_generator(self, string):
37-
results = BASIC_AUTH_REGEX.findall(string)
38-
for result in results:
39-
yield result
14+
blacklist = [
15+
re.compile(
16+
r'://[^{}\s]+:([^{}\s]+)@'.format(
17+
re.escape(SPECIAL_URL_CHARACTERS),
18+
re.escape(SPECIAL_URL_CHARACTERS),
19+
),
20+
),
21+
]

detect_secrets/plugins/private_key.py

+15-32
Original file line numberDiff line numberDiff line change
@@ -26,43 +26,26 @@
2626
"""
2727
from __future__ import absolute_import
2828

29-
from .base import BasePlugin
30-
from detect_secrets.core.potential_secret import PotentialSecret
29+
import re
3130

31+
from .base import RegexBasedDetector
3232

33-
BLACKLIST = (
34-
'BEGIN RSA PRIVATE KEY',
35-
'BEGIN DSA PRIVATE KEY',
36-
'BEGIN EC PRIVATE KEY',
37-
'BEGIN OPENSSH PRIVATE KEY',
38-
'BEGIN PRIVATE KEY',
39-
'PuTTY-User-Key-File-2',
40-
'BEGIN SSH2 ENCRYPTED PRIVATE KEY',
41-
)
4233

43-
44-
class PrivateKeyDetector(BasePlugin):
34+
class PrivateKeyDetector(RegexBasedDetector):
4535
"""This checks for private keys by determining whether the blacklisted
4636
lines are present in the analyzed string.
4737
"""
4838

4939
secret_type = 'Private Key'
50-
51-
def analyze_string(self, string, line_num, filename):
52-
output = {}
53-
54-
for identifier in self.secret_generator(string):
55-
secret = PotentialSecret(
56-
self.secret_type,
57-
filename,
58-
identifier,
59-
line_num,
60-
)
61-
output[secret] = secret
62-
63-
return output
64-
65-
def secret_generator(self, string):
66-
for line in BLACKLIST:
67-
if line in string:
68-
yield line
40+
blacklist = [
41+
re.compile(regexp)
42+
for regexp in (
43+
r'BEGIN RSA PRIVATE KEY',
44+
r'BEGIN DSA PRIVATE KEY',
45+
r'BEGIN EC PRIVATE KEY',
46+
r'BEGIN OPENSSH PRIVATE KEY',
47+
r'BEGIN PRIVATE KEY',
48+
r'PuTTY-User-Key-File-2',
49+
r'BEGIN SSH2 ENCRYPTED PRIVATE KEY',
50+
)
51+
]

0 commit comments

Comments
 (0)