cargo: strip per-registry credential-provider from .cargo/config.toml#14356
cargo: strip per-registry credential-provider from .cargo/config.toml#14356jeffwidman wants to merge 1 commit intomainfrom
Conversation
PR #14340 disabled Cargo's global credential providers by setting CARGO_REGISTRY_GLOBAL_CREDENTIAL_PROVIDERS to an empty string, relying on the dependabot proxy for all private registry authentication. However, this only affects the global default -- per-registry `credential-provider` settings in the user's `.cargo/config.toml` override the global setting. When a user's config contains lines like: [registries.my-registry] credential-provider = "cargo:token" Cargo ignores the global empty string and invokes the per-registry provider, which looks for CARGO_REGISTRIES_{NAME}_TOKEN env vars that no longer exist (removed in #14030), causing 'no token found' errors. Fix by stripping `credential-provider` lines from .cargo/config.toml before writing it to the temporary working directory. This ensures Cargo makes plain HTTP requests that the dependabot proxy can intercept and authenticate. Fixes #14354
There was a problem hiding this comment.
Pull request overview
Updates the Cargo ecosystem to avoid private-registry authentication failures caused by per-registry credential-provider entries in .cargo/config.toml, ensuring Dependabot relies on the proxy-based auth path.
Changes:
- Add
Helpers.sanitize_cargo_configto removecredential-providerdirectives from Cargo config content. - Sanitize
.cargo/config.tomlbefore writing it into the temporary working directory in both the lockfile updater and version resolver flows. - Add RSpec coverage for config sanitization behavior across common formatting variants.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| cargo/lib/dependabot/cargo/helpers.rb | Adds sanitize_cargo_config helper used to strip credential-provider lines from Cargo config content. |
| cargo/lib/dependabot/cargo/file_updater/lockfile_updater.rb | Applies sanitization when writing .cargo/config.toml into the temp repo for lockfile updating. |
| cargo/lib/dependabot/cargo/update_checker/version_resolver.rb | Applies the same sanitization when writing .cargo/config.toml for version resolution. |
| cargo/spec/dependabot/cargo/helpers_spec.rb | Adds specs validating the sanitization behavior for multiple config layouts/whitespace variants. |
| # causing Cargo to look up tokens via CARGO_REGISTRIES_{NAME}_TOKEN env vars. Since the dependabot proxy | ||
| # handles authentication by intercepting HTTP requests, we need to strip these so Cargo makes plain | ||
| # requests that the proxy can decorate with credentials. | ||
| config_content.gsub(/^\s*credential-provider\s*=.*$/, "") |
There was a problem hiding this comment.
The implementation strips any line starting with credential-provider, regardless of whether it’s in a [registries.*] section. Either tighten the sanitization to only affect per-registry config (as described in the comment) or update the comment/docs to reflect that this removes all credential-provider settings in the file.
| config_content.gsub(/^\s*credential-provider\s*=.*$/, "") | |
| current_section = nil | |
| sanitized_lines = config_content.lines.each_with_object([]) do |line, kept| | |
| if line =~ /^\s*\[(.+?)\]\s*$/ | |
| current_section = Regexp.last_match(1) | |
| end | |
| if current_section&.start_with?("registries.") && line =~ /^\s*credential-provider\s*=/ | |
| next | |
| end | |
| kept << line | |
| end | |
| sanitized_lines.join |
| FileUtils.mkdir_p(File.dirname(T.must(config).name)) | ||
| File.write(T.must(config).name, T.must(config).content) | ||
| File.write(T.must(config).name, Helpers.sanitize_cargo_config(T.must(T.must(config).content))) |
There was a problem hiding this comment.
This T.must(T.must(config).content) nesting makes the write path harder to read. Consider assigning config_file = T.must(config) and content = T.must(config_file.content) (or similar) before calling sanitize_cargo_config so the nil assertions are clearer.
See below for a potential fix:
config_file = T.must(config)
FileUtils.mkdir_p(File.dirname(config_file.name))
config_content = T.must(config_file.content)
sanitized_content = Helpers.sanitize_cargo_config(config_content)
File.write(config_file.name, sanitized_content)
| FileUtils.mkdir_p(File.dirname(T.must(config).name)) | ||
| File.write(T.must(config).name, T.must(config).content) | ||
| File.write(T.must(config).name, Helpers.sanitize_cargo_config(T.must(T.must(config).content))) |
There was a problem hiding this comment.
This T.must(T.must(config).content) nesting makes the write path harder to read. Consider assigning config_file = T.must(config) and content = T.must(config_file.content) (or similar) before calling sanitize_cargo_config so the nil assertions are clearer.
See below for a potential fix:
config_file = T.must(config)
config_content = T.must(config_file.content)
FileUtils.mkdir_p(File.dirname(config_file.name))
File.write(config_file.name, Helpers.sanitize_cargo_config(config_content))
|
closing in favor of: |
Summary
Fix cargo private registry authentication failures when
.cargo/config.tomlcontains per-registrycredential-providersettings.Fixes #14354
Related: #14094, #14030
Problem
PR #14340 disabled Cargo's global credential providers by setting
CARGO_REGISTRY_GLOBAL_CREDENTIAL_PROVIDERS="", relying on the dependabot proxy for all private registry auth. However, this only affects the global default.When a user's
.cargo/config.tomlcontains per-registrycredential-providersettings:Cargo ignores the global empty string and invokes the per-registry provider, which looks for
CARGO_REGISTRIES_{NAME}_TOKENenv vars that no longer exist (removed in #14030), causing:Solution
Strip
credential-providerlines from.cargo/config.tomlbefore writing it to the temporary working directory. This ensures Cargo makes plain HTTP requests that the dependabot proxy can intercept and authenticate.Changes
helpers.rb: Addedsanitize_cargo_configmethod that stripscredential-providerlines from config contentlockfile_updater.rb: Sanitize cargo config before writing to temp directoryversion_resolver.rb: Same as lockfile_updater.rbhelpers_spec.rb: Tests covering variouscredential-providerpatterns (none, single, multiple, mixed, varied whitespace)Checklist