Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## 0.48.1 - Unreleased

- Gmail: leave every send-as alias out of `gmail autoreply` recipients, so messages sent from your own addresses are skipped even with an external Reply-To unless `--allow-self` is set, and the command now stops if it can't read your send-as list (unless `--allow-self` is set). (#1325) — thanks @saariuslystoned.

## 0.48.0 - 2026-11-05

**Highlights:** New backups stay local until you configure a remote; existing configurations keep working. Store service-account keys in the keyring, search Gmail IDs efficiently, and use environment-backed watch tokens, with Calendar fixes and security dependency updates.
Expand Down
42 changes: 37 additions & 5 deletions internal/cmd/gmail_autoreply.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,14 @@ func runGmailAutoReply(ctx context.Context, svc *gmail.Service, account string,
Label: input.Label,
}

from, err := resolveComposeSender(ctx, svc, account, input.From)
sendAs, sendAsErr := listSendAs(ctx, svc)
// Without --allow-self the alias list is what keeps replies away from the
// user's own addresses, so a failed lookup must stop the run. With
// --allow-self those replies are allowed, so keep the previous fallback.
if sendAsErr != nil && !input.AllowSelf {
return summary, fmt.Errorf("list send-as aliases to exclude your own addresses: %w", sendAsErr)
}
from, err := resolveComposeFrom(ctx, svc, account, input.From, sendAs, sendAsErr)
if err != nil {
return summary, err
}
Expand All @@ -183,10 +190,7 @@ func runGmailAutoReply(ctx context.Context, svc *gmail.Service, account string,
return summary, nil
}

selfAddrs := []string{account}
if from.sendingEmail != "" && !strings.EqualFold(from.sendingEmail, account) {
selfAddrs = append(selfAddrs, from.sendingEmail)
}
selfAddrs := selfEmailsForReply(account, from.sendingEmail, sendAs)

for _, messageID := range messageIDs {
msg, err := fetchMessageForAutoReply(ctx, svc, messageID)
Expand Down Expand Up @@ -225,6 +229,13 @@ func runGmailAutoReply(ctx context.Context, svc *gmail.Service, account string,
}

replyMeta := replyInfoFromMessage(msg, false)
if !input.AllowSelf && autoReplyFromIsSelf(replyMeta, selfAddrs) {
result.Action = autoReplyActionSkipped
result.Reason = "no_reply_recipient"
summary.Results = append(summary.Results, result)
summary.Skipped++
continue
}
recipients := autoReplyRecipients(replyMeta, selfAddrs)
if len(recipients) == 0 && input.AllowSelf {
recipients = autoReplyRecipients(replyMeta, nil)
Expand Down Expand Up @@ -326,6 +337,27 @@ func shouldSkipAutoReplyMessage(msg *gmail.Message) (bool, string) {
return false, ""
}

// autoReplyFromIsSelf reports whether From is one of the caller's own
// addresses. The comparison matches autoReplyRecipients: case-insensitive on
// the bare address.
func autoReplyFromIsSelf(info *replyInfo, self []string) bool {
if info == nil || len(self) == 0 {
return false
}
selfSet := make(map[string]struct{}, len(self))
for _, addr := range self {
if trimmed := strings.ToLower(strings.TrimSpace(addr)); trimmed != "" {
selfSet[trimmed] = struct{}{}
}
}
for _, addr := range parseEmailAddresses(info.FromAddr) {
if _, ok := selfSet[strings.ToLower(strings.TrimSpace(addr))]; ok {
return true
}
}
return false
}

func autoReplyRecipients(info *replyInfo, self []string) []string {
replyAddress := strings.TrimSpace(info.ReplyToAddr)
if replyAddress == "" {
Expand Down
254 changes: 254 additions & 0 deletions internal/cmd/gmail_autoreply_sendas_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
package cmd

import (
"encoding/json"
"errors"
"net/http"
"strings"
"testing"

googleapi "google.golang.org/api/googleapi"
)

func TestExecuteGmailAutoReplySendAsAlias(t *testing.T) {
t.Run("alias", func(t *testing.T) {
sendCalls, summary, err := executeGmailAutoReplyFrom(t, "alias@example.com", "", false, 0)
if err != nil {
t.Fatalf("Execute: %v", err)
}
if sendCalls != 0 {
t.Fatalf("expected no messages.send calls, got %d", sendCalls)
}
if summary.Replied != 0 || summary.Skipped != 1 || len(summary.Results) != 1 {
t.Fatalf("unexpected summary: %#v", summary)
}
got := summary.Results[0]
if got.Action != autoReplyActionSkipped || got.Reason != "no_reply_recipient" {
t.Fatalf("unexpected result: %#v", got)
}
})

t.Run("external", func(t *testing.T) {
sendCalls, summary, err := executeGmailAutoReplyFrom(t, "alice@example.com", "", false, 0)
if err != nil {
t.Fatalf("Execute: %v", err)
}
if sendCalls != 1 {
t.Fatalf("expected one messages.send call, got %d", sendCalls)
}
if summary.Replied != 1 || summary.Skipped != 0 || len(summary.Results) != 1 {
t.Fatalf("unexpected summary: %#v", summary)
}
got := summary.Results[0]
if got.Action != "replied" || got.ReplyTo != "alice@example.com" {
t.Fatalf("unexpected result: %#v", got)
}
})

t.Run("display name", func(t *testing.T) {
sendCalls, summary, err := executeGmailAutoReplyFrom(t, `"Alias" <ALIAS@EXAMPLE.COM>`, "", false, 0)
if err != nil {
t.Fatalf("Execute: %v", err)
}
if sendCalls != 0 {
t.Fatalf("expected no messages.send calls, got %d", sendCalls)
}
if summary.Replied != 0 || summary.Skipped != 1 || len(summary.Results) != 1 {
t.Fatalf("unexpected summary: %#v", summary)
}
got := summary.Results[0]
if got.Action != autoReplyActionSkipped || got.Reason != "no_reply_recipient" {
t.Fatalf("unexpected result: %#v", got)
}
})

t.Run("reply-to", func(t *testing.T) {
sendCalls, summary, err := executeGmailAutoReplyFrom(t, "alias@example.com", "victim@external.com", false, 0)
if err != nil {
t.Fatalf("Execute: %v", err)
}
if sendCalls != 0 {
t.Fatalf("expected no messages.send calls, got %d", sendCalls)
}
if summary.Replied != 0 || summary.Skipped != 1 || len(summary.Results) != 1 {
t.Fatalf("unexpected summary: %#v", summary)
}
got := summary.Results[0]
if got.Action != autoReplyActionSkipped || got.Reason != "no_reply_recipient" {
t.Fatalf("unexpected result: %#v", got)
}
})

t.Run("reply-to allow-self", func(t *testing.T) {
sendCalls, summary, err := executeGmailAutoReplyFrom(t, "alias@example.com", "victim@external.com", true, 0)
if err != nil {
t.Fatalf("Execute: %v", err)
}
if sendCalls != 1 {
t.Fatalf("expected one messages.send call, got %d", sendCalls)
}
if summary.Replied != 1 || summary.Skipped != 0 || len(summary.Results) != 1 {
t.Fatalf("unexpected summary: %#v", summary)
}
got := summary.Results[0]
if got.Action != "replied" || got.ReplyTo != "victim@external.com" {
t.Fatalf("unexpected result: %#v", got)
}
})

t.Run("allow-self", func(t *testing.T) {
sendCalls, summary, err := executeGmailAutoReplyFrom(t, "alias@example.com", "", true, 0)
if err != nil {
t.Fatalf("Execute: %v", err)
}
if sendCalls != 1 {
t.Fatalf("expected one messages.send call, got %d", sendCalls)
}
if summary.Replied != 1 || summary.Skipped != 0 || len(summary.Results) != 1 {
t.Fatalf("unexpected summary: %#v", summary)
}
got := summary.Results[0]
if got.Action != "replied" || got.ReplyTo != "alias@example.com" {
t.Fatalf("unexpected result: %#v", got)
}
})

t.Run("send-as forbidden", func(t *testing.T) {
sendCalls, _, err := executeGmailAutoReplyFrom(t, "alias@example.com", "", false, http.StatusForbidden)
if err == nil {
t.Fatal("expected error")
}
if sendCalls != 0 {
t.Fatalf("expected no messages.send calls, got %d", sendCalls)
}
if !strings.Contains(err.Error(), "list send-as aliases to exclude your own addresses") {
t.Fatalf("unexpected error: %v", err)
}
var apiErr *googleapi.Error
if !errors.As(err, &apiErr) || apiErr.Code != http.StatusForbidden {
t.Fatalf("expected wrapped 403, got %v", err)
}
if ExitCode(err) != exitCodePermissionDenied {
t.Fatalf("exit code = %d, want %d", ExitCode(err), exitCodePermissionDenied)
}
})

// --allow-self already permits replies to the user's own addresses, so a
// failed send-as lookup keeps the previous sender fallback instead of
// stopping the run.
t.Run("send-as forbidden with allow-self", func(t *testing.T) {
sendCalls, summary, err := executeGmailAutoReplyFrom(t, "alice@example.com", "", true, http.StatusForbidden)
if err != nil {
t.Fatalf("Execute: %v", err)
}
if sendCalls != 1 || summary.Replied != 1 {
t.Fatalf("sendCalls = %d, summary = %#v, want one reply", sendCalls, summary)
}
})
}

// executeGmailAutoReplyFrom runs `gmail autoreply` through Execute against an
// httptest Gmail server whose send-as list is the primary account plus
// alias@example.com. from and replyTo are the matching message's From and
// Reply-To headers. allowSelf sets --allow-self. sendAsStatus 0 serves the
// send-as list; any other status is returned by GET settings/sendAs.
// The helper fails the test unless that GET happens exactly once.
func executeGmailAutoReplyFrom(t *testing.T, from, replyTo string, allowSelf bool, sendAsStatus int) (int, gmailAutoReplySummary, error) {
t.Helper()
setTestConfigHome(t)

var sendCalls, sendAsGets, followupCalls int
svc, cleanup := newGmailServiceForTest(t, func(w http.ResponseWriter, r *http.Request) {
path := strings.TrimPrefix(r.URL.Path, "/gmail/v1")
w.Header().Set("Content-Type", "application/json")
sendAsList := r.Method == http.MethodGet && path == "/users/me/settings/sendAs"
if !sendAsList {
followupCalls++
}
switch {
case sendAsList:
sendAsGets++
if sendAsStatus != 0 {
w.WriteHeader(sendAsStatus)
_ = json.NewEncoder(w).Encode(map[string]any{
"error": map[string]any{
"code": sendAsStatus,
"message": http.StatusText(sendAsStatus),
"errors": []map[string]any{{
"message": http.StatusText(sendAsStatus),
"domain": "global",
"reason": "forbidden",
}},
},
})
return
}
sendAsListHandler(w)
case r.Method == http.MethodGet && strings.HasPrefix(path, "/users/me/labels"):
_ = json.NewEncoder(w).Encode(map[string]any{
"labels": []map[string]any{
{"id": "Label_1", "name": "AutoReplied", "type": "user"},
},
})
case r.Method == http.MethodGet && strings.HasPrefix(path, "/users/me/messages") && r.URL.Query().Get("q") != "":
_ = json.NewEncoder(w).Encode(map[string]any{
"messages": []map[string]any{{"id": "m1", "threadId": "t1"}},
})
case r.Method == http.MethodGet && path == "/users/me/messages/m1":
headers := []map[string]any{
{"name": "From", "value": from},
{"name": "To", "value": "me@example.com"},
{"name": "Subject", "value": "hello"},
{"name": "Message-ID", "value": "<orig@example.com>"},
}
if replyTo != "" {
headers = append(headers, map[string]any{"name": "Reply-To", "value": replyTo})
}
_ = json.NewEncoder(w).Encode(map[string]any{
"id": "m1",
"threadId": "t1",
"labelIds": []string{"INBOX"},
"payload": map[string]any{
"headers": headers,
},
})
case r.Method == http.MethodPost && path == "/users/me/messages/send":
sendCalls++
_ = json.NewEncoder(w).Encode(map[string]any{"id": "reply1", "threadId": "t1"})
case r.Method == http.MethodPost && path == "/users/me/threads/t1/modify":
_ = json.NewEncoder(w).Encode(map[string]any{"id": "t1"})
default:
http.Error(w, "unexpected "+r.Method+" "+r.URL.RequestURI(), http.StatusNotFound)
}
})
defer cleanup()

args := []string{
"--json", "--no-input", "--account", "me@example.com",
"gmail", "autoreply", "in:inbox", "--body", "thanks",
}
if allowSelf {
args = append(args, "--allow-self")
}
result := executeWithGmailTestService(t, args, svc)
if sendAsGets != 1 {
t.Fatalf("GET settings/sendAs calls = %d, want 1", sendAsGets)
}
if sendAsStatus != 0 && !allowSelf && (sendCalls != 0 || followupCalls != 0) {
t.Fatalf("send-as failure continued: send=%d followup=%d err=%v", sendCalls, followupCalls, result.err)
}
if result.err != nil {
if sendAsStatus == 0 {
t.Fatalf("Execute(%v): %v\nstderr:\n%s\nstdout:\n%s", args, result.err, result.stderr, result.stdout)
}
return sendCalls, gmailAutoReplySummary{}, result.err
}

var payload struct {
AutoReply gmailAutoReplySummary `json:"autoReply"`
}
if err := json.Unmarshal([]byte(result.stdout), &payload); err != nil {
t.Fatalf("decode stdout: %v\n%s", err, result.stdout)
}
return sendCalls, payload.AutoReply, nil
}