Skip to content

Fix SSH hostkey checks for push() #6033

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 2 commits into from
Feb 2, 2023
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
35 changes: 21 additions & 14 deletions cargo-registry-index/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ extern crate tracing;
pub mod testing;

use anyhow::{anyhow, Context};
use git2::cert::Cert;
use git2::CertificateCheckStatus;
use once_cell::sync::Lazy;
use std::cmp::Ordering;
Expand Down Expand Up @@ -442,6 +443,7 @@ impl Repository {
{
let mut origin = self.repository.find_remote("origin")?;
let mut callbacks = git2::RemoteCallbacks::new();
callbacks.certificate_check(Self::certificate_check);
callbacks.credentials(|_, user_from_url, cred_type| {
self.credentials.git2_callback(user_from_url, cred_type)
});
Expand Down Expand Up @@ -515,26 +517,31 @@ impl Repository {
credentials.git2_callback(user_from_url, cred_type)
});

callbacks.certificate_check(|cert, hostname| {
if hostname == "github.com" {
if let Some(hash) = cert.as_hostkey().and_then(|key| key.hash_sha256()) {
if GITHUB_FINGERPRINTS
.iter()
.any(|fingerprint| fingerprint == hash)
{
return Ok(CertificateCheckStatus::CertificateOk);
}
}
}

Ok(CertificateCheckStatus::CertificatePassthrough)
});
callbacks.certificate_check(Self::certificate_check);

let mut opts = git2::FetchOptions::new();
opts.remote_callbacks(callbacks);
opts
}

fn certificate_check(
cert: &Cert,
hostname: &str,
) -> Result<CertificateCheckStatus, git2::Error> {
if hostname == "github.com" {
if let Some(hash) = cert.as_hostkey().and_then(|key| key.hash_sha256()) {
if GITHUB_FINGERPRINTS
.iter()
.any(|fingerprint| fingerprint == hash)
{
return Ok(CertificateCheckStatus::CertificateOk);
}
}
}

Ok(CertificateCheckStatus::CertificatePassthrough)
}

/// Reset `HEAD` to a single commit with all the index contents, but no parent
pub fn squash_to_single_commit(&self, msg: &str) -> anyhow::Result<()> {
let tree = self.repository.find_commit(self.head_oid()?)?.tree()?;
Expand Down