Skip to content

Commit 087285d

Browse files
committed
add glob pattern support for known_hosts
1 parent a154422 commit 087285d

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/cargo/sources/git/known_hosts.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
//! and revoked markers. See "FIXME" comments littered in this file.
2424
2525
use crate::util::context::{Definition, GlobalContext, Value};
26+
use crate::util::restricted_names::is_glob_pattern;
2627
use crate::CargoResult;
2728
use base64::engine::general_purpose::STANDARD;
2829
use base64::engine::general_purpose::STANDARD_NO_PAD;
@@ -588,7 +589,15 @@ impl KnownHost {
588589
}
589590
for pattern in self.patterns.split(',') {
590591
let pattern = pattern.to_lowercase();
591-
// FIXME: support * and ? wildcards
592+
let is_glob = is_glob_pattern(&pattern);
593+
594+
if is_glob {
595+
match glob::Pattern::new(&pattern) {
596+
Ok(glob) => match_found |= glob.matches(&host),
597+
Err(e) => tracing::warn!("failed to interpret hostname as glob pattern: {e}"),
598+
}
599+
}
600+
592601
if let Some(pattern) = pattern.strip_prefix('!') {
593602
if pattern == host {
594603
return false;
@@ -696,13 +705,16 @@ mod tests {
696705
|1|QxzZoTXIWLhUsuHAXjuDMIV3FjQ=|M6NCOIkjiWdCWqkh5+Q+/uFLGjs= ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIHgN3O21U4LWtP5OzjTzPnUnSDmCNDvyvlaj6Hi65JC eric@host
697706
# Negation isn't terribly useful without globs.
698707
neg.example.com,!neg.example.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOXfUnaAHTlo1Qi//rNk26OcmHikmkns1Z6WW/UuuS3K eric@host
708+
# Glob patterns
709+
*.asterisk.glob.example.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIO6/wm8Z5aVL2cDyALY6zE7KVW0s64utWTUmbAvvSKlI eric@host
710+
test?.question.glob.example.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKceiey2vuK/WB/kLsiGa85xw897JzvGGaHmkAZbVHf3 eric@host
699711
"#;
700712

701713
#[test]
702714
fn known_hosts_parse() {
703715
let kh_path = Path::new("/home/abc/.known_hosts");
704716
let khs = load_hostfile_contents(kh_path, COMMON_CONTENTS);
705-
assert_eq!(khs.len(), 12);
717+
assert_eq!(khs.len(), 14);
706718
match &khs[0].location {
707719
KnownHostLocation::File { path, lineno } => {
708720
assert_eq!(path, kh_path);
@@ -740,6 +752,12 @@ mod tests {
740752
assert!(khs[10].host_matches("hashed.example.com"));
741753
assert!(!khs[10].host_matches("example.com"));
742754
assert!(!khs[11].host_matches("neg.example.com"));
755+
756+
// Glob patterns
757+
assert!(khs[12].host_matches("matches.asterisk.glob.example.com"));
758+
assert!(!khs[12].host_matches("matches.not.glob.example.com"));
759+
assert!(khs[13].host_matches("test3.question.glob.example.com"));
760+
assert!(!khs[13].host_matches("test120.question.glob.example.com"));
743761
}
744762

745763
#[test]

0 commit comments

Comments
 (0)