Skip to content

Commit 80c849e

Browse files
committed
fix(config-include): disallow glob and template syntax
This reserves future possibility for us to support globa syntax and templated paths. Addressing rust-lang#16284 (comment)
1 parent 61fe875 commit 80c849e

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
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: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -698,13 +698,7 @@ fn disallow_glob_syntax() {
698698
could not load Cargo configuration
699699
700700
Caused by:
701-
failed to load config include `config-*.toml` from `[ROOT]/.cargo/config.toml`
702-
703-
Caused by:
704-
failed to read configuration file `[ROOT]/.cargo/config-*.toml`
705-
706-
Caused by:
707-
[NOT_FOUND]
701+
expected a config include path without glob patterns, but found `config-*.toml` from `[ROOT]/.cargo/config.toml`
708702
"#]],
709703
);
710704
}
@@ -722,13 +716,7 @@ fn disallow_template_syntax() {
722716
could not load Cargo configuration
723717
724718
Caused by:
725-
failed to load config include `{workspace-root}/config.toml` from `[ROOT]/.cargo/config.toml`
726-
727-
Caused by:
728-
failed to read configuration file `[ROOT]/.cargo/{workspace-root}/config.toml`
729-
730-
Caused by:
731-
[NOT_FOUND]
719+
expected a config include path without template braces, but found `{workspace-root}/config.toml` from `[ROOT]/.cargo/config.toml`
732720
"#]],
733721
);
734722
}

0 commit comments

Comments
 (0)