Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(email): preserve UTF-8 in misdeclared Gmail bodies (#6133)
gbirman587c7d57c02c
diff --git a/crates/email_api_client/src/outbound/gmail/convert/payload.rs b/crates/email_api_client/src/outbound/gmail/convert/payload.rsindex 26e5cb01b4..208fad3038 100644--- a/crates/email_api_client/src/outbound/gmail/convert/payload.rs+++ b/crates/email_api_client/src/outbound/gmail/convert/payload.rs@@ -144,16 +144,24 @@ fn parse_part(part: &MessagePart, message_id: &str, parsed: &mut ParsedGmailPayl}}-/// Decodes part bytes honoring the part's declared `charset`, so non-UTF-8-/// bodies (e.g. ISO-8859-1, Windows-1252, Shift_JIS) are not mangled by a-/// blind lossy UTF-8 conversion. Unknown or missing charsets fall back to-/// lossy UTF-8.+/// Decodes part bytes using UTF-8 when valid, otherwise honoring the part's+/// declared `charset`.+///+/// Some senders emit UTF-8 bytes while incorrectly declaring a legacy+/// single-byte charset. Trusting that declaration turns a bullet (`•`) into+/// mojibake (`•`). Valid UTF-8 is therefore preferred over a conflicting+/// single-byte declaration. Multi-byte encodings still take precedence, and+/// unknown or missing charsets fall back to lossy UTF-8.fn decode_part_text(bytes: &[u8], part: &MessagePart) -> String {let encoding = find_header(&part.headers, "Content-Type").and_then(content_type_charset).and_then(|label| encoding_rs::Encoding::for_label(label.as_bytes()));match encoding {+ Some(encoding) if encoding.is_single_byte() => match str::from_utf8(bytes) {+ Ok(text) => text.to_owned(),+ Err(_) => encoding.decode(bytes).0.into_owned(),+ },Some(encoding) => encoding.decode(bytes).0.into_owned(),None => String::from_utf8_lossy(bytes).into_owned(),}diff --git a/crates/email_api_client/src/outbound/gmail/convert/payload/test.rs b/crates/email_api_client/src/outbound/gmail/convert/payload/test.rsindex 72b5221559..33268bfcb5 100644--- a/crates/email_api_client/src/outbound/gmail/convert/payload/test.rs+++ b/crates/email_api_client/src/outbound/gmail/convert/payload/test.rs@@ -78,6 +78,23 @@ fn honors_declared_non_utf8_charsets() {assert_eq!(parsed.body_text.as_deref(), Some("café"));}+#[test]+fn prefers_valid_utf8_over_a_misdeclared_single_byte_charset() {+ let body = "<p>• A dedicated Macro product walkthrough</p>";+ let encoded = URL_SAFE.encode(body);+ let mut part = payload("text/html", &encoded);+ part.headers.push(Header {+ name: "Content-Type".into(),+ value: "text/html; charset=Windows-1252".into(),+ });++ let parsed = parse_gmail_payload(&part, "message").unwrap();+ let html = parsed.body_html_sanitized.unwrap();++ assert!(html.contains("• A dedicated Macro product walkthrough"));+ assert!(!html.contains("•"));+}+#[test]fn unknown_charsets_fall_back_to_lossy_utf8() {let encoded = URL_SAFE.encode("Hello");