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
5 changes: 5 additions & 0 deletions .changeset/fix-gmail-rfc2047-draft-address-headers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@googleworkspace/cli": patch
---

fix(gmail): RFC 2047 encode non-ASCII draft From/Cc/Bcc headers
36 changes: 33 additions & 3 deletions src/helpers/gmail/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,17 +506,26 @@ impl MessageBuilder<'_> {
));

if let Some(from) = self.from {
headers.push_str(&format!("\r\nFrom: {}", sanitize_header_value(from)));
headers.push_str(&format!(
"\r\nFrom: {}",
encode_header_value(&sanitize_header_value(from))
));
}
Comment on lines 508 to 513
Copy link
Contributor

Choose a reason for hiding this comment

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

high

While this change correctly applies RFC 2047 encoding to the From, Cc, and Bcc headers, the To header is not being encoded. This is inconsistent and could lead to malformed emails if the To field contains non-ASCII characters in display names (e.g., "日本語名前" <user@example.com>).

For correctness and consistency with other address headers, the To header value should also be passed through encode_header_value. The To header is currently only sanitized on line 485.


if let Some(cc) = self.cc {
headers.push_str(&format!("\r\nCc: {}", sanitize_header_value(cc)));
headers.push_str(&format!(
"\r\nCc: {}",
encode_header_value(&sanitize_header_value(cc))
));
}

// The Gmail API reads the Bcc header to route to those recipients,
// then strips it before delivery.
if let Some(bcc) = self.bcc {
headers.push_str(&format!("\r\nBcc: {}", sanitize_header_value(bcc)));
headers.push_str(&format!(
"\r\nBcc: {}",
encode_header_value(&sanitize_header_value(bcc))
));
}

format!("{}\r\n\r\n{}", headers, body)
Expand Down Expand Up @@ -1316,6 +1325,27 @@ mod tests {
assert!(!raw.contains("Solar — Quote Request"));
}

#[test]
fn test_message_builder_encodes_non_ascii_optional_headers() {
let raw = MessageBuilder {
to: "alice@example.com",
subject: "Hello",
from: Some("\"日本語名前\" <from@example.com>"),
cc: Some("\"日本語名前\" <cc@example.com>"),
bcc: Some("\"日本語名前\" <bcc@example.com>"),
threading: None,
html: false,
}
.build("Body");

assert!(raw.contains("From: =?UTF-8?B?"));
assert!(raw.contains("Cc: =?UTF-8?B?"));
assert!(raw.contains("Bcc: =?UTF-8?B?"));
assert!(!raw.contains("From: \"日本語名前\" <from@example.com>"));
assert!(!raw.contains("Cc: \"日本語名前\" <cc@example.com>"));
assert!(!raw.contains("Bcc: \"日本語名前\" <bcc@example.com>"));
}

#[test]
fn test_message_builder_sanitizes_crlf_injection() {
let raw = MessageBuilder {
Expand Down
Loading