diff --git a/npm_and_yarn/lib/dependabot/npm_and_yarn/file_updater/npmrc_builder.rb b/npm_and_yarn/lib/dependabot/npm_and_yarn/file_updater/npmrc_builder.rb index 2101467e51b..7415ca6dfce 100644 --- a/npm_and_yarn/lib/dependabot/npm_and_yarn/file_updater/npmrc_builder.rb +++ b/npm_and_yarn/lib/dependabot/npm_and_yarn/file_updater/npmrc_builder.rb @@ -114,19 +114,19 @@ def build_npmrc_from_yarnrc def credential_lines_for_npmrc lines = [] registry_credentials.each do |cred| - registry = cred.fetch("registry") + registry = cred.fetch("registry").sub(%r{\/?$}, "/") lines << registry_scope(registry) if registry_scope(registry) token = cred.fetch("token") if token.include?(":") encoded_token = Base64.encode64(token).delete("\n") - lines << "//#{registry}/:_auth=#{encoded_token}" + lines << "//#{registry}:_auth=#{encoded_token}" elsif Base64.decode64(token).ascii_only? && Base64.decode64(token).include?(":") - lines << %(//#{registry}/:_auth=#{token.delete("\n")}) + lines << %(//#{registry}:_auth=#{token.delete("\n")}) else - lines << "//#{registry}/:_authToken=#{token}" + lines << "//#{registry}:_authToken=#{token}" end end @@ -163,7 +163,7 @@ def registry_scope(registry) # This just seems unlikely return unless scopes.uniq.count == 1 - "@#{scopes.first}:registry=https://#{registry}/" + "@#{scopes.first}:registry=https://#{registry}" end def registry_credentials diff --git a/npm_and_yarn/spec/dependabot/npm_and_yarn/file_updater/npmrc_builder_spec.rb b/npm_and_yarn/spec/dependabot/npm_and_yarn/file_updater/npmrc_builder_spec.rb index 226d586ad2a..77e30183122 100644 --- a/npm_and_yarn/spec/dependabot/npm_and_yarn/file_updater/npmrc_builder_spec.rb +++ b/npm_and_yarn/spec/dependabot/npm_and_yarn/file_updater/npmrc_builder_spec.rb @@ -175,6 +175,28 @@ end it { is_expected.to eq("//registry.npmjs.org/:_authToken=my_token") } + context "where the registry has a trailing slash" do + let(:credentials) do + [{ + "type" => "git_source", + "host" => "github.com", + "username" => "x-access-token", + "password" => "token" + }, { + "type" => "npm_registry", + "registry" => "artifactory.jfrog.com"\ + "/artifactory/api/npm/dependabot/", + "token" => "my_token" + }] + end + + it "only adds a single trailing slash" do + expect(npmrc_content). + to eq("//artifactory.jfrog.com/"\ + "artifactory/api/npm/dependabot/:_authToken=my_token") + end + end + context "that match a scoped package" do let(:credentials) do [{