Skip to content

Redact credentials in gem sources and gem env output - #10139

Closed
sourya2704 wants to merge 1 commit into
ruby:masterfrom
sourya2704:redact-source-credentials
Closed

sourya2704 wants to merge 1 commit into
ruby:masterfrom
sourya2704:redact-source-credentials

Conversation

@sourya2704

Copy link
Copy Markdown

What was the end-user or developer problem that led to this PR?

Fixes #9831.

Private gem sources often have credentials in the URL, like https://user:password@gems.example.com/ or https://token@gems.example.com/. gem sources and gem env print those URLs as they're stored, so the password ends up in the terminal, in CI logs and in pasted bug reports:

$ gem sources
*** CURRENT SOURCES ***

https://example-user:example-password@example.invalid/

$ gem env
...
  - GEM CONFIGURATION:
     - :sources => ["https://example-user:example-password@example.invalid/"]
     - :http_proxy => "http://proxy-user:proxy-password@proxy.invalid"
  - REMOTE SOURCES:
     - https://example-user:example-password@example.invalid/

RubyGems already redacts these URLs in some places, like Error fetching ... in gem sources (#4564 did the same for verbose mode), but not in the normal output of these two commands.

What is your fix for the problem, implemented in this PR?

Use Gem::Uri.redact everywhere these commands print a source URL. It replaces the password with REDACTED, or the user when it's a token:

  • gem sources and gem sources --list
  • the --add, --append, --prepend and --remove messages
  • the typo squatting prompt and the http://rubygems.org prompt
  • gem env remotesources
  • gem env: the REMOTE SOURCES section, and the :sources and :http_proxy values in GEM CONFIGURATION. Those values come straight from .gemrc, so they're redacted too.

With this PR:

$ gem sources
*** CURRENT SOURCES ***

https://example-user:REDACTED@example.invalid/

$ gem env
...
  - GEM CONFIGURATION:
     - :sources => ["https://example-user:REDACTED@example.invalid/"]
     - :http_proxy => "http://proxy-user:REDACTED@proxy.invalid"
  - REMOTE SOURCES:
     - https://example-user:REDACTED@example.invalid/

Only the output changes. The URLs stored in .gemrc and used for fetching keep their credentials, and test_execute_add_redacts_credentials checks that Gem.sources still has the full URL.

One trade-off, discussed in #9831: you can't copy a URL from gem sources into gem sources --remove anymore. The full URL is still in .gemrc, and I think not leaking it is worth more.

environment_command.rb now requires ../uri. Without it, plain gem env raised NameError: uninitialized constant Gem::Uri because nothing had loaded it yet. The tests didn't catch that because the test helper loads it, so I also checked the real commands with a .gemrc like the one above.

Make sure the following tasks are checked

Tests: 7 new tests in test_gem_commands_sources_command.rb and test_gem_commands_environment_command.rb. All 7 fail without the fix. All tests in both files pass, and bin/rubocop reports no offenses on the changed files.

`gem sources`, `gem env` and `gem env remotesources` printed source URLs
as they are stored, so a password or token in a private source URL, or
in `http_proxy`, ended up in the terminal, CI logs and bug reports.

Use Gem::Uri.redact wherever these commands print a source URL: the
source list, the add/append/prepend/remove messages, the typo squatting
and insecure rubygems.org prompts, the REMOTE SOURCES section and the
:sources and :http_proxy values in GEM CONFIGURATION. The URLs saved in
the configuration are unchanged.

Fixes ruby#9831
@hsbt

hsbt commented Nov 27, 2026

Copy link
Copy Markdown
Member

I decided in #9831 to keep printing source URLs as configured, so I'm closing this.

@hsbt hsbt closed this Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Should gem sources and gem env redact credentials embedded in source URLs?

2 participants