Skip to content

Commit 8ad4923

Browse files
author
Joshua Li
committed
refactor PrivateKeyDetector to use RegexBasedDetector
1 parent 5f4a055 commit 8ad4923

File tree

1 file changed

+15
-58
lines changed

1 file changed

+15
-58
lines changed

detect_secrets/plugins/private_key.py

+15-58
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,25 @@
1-
"""
2-
This code was extracted in part from
3-
https://github.com/pre-commit/pre-commit-hooks. Using similar heuristic logic,
4-
we adapted it to fit our plugin infrastructure, to create an organized,
5-
concerted effort in detecting all type of secrets in code.
6-
7-
Copyright (c) 2014 pre-commit dev team: Anthony Sottile, Ken Struys
8-
9-
Permission is hereby granted, free of charge, to any person obtaining a copy
10-
of this software and associated documentation files (the "Software"), to deal
11-
in the Software without restriction, including without limitation the rights
12-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13-
copies of the Software, and to permit persons to whom the Software is
14-
furnished to do so, subject to the following conditions:
15-
16-
The above copyright notice and this permission notice shall be included in
17-
all copies or substantial portions of the Software.
18-
19-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25-
THE SOFTWARE.
26-
"""
271
from __future__ import absolute_import
282

29-
from .base import BasePlugin
30-
from detect_secrets.core.potential_secret import PotentialSecret
31-
3+
import re
324

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-
)
5+
from .base import RegexBasedDetector
426

437

44-
class PrivateKeyDetector(BasePlugin):
8+
class PrivateKeyDetector(RegexBasedDetector):
459
"""This checks for private keys by determining whether the blacklisted
4610
lines are present in the analyzed string.
4711
"""
4812

4913
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
14+
blacklist = [
15+
re.compile(regexp)
16+
for regexp in (
17+
r'BEGIN RSA PRIVATE KEY',
18+
r'BEGIN DSA PRIVATE KEY',
19+
r'BEGIN EC PRIVATE KEY',
20+
r'BEGIN OPENSSH PRIVATE KEY',
21+
r'BEGIN PRIVATE KEY',
22+
r'PuTTY-User-Key-File-2',
23+
r'BEGIN SSH2 ENCRYPTED PRIVATE KEY',
24+
)
25+
]

0 commit comments

Comments
 (0)