diff --git a/internal/services/email_imap.go b/internal/services/email_imap.go index 88dd61f..3c7f3e7 100644 --- a/internal/services/email_imap.go +++ b/internal/services/email_imap.go @@ -120,7 +120,10 @@ func (c *imapClient) sendCommandOK(cmd string) ([]string, error) { // " BAD ..." otherwise; only the second field matters. parts := strings.SplitN(status, " ", 3) 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 } diff --git a/internal/services/email_imap_test.go b/internal/services/email_imap_test.go new file mode 100644 index 0000000..0a2da1a --- /dev/null +++ b/internal/services/email_imap_test.go @@ -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 someone@example.com "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(), "someone@example.com") { + 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) + } +}