Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metadata: escape display name in email addresses #1832

Merged
merged 3 commits into from
Nov 2, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ impl Metadata21 {
for author in authors {
match (&author.name, &author.email) {
(Some(name), Some(email)) => {
emails.push(format!("{name} <{email}>"));
emails.push(format_email_with_display_name(name, email));
robsdedude marked this conversation as resolved.
Show resolved Hide resolved
}
(Some(name), None) => {
names.push(name.as_str());
Expand All @@ -251,7 +251,7 @@ impl Metadata21 {
for maintainer in maintainers {
match (&maintainer.name, &maintainer.email) {
(Some(name), Some(email)) => {
emails.push(format!("{name} <{email}>"));
emails.push(format_email_with_display_name(name, email));
robsdedude marked this conversation as resolved.
Show resolved Hide resolved
}
(Some(name), None) => {
names.push(name.as_str());
Expand Down Expand Up @@ -555,6 +555,23 @@ impl Metadata21 {
}
}

/// Escape email addresses with display name if necessary
/// according to RFC 822 Section 3.3. "specials".
fn format_email_with_display_name(display_name: &str, email: &str) -> String {
robsdedude marked this conversation as resolved.
Show resolved Hide resolved
if display_name.chars().any(|c| {
matches!(
c,
'(' | ')' | '<' | '>' | '@' | ',' | ';' | ':' | '\\' | '"' | '.' | '[' | ']'
)
}) {
return format!(
"\"{}\" <{email}>",
display_name.replace('\\', "\\\\").replace('\"', "\\\"")
);
}
format!("{display_name} <{email}>")
}

/// Fold long header field according to RFC 5322 section 2.2.3
/// https://datatracker.ietf.org/doc/html/rfc5322#section-2.2.3
fn fold_header(text: &str) -> String {
Expand Down