Keep the IMAP password out of error messages
A refused IMAP command's error quoted the whole command, so a failed
LOGIN put the password into the log on every sync. The error now names
only the verb ("IMAP LOGIN failed: ..."). Carried over from web_server's
backend, which logged it the same way.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -120,7 +120,10 @@ func (c *imapClient) sendCommandOK(cmd string) ([]string, error) {
|
|||||||
// "<tag> BAD ..." otherwise; only the second field matters.
|
// "<tag> BAD ..." otherwise; only the second field matters.
|
||||||
parts := strings.SplitN(status, " ", 3)
|
parts := strings.SplitN(status, " ", 3)
|
||||||
if len(parts) < 2 || parts[1] != "OK" {
|
if len(parts) < 2 || parts[1] != "OK" {
|
||||||
return untagged, fmt.Errorf("command %q failed: %s", cmd, status)
|
// Only the verb, never the whole command: a failed LOGIN carries the
|
||||||
|
// password, and this error goes to the log.
|
||||||
|
verb, _, _ := strings.Cut(cmd, " ")
|
||||||
|
return untagged, fmt.Errorf("IMAP %s failed: %s", verb, status)
|
||||||
}
|
}
|
||||||
return untagged, nil
|
return untagged, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
package services
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"net"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// A refused command's error is logged by the sync, so it must not quote the
|
||||||
|
// command's arguments: for LOGIN those include the password.
|
||||||
|
func TestIMAPCommandErrorOmitsArguments(t *testing.T) {
|
||||||
|
client, server := net.Pipe()
|
||||||
|
defer client.Close()
|
||||||
|
go func() {
|
||||||
|
defer server.Close()
|
||||||
|
r := bufio.NewReader(server)
|
||||||
|
line, _ := r.ReadString('\n')
|
||||||
|
tag, _, _ := strings.Cut(line, " ")
|
||||||
|
server.Write([]byte(tag + " NO Basic authentication is disabled.\r\n"))
|
||||||
|
}()
|
||||||
|
|
||||||
|
c := &imapClient{conn: client, reader: bufio.NewReader(client)}
|
||||||
|
_, err := c.sendCommandOK(`LOGIN [email protected] "hunter2-secret"`)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("expected the refused LOGIN to be an error")
|
||||||
|
}
|
||||||
|
if strings.Contains(err.Error(), "hunter2-secret") || strings.Contains(err.Error(), "[email protected]") {
|
||||||
|
t.Fatalf("error quotes the command's arguments: %v", err)
|
||||||
|
}
|
||||||
|
if want := "IMAP LOGIN failed: A0001 NO Basic authentication is disabled."; err.Error() != want {
|
||||||
|
t.Fatalf("error = %q, want %q", err, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user