Skip to content

Commit 3cfada3

Browse files
hsbtclaude
andcommitted
Only coerce exact true and false gemrc values to booleans
Alternation binds looser than the anchors, so `/\Atrue|false\Z/` reads as `(\Atrue)|(false\Z)` and quietly turned every gemrc string starting with `true` or ending with `false` into the boolean false. `:credential_store: truestore` disabled the store while `RUBYGEMS_CREDENTIAL_STORE=truestore` returned the backend name. The neighbouring branches move to `\z` for the same reason, since `\Z` also accepts a trailing newline and let `":foo\n"` become a symbol that still carried it. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent 4846a48 commit 3cfada3

2 files changed

Lines changed: 21 additions & 7 deletions

File tree

lib/rubygems/config_file.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -763,13 +763,13 @@ def self.normalize_credentials_key(host)
763763

764764
def self.deep_transform_config_keys!(config)
765765
config.transform_keys! do |k|
766-
if k.match?(/\A:(.*)\Z/)
766+
if k.match?(/\A:(.*)\z/)
767767
k[1..-1].to_sym
768-
elsif k.include?("__") || k.match?(%r{/\Z})
768+
elsif k.include?("__") || k.end_with?("/")
769769
if k.is_a?(Symbol)
770-
k.to_s.gsub(/__/,".").gsub(%r{/\Z}, "").to_sym
770+
k.to_s.gsub(/__/,".").delete_suffix("/").to_sym
771771
else
772-
k.dup.gsub(/__/,".").gsub(%r{/\Z}, "")
772+
k.dup.gsub(/__/,".").delete_suffix("/")
773773
end
774774
else
775775
k
@@ -778,11 +778,11 @@ def self.deep_transform_config_keys!(config)
778778

779779
config.transform_values! do |v|
780780
if v.is_a?(String)
781-
if v.match?(/\A:(.*)\Z/)
781+
if v.match?(/\A:(.*)\z/)
782782
v[1..-1].to_sym
783-
elsif v.match?(/\A[+-]?\d+\Z/)
783+
elsif v.match?(/\A[+-]?\d+\z/)
784784
v.to_i
785-
elsif v.match?(/\Atrue|false\Z/)
785+
elsif v.match?(/\A(?:true|false)\z/)
786786
v == "true"
787787
elsif v.empty?
788788
nil

test/rubygems/test_gem_config_file.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,20 @@ def test_accept_string_key
896896
assert_equal false, @cfg.verbose
897897
end
898898

899+
def test_gemrc_coerces_only_exact_boolean_spellings
900+
File.open @temp_conf, "w" do |fp|
901+
fp.puts ":credential_store: truestore"
902+
fp.puts ":ssl_ca_cert: /home/me/certs-false"
903+
fp.puts ":verbose: false"
904+
end
905+
906+
util_config_file
907+
908+
assert_equal "truestore", @cfg.credential_store
909+
assert_equal "/home/me/certs-false", @cfg.ssl_ca_cert
910+
assert_equal false, @cfg.verbose
911+
end
912+
899913
def test_load_ssl_verify_mode_from_config
900914
File.open @temp_conf, "w" do |fp|
901915
fp.puts ":ssl_verify_mode: 1"

0 commit comments

Comments
 (0)