11from detectors .find_secrets import detect_ast_secrets
22
33
4+ PASSWORD_REASON = (
5+ "variable name matched password/pwd/passwd pattern and value met minimum length"
6+ )
7+ API_KEY_REASON = (
8+ "variable name matched api_key/apikey pattern and value met minimum length"
9+ )
10+ TOKEN_REASON = "variable name matched token pattern and value met minimum length"
11+ SECRET_REASON = "variable name matched secret pattern and value met minimum length"
12+ AWS_REASON = "value matched AKIA-prefixed AWS access key pattern"
13+
14+
415# Detect a basic hardcoded password assignment
516def test_ast_basic_password ():
617 code = 'password = "abcdef"'
718 result = detect_ast_secrets (code )
819
9- assert result == [(1 , "Password" , "HIGH" , "abcdef" )]
20+ assert result == [(1 , "Password" , "HIGH" , "abcdef" , PASSWORD_REASON )]
1021
1122
1223# Detect password assigned through a simple object attribute
1324def test_ast_attribute_password ():
1425 code = 'self.password = "abcdef"'
1526 result = detect_ast_secrets (code )
1627
17- assert result == [(1 , "Password" , "HIGH" , "abcdef" )]
28+ assert result == [(1 , "Password" , "HIGH" , "abcdef" , PASSWORD_REASON )]
1829
1930
2031# Detect API key assigned through an attribute
2132def test_ast_api_key ():
2233 code = 'config.api_key = "12345678"'
2334 result = detect_ast_secrets (code )
2435
25- assert result == [(1 , "API Key" , "HIGH" , "12345678" )]
36+ assert result == [(1 , "API Key" , "HIGH" , "12345678" , API_KEY_REASON )]
2637
2738
2839# Detect token with correct MEDIUM severity
2940def test_ast_token ():
3041 code = 'user.token = "qwerty123"'
3142 result = detect_ast_secrets (code )
3243
33- assert result == [(1 , "Token" , "MEDIUM" , "qwerty123" )]
44+ assert result == [(1 , "Token" , "MEDIUM" , "qwerty123" , TOKEN_REASON )]
3445
3546
3647# Detect generic secret assignment
3748def test_ast_secret ():
3849 code = 'client_secret = "abcdef"'
3950 result = detect_ast_secrets (code )
4051
41- assert result == [(1 , "Secret" , "MEDIUM" , "abcdef" )]
52+ assert result == [(1 , "Secret" , "MEDIUM" , "abcdef" , SECRET_REASON )]
4253
4354
4455# Detect AWS access key by value, regardless of variable name
4556def test_ast_aws_access_key_value ():
4657 code = 'random_var = "AKIAEXAMPLE123456789"'
4758 result = detect_ast_secrets (code )
4859
49- assert result == [(1 , "AWS Access Key" , "HIGH" , "AKIAEXAMPLE123456789" )]
60+ assert result == [(1 , "AWS Access Key" , "HIGH" , "AKIAEXAMPLE123456789" , AWS_REASON )]
5061
5162
5263# Detect multiple classifications when both value and variable name match
@@ -55,8 +66,8 @@ def test_ast_aws_access_key_with_api_key_variable():
5566 result = detect_ast_secrets (code )
5667
5768 assert result == [
58- (1 , "AWS Access Key" , "HIGH" , "AKIAEXAMPLE123456789" ),
59- (1 , "API Key" , "HIGH" , "AKIAEXAMPLE123456789" ),
69+ (1 , "AWS Access Key" , "HIGH" , "AKIAEXAMPLE123456789" , AWS_REASON ),
70+ (1 , "API Key" , "HIGH" , "AKIAEXAMPLE123456789" , API_KEY_REASON ),
6071 ]
6172
6273
@@ -94,9 +105,9 @@ def test_ast_multiple_assignments():
94105 result = detect_ast_secrets (code )
95106
96107 assert result == [
97- (2 , "Password" , "HIGH" , "abcdef" ),
98- (3 , "API Key" , "HIGH" , "12345678" ),
99- (4 , "Token" , "MEDIUM" , "qwerty123" ),
108+ (2 , "Password" , "HIGH" , "abcdef" , PASSWORD_REASON ),
109+ (3 , "API Key" , "HIGH" , "12345678" , API_KEY_REASON ),
110+ (4 , "Token" , "MEDIUM" , "qwerty123" , TOKEN_REASON ),
100111 ]
101112
102113
@@ -113,15 +124,15 @@ def test_ast_uppercase_variable():
113124 code = 'PASSWORD = "abcdef"'
114125 result = detect_ast_secrets (code )
115126
116- assert result == [(1 , "Password" , "HIGH" , "abcdef" )]
127+ assert result == [(1 , "Password" , "HIGH" , "abcdef" , PASSWORD_REASON )]
117128
118129
119130# Process multiple assignment targets correctly
120131def test_ast_multiple_targets ():
121132 code = 'a = password = "abcdef"'
122133 result = detect_ast_secrets (code )
123134
124- assert result == [(1 , "Password" , "HIGH" , "abcdef" )]
135+ assert result == [(1 , "Password" , "HIGH" , "abcdef" , PASSWORD_REASON )]
125136
126137
127138# Process multiple sensitive targets assigned the same value
@@ -130,8 +141,8 @@ def test_ast_multiple_sensitive_targets():
130141 result = detect_ast_secrets (code )
131142
132143 assert result == [
133- (1 , "Password" , "HIGH" , "abcdef" ),
134- (1 , "Token" , "MEDIUM" , "abcdef" ),
144+ (1 , "Password" , "HIGH" , "abcdef" , PASSWORD_REASON ),
145+ (1 , "Token" , "MEDIUM" , "abcdef" , TOKEN_REASON ),
135146 ]
136147
137148
@@ -140,15 +151,15 @@ def test_ast_nested_attribute():
140151 code = 'self.config.db.password = "abcdef"'
141152 result = detect_ast_secrets (code )
142153
143- assert result == [(1 , "Password" , "HIGH" , "abcdef" )]
154+ assert result == [(1 , "Password" , "HIGH" , "abcdef" , PASSWORD_REASON )]
144155
145156
146157# Detect API keys in deeply nested attribute chains
147158def test_ast_deep_nested_api_key ():
148159 code = 'settings.auth.credentials.api_key = "12345678"'
149160 result = detect_ast_secrets (code )
150161
151- assert result == [(1 , "API Key" , "HIGH" , "12345678" )]
162+ assert result == [(1 , "API Key" , "HIGH" , "12345678" , API_KEY_REASON )]
152163
153164
154165# Ignore unsupported assignment targets such as subscript assignments
@@ -164,7 +175,9 @@ def test_ast_complex_password_value():
164175 code = 'password = "abc_def-123#$%^&*()"'
165176 result = detect_ast_secrets (code )
166177
167- assert result == [(1 , "Password" , "HIGH" , "abc_def-123#$%^&*()" )]
178+ assert result == [
179+ (1 , "Password" , "HIGH" , "abc_def-123#$%^&*()" , PASSWORD_REASON )
180+ ]
168181
169182
170183# Handle indented multiline code by normalizing indentation
@@ -175,4 +188,4 @@ def test_ast_dedented_multiline_code():
175188 """
176189 result = detect_ast_secrets (code )
177190
178- assert result == [(2 , "Password" , "HIGH" , "abcdef" )]
191+ assert result == [(2 , "Password" , "HIGH" , "abcdef" , PASSWORD_REASON )]
0 commit comments