all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Lukas Wagner <l.wagner@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH proxmox stable-bookworm] fix #6188: notify: smtp: quote sender's display name if needed
Date: Wed, 16 Jul 2025 11:13:26 +0200	[thread overview]
Message-ID: <20250716091326.144240-1-l.wagner@proxmox.com> (raw)

RFC5322 requires to quote the display name if certain special characters
are used (see diff).

(backported from commit 7262fae25c7cd25df9443158e3542ad58e7c0ebe on
master)

Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
---
 proxmox-notify/src/endpoints/smtp.rs | 55 +++++++++++++++++++++++++++-
 1 file changed, 54 insertions(+), 1 deletion(-)

diff --git a/proxmox-notify/src/endpoints/smtp.rs b/proxmox-notify/src/endpoints/smtp.rs
index b88e6c95..ada57693 100644
--- a/proxmox-notify/src/endpoints/smtp.rs
+++ b/proxmox-notify/src/endpoints/smtp.rs
@@ -1,3 +1,4 @@
+use std::borrow::Cow;
 use std::time::Duration;
 
 use lettre::message::header::{HeaderName, HeaderValue};
@@ -213,8 +214,10 @@ impl Endpoint for SmtpEndpoint {
             .clone()
             .unwrap_or_else(|| context().default_sendmail_author());
 
+        let quoted_author = quote_name_if_needed(&author);
+
         let mut email_builder =
-            Message::builder().from(parse_address(&format!("{author} <{mail_from}>"))?);
+            Message::builder().from(parse_address(&format!("{quoted_author} <{mail_from}>"))?);
 
         for recipient in recipients {
             email_builder = email_builder.to(parse_address(&recipient)?);
@@ -371,3 +374,53 @@ impl Endpoint for SmtpEndpoint {
         self.config.disable.unwrap_or_default()
     }
 }
+
+/// Quote mail name if required by RFC5322.
+fn quote_name_if_needed(name: &str) -> Cow<str> {
+    // See https://datatracker.ietf.org/doc/html/rfc5322#section-3.2.3
+    let needs_quotes = name.chars().any(|c| {
+        matches!(
+            c,
+            '(' | ')' | '<' | '>' | '@' | ',' | ';' | ':' | '\\' | '"' | '[' | ']' | '='
+        )
+    });
+
+    if needs_quotes {
+        // Quoted strings may contain all printable chars except \ and ", they need to be
+        // escaped. Non-ASCII UTF-8 characters are later encoded by lettre, we do not
+        // need to care about them here.
+        let mut result = name.replace(r#"\"#, r#"\\"#);
+        result = result.replace(r#"""#, r#"\""#);
+        Cow::Owned(format!("\"{result}\""))
+    } else {
+        Cow::Borrowed(name)
+    }
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_quote_mail_name() {
+        assert_eq!(
+            &quote_name_if_needed("Firstname Lastname"),
+            "Firstname Lastname"
+        );
+
+        assert_eq!(
+            &quote_name_if_needed("Firstname \"Nickname\" Lastname"),
+            "\"Firstname \\\"Nickname\\\" Lastname\""
+        );
+
+        assert_eq!(
+            &quote_name_if_needed("Proxmox VE (somehost)"),
+            "\"Proxmox VE (somehost)\""
+        );
+
+        assert_eq!(
+            &quote_name_if_needed("Firstname \\ Lastname"),
+            "\"Firstname \\\\ Lastname\""
+        );
+    }
+}
-- 
2.39.5



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


             reply	other threads:[~2025-07-16  9:13 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-16  9:13 Lukas Wagner [this message]
2025-07-16 10:30 ` [pve-devel] applied: " Thomas Lamprecht

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250716091326.144240-1-l.wagner@proxmox.com \
    --to=l.wagner@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal