mirror of
https://github.com/bitwarden/server.git
synced 2025-07-01 16:12:49 -05:00
Add support for international domain names (IDN) in email addresses (#1512)
* Adjust email address checking to handle unicode * ASCII only in local part * allow unicode in second-level and top-level domain * Add PunyEncoding/Decoding methods and tests * Use PunyEncoding for outbound email recipients * Use MailKit for punycode, handle edge cases * Punyencode all email addresses in mailServices * Remove punyencoding from HandlebarsMailService * Add to punyencoding tests * Use more inclusive e-mail error * Fix comment wording * Apply StrictEmail checking to emergency access invite * Remove punyDecode helper
This commit is contained in:
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Core.Models.Mail;
|
||||
using Bit.Core.Settings;
|
||||
using Bit.Core.Utilities;
|
||||
using System.Linq;
|
||||
using Amazon.SimpleEmail;
|
||||
using Amazon;
|
||||
@ -54,11 +55,13 @@ namespace Bit.Core.Services
|
||||
throw new ArgumentNullException(nameof(globalSettings.Amazon.Region));
|
||||
}
|
||||
|
||||
var replyToEmail = CoreHelpers.PunyEncode(globalSettings.Mail.ReplyToEmail);
|
||||
|
||||
_globalSettings = globalSettings;
|
||||
_hostingEnvironment = hostingEnvironment;
|
||||
_logger = logger;
|
||||
_client = amazonSimpleEmailService;
|
||||
_source = $"\"{globalSettings.SiteName}\" <{globalSettings.Mail.ReplyToEmail}>";
|
||||
_source = $"\"{globalSettings.SiteName}\" <{replyToEmail}>";
|
||||
_senderTag = $"Server_{globalSettings.ProjectName?.Replace(' ', '_')}";
|
||||
if (!string.IsNullOrWhiteSpace(_globalSettings.Mail.AmazonConfigSetName))
|
||||
{
|
||||
@ -79,7 +82,9 @@ namespace Bit.Core.Services
|
||||
Source = _source,
|
||||
Destination = new Destination
|
||||
{
|
||||
ToAddresses = message.ToEmails.ToList()
|
||||
ToAddresses = message.ToEmails
|
||||
.Select(email => CoreHelpers.PunyEncode(email))
|
||||
.ToList()
|
||||
},
|
||||
Message = new Message
|
||||
{
|
||||
@ -107,7 +112,9 @@ namespace Bit.Core.Services
|
||||
|
||||
if (message.BccEmails?.Any() ?? false)
|
||||
{
|
||||
request.Destination.BccAddresses = message.BccEmails.ToList();
|
||||
request.Destination.BccAddresses = message.BccEmails
|
||||
.Select(email => CoreHelpers.PunyEncode(email))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(message.Category))
|
||||
|
@ -13,6 +13,7 @@ namespace Bit.Core.Services
|
||||
private readonly GlobalSettings _globalSettings;
|
||||
private readonly ILogger<MailKitSmtpMailDeliveryService> _logger;
|
||||
private readonly string _replyDomain;
|
||||
private readonly string _replyEmail;
|
||||
|
||||
public MailKitSmtpMailDeliveryService(
|
||||
GlobalSettings globalSettings,
|
||||
@ -22,9 +23,12 @@ namespace Bit.Core.Services
|
||||
{
|
||||
throw new ArgumentNullException(nameof(globalSettings.Mail.Smtp.Host));
|
||||
}
|
||||
if (globalSettings.Mail?.ReplyToEmail?.Contains("@") ?? false)
|
||||
|
||||
_replyEmail = CoreHelpers.PunyEncode(globalSettings.Mail?.ReplyToEmail);
|
||||
|
||||
if (_replyEmail.Contains("@"))
|
||||
{
|
||||
_replyDomain = globalSettings.Mail.ReplyToEmail.Split('@')[1];
|
||||
_replyDomain = _replyEmail.Split('@')[1];
|
||||
}
|
||||
|
||||
_globalSettings = globalSettings;
|
||||
@ -34,7 +38,7 @@ namespace Bit.Core.Services
|
||||
public async Task SendEmailAsync(Models.Mail.MailMessage message)
|
||||
{
|
||||
var mimeMessage = new MimeMessage();
|
||||
mimeMessage.From.Add(new MailboxAddress(_globalSettings.SiteName, _globalSettings.Mail.ReplyToEmail));
|
||||
mimeMessage.From.Add(new MailboxAddress(_globalSettings.SiteName, _replyEmail));
|
||||
mimeMessage.Subject = message.Subject;
|
||||
if (!string.IsNullOrWhiteSpace(_replyDomain))
|
||||
{
|
||||
@ -43,14 +47,16 @@ namespace Bit.Core.Services
|
||||
|
||||
foreach (var address in message.ToEmails)
|
||||
{
|
||||
mimeMessage.To.Add(MailboxAddress.Parse(address));
|
||||
var punyencoded = CoreHelpers.PunyEncode(address);
|
||||
mimeMessage.To.Add(MailboxAddress.Parse(punyencoded));
|
||||
}
|
||||
|
||||
if (message.BccEmails != null)
|
||||
{
|
||||
foreach (var address in message.BccEmails)
|
||||
{
|
||||
mimeMessage.Bcc.Add(MailboxAddress.Parse(address));
|
||||
var punyencoded = CoreHelpers.PunyEncode(address);
|
||||
mimeMessage.Bcc.Add(MailboxAddress.Parse(punyencoded));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -7,6 +7,7 @@ using System.Collections.Generic;
|
||||
using Newtonsoft.Json;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using System.Text;
|
||||
using Bit.Core.Utilities;
|
||||
|
||||
namespace Bit.Core.Services
|
||||
{
|
||||
@ -25,13 +26,16 @@ namespace Bit.Core.Services
|
||||
IWebHostEnvironment hostingEnvironment,
|
||||
IHttpClientFactory clientFactory)
|
||||
{
|
||||
var postalDomain = CoreHelpers.PunyEncode(globalSettings.Mail.PostalDomain);
|
||||
var replyToEmail = CoreHelpers.PunyEncode(globalSettings.Mail.ReplyToEmail);
|
||||
|
||||
_globalSettings = globalSettings;
|
||||
_logger = logger;
|
||||
_clientFactory = clientFactory;
|
||||
_baseTag = $"Env_{hostingEnvironment.EnvironmentName}-" +
|
||||
$"Server_{globalSettings.ProjectName?.Replace(' ', '_')}";
|
||||
_from = $"\"{globalSettings.SiteName}\" <no-reply@{_globalSettings.Mail.PostalDomain}>";
|
||||
_reply = $"\"{globalSettings.SiteName}\" <{globalSettings.Mail.ReplyToEmail}>";
|
||||
_from = $"\"{globalSettings.SiteName}\" <no-reply@{postalDomain}>";
|
||||
_reply = $"\"{globalSettings.SiteName}\" <{replyToEmail}>";
|
||||
}
|
||||
|
||||
public async Task SendEmailAsync(Models.Mail.MailMessage message)
|
||||
@ -50,7 +54,7 @@ namespace Bit.Core.Services
|
||||
};
|
||||
foreach (var address in message.ToEmails)
|
||||
{
|
||||
request.to.Add(address);
|
||||
request.to.Add(CoreHelpers.PunyEncode(address));
|
||||
}
|
||||
|
||||
if (message.BccEmails != null)
|
||||
@ -58,7 +62,7 @@ namespace Bit.Core.Services
|
||||
request.bcc = new List<string>();
|
||||
foreach (var address in message.BccEmails)
|
||||
{
|
||||
request.bcc.Add(address);
|
||||
request.bcc.Add(CoreHelpers.PunyEncode(address));
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user