Skip to content

cargo: strip credential-provider from .cargo/config.toml via TOML parsing#14359

Merged
jeffwidman merged 1 commit intomainfrom
fix-cargo-strip-credential-provider-from-config
Mar 4, 2026
Merged

cargo: strip credential-provider from .cargo/config.toml via TOML parsing#14359
jeffwidman merged 1 commit intomainfrom
fix-cargo-strip-credential-provider-from-config

Conversation

@jeffwidman
Copy link
Member

Problem

PR #14340 set CARGO_REGISTRY_GLOBAL_CREDENTIAL_PROVIDERS to an empty string to disable Cargo's credential lookup globally. However, per-registry credential-provider settings in .cargo/config.toml override the global env var:

[registries.artifactory]
credential-provider = "cargo:token"

Cargo then tries to look up tokens locally and fails with no token found.

Solution

Parse .cargo/config.toml with TomlRB and remove credential-provider keys from both per-registry ([registries.*]) and global ([registry]) sections before writing the config to the temporary working directory.

Combined with the existing CARGO_REGISTRY_GLOBAL_CREDENTIAL_PROVIDERS="" from #14340, this ensures Cargo never tries to authenticate on its own. All HTTP requests go through the dependabot proxy unauthenticated, and the proxy injects the real credentials transparently.

Why this works

Dependabot always proxies all Cargo HTTP traffic. The proxy intercepts requests and injects the real registry credentials. Cargo itself never needs to authenticate — it just needs to not try to look up tokens. This PR + #14340 together ensure that:

  1. Global providers disabled (via CARGO_REGISTRY_GLOBAL_CREDENTIAL_PROVIDERS="") — cargo: Bypass Cargo credential providers, rely on proxy for registry auth #14340
  2. Per-registry providers removed (via TOML parsing in this PR)

Approach

Uses TomlRB to properly parse and rewrite the config rather than regex, avoiding edge cases with TOML formatting. Falls back to the original content if parsing fails.

See also

Fixes #14354
Related: #14030, #14094

@jeffwidman jeffwidman requested a review from a team as a code owner March 3, 2026 22:49
Copilot AI review requested due to automatic review settings March 3, 2026 22:49
@github-actions github-actions bot added the L: rust:cargo Rust crates via cargo label Mar 3, 2026
@jeffwidman jeffwidman force-pushed the fix-cargo-strip-credential-provider-from-config branch from 23a8e63 to 155b6a3 Compare March 3, 2026 22:52
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR ensures Dependabot’s Cargo integration doesn’t trigger Cargo’s local credential lookup when a repo’s .cargo/config(.toml) sets credential-provider, so authentication is handled exclusively by the Dependabot proxy.

Changes:

  • Add Helpers.sanitize_cargo_config to parse TOML and remove credential-provider from [registries.*] and [registry].
  • Write a sanitized Cargo config into the temporary working directory in both the lockfile updater and version resolver paths.
  • Add RSpec coverage for sanitizing per-registry, global, mixed, and malformed TOML configs.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
cargo/lib/dependabot/cargo/helpers.rb Adds TOML-based sanitization to remove credential-provider (with parse-error fallback).
cargo/lib/dependabot/cargo/file_updater/lockfile_updater.rb Writes sanitized Cargo config into the temp workspace before running Cargo.
cargo/lib/dependabot/cargo/update_checker/version_resolver.rb Writes sanitized Cargo config into the temp workspace before running Cargo.
cargo/spec/dependabot/cargo/helpers_spec.rb Adds tests for sanitization behavior and malformed TOML fallback.

@jeffwidman jeffwidman force-pushed the fix-cargo-strip-credential-provider-from-config branch from 2490011 to 47d80d5 Compare March 3, 2026 23:25
@jeffwidman jeffwidman force-pushed the fix-cargo-strip-credential-provider-from-config branch from 47d80d5 to 0816e14 Compare March 3, 2026 23:39
kbukum1
kbukum1 previously approved these changes Mar 3, 2026
@kbukum1 kbukum1 self-requested a review March 3, 2026 23:41
@kbukum1 kbukum1 requested a review from Copilot March 3, 2026 23:41
@jeffwidman jeffwidman force-pushed the fix-cargo-strip-credential-provider-from-config branch from 0816e14 to 51743e4 Compare March 3, 2026 23:41
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.


You can also share your feedback on Copilot code review. Take the survey.

File.write(lockfile.name, lockfile.content)
File.write(T.must(toolchain).name, T.must(toolchain).content) if toolchain
return unless config
config_file = config
Copy link
Member Author

@jeffwidman jeffwidman Mar 3, 2026

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.

@jeffwidman jeffwidman force-pushed the fix-cargo-strip-credential-provider-from-config branch from 51743e4 to c861903 Compare March 3, 2026 23:46
## Problem

PR #14340 set `CARGO_REGISTRY_GLOBAL_CREDENTIAL_PROVIDERS=""` to disable
Cargo's credential lookup globally. However, per-registry
`credential-provider` settings in .cargo/config.toml override the global
env var:

  [registries.artifactory]
  credential-provider = "cargo:token"

Cargo then invokes `cargo:token` for that registry, tries to look up
`CARGO_REGISTRIES_ARTIFACTORY_TOKEN`, finds nothing, and fails with
'no token found'.

## Solution

Parse .cargo/config.toml with TomlRB and strip `credential-provider` keys
from both `[registries.*]` and `[registry]` sections before writing the
config to the temporary working directory.

Combined with the existing `CARGO_REGISTRY_GLOBAL_CREDENTIAL_PROVIDERS=""`
from #14340, this ensures Cargo never tries to authenticate on its own —
all HTTP requests go through the dependabot proxy unauthenticated, and the
proxy injects the real credentials transparently.

Uses `is_a?(Hash)` guards rather than `T.let` casts so that unexpected
config structure (valid TOML but not the shape we expect) is safely skipped
rather than raising.

Fixes #14354
@jeffwidman jeffwidman force-pushed the fix-cargo-strip-credential-provider-from-config branch from c861903 to 24f8437 Compare March 3, 2026 23:51

TomlRB.dump(parsed)
rescue TomlRB::Error => e
raise Dependabot::DependencyFileNotParseable.new(
Copy link
Member Author

Choose a reason for hiding this comment

The 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.

@jeffwidman jeffwidman merged commit 3ca8498 into main Mar 4, 2026
90 checks passed
@jeffwidman jeffwidman deleted the fix-cargo-strip-credential-provider-from-config branch March 4, 2026 00:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

L: rust:cargo Rust crates via cargo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cargo: no token found for Artifactory

3 participants