Skip to content

fix(gmail): decode message bodies by declared charset instead of dropping non-UTF-8 (P1 data loss)#114

Merged
bhekanik merged 2 commits into
mainfrom
fix/gmail-charset-body-loss
Jul 11, 2026
Merged

fix(gmail): decode message bodies by declared charset instead of dropping non-UTF-8 (P1 data loss)#114
bhekanik merged 2 commits into
mainfrom
fix/gmail-charset-body-loss

Conversation

@bhekanik

@bhekanik bhekanik commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

Problem (P1 data loss)

base64_decode_url ended in String::from_utf8(bytes)? and walk_parts called it via .ok(). Gmail returns each part's ORIGINAL bytes untranscoded, so a charset=windows-1252/iso-8859-1/shift_jis body (e.g. 0xE9=é, 0x92=') failed strict UTF-8 → Nonetext_plain/text_html never set → the entire body silently vanished (envelope/subject/snippet remained). IMAP path was unaffected.

Fix

Split base64→bytes from bytes→string; decode each text leaf with its declared Content-Type charset via encoding_rs (default UTF-8, lossy fallback — content is never dropped). charset_from_content_type parses the charset= param (case-insensitive, quote-stripped). extract_body signature preserved; provider logic stays in the provider crate. Added encoding_rs (already locked transitively via mail-parser). 3 regression tests; 70 provider-gmail tests pass.

CI note: workspace clippy red on pre-existing 1.97 lints until #111 merges; will rebase (Cargo.lock).


Summary by cubic

Decode Gmail message bodies using the charset declared in Content-Type instead of assuming UTF‑8, preventing non‑UTF‑8 text from being dropped. Defaults to UTF‑8; unknown labels use a lossy fallback so content isn’t lost.

  • Bug Fixes

    • Split base64→bytes from bytes→string; decode by declared charset via encoding_rs.
    • Parse charset= from Content-Type (case-insensitive, strips quotes).
    • Apply to text/plain, text/html, and text/calendar; extract_body unchanged; IMAP path unaffected.
    • Added regression tests for windows-1252, quoted ISO-8859-1, and UTF‑8 multibyte.
  • Dependencies

    • Added encoding_rs to the workspace and provider-gmail.

Written for commit e5ec17f. Summary will update on new commits.

Review in cubic

@vercel

vercel Bot commented Jul 11, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
mxr-mail Ready Ready Preview, Comment Jul 11, 2026 2:27am

@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@bhekanik, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 5 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 628c4348-ba43-4567-be49-7b81ef4aa650

📥 Commits

Reviewing files that changed from the base of the PR and between ca2b24b and e5ec17f.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (3)
  • Cargo.toml
  • crates/provider-gmail/Cargo.toml
  • crates/provider-gmail/src/parse.rs
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/gmail-charset-body-loss

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 4 files

Re-trigger cubic

bhekanik added 2 commits July 11, 2026 03:24
…ping non-UTF-8

Gmail's API returns each part's original bytes untranscoded, so a
text/plain; charset=windows-1252 (or iso-8859-1, shift_jis) body failed
the strict from_utf8 in base64_decode_url; .ok() then turned it into None
and the entire body was silently lost while envelope/subject/snippet
remained.

Split base64 decoding from string decoding: decode_body_part now decodes
the part's bytes using the charset declared in its Content-Type header
(via encoding_rs), defaulting to UTF-8 and falling back to lossy decode
for unknown labels so content is never dropped. The three text leaves in
walk_parts (text/plain, text/html, text/calendar) are now charset-aware.
The IMAP path already charset-decoded via mail_parser and is unaffected.
@bhekanik bhekanik force-pushed the fix/gmail-charset-body-loss branch from fa6dc7c to e5ec17f Compare July 11, 2026 02:24
@bhekanik bhekanik merged commit b60033c into main Jul 11, 2026
26 checks passed
@bhekanik bhekanik deleted the fix/gmail-charset-body-loss branch July 11, 2026 02:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant