File tree Expand file tree Collapse file tree 2 files changed +56
-21
lines changed Expand file tree Collapse file tree 2 files changed +56
-21
lines changed Original file line number Diff line number Diff line change 18
18
# A mixin that adds the ability to turn a +String+ into sanitized uri
19
19
class String
20
20
21
+ # Takes the uri query params and strips out credentials
22
+ #
23
+ # @return [String] the sanitized query params
24
+ def handle_params ( params )
25
+ keywords = /key
26
+ |password
27
+ |username
28
+ |cred(ential)*(s)*
29
+ |password
30
+ |token
31
+ |api[-_]token
32
+ |api
33
+ |auth(entication)*
34
+ |access[-_]token
35
+ |secret[-_]token/ix
36
+
37
+ query_params = ''
38
+
39
+ params . each do |key , value |
40
+ match = key . match ( keywords )
41
+
42
+ if match
43
+ params [ key ] = if match [ 0 ] == 'Api-Token' && value =~ /dt\w */
44
+ value . gsub ( /(dt\w *\. \w *)\. \w */ , '\1.REDACTED' )
45
+ else
46
+ '***'
47
+ end
48
+ end
49
+
50
+ query_params += key + '=' + params [ key ] + '&'
51
+ end
52
+
53
+ query_params
54
+ end
55
+
21
56
# Takes a uri and strips out any credentials it may contain.
22
57
#
23
58
# @return [String] the sanitized uri
24
59
def sanitize_uri
25
- keywords = /key|password|username|cred[entials]*[s]*|password|token|api[-_]token|api|auth[entication]*|access[-_]token|secret[-_]token/i
26
-
27
60
rich_uri = URI ( self )
28
61
rich_uri . user = nil
29
62
rich_uri . password = nil
30
63
31
- if ( rich_uri . query )
32
- params = Hash [ URI . decode_www_form rich_uri . query ]
33
-
34
- query_params = ""
35
-
36
- params . each do |key , value |
37
- match = key . match ( keywords )
38
-
39
- if ( match )
40
- if ( match [ 0 ] == "Api-Token" && value =~ /dt\w */ )
41
- params [ key ] = value . gsub ( /(dt\w *\. \w *)\. \w */ , '\1.REDACTED' )
42
- else
43
- params [ key ] = "***"
44
- end
45
- end
46
-
47
- query_params += key + "=" + params [ key ] + "&"
48
- end
64
+ if rich_uri . query
65
+ params = ( URI . decode_www_form rich_uri . query ) . to_h
66
+ query_params = handle_params ( params )
49
67
rich_uri . query = query_params . chop
50
68
end
69
+
51
70
rich_uri . to_s
52
71
end
53
72
end
Original file line number Diff line number Diff line change 23
23
include_context 'with application help'
24
24
25
25
it 'sanitizes uri with credentials in' do
26
- expect ( 'https://myuser:mypass@myhost/path/to/file' . sanitize_uri ) . to eq ( 'https://myhost/path/to/file' )
26
+ expect ( 'https://myuser:mypass@myhost/path/to/file' \
27
+ '?authentication=verysecret' \
28
+ '&cred=verysecret' \
29
+ '&password=verysecret' \
30
+ '&include=java' \
31
+ '&bitness=64' \
32
+ '&Api-Token=dt0c01.H67ALCXCXK7PWAAOQLENSRET.PRIVATEPART' \
33
+ '&secret-token=verysecret' \
34
+ '&token=123456789' . sanitize_uri ) . to eq ( 'https://myhost/path/to/file' \
35
+ '?authentication=***' \
36
+ '&cred=***' \
37
+ '&password=***' \
38
+ '&include=java' \
39
+ '&bitness=64' \
40
+ '&Api-Token=dt0c01.H67ALCXCXK7PWAAOQLENSRET.REDACTED' \
41
+ '&secret-token=***' \
42
+ '&token=***' )
27
43
end
28
44
29
45
it 'does not sanatize uri with no credentials in' do
You can’t perform that action at this time.
0 commit comments