Skip to content

Commit d40e4db

Browse files
authored
Merge pull request #428 from Yelp/feature/adding-alphanumerical-filter
adding filters.heuristic.is_not_alphanumeric_string
2 parents c86ca85 + bca4634 commit d40e4db

File tree

6 files changed

+23
-4
lines changed

6 files changed

+23
-4
lines changed

detect_secrets/filters/heuristic.py

+8
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,14 @@ def is_lock_file(filename: str) -> bool:
197197
}
198198

199199

200+
def is_not_alphanumeric_string(secret: str) -> bool:
201+
"""
202+
This assumes that secrets should have at least ONE letter in them.
203+
This helps avoid clear false positives, like `*****`.
204+
"""
205+
return not bool(set(string.ascii_letters) & set(secret))
206+
207+
200208
def is_swagger_file(filename: str) -> bool:
201209
"""
202210
Filters swagger files and paths, like swagger-ui.html or /swagger/.

detect_secrets/settings.py

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ def clear(self) -> None:
119119
'detect_secrets.filters.heuristic.is_prefixed_with_dollar_sign',
120120
'detect_secrets.filters.heuristic.is_indirect_reference',
121121
'detect_secrets.filters.heuristic.is_lock_file',
122+
'detect_secrets.filters.heuristic.is_not_alphanumeric_string',
122123
'detect_secrets.filters.heuristic.is_swagger_file',
123124
}
124125
}

docs/filters.md

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ the `detect_secrets.filters` namespace.
5151
| `heuristic.is_likely_id_string` | Ignores secret values prefixed with `id`. |
5252
| `heuristic.is_lock_file` | Ignores common lock files. |
5353
| `heuristic.is_non_text_file` | Ignores non-text files (e.g. archives, images). |
54+
| `heuristic.is_not_alphanumeric_string` | Ignores secrets that do not have a single alphanumeric character in it. |
5455
| `heuristic.is_potential_uuid` | Ignores uuid looking secret values. |
5556
| `heuristic.is_prefixed_with_dollar_sign` | Primarily for `KeywordDetector`, filters secrets like `secret = $variableName;`. |
5657
| `heuristic.is_sequential_string` | Ignores secrets like `abcdefg`. |

test_data/config.ini

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ keyA =
1818
value3
1919
; This is another comment
2020

21-
keyB = 456789123
22-
567891234
21+
keyB = 456789123a
22+
567891234b
2323

2424
keyC =
2525

tests/core/secrets_collection_test.py

-2
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,7 @@ def test_file_based_success_config():
8181

8282
assert [str(secret).splitlines()[1] for _, secret in secrets] == [
8383
'Location: test_data/config.ini:2',
84-
'Location: test_data/config.ini:6',
8584
'Location: test_data/config.ini:10',
86-
'Location: test_data/config.ini:15',
8785
'Location: test_data/config.ini:21',
8886
'Location: test_data/config.ini:22',
8987
'Location: test_data/config.ini:32',

tests/filters/heuristic_filter_test.py

+11
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,17 @@ def test_is_lock_file():
134134
assert not filters.heuristic.is_lock_file('Gemfilealock')
135135

136136

137+
@pytest.mark.parametrize(
138+
'secret, result',
139+
(
140+
('*****', True),
141+
('a&b23?!', False),
142+
),
143+
)
144+
def test_is_not_alphanumeric_string(secret, result):
145+
assert filters.heuristic.is_not_alphanumeric_string(secret) is result
146+
147+
137148
@pytest.mark.parametrize(
138149
'filename, result',
139150
(

0 commit comments

Comments
 (0)