diff --git a/CHANGELOG.md b/CHANGELOG.md index 861521fad..318f7c512 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/internal/cmd/webid.go b/internal/cmd/webid.go index 88263a178..2d004da4e 100644 --- a/internal/cmd/webid.go +++ b/internal/cmd/webid.go @@ -2,6 +2,9 @@ package cmd import ( "encoding/base64" + "math/big" + "slices" + "strconv" "strings" "unicode" ) @@ -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 } @@ -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 } @@ -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 } @@ -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 +// "". +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 == "" { diff --git a/internal/cmd/webid_test.go b/internal/cmd/webid_test.go index 7b5dc79cc..374ec3ce9 100644 --- a/internal/cmd/webid_test.go +++ b/internal/cmd/webid_test.go @@ -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}, } @@ -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 {