-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Description
Problem
Say we had a hostname as follows in the known_hosts file : *github.com,!*h.github.com ecdsa-sha2-nistp256 AAAAC3NzaC1lZDI...
The expected behaviour as per the sshd manual is to not recognise ssh.github.com.
Using git or ssh in the above case will prompt you to add the fingerprint to known_hosts.
However, when cargo update is run with a dependency: ripgrep = { git = "ssh://git@ssh.github.com/BurntSushi/ripgrep.git" }, Cargo fetches the dependencies without throwing an error (which should be expected when a host is not recognised).
Upon some investigation, the issue comes from this block of code here
cargo/src/cargo/sources/git/known_hosts.rs
Lines 610 to 632 in b9f0d83
| for pattern in self.patterns.split(',') { | |
| let pattern = pattern.to_lowercase(); | |
| 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; | |
| } | |
| } else { | |
| match_found |= pattern == host; | |
| } | |
| } |
The first pattern (*github.com) matches and sets match_found = true. For the second pattern (!*h.github.com ), It validates the host against the glob(which should return false but match_found is already true) and it gets trough the negation check.
Steps
-
Remove the existing github.com entries from
known_hostsand add this fingerprint:
*github.com,!*h.github.com ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBEmKSENjQEezOmxkZMy7opKgwFB9nkt5YRrYMjNuG5N87uRgg6CLrbo5wAdT/y6v0mKV0U2w0WZ2YB/++Tpockg= -
Add a dependency to your manifest that uses
ssh.github.com:
[dependencies]
ripgrep = { git = "ssh://git@ssh.github.com/BurntSushi/ripgrep.git" }- To not use the git cli, add this to your
config.toml
[net]
git-fetch-with-cli = false- Run
cargo updateand you should not see any error (but Cargo should throw an error here) - Try the same with
git-fetch-with-cli = trueand you should see git asking to add a fingerprint toknow_hosts
Possible Solution(s)
In the block of code above, we could just first check if its a negation and then parse the glob. if the glob matches under negation then outright return false.
Notes
No response
Version
cargo 1.94.0-nightly (b54051b15 2025-12-30)
release: 1.94.0-nightly
commit-hash: b54051b1505281ec7a45a250140a0ff25d33f319
commit-date: 2025-12-30
host: x86_64-unknown-linux-gnu
libgit2: 1.9.1 (sys:0.20.2 vendored)
libcurl: 8.15.0-DEV (sys:0.4.83+curl-8.15.0 vendored ssl:OpenSSL/3.5.4)
ssl: OpenSSL 3.5.4 30 Sep 2025
os: Ubuntu 24.4.0 (noble) [64-bit]