feat(ssh): wire CheckHostIP from sshconfig for DNS-spoofing detection#378
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR wires the CheckHostIP ssh config option into the SSH host-key verification flow by wrapping the existing known_hosts host key callback with an additional “resolved IP mismatch” check to detect potential DNS spoofing.
Changes:
- Added
hostkey.WithCheckHostIP(...)wrapper that resolves hostnames to IPs and checks those IPs inknown_hostsfor conflicting keys (read-only, DNS failures/nonexistent IP entries are non-fatal). - Updated
protocol/ssh/connection.goto conditionally apply the wrapper whensshconfig.CheckHostIPis enabled, and adjusted global known_hosts selection to return the chosen path for wrapping. - Added focused unit tests for the new behavior and a test-only hook to stub DNS resolution.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| protocol/ssh/hostkey/export_test.go | Exposes a test-only hook to override hostname resolution. |
| protocol/ssh/hostkey/callbacks.go | Implements WithCheckHostIP and the IP-verification wrapper logic. |
| protocol/ssh/hostkey/callbacks_test.go | Adds unit tests covering mismatch detection, unknown IP non-fatal behavior, DNS failure behavior, permissive behavior, and IP-host skip behavior. |
| protocol/ssh/connection.go | Wraps host-key callbacks with WithCheckHostIP when CheckHostIP is enabled; returns selected global known_hosts path for wrapping. |
| protocol/ssh/connection_test.go | Adds an integration-style test ensuring hostkeyCallback enables CheckHostIP wrapping without breaking known_hosts verification. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
When CheckHostIP is enabled, the host key callback additionally resolves the connecting hostname to IP addresses and checks whether any of those IPs appear in known_hosts with a different key. A mismatch returns ErrHostKeyMismatch; unknown IPs and DNS failures are non-fatal. The IP check is read-only — no new entries are written for resolved IPs (writing both hostname and IP on first connect is deferred as a follow-on). Signed-off-by: Kimmo Lehto <klehto@mirantis.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Comments suppressed due to low confidence (1)
protocol/ssh/hostkey/callbacks_test.go:16
- The tests override a package-level variable (hostkey.LookupHostFunc). Without any synchronization, this becomes race-prone if these tests (or future tests in the same package) are run with t.Parallel(), since multiple tests could swap the global resolver concurrently. Adding a local mutex around the override makes the helper safe and documents that the override is global.
import (
"crypto/ed25519"
"crypto/rand"
"fmt"
"net"
"os"
"path/filepath"
"testing"
"github.com/k0sproject/rig/v2/protocol/ssh/hostkey"
"github.com/stretchr/testify/require"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/knownhosts"
)
Signed-off-by: Kimmo Lehto <klehto@mirantis.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When CheckHostIP is enabled, the host key callback additionally resolves the connecting hostname to IP addresses and checks whether any of those IPs appear in known_hosts with a different key. A mismatch returns ErrHostKeyMismatch; unknown IPs and DNS failures are non-fatal.
The IP check is read-only — no new entries are written for resolved IPs (writing both hostname and IP on first connect is deferred as a follow-on).