Skip to content

plumbing: transport/ssh, honor custom host key callbacks#2145

Open
puneetdixit200 wants to merge 3 commits into
go-git:mainfrom
puneetdixit200:fix/1234-custom-hostkey-callback
Open

plumbing: transport/ssh, honor custom host key callbacks#2145
puneetdixit200 wants to merge 3 commits into
go-git:mainfrom
puneetdixit200:fix/1234-custom-hostkey-callback

Conversation

@puneetdixit200

@puneetdixit200 puneetdixit200 commented May 20, 2026

Copy link
Copy Markdown

Summary

  • keep custom HostKeyCallback configurations usable when known_hosts files are missing
  • preserve known_hosts-derived HostKeyAlgorithms for custom callbacks when known_hosts is available and the caller leaves HostKeyAlgorithms empty
  • clarify SSH trace output when the client uses default host key algorithm negotiation
  • avoid process-wide SSH_KNOWN_HOSTS mutation in regression tests by injecting known_hosts files through the transport

Fixes #1234

Validation

  • Red check before implementation: go test ./plumbing/transport/ssh -run TestSSHTransport_CustomHostKeyCallbackWithoutKnownHosts -count=1 failed with unable to find any valid known_hosts file, set SSH_KNOWN_HOSTS env variable instead of reaching the dialer
  • go test ./plumbing/transport/ssh -count=1
  • go test ./plumbing/transport/... -count=1
  • go test -race ./plumbing/transport/ssh ./plumbing/transport/ssh/knownhosts -count=1
  • make validate-lint
  • git diff --check and git diff HEAD^ --check
  • Latest review-thread refresh: go test ./plumbing/transport/ssh and git diff --check upstream/main...HEAD

Also ran make test, which executes go test -race ./.... The touched SSH/transport packages passed in that run, but the full target failed in the existing local plumbing/format/gitignore conformance oracle against Apple Git 2.39.5 for foo**/bar matching foobar/foobar/baz; that package is outside this change.

AI assistance disclosure

OpenAI GPT-5 assisted with issue/code navigation, drafting the focused tests and patches, and selecting validation commands. I reviewed the final diff and validation output before submitting.

Copilot AI review requested due to automatic review settings May 20, 2026 21:49

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Updates SSH transport connection setup to avoid relying on known_hosts when callers provide a custom HostKeyCallback, and adds a regression test to ensure missing known_hosts doesn’t block connection attempts.

Changes:

  • Skip deriving HostKeyAlgorithms from known_hosts when a custom HostKeyCallback is provided.
  • Add a test covering custom host key callbacks with a missing known_hosts path.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
plumbing/transport/ssh/ssh.go Adjusts host key algorithm derivation logic to only use known_hosts for the default callback path.
plumbing/transport/ssh/ssh_test.go Adds a regression test ensuring missing known_hosts does not prevent dialing when using a custom callback.

Comment thread plumbing/transport/ssh/ssh_test.go Outdated
Comment thread plumbing/transport/ssh/ssh.go Outdated
Comment thread plumbing/transport/ssh/ssh.go Outdated
@puneetdixit200 puneetdixit200 force-pushed the fix/1234-custom-hostkey-callback branch 6 times, most recently from b44c5bf to 1afa266 Compare May 28, 2026 15:36
@puneetdixit200

Copy link
Copy Markdown
Author

Rebased this branch onto current main (head 1afa266). No code changes beyond replaying the existing commits.

Verified locally:

  • gofmt on the changed Go files
  • GOMODCACHE=/tmp/go-git-2145-gomodcache GOCACHE=/tmp/go-git-2145-gocache go test ./plumbing/transport/ssh -run 'TestSSHTransport_CustomHostKeyCallback(WithoutKnownHosts|UsesKnownHostsAlgorithms)$' -count=1
  • GOMODCACHE=/tmp/go-git-2145-gomodcache GOCACHE=/tmp/go-git-2145-gocache go test ./plumbing/transport/ssh -count=1
  • git diff --check upstream/main...HEAD

@puneetdixit200 puneetdixit200 force-pushed the fix/1234-custom-hostkey-callback branch from 1afa266 to 2ca9bfd Compare May 30, 2026 10:44
@puneetdixit200

Copy link
Copy Markdown
Author

Rebased this onto current main. I rechecked the SSH transport changes with:

  • gofmt -l plumbing/transport/ssh/auth.go plumbing/transport/ssh/ssh.go plumbing/transport/ssh/ssh_test.go
  • git diff --check upstream/main...HEAD
  • go test ./plumbing/transport/ssh -run 'TestSSHTransport_CustomHostKeyCallback(WithoutKnownHosts|UsesKnownHostsAlgorithms)$' -count=1\n- go test ./plumbing/transport/ssh -count=1

…t#1234

When a caller supplies HostKeyCallback, avoid probing default known_hosts to derive HostKeyAlgorithms. Document the behavior and keep the regression test serialized around its temporary known_hosts override.

Assisted-by: OpenAI GPT-5 <[email protected]>
Signed-off-by: Puneet Dixit <[email protected]>
Let the custom callback regression inject a missing known_hosts path on the transport instance instead of mutating SSH_KNOWN_HOSTS process-wide.

Assisted-by: OpenAI GPT-5 <[email protected]>
Signed-off-by: Puneet Dixit <[email protected]>
Keep deriving HostKeyAlgorithms from known_hosts for custom host key callbacks when known_hosts is available. Missing known_hosts files are ignored only for custom callbacks so callers can still provide their own verifier without requiring local known_hosts state.

Assisted-by: OpenAI GPT-5 <[email protected]>

Signed-off-by: Puneet Dixit <[email protected]>
@puneetdixit200 puneetdixit200 force-pushed the fix/1234-custom-hostkey-callback branch from 2ca9bfd to 36fa61b Compare June 2, 2026 23:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Auth with known hosts callback always fails if no known_hosts file is found in the standard locations

2 participants