Skip to content
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 0.48.2 - Unreleased

- Gmail: read a message body in its declared charset, without a crash, when its `Content-Type` header has unusual bytes (an invalid byte, `İ`, `K`) before `charset=`. (#1356)
- Reliability: concurrent Google API requests share one rate-limit pause (at most 8s without `Retry-After`) and give up after 12 rate-limit responses or a 2-minute budget instead of six retries, so large Gmail searches no longer fail when one request runs out of retries. (#1357)
- Auth: automatic reauthorization no longer replays a stored Google Ads (`adwords`) grant unless the failing command needs Ads, so an old Ads grant cannot trigger Google's passkey gate; restore it with `gog auth add <email> --services user,ads`. (#1353)
- Dependencies: refresh the MCP and Google API clients, networking libraries, and tracking-worker tooling while preserving the Go 1.26 minimum, 24-hour worker cooldown, and security overrides.
Expand Down
37 changes: 37 additions & 0 deletions internal/gmailcontent/charset_header_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package gmailcontent

import "testing"

// The Content-Type header is written by whoever sent the message. Bytes before
// the charset parameter whose lower-case form has a different length (an
// invalid UTF-8 byte, U+0130, U+212A) must not change where the label is read
// from, and must not panic.
func TestDecodeBodyCharset_HeaderBytesBeforeLabel(t *testing.T) {
for _, tc := range []struct {
name string
contentType string
in string
want string
}{
{"invalid byte before the label", "text/plain; name=\xb6; charset=windows-1252;;", "\x93", "“"},
{"dotted capital I before the label", "text/plain; name=İ; charset=windows-1252;;", "\x93", "“"},
{"kelvin sign before the label", "text/plain; name=K; charset=windows-1252;;", "\x93", "“"},
{"label cut short by the shift", "\xb6ChArset=", "0", "0"},
} {
t.Run(tc.name, func(t *testing.T) {
if got := string(DecodeBodyCharset([]byte(tc.in), tc.contentType)); got != tc.want {
t.Fatalf("got %q, want %q", got, tc.want)
}
})
}
}

func FuzzDecodeBodyCharset(f *testing.F) {
f.Add([]byte("h\xe9llo"), "text/plain; charset=iso-8859-1")
f.Add([]byte("\x1b$B$3$s\x1b(B"), "text/plain; charset=iso-2022-jp")
f.Add([]byte("abc"), `text/html; charset="utf-16"`)
f.Add([]byte("0"), "\xb6ChArset=")
f.Fuzz(func(_ *testing.T, data []byte, contentType string) {
_ = DecodeBodyCharset(data, contentType)
})
}
20 changes: 18 additions & 2 deletions internal/gmailcontent/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,11 @@ func charsetLabelFromContentType(contentType string) string {
return label
}
}
lower := strings.ToLower(contentType)
idx := strings.Index(lower, "charset=")
// Search an ASCII-only lower-case copy: it has the same length as
// contentType, so idx is valid in both. strings.ToLower can change the
// length (invalid UTF-8 bytes, U+0130, U+212A), which moved the slice
// below off the label or out of range.
idx := strings.Index(asciiLower(contentType), "charset=")

if idx == -1 {
return ""
Expand All @@ -274,6 +277,19 @@ func charsetLabelFromContentType(contentType string) string {
return strings.Trim(label, "\"'")
}

// asciiLower lower-cases A-Z and keeps every other byte, so each index into
// the result is the same index into s.
func asciiLower(s string) string {
b := []byte(s)
for i, c := range b {
if 'A' <= c && c <= 'Z' {
b[i] = c + ('a' - 'A')
}
}

return string(b)
}

func decodeWithCharsetLabel(data []byte, charsetLabel string) ([]byte, bool) {
label := strings.TrimSpace(charsetLabel)
if label == "" {
Expand Down