Skip to content

Commit 9f72ecc

Browse files
committed
ssh/knownhosts: treat only ASCII space and tab as whitespace
The previous implementation used bytes.TrimSpace, which strips all Unicode whitespace categories (e.g., non-breaking spaces). However, OpenSSH's known_hosts parser (hostfile.c) strictly treats only ASCII space (0x20) and horizontal tab (0x09) as separators. This discrepancy meant the Go parser might interpret fields differently than OpenSSH, potentially treating parts of a key or hostname as separators if they contained Unicode whitespace. This change replaces bytes.TrimSpace with a local trimSpace helper that only trims " \t", ensuring parsing behavior consistent with the reference implementation. This issue was found during a security audit by NCC Group Cryptography Services, sponsored by Teleport, and was assessed and is being fixed as a non-security bug. Change-Id: Ia536889636de2c167d2507c01e3f1b7c033c9a8f Reviewed-on: https://go-review.googlesource.com/c/crypto/+/782426 Reviewed-by: David Chase <[email protected]> Reviewed-by: Filippo Valsorda <[email protected]> LUCI-TryBot-Result: [email protected] <[email protected]> Reviewed-by: Junyang Shao <[email protected]>
1 parent 8f405a4 commit 9f72ecc

2 files changed

Lines changed: 48 additions & 2 deletions

File tree

‎ssh/knownhosts/knownhosts.go‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ func nextWord(line []byte) (string, []byte) {
178178
return string(line), nil
179179
}
180180

181-
return string(line[:i]), bytes.TrimSpace(line[i:])
181+
return string(line[:i]), trimSpace(line[i:])
182182
}
183183

184184
func parseLine(line []byte) (marker, host string, key ssh.PublicKey, err error) {
@@ -387,7 +387,7 @@ func (db *hostKeyDB) Read(r io.Reader, filename string) error {
387387
for scanner.Scan() {
388388
lineNum++
389389
line := scanner.Bytes()
390-
line = bytes.TrimSpace(line)
390+
line = trimSpace(line)
391391
if len(line) == 0 || line[0] == '#' {
392392
continue
393393
}
@@ -535,3 +535,10 @@ func newHashedHost(encoded string) (*hashedHost, error) {
535535
func (h *hashedHost) match(a addr) bool {
536536
return bytes.Equal(hashHost(Normalize(a.String()), h.salt), h.hash)
537537
}
538+
539+
// trimSpace removes leading and trailing ASCII whitespace (space and tab). It
540+
// is used instead of bytes.TrimSpace to match OpenSSH behavior, which strictly
541+
// parses only ASCII space (0x20) and tab (0x09) as whitespace.
542+
func trimSpace(in []byte) []byte {
543+
return bytes.Trim(in, " \t")
544+
}

‎ssh/knownhosts/knownhosts_test.go‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"bytes"
99
"crypto/ed25519"
1010
"crypto/rand"
11+
"encoding/base64"
1112
"fmt"
1213
"net"
1314
"reflect"
@@ -407,3 +408,41 @@ func TestIssue36126(t *testing.T) {
407408
t.Errorf("should have passed the check, got %v", err)
408409
}
409410
}
411+
412+
func TestUnicodeSpace(t *testing.T) {
413+
line := fmt.Sprintf("server.org %s  %s", edKey.Type(), base64.StdEncoding.EncodeToString(edKey.Marshal()))
414+
415+
db := newHostKeyDB()
416+
err := db.Read(bytes.NewBufferString(line), "testdb")
417+
418+
if err == nil {
419+
t.Fatal("Read succeeded on line with Unicode space, expected error due to strict ASCII parsing")
420+
}
421+
}
422+
423+
func TestUnicodeSpaceTrimming(t *testing.T) {
424+
const unicodeSpace = " "
425+
line := fmt.Sprintf("%sserver.org %s", unicodeSpace, edKeyStr)
426+
427+
db := newHostKeyDB()
428+
err := db.Read(bytes.NewBufferString(line), "testdb")
429+
430+
if err != nil {
431+
t.Fatalf("Read failed: %v", err)
432+
}
433+
434+
lines := db.lines
435+
if len(lines) != 1 {
436+
t.Fatalf("Expected 1 line, got %d", len(lines))
437+
}
438+
439+
cleanAddr := addr{host: "server.org", port: "22"}
440+
dirtyAddr := addr{host: unicodeSpace + "server.org", port: "22"}
441+
442+
if lines[0].match(cleanAddr) {
443+
t.Errorf("Matched clean host 'server.org', implying Unicode space was trimmed")
444+
}
445+
if !lines[0].match(dirtyAddr) {
446+
t.Errorf("Did not match dirty host, implying parsing issue")
447+
}
448+
}

0 commit comments

Comments
 (0)