Skip to content

Commit f73c65b

Browse files
committed
Tests added
1 parent 10ea15a commit f73c65b

File tree

2 files changed

+56
-21
lines changed

2 files changed

+56
-21
lines changed

lib/java_buildpack/util/sanitizer.rb

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,36 +18,55 @@
1818
# A mixin that adds the ability to turn a +String+ into sanitized uri
1919
class 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
5372
end

spec/java_buildpack/util/sanitize_spec.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,23 @@
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

0 commit comments

Comments
 (0)