From 74f810a0152f4c50a16195f6b9ff44afc35594e8 Mon Sep 17 00:00:00 2001 From: Han-Wen Nienhuys Date: Fri, 8 May 2015 18:14:00 +0200 Subject: [PATCH] x/crypto/ssh: bail early if a server has no auth methods configured. Change-Id: I58fdfbe00fcc4ca09da9699edcc181cc512feef7 Reviewed-on: https://go-review.googlesource.com/9807 Reviewed-by: JP Sugarbroad Reviewed-by: Adam Langley --- ssh/server.go | 4 ++++ ssh/session_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/ssh/server.go b/ssh/server.go index 52aee11..baedf5b 100644 --- a/ssh/server.go +++ b/ssh/server.go @@ -168,6 +168,10 @@ func (s *connection) serverHandshake(config *ServerConfig) (*Permissions, error) return nil, errors.New("ssh: server has no host keys") } + if !config.NoClientAuth && config.PasswordCallback == nil && config.PublicKeyCallback == nil && config.KeyboardInteractiveCallback == nil { + return nil, errors.New("ssh: no authentication methods configured but NoClientAuth is also false") + } + if config.ServerVersion != "" { s.serverVersion = []byte(config.ServerVersion) } else { diff --git a/ssh/session_test.go b/ssh/session_test.go index 88e66bf..7ce44f5 100644 --- a/ssh/session_test.go +++ b/ssh/session_test.go @@ -9,9 +9,11 @@ package ssh import ( "bytes" crypto_rand "crypto/rand" + "errors" "io" "io/ioutil" "math/rand" + "net" "testing" "golang.org/x/crypto/ssh/terminal" @@ -678,3 +680,41 @@ func TestSessionID(t *testing.T) { t.Errorf("client and server SessionID were empty.") } } + +type noReadConn struct { + readSeen bool + net.Conn +} + +func (c *noReadConn) Close() error { + return nil +} + +func (c *noReadConn) Read(b []byte) (int, error) { + c.readSeen = true + return 0, errors.New("noReadConn error") +} + +func TestInvalidServerConfiguration(t *testing.T) { + c1, c2, err := netPipe() + if err != nil { + t.Fatalf("netPipe: %v", err) + } + defer c1.Close() + defer c2.Close() + + serveConn := noReadConn{Conn: c1} + serverConf := &ServerConfig{} + + NewServerConn(&serveConn, serverConf) + if serveConn.readSeen { + t.Fatalf("NewServerConn attempted to Read() from Conn while configuration is missing host key") + } + + serverConf.AddHostKey(testSigners["ecdsa"]) + + NewServerConn(&serveConn, serverConf) + if serveConn.readSeen { + t.Fatalf("NewServerConn attempted to Read() from Conn while configuration is missing authentication method") + } +}