Skip to content
This repository has been archived by the owner on Apr 14, 2021. It is now read-only.

When a 401 response is received, raise BadAuthenticationError if credentials were present in the request URI #6928

Merged
3 commits merged into from
Apr 28, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/bundler/fetcher/downloader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def fetch(uri, headers = {}, counter = 0)
when Net::HTTPRequestEntityTooLarge
raise FallbackError, response.body
when Net::HTTPUnauthorized
raise BadAuthenticationError, uri.host if uri.userinfo
deivid-rodriguez marked this conversation as resolved.
Show resolved Hide resolved
raise AuthenticationRequiredError, uri.host
when Net::HTTPNotFound
raise FallbackError, "Net::HTTPNotFound: #{URICredentialsFilter.credential_filtered_uri(uri)}"
Expand Down
1 change: 1 addition & 0 deletions lib/bundler/fetcher/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def specs(_gem_names)
when /certificate verify failed/
raise CertificateFailureError.new(display_uri)
when /401/
raise BadAuthenticationError, remote_uri if remote_uri.userinfo
raise AuthenticationRequiredError, remote_uri
when /403/
raise BadAuthenticationError, remote_uri if remote_uri.userinfo
Expand Down
10 changes: 10 additions & 0 deletions spec/bundler/fetcher/downloader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
let(:connection) { double(:connection) }
let(:redirect_limit) { 5 }
let(:uri) { URI("http://www.uri-to-fetch.com/api/v2/endpoint") }
let(:uri_with_creds) { URI("http://user:password@uri-to-fetch.com/api/v2/endpoint") }
let(:options) { double(:options) }

subject { described_class.new(connection, redirect_limit) }
Expand Down Expand Up @@ -82,6 +83,15 @@
expect { subject.fetch(uri, options, counter) }.to raise_error(Bundler::Fetcher::AuthenticationRequiredError,
/Authentication is required for www.uri-to-fetch.com/)
end

context "when the there are credentials provided in the request" do
let(:uri) { URI("http://user:password@www.uri-to-fetch.com") }

it "should raise a Bundler::Fetcher::BadAuthenticationError that doesn't contain the password" do
expect { subject.fetch(uri, options, counter) }.
to raise_error(Bundler::Fetcher::BadAuthenticationError, /Bad username or password for www.uri-to-fetch.com/)
end
end
end

context "when the request response is a Net::HTTPNotFound" do
Expand Down
23 changes: 20 additions & 3 deletions spec/bundler/fetcher/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,26 @@
context "when a 401 response occurs" do
let(:error_message) { "401" }

it "should raise a Bundler::Fetcher::AuthenticationRequiredError" do
expect { subject.specs(gem_names) }.to raise_error(Bundler::Fetcher::AuthenticationRequiredError,
%r{Authentication is required for http://remote-uri.org})
before do
allow(remote_uri).to receive(:userinfo).and_return(userinfo)
end

context "and there was userinfo" do
let(:userinfo) { double(:userinfo) }

it "should raise a Bundler::Fetcher::BadAuthenticationError" do
expect { subject.specs(gem_names) }.to raise_error(Bundler::Fetcher::BadAuthenticationError,
%r{Bad username or password for http://remote-uri.org})
end
end

context "and there was no userinfo" do
let(:userinfo) { nil }

it "should raise a Bundler::Fetcher::AuthenticationRequiredError" do
expect { subject.specs(gem_names) }.to raise_error(Bundler::Fetcher::AuthenticationRequiredError,
%r{Authentication is required for http://remote-uri.org})
end
end
end

Expand Down