Skip to content

Commit 2b7dbd9

Browse files
authored
fix(config-include): disallow glob and template syntax (#16285)
### What does this PR try to resolve? This reserves future possibility for us to support globa syntax and templated paths. Addressing #16284 (comment)
2 parents 1a7d588 + 80c849e commit 2b7dbd9

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

src/cargo/util/context/mod.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
//! read the environment variable due to ambiguity. (See `ConfigMapAccess` for
6262
//! more details.)
6363
64-
use crate::util::cache_lock::{CacheLock, CacheLockMode, CacheLocker};
6564
use std::borrow::Cow;
6665
use std::collections::{HashMap, HashSet};
6766
use std::env;
@@ -85,9 +84,11 @@ use crate::ops::RegistryCredentialConfig;
8584
use crate::sources::CRATES_IO_INDEX;
8685
use crate::sources::CRATES_IO_REGISTRY;
8786
use crate::util::OnceExt as _;
87+
use crate::util::cache_lock::{CacheLock, CacheLockMode, CacheLocker};
8888
use crate::util::errors::CargoResult;
8989
use crate::util::network::http::configure_http_handle;
9090
use crate::util::network::http::http_handle;
91+
use crate::util::restricted_names::is_glob_pattern;
9192
use crate::util::{CanonicalUrl, closest_msg, internal};
9293
use crate::util::{Filesystem, IntoUrl, IntoUrlWithBase, Rustc};
9394

@@ -1499,6 +1500,26 @@ impl GlobalContext {
14991500
include.def,
15001501
)
15011502
}
1503+
1504+
if let Some(path) = include.path.to_str() {
1505+
// Ignore non UTF-8 bytes as glob and template syntax are for textual config.
1506+
if is_glob_pattern(path) {
1507+
bail!(
1508+
"expected a config include path without glob patterns, \
1509+
but found `{}` from `{}`",
1510+
include.path.display(),
1511+
include.def,
1512+
)
1513+
}
1514+
if path.contains(&['{', '}']) {
1515+
bail!(
1516+
"expected a config include path without template braces, \
1517+
but found `{}` from `{}`",
1518+
include.path.display(),
1519+
include.def,
1520+
)
1521+
}
1522+
}
15021523
}
15031524

15041525
Ok(includes)

tests/testsuite/config_include.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,3 +724,39 @@ Caused by:
724724
"#]],
725725
);
726726
}
727+
728+
#[cargo_test]
729+
fn disallow_glob_syntax() {
730+
// Reserved for future extension
731+
write_config_toml("include = 'config-*.toml'");
732+
let gctx = GlobalContextBuilder::new()
733+
.unstable_flag("config-include")
734+
.build_err();
735+
assert_error(
736+
gctx.unwrap_err(),
737+
str![[r#"
738+
could not load Cargo configuration
739+
740+
Caused by:
741+
expected a config include path without glob patterns, but found `config-*.toml` from `[ROOT]/.cargo/config.toml`
742+
"#]],
743+
);
744+
}
745+
746+
#[cargo_test]
747+
fn disallow_template_syntax() {
748+
// Reserved for future extension
749+
write_config_toml("include = '{workspace-root}/config.toml'");
750+
let gctx = GlobalContextBuilder::new()
751+
.unstable_flag("config-include")
752+
.build_err();
753+
assert_error(
754+
gctx.unwrap_err(),
755+
str![[r#"
756+
could not load Cargo configuration
757+
758+
Caused by:
759+
expected a config include path without template braces, but found `{workspace-root}/config.toml` from `[ROOT]/.cargo/config.toml`
760+
"#]],
761+
);
762+
}

0 commit comments

Comments
 (0)