fix(gmail): skip charset re-decoding when body is already valid UTF-8#511
Closed
dinakars777 wants to merge 1 commit intoopenclaw:mainfrom
Closed
fix(gmail): skip charset re-decoding when body is already valid UTF-8#511dinakars777 wants to merge 1 commit intoopenclaw:mainfrom
dinakars777 wants to merge 1 commit intoopenclaw:mainfrom
Conversation
The Gmail API normalizes body.data to UTF-8 before base64url-encoding but preserves the original MIME Content-Type headers verbatim. This caused decodeBodyCharset to re-decode already-UTF-8 bytes as the original charset (e.g., ISO-2022-JP, Shift-JIS, GBK), producing garbled output for emails in those encodings. Add a utf8.Valid() guard before charset conversion: if the data is already valid UTF-8, skip re-decoding. Genuine non-UTF-8 raw bytes are almost never valid UTF-8, so this guard is safe. Closes steipete#446
Collaborator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
decodeBodyCharsetre-decodes already-UTF-8 data as the original charset (e.g., ISO-2022-JP, Shift-JIS, GBK) when the MIME header declares a non-UTF-8 charset, producing garbled output. The Gmail API normalizesbody.datato UTF-8 before base64url-encoding but preserves the original MIME headers verbatim.Steps to Reproduce (before fix)
Content-Type: text/plain; charset="iso-2022-jp"gog gmail read <threadId>\ufffd)Changes
Add a
utf8.Valid(data)guard before charset conversion. If the data is already valid UTF-8, skip re-decoding. Genuine non-UTF-8 raw bytes (like ISO-2022-JP or Shift-JIS) are almost never valid UTF-8, so this guard is safe.Affected Encodings
Confirmed:
iso-2022-jp. Likely also:shift_jis,euc-jp,gb2312,gbk,euc-kr,windows-1252,iso-8859-1.Test plan
go build ./...compiles cleanlygo test ./internal/cmd/... -run GmailGet -vpassesCloses #446