Skip to content

Commit f6db12d

Browse files
authoredOct 11, 2020
fix(security): hide personal access token given in uri (pact-foundation#225)
something like https://pat@my-pact-server/pact.json is possible where pat stands for personal access token and is a secret. fix the current behavior where only https://user:password@my-pact-server/pact.json is checked
1 parent 3cd57cb commit f6db12d

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed
 

‎lib/pact/provider/pact_uri.rb

+13-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def == other
1717
end
1818

1919
def basic_auth?
20-
!!username
20+
!!username && !!password
2121
end
2222

2323
def username
@@ -29,12 +29,23 @@ def password
2929
end
3030

3131
def to_s
32-
if basic_auth? && uri.start_with?('http://', 'https://')
32+
if basic_auth? && http_or_https_uri?
3333
URI(@uri).tap { |x| x.userinfo="#{username}:*****"}.to_s
34+
elsif personal_access_token? && http_or_https_uri?
35+
URI(@uri).tap { |x| x.userinfo="*****"}.to_s
3436
else
3537
uri
3638
end
3739
end
40+
41+
private def personal_access_token?
42+
!!username && !password
43+
end
44+
45+
private def http_or_https_uri?
46+
uri.start_with?('http://', 'https://')
47+
end
48+
3849
end
3950
end
4051
end

‎spec/lib/pact/provider/pact_uri_spec.rb

+19-2
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
end
2424

2525
describe '#to_s' do
26-
context 'with userinfo provided' do
26+
context 'with basic auth provided' do
2727
let(:password) { 'my_password' }
2828
let(:options) { { username: username, password: password } }
2929

30-
it 'should include user name and password' do
30+
it 'should include user name and and hide password' do
3131
expect(pact_uri.to_s).to eq('http://pact:*****@uri')
3232
end
3333

@@ -40,6 +40,23 @@
4040
end
4141
end
4242

43+
context 'with personal access token provided' do
44+
let(:pat) { 'should_be_secret' }
45+
let(:options) { { username: pat } }
46+
47+
it 'should hide the pat' do
48+
expect(pact_uri.to_s).to eq('http://*****@uri')
49+
end
50+
51+
context 'when pat credentials have been set for a local file (eg. via environment variables, unintentionally)' do
52+
let(:uri) { '/some/file thing.json' }
53+
54+
it 'does not blow up' do
55+
expect(pact_uri.to_s).to eq uri
56+
end
57+
end
58+
end
59+
4360
context 'without userinfo' do
4461
let(:options) { {} }
4562

0 commit comments

Comments
 (0)
Please sign in to comment.