Skip to content

Commit e07c7d1

Browse files
authored
Make is_likely_id_string More Strict (#526)
* Make is_likely_id_string more strict to avoid filtering true positives * Clarify test comment * Fix regex capturing groups * Add plural ids to is_likely_id_string regex * Add regex detail comments
1 parent 9ea24b1 commit e07c7d1

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

detect_secrets/filters/heuristic.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,14 @@ def is_likely_id_string(secret: str, line: str) -> bool:
6868

6969
@lru_cache(maxsize=1)
7070
def _get_id_detector_regex() -> Pattern:
71-
return re.compile(r'id[^a-z0-9]', re.IGNORECASE)
71+
"""
72+
Regex Details:
73+
^(id|myid|userid) -> Common id identifiers with no prefix
74+
_id -> id identifier with prefixes allowed
75+
s? -> Optional plural id identifier
76+
[^a-z0-9] -> Non-letter/numeric character
77+
"""
78+
return re.compile(r'(^(id|myid|userid)|_id)s?[^a-z0-9]', re.IGNORECASE)
7279

7380

7481
def is_non_text_file(filename: str) -> bool:

tests/filters/heuristic_filter_test.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ class TestIsLikelyIdString:
6363
('RANDOM_STRING', 'myid: RANDOM_STRING'),
6464
('RANDOM_STRING', 'myid=RANDOM_STRING'),
6565
('RANDOM_STRING', 'myid = RANDOM_STRING'),
66+
('RANDOM_STRING', 'userid: RANDOM_STRING'),
67+
('RANDOM_STRING', 'userid=RANDOM_STRING'),
68+
('RANDOM_STRING', 'userid = RANDOM_STRING'),
69+
('RANDOM_STRING', 'data test_id: RANDOM_STRING'),
70+
('RANDOM_STRING', 'data test_id=RANDOM_STRING'),
71+
('RANDOM_STRING', 'data test_id = RANDOM_STRING'),
72+
('RANDOM_STRING', 'ids = RANDOM_STRING, RANDOM_STRING'),
73+
('RANDOM_STRING', 'my_ids: RANDOM_STRING, RANDOM_STRING'),
6674
],
6775
)
6876
def test_success(self, secret, line):
@@ -79,6 +87,9 @@ def test_success(self, secret, line):
7987
8088
# fail silently if the secret isn't even on the line
8189
('SOME_RANDOM_STRING', 'id: SOME_OTHER_RANDOM_STRING'),
90+
91+
# fail although the word david ends in id
92+
('RANDOM_STRING', 'postgres://david:RANDOM_STRING'),
8293
],
8394
)
8495
def test_failure(self, secret, line):

0 commit comments

Comments
 (0)