diff --git a/lib/rubygems/commands/environment_command.rb b/lib/rubygems/commands/environment_command.rb index a5eb521a5324..f72d0ec7b34a 100644 --- a/lib/rubygems/commands/environment_command.rb +++ b/lib/rubygems/commands/environment_command.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require_relative "../command" +require_relative "../uri" class Gem::Commands::EnvironmentCommand < Gem::Command def initialize @@ -87,7 +88,7 @@ def execute when /^user_gemdir/, /^user_gemhome/ then Gem.user_dir when /^remotesources/ then - Gem.sources.to_a.join("\n") + Gem.sources.map {|s| Gem::Uri.redact(s).to_s }.join("\n") when /^platform/ then Gem.platforms.join(File::PATH_SEPARATOR) when /^credentials/, /^creds/ then @@ -147,12 +148,13 @@ def show_environment # :nodoc: out << " - GEM CONFIGURATION:\n" Gem.configuration.each do |name, value| value = value.gsub(/./, "*") if name == "gemcutter_key" + value = redact_uris(value) if [:sources, :http_proxy].include?(name) out << " - #{name.inspect} => #{value.inspect}\n" end out << " - REMOTE SOURCES:\n" Gem.sources.each do |s| - out << " - #{s}\n" + out << " - #{Gem::Uri.redact(s)}\n" end out << " - SHELL PATH:\n" @@ -165,6 +167,17 @@ def show_environment # :nodoc: private + ## + # Redacts credentials in a URI string or an array of URI strings + + def redact_uris(value) + case value + when Array then value.map {|v| redact_uris(v) } + when String then Gem::Uri.redact(value).to_s + else value + end + end + ## # Git binary path diff --git a/lib/rubygems/commands/sources_command.rb b/lib/rubygems/commands/sources_command.rb index f8ebf602de3f..177eaca2b852 100644 --- a/lib/rubygems/commands/sources_command.rb +++ b/lib/rubygems/commands/sources_command.rb @@ -51,7 +51,7 @@ def initialize def add_source(source_uri) # :nodoc: source = build_new_source(source_uri) - source_uri = source.uri.to_s + source_uri = Gem::Uri.redact(source.uri).to_s begin if Gem.sources.include? source @@ -74,7 +74,7 @@ def add_source(source_uri) # :nodoc: def append_source(source_uri) # :nodoc: source = build_new_source(source_uri) - source_uri = source.uri.to_s + source_uri = Gem::Uri.redact(source.uri).to_s begin source.load_specs :released @@ -98,7 +98,7 @@ def append_source(source_uri) # :nodoc: def prepend_source(source_uri) # :nodoc: source = build_new_source(source_uri) - source_uri = source.uri.to_s + source_uri = Gem::Uri.redact(source.uri).to_s begin source.load_specs :released @@ -123,7 +123,7 @@ def prepend_source(source_uri) # :nodoc: def check_typo_squatting(source) if source.typo_squatting?("rubygems.org") question = <<-QUESTION.chomp -#{source.uri} is too similar to https://rubygems.org +#{Gem::Uri.redact(source.uri)} is too similar to https://rubygems.org Do you want to add this source? QUESTION @@ -151,7 +151,7 @@ def check_rubygems_https(source_uri) # :nodoc: if uri.scheme && uri.scheme.casecmp("http").zero? && uri.host.casecmp("rubygems.org").zero? question = <<-QUESTION.chomp -https://rubygems.org is recommended for security over #{uri} +https://rubygems.org is recommended for security over #{Gem::Uri.redact(uri)} Do you want to add this insecure source? QUESTION @@ -247,7 +247,7 @@ def list # :nodoc: say list.each do |src| - say src + say Gem::Uri.redact(src).to_s end end @@ -278,7 +278,7 @@ def execute def remove_source(source_uri) # :nodoc: source = build_source(source_uri) - source_uri = source.uri.to_s + source_uri = Gem::Uri.redact(source.uri).to_s if configured_sources&.include? source Gem.sources.delete source diff --git a/test/rubygems/test_gem_commands_environment_command.rb b/test/rubygems/test_gem_commands_environment_command.rb index e27de544c6f5..99412c137bde 100644 --- a/test/rubygems/test_gem_commands_environment_command.rb +++ b/test/rubygems/test_gem_commands_environment_command.rb @@ -130,6 +130,52 @@ def test_execute_remotesources Gem.sources.replace orig_sources end + def test_execute_remotesources_redacts_credentials + orig_sources = Gem.sources.dup + Gem.sources.replace %w[http://u:secret@gems.example.com https://secret-token@gems.example.com] + + @cmd.send :handle_options, %w[remotesources] + + use_ui @ui do + @cmd.execute + end + + expected = <<-EOF +http://u:REDACTED@gems.example.com +https://REDACTED@gems.example.com + EOF + + assert_equal expected, @ui.output + assert_equal "", @ui.error + ensure + Gem.sources.replace orig_sources + end + + def test_execute_redacts_credentials + orig_sources = Gem.sources.dup + orig_config_sources = Gem.configuration[:sources] + orig_http_proxy = Gem.configuration[:http_proxy] + Gem.sources.replace %w[http://u:secret@gems.example.com] + Gem.configuration[:sources] = %w[http://u:secret@gems.example.com] + Gem.configuration[:http_proxy] = "http://proxy-user:secret@proxy.example.com" + + @cmd.send :handle_options, %w[] + + use_ui @ui do + @cmd.execute + end + + assert_includes @ui.output, ':sources => ["http://u:REDACTED@gems.example.com"]' + assert_includes @ui.output, ':http_proxy => "http://proxy-user:REDACTED@proxy.example.com"' + assert_includes @ui.output, "REMOTE SOURCES:\n - http://u:REDACTED@gems.example.com\n" + refute_match(/secret/, @ui.output) + assert_empty @ui.error + ensure + Gem.sources.replace orig_sources + Gem.configuration[:sources] = orig_config_sources + Gem.configuration[:http_proxy] = orig_http_proxy + end + def test_execute_unknown @cmd.send :handle_options, %w[unknown] diff --git a/test/rubygems/test_gem_commands_sources_command.rb b/test/rubygems/test_gem_commands_sources_command.rb index 71c6d5ce1668..7c6d8da133be 100644 --- a/test/rubygems/test_gem_commands_sources_command.rb +++ b/test/rubygems/test_gem_commands_sources_command.rb @@ -335,6 +335,69 @@ def test_execute_append_nonexistent_source assert_equal "", @ui.error end + def test_execute_add_redacts_credentials + uri = "http://u:secret@beta-gems.example.com" + + setup_fake_source(uri) + + @cmd.handle_options %W[--add #{uri}] + + use_ui @ui do + @cmd.execute + end + + assert_equal [@gem_repo, uri], Gem.sources + + expected = <<-EOF +http://u:REDACTED@beta-gems.example.com added to sources + EOF + + assert_equal expected, @ui.output + assert_equal "", @ui.error + end + + def test_execute_add_typo_squatting_source_redacts_credentials + uri = "https://u:secret@rubyems.org" + + setup_fake_source(uri) + + @cmd.handle_options %W[--add #{uri}] + + ui = Gem::MockGemUi.new("n") + + use_ui ui do + assert_raise Gem::MockGemUi::TermError do + @cmd.execute + end + end + + expected = "https://u:REDACTED@rubyems.org is too similar to https://rubygems.org\n\nDo you want to add this source? [yn] " + + assert_equal expected, ui.output + assert_empty ui.error + end + + def test_execute_add_http_rubygems_org_redacts_credentials + uri = "http://u:secret@rubygems.org/" + + setup_fake_source(uri) + + @cmd.handle_options %W[--add #{uri}] + + ui = Gem::MockGemUi.new("n") + + use_ui ui do + assert_raise Gem::MockGemUi::TermError do + @cmd.execute + end + end + + expected = "https://rubygems.org is recommended for security over http://u:REDACTED@rubygems.org/\n\nDo you want to add this insecure source? [yn] " + + assert_equal expected, ui.output + assert_empty ui.error + end + def test_execute_add_existent_source_invalid_uri spec_fetcher @@ -752,6 +815,52 @@ def test_execute_remove Gem.configuration.sources = nil end + def test_execute_list_redacts_credentials + Gem.configuration.sources = %w[ + http://u:secret@beta-gems.example.com + https://secret-token@gems.example.com/ + ] + + @cmd.handle_options %w[--list] + + use_ui @ui do + @cmd.execute + end + + expected = <<-EOF +*** CURRENT SOURCES *** + +http://u:REDACTED@beta-gems.example.com +https://REDACTED@gems.example.com/ + EOF + + assert_equal expected, @ui.output + assert_equal "", @ui.error + ensure + Gem.configuration.sources = nil + end + + def test_execute_remove_redacts_credentials + uri = "http://u:secret@beta-gems.example.com" + + Gem.configuration.sources = [uri] + + setup_fake_source(uri) + + @cmd.handle_options %W[--remove #{uri}] + + use_ui @ui do + @cmd.execute + end + + expected = "http://u:REDACTED@beta-gems.example.com removed from sources\n" + + assert_equal expected, @ui.output + assert_equal "", @ui.error + ensure + Gem.configuration.sources = nil + end + def test_execute_remove_no_network Gem.configuration.sources = [@new_repo]