lib: fix an out of bounds panic in the server

If the message doesn't contain ':', we don't properly discard the
message, so we end up slicing it like msg[:-1].

This can be reproduced if one runs 'aerc foo', as the server receives
'foo' as the message.

'aerc foo' still doesn't do anything very user friendly, but at least it
doesn't panic horribly.

While at it, do the 'got message' log at the very beginning, so that the
user can see what message the server got before reporting the command as
invalid.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
This commit is contained in:
Daniel Martí 2020-02-13 09:25:55 +00:00 committed by Drew DeVault
parent a82fa2bf23
commit 72f55b857b
1 changed files with 2 additions and 1 deletions

View File

@ -61,10 +61,11 @@ func (as *AercServer) handleClient(conn net.Conn) {
for scanner.Scan() { for scanner.Scan() {
conn.SetDeadline(time.Now().Add(1 * time.Minute)) conn.SetDeadline(time.Now().Add(1 * time.Minute))
msg := scanner.Text() msg := scanner.Text()
as.logger.Printf("unix:%d: got message %s", clientId, msg)
if !strings.ContainsRune(msg, ':') { if !strings.ContainsRune(msg, ':') {
conn.Write([]byte("error: invalid command\n")) conn.Write([]byte("error: invalid command\n"))
continue
} }
as.logger.Printf("unix:%d: got message %s", clientId, msg)
prefix := msg[:strings.IndexRune(msg, ':')] prefix := msg[:strings.IndexRune(msg, ':')]
switch prefix { switch prefix {
case "mailto": case "mailto":