1
1
import re
2
-
3
2
import requests
4
3
4
+ from .base import RegexBasedDetector
5
5
from detect_secrets .core .constants import VerifiedResult
6
- from detect_secrets .plugins .base import RegexBasedDetector
7
6
8
7
9
- class SoftlayerDetector (RegexBasedDetector ):
10
- """Scans for Softlayer credentials."""
8
+ class SoftLayerDetector (RegexBasedDetector ):
11
9
12
10
secret_type = 'SoftLayer Credentials'
13
11
14
12
# opt means optional
15
- sl = r'(?:softlayer|sl)(?:_|-|)(?:api|)'
16
- key_or_pass = r'(?:key|pwd|password|pass|token)'
13
+ opt_quote = r'(?:"|\'|)'
14
+ opt_dashes = r'(?:--|)'
15
+ sl = r'(?:softlayer|sl)'
16
+ opt_dash_undrscr = r'(?:_|-|)'
17
+ opt_api = r'(?:api|)'
18
+ key_or_pass = r'(?:key|pwd|password|pass)'
19
+ opt_space = r'(?: |)'
20
+ opt_equals = r'(?:=|:|:=|=>|)'
17
21
secret = r'([a-z0-9]{64})'
18
22
denylist = [
19
- RegexBasedDetector .assign_regex_generator (
20
- prefix_regex = sl ,
21
- secret_keyword_regex = key_or_pass ,
22
- secret_regex = secret ,
23
+ re .compile (
24
+ r'{opt_quote}{opt_dashes}{sl}{opt_dash_undrscr}{opt_api}{opt_dash_undrscr}{key_or_pass}'
25
+ '{opt_quote}{opt_space}{opt_equals}{opt_space}{opt_quote}{secret}{opt_quote}' .format (
26
+ opt_quote = opt_quote ,
27
+ opt_dashes = opt_dashes ,
28
+ sl = sl ,
29
+ opt_dash_undrscr = opt_dash_undrscr ,
30
+ opt_api = opt_api ,
31
+ key_or_pass = key_or_pass ,
32
+ opt_space = opt_space ,
33
+ opt_equals = opt_equals ,
34
+ secret = secret ,
35
+ ), flags = re .IGNORECASE ,
23
36
),
24
-
25
37
re .compile (
26
38
r'(?:http|https)://api.softlayer.com/soap/(?:v3|v3.1)/([a-z0-9]{64})' ,
27
39
flags = re .IGNORECASE ,
28
40
),
29
41
]
30
42
31
43
def verify (self , token , content ):
32
- usernames = find_username (content )
44
+ usernames = get_username (content )
33
45
if not usernames :
34
46
return VerifiedResult .UNVERIFIED
35
47
@@ -39,21 +51,32 @@ def verify(self, token, content):
39
51
return VerifiedResult .VERIFIED_FALSE
40
52
41
53
42
- def find_username (content ):
54
+ def get_username (content ):
43
55
# opt means optional
44
- username_keyword = (
45
- r'(?:'
46
- r'username|id|user|userid|user-id|user-name|'
47
- r'name|user_id|user_name|uname'
48
- r')'
49
- )
56
+ opt_quote = r'(?:"|\'|)'
57
+ opt_dashes = r'(?:--|)'
58
+ opt_sl = r'(?:softlayer|sl|)'
59
+ opt_dash_undrscr = r'(?:_|-|)'
60
+ opt_api = r'(?:api|)'
61
+ username_keyword = r'(?:username|id|user|userid|user-id|user-name|name|user_id|user_name|uname)'
62
+ opt_space = r'(?: |)'
63
+ opt_equals = r'(?:=|:|:=|=>|)'
64
+ seperator = r'(?: |=|:|:=|=>)+'
50
65
username = r'(\w(?:\w|_|@|\.|-)+)'
51
66
regex = re .compile (
52
- RegexBasedDetector .assign_regex_generator (
53
- prefix_regex = SoftlayerDetector .sl ,
54
- secret_keyword_regex = username_keyword ,
55
- secret_regex = username ,
56
- ),
67
+ r'{opt_quote}{opt_dashes}{opt_sl}{opt_dash_undrscr}{opt_api}{opt_dash_undrscr}'
68
+ '{username_keyword}{opt_quote}{seperator}{opt_quote}{username}{opt_quote}' .format (
69
+ opt_quote = opt_quote ,
70
+ opt_dashes = opt_dashes ,
71
+ opt_sl = opt_sl ,
72
+ opt_dash_undrscr = opt_dash_undrscr ,
73
+ opt_api = opt_api ,
74
+ username_keyword = username_keyword ,
75
+ opt_space = opt_space ,
76
+ opt_equals = opt_equals ,
77
+ username = username ,
78
+ seperator = seperator ,
79
+ ), flags = re .IGNORECASE ,
57
80
)
58
81
59
82
return [
@@ -64,16 +87,16 @@ def find_username(content):
64
87
65
88
66
89
def verify_softlayer_key (username , token ):
67
- headers = {'Content-type' : 'application/json' }
68
90
try :
91
+ headers = {'Content-type' : 'application/json' }
69
92
response = requests .get (
70
93
'https://api.softlayer.com/rest/v3/SoftLayer_Account.json' ,
71
94
auth = (username , token ), headers = headers ,
72
95
)
73
- except requests .exceptions .RequestException :
74
- return VerifiedResult .UNVERIFIED
75
96
76
- if response .status_code == 200 :
77
- return VerifiedResult .VERIFIED_TRUE
78
- else :
79
- return VerifiedResult .VERIFIED_FALSE
97
+ if response .status_code == 200 :
98
+ return VerifiedResult .VERIFIED_TRUE
99
+ else :
100
+ return VerifiedResult .VERIFIED_FALSE
101
+ except Exception :
102
+ return VerifiedResult .UNVERIFIED
0 commit comments