Skip to content

Commit 2b07d6c

Browse files
Merge pull request #974 from rbamberger/filter-log-messages
Handle Dynatrace API Token in the sanitizer
2 parents b890725 + b44919f commit 2b07d6c

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

lib/java_buildpack/util/sanitizer.rb

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,46 @@
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, _|
40+
params[key] = '***' if key.match(keywords)
41+
query_params += key + '=' + params[key] + '&'
42+
end
43+
44+
query_params
45+
end
46+
2147
# Takes a uri and strips out any credentials it may contain.
2248
#
2349
# @return [String] the sanitized uri
2450
def sanitize_uri
2551
rich_uri = URI(self)
2652
rich_uri.user = nil
2753
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+
2861
rich_uri.to_s
2962
end
30-
3163
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=***'\
41+
'&secret-token=***'\
42+
'&token=***')
2743
end
2844

2945
it 'does not sanatize uri with no credentials in' do

0 commit comments

Comments
 (0)