Skip to content

Commit fe3e989

Browse files
committed
🔭 [Keyword Plugin] Make quotes required for .java
Make a `QUOTES_REQUIRED_FILETYPES` dict for efficiency
1 parent 3c9c6c4 commit fe3e989

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

detect_secrets/plugins/common/filetype.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33

44
class FileType(Enum):
55
CLS = 0
6-
JAVASCRIPT = 1
7-
PHP = 2
8-
PYTHON = 3
9-
YAML = 4
10-
OTHER = 5
6+
JAVA = 1
7+
JAVASCRIPT = 2
8+
PHP = 3
9+
PYTHON = 4
10+
YAML = 5
11+
OTHER = 6
1112

1213

1314
def determine_file_type(filename):
@@ -18,6 +19,8 @@ def determine_file_type(filename):
1819
"""
1920
if filename.endswith('.cls'):
2021
return FileType.CLS
22+
elif filename.endswith('.java'):
23+
return FileType.JAVA
2124
elif filename.endswith('.js'):
2225
return FileType.JAVASCRIPT
2326
elif filename.endswith('.php'):

detect_secrets/plugins/keyword.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@
147147
FOLLOWED_BY_EQUAL_SIGNS_QUOTES_REQUIRED_REGEX: 9,
148148
FOLLOWED_BY_QUOTES_AND_SEMICOLON_REGEX: 5,
149149
}
150+
QUOTES_REQUIRED_FILETYPES = {
151+
FileType.CLS,
152+
FileType.JAVA,
153+
FileType.PYTHON,
154+
}
150155

151156

152157
class KeywordDetector(BasePlugin):
@@ -192,10 +197,7 @@ def analyze_string_content(self, string, line_num, filename):
192197
def secret_generator(self, string, filetype):
193198
lowered_string = string.lower()
194199

195-
if filetype in (
196-
FileType.CLS,
197-
FileType.PYTHON,
198-
):
200+
if filetype in QUOTES_REQUIRED_FILETYPES:
199201
blacklist_regex_to_group = QUOTES_REQUIRED_BLACKLIST_REGEX_TO_GROUP
200202
else:
201203
blacklist_regex_to_group = BLACKLIST_REGEX_TO_GROUP

tests/plugins/keyword_test.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
from testing.mocks import mock_file_object
99

1010

11+
QUOTES_REQUIRED_FILE_EXTENSIONS = (
12+
'.cls',
13+
'.java',
14+
'.py',
15+
)
1116
STANDARD_NEGATIVES = [
1217
# FOLLOWED_BY_COLON_RE
1318
'theapikey: ""', # Nothing in the quotes
@@ -111,10 +116,7 @@ def test_analyze_with_line_exclude(self, file_content):
111116
'my_password ={{h}o)p${e]nob(ody[finds>-_$#thisone}}',
112117
'the_password={{h}o)p${e]nob(ody[finds>-_$#thisone}}\n',
113118
}
114-
) for file_extension in (
115-
'.cls',
116-
'.py',
117-
)
119+
) for file_extension in QUOTES_REQUIRED_FILE_EXTENSIONS
118120
),
119121
)
120122
def test_analyze_quotes_required_positives(self, file_content, file_extension):
@@ -190,10 +192,7 @@ def test_analyze_php_negatives(self, file_content):
190192
'my_password =hope]nobody[finds>-_$#thisone',
191193
'the_password=hope]nobody[finds>-_$#thisone\n',
192194
]
193-
) for file_extension in (
194-
'.cls',
195-
'.py',
196-
)
195+
) for file_extension in QUOTES_REQUIRED_FILE_EXTENSIONS
197196
),
198197
)
199198
def test_analyze_quotes_required_negatives(self, file_content, file_extension):

0 commit comments

Comments
 (0)