Skip to content

Commit 72f44d8

Browse files
authored
Merge pull request #448 from pablosantiagolopez/feature/config-keyword-plugin
Keyword plugin: default regex modification
2 parents f4f7247 + c88f2e1 commit 72f44d8

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

Diff for: detect_secrets/plugins/keyword.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@
193193
),
194194
flags=re.IGNORECASE,
195195
)
196-
DENYLIST_REGEX_TO_GROUP = {
196+
CONFIG_DENYLIST_REGEX_TO_GROUP = {
197197
FOLLOWED_BY_COLON_REGEX: 4,
198198
PRECEDED_BY_EQUAL_COMPARISON_SIGNS_QUOTES_REQUIRED_REGEX: 2,
199199
FOLLOWED_BY_EQUAL_SIGNS_REGEX: 5,
@@ -230,6 +230,11 @@
230230
FileType.PYTHON: QUOTES_REQUIRED_DENYLIST_REGEX_TO_GROUP,
231231
FileType.SWIFT: QUOTES_REQUIRED_DENYLIST_REGEX_TO_GROUP,
232232
FileType.TERRAFORM: QUOTES_REQUIRED_DENYLIST_REGEX_TO_GROUP,
233+
FileType.YAML: CONFIG_DENYLIST_REGEX_TO_GROUP,
234+
FileType.CONFIG: CONFIG_DENYLIST_REGEX_TO_GROUP,
235+
FileType.INI: CONFIG_DENYLIST_REGEX_TO_GROUP,
236+
FileType.PROPERTIES: CONFIG_DENYLIST_REGEX_TO_GROUP,
237+
FileType.TOML: CONFIG_DENYLIST_REGEX_TO_GROUP,
233238
}
234239

235240

@@ -260,7 +265,6 @@ def analyze_string(
260265
if denylist_regex_to_group is None:
261266
attempts = [
262267
QUOTES_REQUIRED_DENYLIST_REGEX_TO_GROUP,
263-
DENYLIST_REGEX_TO_GROUP,
264268
]
265269
else:
266270
attempts = [denylist_regex_to_group]
@@ -284,7 +288,7 @@ def analyze_line(
284288
**kwargs: Any,
285289
) -> Set[PotentialSecret]:
286290
filetype = determine_file_type(filename)
287-
denylist_regex_to_group = REGEX_BY_FILETYPE.get(filetype, DENYLIST_REGEX_TO_GROUP)
291+
denylist_regex_to_group = REGEX_BY_FILETYPE.get(filetype, QUOTES_REQUIRED_DENYLIST_REGEX_TO_GROUP) # noqa: E501
288292
return super().analyze_line(
289293
filename=filename,
290294
line=line,

Diff for: detect_secrets/util/filetype.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ class FileType(Enum):
1717
C_SHARP = 11
1818
C = 12
1919
C_PLUS_PLUS = 13
20-
OTHER = 14
20+
CONFIG = 14
21+
INI = 15
22+
PROPERTIES = 16
23+
TOML = 17
24+
OTHER = 18
2125

2226

2327
def determine_file_type(filename: str) -> FileType:
@@ -39,5 +43,12 @@ def determine_file_type(filename: str) -> FileType:
3943
'.yml': FileType.YAML,
4044
'.cs': FileType.C_SHARP,
4145
'.c': FileType.C,
42-
'.cpp': FileType.C_PLUS_PLUS
46+
'.cpp': FileType.C_PLUS_PLUS,
47+
'.cnf': FileType.CONFIG,
48+
'.conf': FileType.CONFIG,
49+
'.cfg': FileType.CONFIG,
50+
'.cf': FileType.CONFIG,
51+
'.ini': FileType.INI,
52+
'.properties': FileType.PROPERTIES,
53+
'.toml': FileType.TOML
4354
}.get(file_extension, FileType.OTHER)

Diff for: tests/filters/heuristic_filter_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def test_failure(self, secret, line):
9090
(
9191
('secret = {hunter2}', False),
9292
('secret = <hunter2>', False),
93-
('secret = hunter2', True),
93+
('secret = "hunter2"', True),
9494
('secret= ${hunter2}', False),
9595
),
9696
)

Diff for: tests/plugins/keyword_test.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
LONG_LINE = '<img src="data:image/png;base64,{}\n"\n>'.format(base64.b64encode((str(randint(0, 9)) * 30500).encode())) # noqa: E501
1717

18-
GENERIC_TEST_CASES = [
18+
CONFIG_TEST_CASES = [
1919
('password = "{}"'.format(WHITES_SECRET), WHITES_SECRET),
2020
('password_super_secure = "{}"'.format(WHITES_SECRET), WHITES_SECRET), # Suffix
2121
('my_password_super_secure = "{}"'.format(WHITES_SECRET), WHITES_SECRET), # Prefix/suffix
@@ -40,7 +40,7 @@
4040
('password = {}'.format(SYMBOL_SECRET), None), # At least 1 alphanumeric character is required
4141
('api_key = ""', None), # Nothing in the quotes
4242
("secret: ''", None), # Nothing in the quotes
43-
('secret = "abcdefghi"', None), # Alphabet sequential string
43+
('password = "somefakekey"', None), # 'fake' in the secret
4444
('password: ${link}', None), # Has a ${ followed by a }
4545
('some_key = "real_secret"', None), # We cannot make 'key' a Keyword, too noisy)
4646
('private_key "hopenobodyfindsthisone\';', None), # Double-quote does not match single-quote)
@@ -144,7 +144,6 @@
144144
('password = {}'.format(COMMON_SECRET), None), # Secret without quotes
145145
('api_key = ""', None), # Nothing in the quotes
146146
("secret: ''", None), # Nothing in the quotes
147-
('password = "somefakekey"', None), # 'fake' in the secret
148147
('password: ${link}', None), # Has a ${ followed by a }
149148
('some_key = "real_secret"', None), # We cannot make 'key' a Keyword, too noisy)
150149
('private_key "hopenobodyfindsthisone\';', None), # Double-quote does not match single-quote)
@@ -163,7 +162,7 @@ def parse_test_cases(test_cases):
163162
'file_extension, line, expected_secret',
164163
(
165164
parse_test_cases([
166-
(None, GENERIC_TEST_CASES),
165+
('conf', CONFIG_TEST_CASES),
167166
('go', GOLANG_TEST_CASES),
168167
('m', COMMON_C_TEST_CASES),
169168
('c', COMMON_C_TEST_CASES),
@@ -175,6 +174,7 @@ def parse_test_cases(test_cases):
175174
('js', QUOTES_REQUIRED_TEST_CASES),
176175
('swift', QUOTES_REQUIRED_TEST_CASES),
177176
('tf', QUOTES_REQUIRED_TEST_CASES),
177+
(None, QUOTES_REQUIRED_TEST_CASES),
178178
])
179179
),
180180
)

0 commit comments

Comments
 (0)