if the first smtp or imap command is invalid, shut down the connection instead of trying to read more

this is quite common on the internet. the other side may be trying some other
protocol, e.g. http, or some common vulnerability. we don't want to spam our
own logs with multiple invalid lines. if the first command is valid, but later
are not, we'll keep trying to process them. so this only affects protocol
sessions that are very likely not smtp/imap.

also remove a few more sleeps during tests, making imapserver and smtpserver tests a bit faster.
This commit is contained in:
Mechiel Lukkien
2023-03-10 10:23:43 +01:00
parent 2c07645ab4
commit e413c906b1
5 changed files with 99 additions and 19 deletions

View File

@ -26,6 +26,7 @@ func init() {
// Don't slow down tests.
badClientDelay = 0
authFailDelay = 0
}
func tocrlf(s string) string {
@ -397,8 +398,6 @@ func TestLogin(t *testing.T) {
func TestState(t *testing.T) {
tc := start(t)
tc.transactf("bad", "boguscommand")
notAuthenticated := []string{"starttls", "authenticate", "login"}
authenticatedOrSelected := []string{"enable", "select", "examine", "create", "delete", "rename", "subscribe", "unsubscribe", "list", "namespace", "status", "append", "idle", "lsub"}
selected := []string{"close", "unselect", "expunge", "search", "fetch", "store", "copy", "move", "uid expunge"}
@ -421,6 +420,21 @@ func TestState(t *testing.T) {
for _, cmd := range append(append([]string{}, notAuthenticated...), selected...) {
tc.transactf("no", "%s", cmd)
}
tc.transactf("bad", "boguscommand")
}
func TestNonIMAP(t *testing.T) {
tc := start(t)
defer tc.close()
// imap greeting has already been read, we sidestep the imapclient.
_, err := fmt.Fprintf(tc.conn, "bogus\r\n")
tc.check(err, "write bogus command")
tc.readprefixline("* BYE ")
if _, err := tc.conn.Read(make([]byte, 1)); err == nil {
t.Fatalf("connection not closed after initial bad command")
}
}
func TestLiterals(t *testing.T) {