Skip to content

add glob pattern support for known_hosts #15508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 10, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions src/cargo/sources/git/known_hosts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
//! and revoked markers. See "FIXME" comments littered in this file.

use crate::util::context::{Definition, GlobalContext, Value};
use crate::util::restricted_names::is_glob_pattern;
use crate::CargoResult;
use base64::engine::general_purpose::STANDARD;
use base64::engine::general_purpose::STANDARD_NO_PAD;
Expand Down Expand Up @@ -588,7 +589,19 @@ impl KnownHost {
}
for pattern in self.patterns.split(',') {
let pattern = pattern.to_lowercase();
// FIXME: support * and ? wildcards
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was under the impression that this feature is tracked in #11577, but apparently not.

It seems like OpenSSH supports only * and ? wildcards. One caveat of using glob here is we provide more than we should. Anyway, it is good to catch up with the common format.

let is_glob = is_glob_pattern(&pattern);

if is_glob {
match glob::Pattern::new(&pattern) {
Ok(glob) => match_found |= glob.matches(&host),
Err(e) => {
tracing::warn!(
"failed to interpret hostname `{pattern}` as glob pattern: {e}"
)
}
}
}

if let Some(pattern) = pattern.strip_prefix('!') {
if pattern == host {
return false;
Expand Down Expand Up @@ -696,13 +709,16 @@ mod tests {
|1|QxzZoTXIWLhUsuHAXjuDMIV3FjQ=|M6NCOIkjiWdCWqkh5+Q+/uFLGjs= ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIHgN3O21U4LWtP5OzjTzPnUnSDmCNDvyvlaj6Hi65JC eric@host
# Negation isn't terribly useful without globs.
neg.example.com,!neg.example.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOXfUnaAHTlo1Qi//rNk26OcmHikmkns1Z6WW/UuuS3K eric@host
# Glob patterns
*.asterisk.glob.example.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIO6/wm8Z5aVL2cDyALY6zE7KVW0s64utWTUmbAvvSKlI eric@host
test?.question.glob.example.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKceiey2vuK/WB/kLsiGa85xw897JzvGGaHmkAZbVHf3 eric@host
"#;

#[test]
fn known_hosts_parse() {
let kh_path = Path::new("/home/abc/.known_hosts");
let khs = load_hostfile_contents(kh_path, COMMON_CONTENTS);
assert_eq!(khs.len(), 12);
assert_eq!(khs.len(), 14);
match &khs[0].location {
KnownHostLocation::File { path, lineno } => {
assert_eq!(path, kh_path);
Expand Down Expand Up @@ -740,6 +756,12 @@ mod tests {
assert!(khs[10].host_matches("hashed.example.com"));
assert!(!khs[10].host_matches("example.com"));
assert!(!khs[11].host_matches("neg.example.com"));

// Glob patterns
assert!(khs[12].host_matches("matches.asterisk.glob.example.com"));
assert!(!khs[12].host_matches("matches.not.glob.example.com"));
assert!(khs[13].host_matches("test3.question.glob.example.com"));
assert!(!khs[13].host_matches("test120.question.glob.example.com"));
}

#[test]
Expand Down