Skip to content

Commit fbe35bc

Browse files
samisalamiwstimon
authored andcommitted
[rubygems/rubygems] Fix private registry credentials being written to logs
ruby/rubygems@d070fa10c1 Co-authored-by: Artem Ignatyev <[email protected]>
1 parent 10d694a commit fbe35bc

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

lib/bundler/rubygems_integration.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,9 @@ def fetch_all_remote_specs(remote, gem_remote_fetcher)
393393
def download_gem(spec, uri, cache_dir, fetcher)
394394
require "rubygems/remote_fetcher"
395395
uri = Bundler.settings.mirror_for(uri)
396-
Bundler::Retry.new("download gem from #{uri}").attempts do
396+
redacted_uri = Gem::Uri.redact(uri)
397+
398+
Bundler::Retry.new("download gem from #{redacted_uri}").attempts do
397399
gem_file_name = spec.file_name
398400
local_gem_path = File.join cache_dir, gem_file_name
399401
return if File.exist? local_gem_path
@@ -415,7 +417,7 @@ def download_gem(spec, uri, cache_dir, fetcher)
415417
end
416418
end
417419
rescue Gem::RemoteFetcher::FetchError => e
418-
raise Bundler::HTTPError, "Could not download gem from #{uri} due to underlying error <#{e.message}>"
420+
raise Bundler::HTTPError, "Could not download gem from #{redacted_uri} due to underlying error <#{e.message}>"
419421
end
420422

421423
def build(spec, skip_validation = false)

spec/bundler/bundler/rubygems_integration_spec.rb

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232

3333
describe "#download_gem" do
3434
let(:bundler_retry) { double(Bundler::Retry) }
35-
let(:uri) { Gem::URI.parse("https://foo.bar") }
3635
let(:cache_dir) { "#{Gem.path.first}/cache" }
3736
let(:spec) do
3837
spec = Gem::Specification.new("Foo", Gem::Version.new("2.5.2"))
@@ -41,13 +40,47 @@
4140
end
4241
let(:fetcher) { double("gem_remote_fetcher") }
4342

44-
it "successfully downloads gem with retries" do
45-
expect(Bundler::Retry).to receive(:new).with("download gem from #{uri}/").
46-
and_return(bundler_retry)
47-
expect(bundler_retry).to receive(:attempts).and_yield
48-
expect(fetcher).to receive(:cache_update_path)
43+
context "when uri is public" do
44+
let(:uri) { Gem::URI.parse("https://foo.bar") }
4945

50-
Bundler.rubygems.download_gem(spec, uri, cache_dir, fetcher)
46+
it "successfully downloads gem with retries" do
47+
expect(Bundler::Retry).to receive(:new).with("download gem from #{uri}/").
48+
and_return(bundler_retry)
49+
expect(bundler_retry).to receive(:attempts).and_yield
50+
expect(fetcher).to receive(:cache_update_path)
51+
52+
Bundler.rubygems.download_gem(spec, uri, cache_dir, fetcher)
53+
end
54+
end
55+
56+
context "when uri contains userinfo part" do
57+
let(:uri) { Gem::URI.parse("https://#{userinfo}@foo.bar") }
58+
59+
context "with user and password" do
60+
let(:userinfo) { "user:password" }
61+
62+
it "successfully downloads gem with retries with filtered log" do
63+
expect(Bundler::Retry).to receive(:new).with("download gem from https://user:[email protected]/").
64+
and_return(bundler_retry)
65+
expect(bundler_retry).to receive(:attempts).and_yield
66+
expect(fetcher).to receive(:cache_update_path)
67+
68+
Bundler.rubygems.download_gem(spec, uri, cache_dir, fetcher)
69+
end
70+
end
71+
72+
context "with token [as user]" do
73+
let(:userinfo) { "token" }
74+
75+
it "successfully downloads gem with retries with filtered log" do
76+
expect(Bundler::Retry).to receive(:new).with("download gem from https://[email protected]/").
77+
and_return(bundler_retry)
78+
expect(bundler_retry).to receive(:attempts).and_yield
79+
expect(fetcher).to receive(:cache_update_path)
80+
81+
Bundler.rubygems.download_gem(spec, uri, cache_dir, fetcher)
82+
end
83+
end
5184
end
5285
end
5386

0 commit comments

Comments
 (0)