Skip to content

Commit

Permalink
Working on '\r\n' CRLF in Parser
Browse files Browse the repository at this point in the history
  • Loading branch information
DenBeke committed Oct 21, 2015
1 parent e8e971e commit dc5cdda
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 39 deletions.
4 changes: 4 additions & 0 deletions smtp/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ func parseLine(line string) (verb string, args []string, err error) {
return "", []string{}, errors.New("Line too long")
}

// Strip \n and \r
line = strings.TrimSuffix(line, "\n")
line = strings.TrimSuffix(line, "\r")

This comment has been minimized.

Copy link
@trtstm

trtstm Oct 21, 2015

Contributor

If other servers follow the standard we can just do
line = strings.TrimSuffix(line, "\r\n")
Does google use \n or \r\n?
Edit: It's probably safest to do it your way...

This comment has been minimized.

Copy link
@DenBeke

DenBeke Oct 22, 2015

Author Contributor

Edit: It's probably safest to do it your way...

But what if some MS server sends HELO blablabla\nblablabla ? :)

This comment has been minimized.

Copy link
@trtstm

trtstm Oct 22, 2015

Contributor

Don't think a newline is allowed in the HELO cmd.

i := strings.Index(line, " ")
if i == -1 {
verb = strings.ToUpper(strings.TrimSpace(line))
Expand Down
87 changes: 48 additions & 39 deletions smtp/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,20 @@ import (
func TestParser(t *testing.T) {

Convey("Testing parser", t, func() {
commands :=
`HELO relay.example.org
EHLO other.example.org
MAIL FROM:<bob@example.org>
RCPT TO:<alice@example.com>
RCPT TO:<theboss@example.com>
SEND
SOML
SAML
RSET
VRFY jones
EXPN staff
NOOP
QUIT
`
commands := ""
commands += "HELO relay.example.org\r\n"
commands += "EHLO other.example.org\r\n"
commands += "MAIL FROM:<bob@example.org>\r\n"
commands += "RCPT TO:<alice@example.com>\r\n"
commands += "RCPT TO:<theboss@example.com>\r\n"
commands += "SEND\r\n"
commands += "SOML\r\n"
commands += "SAML\r\n"
commands += "RSET\r\n"
commands += "VRFY jones\r\n"
commands += "EXPN staff\r\n"
commands += "NOOP\r\n"
commands += "QUIT\r\n"

br := bufio.NewReader(strings.NewReader(commands))

Expand All @@ -49,6 +48,7 @@ QUIT
}

for _, expectedCommand := range expectedCommands {
Print(expectedCommand)
command, err := p.ParseCommand(br)
So(err, ShouldEqual, nil)
So(command, ShouldResemble, expectedCommand)
Expand All @@ -57,12 +57,11 @@ QUIT
})

Convey("Testing parser DATA cmd", t, func() {
commands :=
`DATA
Some usefull data.
.
quit
`
commands := ""
commands += "DATA\r\n"
commands += "Some usefull data.\r\n"
commands += ".\r\n"
commands += "quit\r\n"

br := bufio.NewReader(strings.NewReader(commands))
p := parser{}
Expand All @@ -74,9 +73,9 @@ quit
So(ok, ShouldEqual, true)
br2 := bufio.NewReader(dataCommand.R.r)
line, _ := br2.ReadString('\n')
So(line, ShouldEqual, "Some usefull data.\n")
So(line, ShouldEqual, "Some usefull data.\r\n")
line, _ = br2.ReadString('\n')
So(line, ShouldEqual, ".\n")
So(line, ShouldEqual, ".\r\n")

command, err = p.ParseCommand(br)
So(err, ShouldEqual, nil)
Expand All @@ -86,22 +85,22 @@ quit

Convey("Testing parser with invalid commands", t, func() {

commands := `RCPT
helo
ehlo
RCPT TO:some invalid email
rcpt :valid@mail.be
RCPT :valid@mail.be
RCPT TA:valid@mail.be
MAIL
MAIL from:some invalid email
MAIL :valid@mail.be
MAIL FROA:valid@mail.be
MAIL To some@invalid
UNKN some unknown command
`
commands := ""
commands += "RCPT\r\n"
commands += "helo\r\n"
commands += "ehlo\r\n"
commands += "\r\n"
commands += " \r\n"
commands += "RCPT TO:some invalid email\r\n"
commands += "rcpt :valid@mail.be\r\n"
commands += "RCPT :valid@mail.be\r\n"
commands += "RCPT TA:valid@mail.be\r\n"
commands += "MAIL\r\n"
commands += "MAIL from:some invalid email\r\n"
commands += "MAIL :valid@mail.be\r\n"
commands += "MAIL FROA:valid@mail.be\r\n"
commands += "MAIL To some@invalid\r\n"
commands += "UNKN some unknown command\r\n"

br := bufio.NewReader(strings.NewReader(commands))

Expand Down Expand Up @@ -154,6 +153,16 @@ UNKN some unknown command
verb: "MAIL",
args: []string{"FROM:<bob@example.org>"},
},
{
line: "HELO some_ctrl_char\r\n",
verb: "HELO",
args: []string{"some_ctrl_char"},
},
{
line: "HELO some_ctrl_char\n",
verb: "HELO",
args: []string{"some_ctrl_char"},
},
}

for _, test := range tests {
Expand Down

0 comments on commit dc5cdda

Please sign in to comment.