-
Notifications
You must be signed in to change notification settings - Fork 1.3k
cargo: strip credential-provider from .cargo/config.toml via TOML parsing #14359
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jeffwidman
merged 1 commit into
main
from
fix-cargo-strip-credential-provider-from-config
Mar 4, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,61 @@ | ||
| # typed: strong | ||
| # typed: strict | ||
jeffwidman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # frozen_string_literal: true | ||
|
|
||
| require "toml-rb" | ||
|
|
||
| module Dependabot | ||
| module Cargo | ||
| module Helpers | ||
| extend T::Sig | ||
|
|
||
| # Disable Cargo's *global* credential providers so that Cargo does not attempt to look up registry tokens | ||
| # on its own. The dependabot proxy (https://github.com/dependabot/proxy/) handles all registry authentication | ||
| # transparently by intercepting HTTP requests and injecting the appropriate credentials. | ||
| # | ||
| # Note: this only affects the global/default credential provider. Per-registry `credential-provider` settings | ||
| # in .cargo/config.toml override this env var, so those are stripped separately by `sanitize_cargo_config`. | ||
| # | ||
| # Uses ||= so developers can override by setting CARGO_REGISTRY_GLOBAL_CREDENTIAL_PROVIDERS=cargo:token in their | ||
| # shell (along with the appropriate CARGO_REGISTRIES_{NAME}_TOKEN vars) for local development without the proxy. | ||
| sig { void } | ||
| def self.bypass_cargo_credential_providers | ||
| # Disable Cargo's built-in credential providers entirely so that Cargo does not attempt to look up registry | ||
| # tokens on its own. The dependabot proxy (https://github.com/dependabot/proxy/) handles all registry | ||
| # authentication transparently by intercepting HTTP requests and injecting the appropriate credentials. | ||
| # | ||
| # Uses ||= so developers can override by setting CARGO_REGISTRY_GLOBAL_CREDENTIAL_PROVIDERS=cargo:token in their | ||
| # shell (along with the appropriate CARGO_REGISTRIES_{NAME}_TOKEN vars) for local development without the proxy. | ||
| ENV["CARGO_REGISTRY_GLOBAL_CREDENTIAL_PROVIDERS"] ||= "" | ||
| end | ||
|
|
||
| # Strip per-registry `credential-provider` settings from .cargo/config.toml. | ||
| # | ||
| # Users may have entries like: | ||
| # [registries.my-registry] | ||
| # credential-provider = "cargo:token" | ||
| # | ||
| # These per-registry settings override the global CARGO_REGISTRY_GLOBAL_CREDENTIAL_PROVIDERS env var, | ||
| # causing Cargo to look up tokens locally. Since the dependabot proxy handles all registry authentication | ||
| # transparently, we remove these so Cargo makes plain unauthenticated requests that the proxy can intercept. | ||
| sig { params(config_content: String).returns(String) } | ||
| def self.sanitize_cargo_config(config_content) | ||
| parsed = TomlRB.parse(config_content) | ||
| return config_content unless parsed.is_a?(Hash) | ||
|
|
||
| registries = parsed["registries"] | ||
| if registries.is_a?(Hash) | ||
| registries.each_value do |registry_config| | ||
| registry_config.delete("credential-provider") if registry_config.is_a?(Hash) | ||
| end | ||
| end | ||
|
|
||
| # Also strip credential-provider from [registry] (crates.io default registry). Users who `cargo publish` | ||
| # from CI may have this set. It's a per-registry override that takes precedence over the global env var, | ||
| # so we need to remove it to prevent Cargo from trying to look up a token. | ||
| registry = parsed["registry"] | ||
| registry.delete("credential-provider") if registry.is_a?(Hash) | ||
|
|
||
| TomlRB.dump(parsed) | ||
| rescue TomlRB::Error => e | ||
| raise Dependabot::DependencyFileNotParseable.new( | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a well-known error that we display to users so that if they hit problems, they can hopefully self-diagnose. |
||
| ".cargo/config.toml", | ||
| "Failed to parse Cargo config file: #{e.message}" | ||
| ) | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is Sorbet variable narrowing... so that we don't have to have another
T.must()call... this is arguably more readable.