Skip to content
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

Disallow setting registry tokens with --config #10580

Merged
merged 1 commit into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/cargo/util/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1244,10 +1244,28 @@ impl Config {
);
}

let toml_v = toml::from_document(doc).with_context(|| {
let toml_v: toml::Value = toml::from_document(doc).with_context(|| {
format!("failed to parse value from --config argument `{arg}`")
})?;

if toml_v
.get("registry")
.and_then(|v| v.as_table())
.and_then(|t| t.get("token"))
.is_some()
{
bail!("registry.token cannot be set through --config for security reasons");
} else if let Some((k, _)) = toml_v
.get("registries")
.and_then(|v| v.as_table())
.and_then(|t| t.iter().find(|(_, v)| v.get("token").is_some()))
{
bail!(
"registries.{}.token cannot be set through --config for security reasons",
k
);
}

CV::from_toml(Definition::Cli, toml_v)
.with_context(|| format!("failed to convert --config argument `{arg}`"))?
};
Expand Down
18 changes: 18 additions & 0 deletions tests/testsuite/config_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,24 @@ b=2` was not a TOML dotted key expression (such as `build.jobs = 2`)",
);
}

#[cargo_test]
fn no_disallowed_values() {
let config = ConfigBuilder::new()
.config_arg("registry.token=\"hello\"")
.build_err();
assert_error(
config.unwrap_err(),
"registry.token cannot be set through --config for security reasons",
);
let config = ConfigBuilder::new()
.config_arg("registries.crates-io.token=\"hello\"")
.build_err();
assert_error(
config.unwrap_err(),
"registries.crates-io.token cannot be set through --config for security reasons",
);
}

#[cargo_test]
fn no_inline_table_value() {
// Disallow inline tables
Expand Down