Skip to content

Commit 6129f06

Browse files
committed
ssh/knownhosts: fix bracket normalisation
1. When using the bracketed syntax, the port is mandatory, even if it's the default one. Otherwise, OpenSSH rejects it with: "address [abcd:abcd:abcd:abcd]: missing port in address". See sshd(8): SSH_KNOWN_HOSTS FILE FORMAT. 2. Brackets are not necessary when using the default port, even for IPv6 addresses. Fixes golang/go#53463
1 parent b4ddeed commit 6129f06

File tree

2 files changed

+4
-6
lines changed

2 files changed

+4
-6
lines changed

ssh/knownhosts/knownhosts.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -439,14 +439,12 @@ func New(files ...string) (ssh.HostKeyCallback, error) {
439439
func Normalize(address string) string {
440440
host, port, err := net.SplitHostPort(address)
441441
if err != nil {
442-
host = address
442+
host = strings.Trim(address, "[]")
443443
port = "22"
444444
}
445445
entry := host
446446
if port != "22" {
447447
entry = "[" + entry + "]:" + port
448-
} else if strings.Contains(host, ":") && !strings.HasPrefix(host, "[") {
449-
entry = "[" + entry + "]"
450448
}
451449
return entry
452450
}

ssh/knownhosts/knownhosts_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ func TestLine(t *testing.T) {
247247
"server.org": "server.org " + edKeyStr,
248248
"server.org:22": "server.org " + edKeyStr,
249249
"server.org:23": "[server.org]:23 " + edKeyStr,
250-
"[c629:1ec4:102:304:102:304:102:304]:22": "[c629:1ec4:102:304:102:304:102:304] " + edKeyStr,
250+
"[c629:1ec4:102:304:102:304:102:304]:22": "c629:1ec4:102:304:102:304:102:304 " + edKeyStr,
251251
"[c629:1ec4:102:304:102:304:102:304]:23": "[c629:1ec4:102:304:102:304:102:304]:23 " + edKeyStr,
252252
} {
253253
if got := Line([]string{in}, edKey); got != want {
@@ -326,8 +326,8 @@ func TestNormalize(t *testing.T) {
326326
"[127.0.0.1]:23": "[127.0.0.1]:23",
327327
"127.0.0.1:23": "[127.0.0.1]:23",
328328
"[a.b.c]:22": "a.b.c",
329-
"[abcd:abcd:abcd:abcd]": "[abcd:abcd:abcd:abcd]",
330-
"[abcd:abcd:abcd:abcd]:22": "[abcd:abcd:abcd:abcd]",
329+
"[abcd:abcd:abcd:abcd]": "abcd:abcd:abcd:abcd",
330+
"[abcd:abcd:abcd:abcd]:22": "abcd:abcd:abcd:abcd",
331331
"[abcd:abcd:abcd:abcd]:23": "[abcd:abcd:abcd:abcd]:23",
332332
} {
333333
got := Normalize(in)

0 commit comments

Comments
 (0)