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 1818# A mixin that adds the ability to turn a +String+ into sanitized uri
1919class String
2020
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+
2156 # Takes a uri and strips out any credentials it may contain.
2257 #
2358 # @return [String] the sanitized uri
2459 def sanitize_uri
25- keywords = /key|password|username|cred[entials]*[s]*|password|token|api[-_]token|api|auth[entication]*|access[-_]token|secret[-_]token/i
26-
2760 rich_uri = URI ( self )
2861 rich_uri . user = nil
2962 rich_uri . password = nil
3063
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 )
4967 rich_uri . query = query_params . chop
5068 end
69+
5170 rich_uri . to_s
5271 end
5372end
Original file line number Diff line number Diff line change 2323 include_context 'with application help'
2424
2525 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=***' )
2743 end
2844
2945 it 'does not sanatize uri with no credentials in' do
You can’t perform that action at this time.
0 commit comments