Outgoing messages were not being properly validated for \r and \n
This commit is contained in:
Shivaram Lingamneni 2021-02-21 17:03:44 -05:00
parent 7516151d4c
commit e489a564c1
2 changed files with 25 additions and 20 deletions

@ -419,7 +419,8 @@ func (ircmsg *IRCMessage) line(tagLimit, clientOnlyTagDataLimit, serverAddedTagD
buf.WriteString("\r\n")
result := buf.Bytes()
if bytes.IndexByte(result, '\x00') != -1 {
toValidate := result[:len(result)-2]
if bytes.IndexByte(toValidate, '\x00') != -1 || bytes.IndexByte(toValidate, '\r') != -1 || bytes.IndexByte(toValidate, '\n') != -1 {
return nil, ErrorLineContainsBadChar
}
return result, nil

@ -220,27 +220,31 @@ func TestEncode(t *testing.T) {
)
}
}
}
// make sure we fail on no command
msg := MakeMessage(nil, "example.com", "", "*")
_, err := msg.LineBytesStrict(true, 0)
if err == nil {
t.Error(
"For", "Test Failure 1",
"expected", "an error",
"got", err,
)
}
var encodeErrorTests = []struct {
tags map[string]string
prefix string
command string
params []string
err error
}{
{tags: nil, command: "PRIVMSG", params: []string{"", "hi"}, err: ErrorBadParam},
{tags: nil, command: "", params: []string{"hi"}, err: ErrorCommandMissing},
{tags: map[string]string{"a\x00b": "hi"}, command: "PING", params: []string{"hi"}, err: ErrorLineContainsBadChar},
{tags: map[string]string{"ab": "hi"}, command: "PING", params: []string{"h\x00i"}, err: ErrorLineContainsBadChar},
{tags: map[string]string{"ab": "hi"}, command: "PING", params: []string{"h\ni"}, err: ErrorLineContainsBadChar},
{tags: map[string]string{"ab": "hi"}, command: "PING", params: []string{"hi\rQUIT"}, err: ErrorLineContainsBadChar},
{tags: map[string]string{"ab": "hi"}, command: "NOTICE", params: []string{"#channel", "hi\r\nQUIT"}, err: ErrorLineContainsBadChar},
}
// make sure we fail with params in right way
msg = MakeMessage(nil, "example.com", "TEST", "*", "t s", "", "Param after empty!")
_, err = msg.LineBytesStrict(true, 0)
if err == nil {
t.Error(
"For", "Test Failure 2",
"expected", "an error",
"got", err,
)
func TestEncodeErrors(t *testing.T) {
for _, ep := range encodeErrorTests {
msg := MakeMessage(ep.tags, ep.prefix, ep.command, ep.params...)
_, err := msg.LineBytesStrict(true, 512)
if err != ep.err {
t.Errorf("For %#v, expected %v, got %v", msg, ep.err, err)
}
}
}