Skip to content

feat: not fail on non-default ports - #6895

Merged
tonistiigi merged 1 commit into
moby:masterfrom
gilescope:giles-fix-sshkeyscan-nonstandard-port
Jun 26, 2026
Merged

feat: not fail on non-default ports#6895
tonistiigi merged 1 commit into
moby:masterfrom
gilescope:giles-fix-sshkeyscan-nonstandard-port

Conversation

@gilescope

@gilescope gilescope commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Description

SSHKeyScan builds a known_hosts entry from the scanned host key but
discards the port, always emitting a bare hostname:

hostname, _, err := net.SplitHostPort(hostport)
...
key = fmt.Sprintf("%s %s", hostname, ...)

For a server reachable on a non-standard port (e.g. git.example.com:2222)
this stores the key under git.example.com, but ssh looks it up under
[git.example.com]:2222 per the SSH_KNOWN_HOSTS format in sshd(8). The
entry never matches, so host-key verification fails for any git source or
SSH endpoint on a non-22 port.

Fix

Render non-standard ports as [host]:port; keep the default port (22) as a
bare hostname (matching ssh-keyscan and the known_hosts format). The
formatting is extracted into a small pure helper, knownHostsServerID, which
is unit-tested directly (a full SSH handshake is impractical to fake in a
unit test).

Test

go test ./util/sshutil/ — new table test covers default port, empty port,
non-standard port, and IPv6 (default and non-standard).

Signed-off-by: Giles Cope <gilescope@gmail.com>
@gilescope
gilescope force-pushed the giles-fix-sshkeyscan-nonstandard-port branch from deef161 to 6678be5 Compare June 24, 2026 05:31
@tonistiigi
tonistiigi merged commit 587d308 into moby:master Jun 26, 2026
192 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants