Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: increase number of public key attempts before failing #451

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
})
}
Loading