Skip to content

Commit

Permalink
config: Deny CARGO_HOME in [env] table (fixes #11590)
Browse files Browse the repository at this point in the history
  • Loading branch information
basile-henry committed Jan 28, 2023
1 parent 9d1e248 commit d09e132
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/cargo/util/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1685,8 +1685,15 @@ impl Config {
}

pub fn env_config(&self) -> CargoResult<&EnvConfig> {
self.env_config
.try_borrow_with(|| self.get::<EnvConfig>("env"))
let env_config = self
.env_config
.try_borrow_with(|| self.get::<EnvConfig>("env"))?;

if env_config.get("CARGO_HOME").is_some() {
bail!("`CARGO_HOME` environment variable should be not be set in `.cargo/config` via `env.CARGO_HOME` as cargo does not use this value directly (only recursive calls to cargo would)");
}

Ok(env_config)
}

/// This is used to validate the `term` table has valid syntax.
Expand Down
26 changes: 26 additions & 0 deletions tests/testsuite/cargo_env_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,32 @@ fn env_invalid() {
.run();
}

#[cargo_test]
fn env_no_cargo_home() {
let p = project()
.file("Cargo.toml", &basic_bin_manifest("foo"))
.file(
"src/main.rs",
r#"
fn main() {
}
"#,
)
.file(
".cargo/config",
r#"
[env]
CARGO_HOME = "/"
"#,
)
.build();

p.cargo("build")
.with_status(101)
.with_stderr_contains("[..]`CARGO_HOME` environment variable should be not be set in `.cargo/config` via `env.CARGO_HOME` as cargo does not use this value directly (only recursive calls to cargo would)")
.run();
}

#[cargo_test]
fn env_force() {
let p = project()
Expand Down

0 comments on commit d09e132

Please sign in to comment.