Skip to content

Commit c85a71e

Browse files
committed
Auto merge of #10580 - jonhoo:restrict-config-cli, r=ehuss
Disallow setting registry tokens with --config As per the concern `restricted-values` in #7722 (comment). r? `@ehuss`
2 parents 304a9e6 + 10c4f32 commit c85a71e

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/cargo/util/config/mod.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1244,10 +1244,28 @@ impl Config {
12441244
);
12451245
}
12461246

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

1251+
if toml_v
1252+
.get("registry")
1253+
.and_then(|v| v.as_table())
1254+
.and_then(|t| t.get("token"))
1255+
.is_some()
1256+
{
1257+
bail!("registry.token cannot be set through --config for security reasons");
1258+
} else if let Some((k, _)) = toml_v
1259+
.get("registries")
1260+
.and_then(|v| v.as_table())
1261+
.and_then(|t| t.iter().find(|(_, v)| v.get("token").is_some()))
1262+
{
1263+
bail!(
1264+
"registries.{}.token cannot be set through --config for security reasons",
1265+
k
1266+
);
1267+
}
1268+
12511269
CV::from_toml(Definition::Cli, toml_v)
12521270
.with_context(|| format!("failed to convert --config argument `{arg}`"))?
12531271
};

tests/testsuite/config_cli.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,24 @@ b=2` was not a TOML dotted key expression (such as `build.jobs = 2`)",
368368
);
369369
}
370370

371+
#[cargo_test]
372+
fn no_disallowed_values() {
373+
let config = ConfigBuilder::new()
374+
.config_arg("registry.token=\"hello\"")
375+
.build_err();
376+
assert_error(
377+
config.unwrap_err(),
378+
"registry.token cannot be set through --config for security reasons",
379+
);
380+
let config = ConfigBuilder::new()
381+
.config_arg("registries.crates-io.token=\"hello\"")
382+
.build_err();
383+
assert_error(
384+
config.unwrap_err(),
385+
"registries.crates-io.token cannot be set through --config for security reasons",
386+
);
387+
}
388+
371389
#[cargo_test]
372390
fn no_inline_table_value() {
373391
// Disallow inline tables

0 commit comments

Comments
 (0)