Skip to content

Commit a4d1237

Browse files
committed
ssh/knownhosts: improve IPv6 support in Normalize
Correctly converts bracketed IPv6: - [abcd::abcd:abcd:abcd] => abcd::abcd:abcd:abcd - [abcd::abcd:abcd:abcd]:22 => abcd::abcd:abcd:abcd - [abcd::abcd:abcd:abcd]:23 => [abcd::abcd:abcd:abcd]:23 Fixes golang/go#53463 Change-Id: Id0a7460d8448a72e2a8c6d46137245bead9ecf9f Reviewed-on: https://go-review.googlesource.com/c/crypto/+/694575 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Filippo Valsorda <[email protected]> Reviewed-by: David Chase <[email protected]> Reviewed-by: Carlos Amedee <[email protected]>
1 parent b8d8dae commit a4d1237

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

ssh/knownhosts/knownhosts.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -421,20 +421,26 @@ func New(files ...string) (ssh.HostKeyCallback, error) {
421421
return certChecker.CheckHostKey, nil
422422
}
423423

424-
// Normalize normalizes an address into the form used in known_hosts
424+
// Normalize normalizes an address into the form used in known_hosts. Supports
425+
// IPv4, hostnames, bracketed IPv6. Any other non-standard formats are returned
426+
// with minimal transformation.
425427
func Normalize(address string) string {
428+
const defaultSSHPort = "22"
429+
426430
host, port, err := net.SplitHostPort(address)
427431
if err != nil {
428432
host = address
429-
port = "22"
433+
port = defaultSSHPort
434+
}
435+
436+
if strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]") {
437+
host = host[1 : len(host)-1]
430438
}
431-
entry := host
432-
if port != "22" {
433-
entry = "[" + entry + "]:" + port
434-
} else if strings.Contains(host, ":") && !strings.HasPrefix(host, "[") {
435-
entry = "[" + entry + "]"
439+
440+
if port == defaultSSHPort {
441+
return host
436442
}
437-
return entry
443+
return "[" + host + "]:" + port
438444
}
439445

440446
// Line returns a line to add append to the known_hosts files.

ssh/knownhosts/knownhosts_test.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ func TestLine(t *testing.T) {
236236
"server.org": "server.org " + edKeyStr,
237237
"server.org:22": "server.org " + edKeyStr,
238238
"server.org:23": "[server.org]:23 " + edKeyStr,
239-
"[c629:1ec4:102:304:102:304:102:304]:22": "[c629:1ec4:102:304:102:304:102:304] " + edKeyStr,
239+
"[c629:1ec4:102:304:102:304:102:304]:22": "c629:1ec4:102:304:102:304:102:304 " + edKeyStr,
240240
"[c629:1ec4:102:304:102:304:102:304]:23": "[c629:1ec4:102:304:102:304:102:304]:23 " + edKeyStr,
241241
} {
242242
if got := Line([]string{in}, edKey); got != want {
@@ -310,14 +310,25 @@ func testHostHash(t *testing.T, hostname, encoded string) {
310310

311311
func TestNormalize(t *testing.T) {
312312
for in, want := range map[string]string{
313-
"127.0.0.1:22": "127.0.0.1",
314-
"[127.0.0.1]:22": "127.0.0.1",
315-
"[127.0.0.1]:23": "[127.0.0.1]:23",
316-
"127.0.0.1:23": "[127.0.0.1]:23",
317-
"[a.b.c]:22": "a.b.c",
318-
"[abcd:abcd:abcd:abcd]": "[abcd:abcd:abcd:abcd]",
319-
"[abcd:abcd:abcd:abcd]:22": "[abcd:abcd:abcd:abcd]",
320-
"[abcd:abcd:abcd:abcd]:23": "[abcd:abcd:abcd:abcd]:23",
313+
"127.0.0.1": "127.0.0.1",
314+
"127.0.0.1:22": "127.0.0.1",
315+
"[127.0.0.1]:22": "127.0.0.1",
316+
"[127.0.0.1]:23": "[127.0.0.1]:23",
317+
"127.0.0.1:23": "[127.0.0.1]:23",
318+
"[a.b.c]:22": "a.b.c",
319+
"[a.b.c]:23": "[a.b.c]:23",
320+
"abcd::abcd:abcd:abcd": "abcd::abcd:abcd:abcd",
321+
"[abcd::abcd:abcd:abcd]": "abcd::abcd:abcd:abcd",
322+
"[abcd::abcd:abcd:abcd]:22": "abcd::abcd:abcd:abcd",
323+
"[abcd::abcd:abcd:abcd]:23": "[abcd::abcd:abcd:abcd]:23",
324+
"2001:db8::1": "2001:db8::1",
325+
"2001:db8::1:22": "2001:db8::1:22",
326+
"[2001:db8::1]:22": "2001:db8::1",
327+
"2001:db8::1:2200": "2001:db8::1:2200",
328+
"a.b.c.d.com:2200": "[a.b.c.d.com]:2200",
329+
"2001::db8:1": "2001::db8:1",
330+
"2001::db8:1:22": "2001::db8:1:22",
331+
"2001::db8:1:2200": "2001::db8:1:2200",
321332
} {
322333
got := Normalize(in)
323334
if got != want {

0 commit comments

Comments
 (0)