Skip to content

Commit

Permalink
feat: increase number of public key attempts before failing
Browse files Browse the repository at this point in the history
  • Loading branch information
rocketeerbkw committed Jun 12, 2024
1 parent 0d6fdd5 commit 703e587
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
17 changes: 11 additions & 6 deletions internal/sshserver/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ import (
// (e.g. via signal)
const shutdownTimeout = 8 * time.Second

// disableSHA1Kex returns a ServerConfig which relies on default for everything
// except key exchange algorithms. There it removes the SHA1 based algorithms.
//
// This works around https://github.com/golang/go/issues/59593
func disableSHA1Kex(_ ssh.Context) *gossh.ServerConfig {
// serverConfig returns a ServerConfig of default values with overriden public
// key algorithms and failure attempts.
func serverConfig(_ ssh.Context) *gossh.ServerConfig {
c := gossh.ServerConfig{}

// Remove the SHA1 based key algorithms.
// This works around https://github.com/golang/go/issues/59593
c.Config.KeyExchanges = []string{
"curve25519-sha256",
"curve25519-sha256@libssh.org",
Expand All @@ -33,6 +34,10 @@ func disableSHA1Kex(_ ssh.Context) *gossh.ServerConfig {
"ecdh-sha2-nistp521",
"diffie-hellman-group14-sha256",
}

// Increase the number of public-key attempts before failure.
c.MaxAuthTries = 18

return &c
}

Expand All @@ -53,7 +58,7 @@ func Serve(
"sftp": ssh.SubsystemHandler(sessionHandler(log, c, true, logAccessEnabled)),
},
PublicKeyHandler: pubKeyAuth(log, nc, c),
ServerConfigCallback: disableSHA1Kex,
ServerConfigCallback: serverConfig,
Banner: banner,
}
for _, hk := range hostKeys {
Expand Down
25 changes: 12 additions & 13 deletions internal/sshserver/serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@ import (
)

func TestDisableSHA1Kex(t *testing.T) {
var testCases = map[string]struct {
input string
expect bool
}{
"no sha1": {input: "diffie-hellman-group14-sha1", expect: false},
}
for name, tc := range testCases {
t.Run(name, func(tt *testing.T) {
conf := disableSHA1Kex(nil)
assert.Equal(tt, tc.expect,
slices.Contains(conf.Config.KeyExchanges, tc.input), name)
})
}
t.Run("no sha1", func(tt *testing.T) {
conf := serverConfig(nil)
assert.Equal(tt, false,
slices.Contains(conf.Config.KeyExchanges, "diffie-hellman-group14-sha1"), "no sha1")
})
}

func TestMaxAuthTries(t *testing.T) {
t.Run("MaxAuthTries", func(tt *testing.T) {
conf := serverConfig(nil)
assert.Equal(tt, 18, conf.MaxAuthTries, "MaxAuthTries")
})
}

0 comments on commit 703e587

Please sign in to comment.