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

Properly handle DATA read timeouts #197

Closed
wants to merge 1 commit into from
Closed
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
15 changes: 12 additions & 3 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -628,18 +628,27 @@ func (c *Conn) handleData(arg string) {
// We have recipients, go to accept data
c.writeResponse(354, EnhancedCode{2, 0, 0}, "Go ahead. End your data with <CR><LF>.<CR><LF>")

defer c.reset()

if c.server.LMTP {
c.handleDataLMTP()
c.reset()
return
}

r := newDataReader(c)
code, enhancedCode, msg := toSMTPStatus(c.Session().Data(r))
err := c.Session().Data(r)
code, enhancedCode, msg := toSMTPStatus(err)
if err == ErrDataTimeout {
// don't copy the data, write response and close the connection
c.writeResponse(code, enhancedCode, msg)
c.reset()
c.Close()
return
}

r.limited = false
io.Copy(ioutil.Discard, r) // Make sure all the data has been consumed
c.writeResponse(code, enhancedCode, msg)
c.reset()
}

func (c *Conn) handleBdat(arg string) {
Expand Down
25 changes: 21 additions & 4 deletions data.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package smtp

import (
"bufio"
"io"
"net"
"time"
)

type EnhancedCode [3]int
Expand Down Expand Up @@ -42,8 +43,14 @@ var ErrDataTooLarge = &SMTPError{
Message: "Maximum message size exceeded",
}

var ErrDataTimeout = &SMTPError{
Code: 451,
EnhancedCode: EnhancedCode{4, 4, 2},
Message: "Timeout waiting for data from client",
}

type dataReader struct {
r *bufio.Reader
c *Conn
state int

limited bool
Expand All @@ -52,7 +59,7 @@ type dataReader struct {

func newDataReader(c *Conn) *dataReader {
dr := &dataReader{
r: c.text.R,
c: c,
}

if c.server.MaxMessageBytes > 0 {
Expand Down Expand Up @@ -87,8 +94,18 @@ func (r *dataReader) Read(b []byte) (n int, err error) {
stateEOF // reached .\r\n end marker line
)
for n < len(b) && r.state != stateEOF {
if r.c.server.ReadTimeout != 0 {
err = r.c.conn.SetReadDeadline(time.Now().Add(r.c.server.ReadTimeout))
if err != nil {
break
}
if e, ok := err.(net.Error); ok && e.Timeout() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, so this checks a SetReadDeadline error? Weird, I would've expected to check the ReadByte error instead?

BTW, a better way to check for the timeout is: errors.Is(err, os.ErrDeadlineExceeded) (see https://pkg.go.dev/net#Conn).

r.c.server.ErrorLog.Printf("data read timeout: %w", err)
err = ErrDataTimeout
}
}
var c byte
c, err = r.r.ReadByte()
c, err = r.c.text.R.ReadByte()
if err != nil {
if err == io.EOF {
err = io.ErrUnexpectedEOF
Expand Down