File tree Expand file tree Collapse file tree 2 files changed +50
-2
lines changed Expand file tree Collapse file tree 2 files changed +50
-2
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 , _ |
40
+ params [ key ] = '***' if key . match ( keywords )
41
+ query_params += key + '=' + params [ key ] + '&'
42
+ end
43
+
44
+ query_params
45
+ end
46
+
21
47
# Takes a uri and strips out any credentials it may contain.
22
48
#
23
49
# @return [String] the sanitized uri
24
50
def sanitize_uri
25
51
rich_uri = URI ( self )
26
52
rich_uri . user = nil
27
53
rich_uri . password = nil
54
+
55
+ if rich_uri . query
56
+ params = ( URI . decode_www_form rich_uri . query ) . to_h
57
+ query_params = handle_params ( params )
58
+ rich_uri . query = query_params . chop
59
+ end
60
+
28
61
rich_uri . to_s
29
62
end
30
-
31
63
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=***' \
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