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
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: accept the opaque view tokens of current Gmail web URLs (`#inbox/FMfcgz…`, `msg-f` tokens) wherever thread or message IDs are taken, decoding them offline to the hex API ID. (#1327)

## 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
76 changes: 76 additions & 0 deletions internal/cmd/webid.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package cmd

import (
"encoding/base64"
"math/big"
"slices"
"strconv"
"strings"
"unicode"
)
Expand All @@ -14,6 +17,9 @@ func normalizeGmailThreadID(input string) string {

u := parseMaybeURL(s)
if u == nil {
if id, ok := decodeGmailViewToken(s, gmailThreadTokenPrefixes); ok {
return id
}
return s
}

Expand Down Expand Up @@ -45,6 +51,9 @@ func normalizeGmailThreadID(input string) string {
if looksLikeHexID(last) {
return last
}
if id, ok := decodeGmailViewToken(last, gmailThreadTokenPrefixes); ok {
return id
}
return s
}

Expand All @@ -56,6 +65,9 @@ func normalizeGmailMessageID(input string) string {

u := parseMaybeURL(s)
if u == nil {
if id, ok := decodeGmailViewToken(s, gmailMessageTokenPrefixes); ok {
return id
}
return s
}

Expand Down Expand Up @@ -83,9 +95,73 @@ func normalizeGmailMessageID(input string) string {
}
}

frag := strings.SplitN(strings.TrimSpace(u.Fragment), "?", 2)[0]
if parts := strings.Split(strings.Trim(frag, "/"), "/"); len(parts) > 0 {
if id, ok := decodeGmailViewToken(parts[len(parts)-1], gmailMessageTokenPrefixes); ok {
return id
}
}

return s
}

const (
gmailViewTokenAlphabet = "BCDFGHJKLMNPQRSTVWXZbcdfghjklmnpqrstvwxz"
base64StdAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
)

var (
gmailThreadTokenPrefixes = []string{"f:", "thread-f:"}
gmailMessageTokenPrefixes = []string{"msg-f:"}
)

// decodeGmailViewToken resolves the opaque token of current Gmail web URLs
// (e.g. #inbox/FMfcgz...) to a hex API ID. The token is a base64 string
// re-encoded in a consonant-only base-40 alphabet; its payload is
// "<prefix><decimal id>".
func decodeGmailViewToken(token string, prefixes []string) (string, bool) {
token = strings.TrimSpace(token)
if len(token) < 16 || looksLikeHexID(token) {
return "", false
}

n := new(big.Int)
base := big.NewInt(int64(len(gmailViewTokenAlphabet)))
for _, r := range token {
i := strings.IndexRune(gmailViewTokenAlphabet, r)
if i < 0 {
return "", false
}
n.Mul(n, base).Add(n, big.NewInt(int64(i)))
}

var b64 []byte
sixtyFour := big.NewInt(64)
rem := new(big.Int)
for n.Sign() > 0 {
n.DivMod(n, sixtyFour, rem)
b64 = append(b64, base64StdAlphabet[rem.Int64()])
}
slices.Reverse(b64)
payload, err := base64.RawStdEncoding.DecodeString(string(b64))
if err != nil {
return "", false
}

for _, prefix := range prefixes {
dec, ok := strings.CutPrefix(string(payload), prefix)
if !ok {
continue
}
id, err := strconv.ParseUint(dec, 10, 64)
if err != nil {
return "", false
}
return strconv.FormatUint(id, 16), true
}
return "", false
}

func normalizeCalendarEventID(input string) string {
s := strings.TrimSpace(input)
if s == "" {
Expand Down
9 changes: 9 additions & 0 deletions internal/cmd/webid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ func TestNormalizeGmailThreadID(t *testing.T) {
{in: "https://mail.google.com/mail/u/0/#inbox/" + threadID, want: threadID},
{in: "mail.google.com/mail/u/0/#all/" + threadID, want: threadID},
{in: "https://mail.google.com/mail/u/0/?ui=2&th=" + threadID + "&view=pt", want: threadID},
{in: "FMfcgzGwHpVzSvQjBWlWNGBCZqCPPPMN", want: threadID},
{in: "https://mail.google.com/mail/u/0/#inbox/FMfcgzGwHpVzSvQjBWlWNGBCZqCPPPMN", want: threadID},
{in: "https://mail.google.com/mail/u/0/#label/Work/FMfcgzGwHpVzSvQjBWlWNGBCZqCPPPMN?compose=new", want: threadID},
{in: "https://mail.google.com/mail/u/0/#inbox/NHgvNZFVxQXXXjLLhpJGtLNblRdNVlnCmwXMpqZCGBq", want: threadID},
{in: "https://mail.google.com/mail/u/0/#inbox/CXKnWZngLGgsFlmgWhRHbMZsQZlckVHtjxwfbCL", want: "https://mail.google.com/mail/u/0/#inbox/CXKnWZngLGgsFlmgWhRHbMZsQZlckVHtjxwfbCL"},
{in: "BCDFGHJKLMNPQRSTVWXZbcdf", want: "BCDFGHJKLMNPQRSTVWXZbcdf"},
{in: "https://example.com/not-gmail/" + threadID, want: "https://example.com/not-gmail/" + threadID},
}

Expand All @@ -38,6 +44,9 @@ func TestNormalizeGmailMessageID(t *testing.T) {
{in: msgID, want: msgID},
{in: "https://mail.google.com/mail/u/0/?ui=2&message_id=" + msgID + "&view=pt", want: msgID},
{in: "https://mail.google.com/mail/u/0/?ui=2&permmsgid=msg-f:" + msgID + "&view=pt", want: msgID},
{in: "CXKnWZngLGgsFlmgWhRHbMZsQZlckVHtjxwfbCL", want: msgID},
{in: "https://mail.google.com/mail/u/0/#inbox/CXKnWZngLGgsFlmgWhRHbMZsQZlckVHtjxwfbCL", want: msgID},
{in: "FMfcgzGwHpVzSvQjBWlWNGBCZqCPPPMP", want: "FMfcgzGwHpVzSvQjBWlWNGBCZqCPPPMP"},
}

for _, tt := range tests {
Expand Down
Loading