Fixed signing in to mailboxes with characters beyond ASCII in the username or password (#1224)

Co-authored-by: Thorsten Sommer <SommerEngineering@users.noreply.github.com>
This commit is contained in:
Peer HogeterpandThorsten Sommer authored and GitHub committed 2026-11-24 15:53:19 +01:00
1 parent f6a4348820
commit c72a1948b3
27 files changed
+1052 -30

No files matched your search

@@ -1,4 +1,5 @@
using System.Net.Sockets;
using System.Text;
using AIStudio.Settings.DataModel;
using AIStudio.Tools.Databases.IndexStore;
@@ -19,7 +20,9 @@ namespace AIStudio.Tools.Mail;
/// Every sign-in is exactly one attempt with the password. An IMAP client would otherwise try one
/// mechanism after the other and the LOGIN command last, and a directory such as Active Directory
/// counts each rejected attempt on its way to locking the account. For the same reason, nothing is
/// sent at all when the password is empty or a setting is unknown to this version.<br/><br/>
/// sent at all when the password is empty, a setting is unknown to this version, or the encoding of
/// the mailbox cannot carry the username and the password, cf. TryPrepareSignIn. AI Studio never
/// switches to another encoding on its own: only the user chooses another one.<br/><br/>
/// Before signing in, the caller has to make sure that no failed sign-in is on record for this
/// mailbox. Whatever fails arrives as MailboxConnectionException with its failure code, and nothing
/// here writes to the log: the answers of a server may name the user, so the caller logs the
@@ -111,12 +114,12 @@ public sealed class ImapMailboxConnector : IAsyncDisposable
/// </summary>
/// <param name="mailbox">The mailbox to connect to.</param>
/// <param name="password">The password, as stored in the OS keyring or just typed in.</param>
/// <param name="policy">Which mail servers the organization allows. Every connection passes here, so this is where a server it does not allow is refused.</param>
/// <param name="policy">Which mail servers the organization allows, and which encodings it names for them. Every connection passes here, so this is where a server it does not allow is refused, and where its encoding takes effect.</param>
/// <param name="token">The cancellation token.</param>
/// <exception cref="MailboxConnectionException">The connection or the sign-in failed.</exception>
public async Task ConnectAsync(DataSourceMailbox mailbox, string password, MailServerPolicy policy, CancellationToken token)
{
if (!TryGetSocketOptions(mailbox.TransportSecurity, out var socketOptions) || mailbox.AuthMethod is not MailboxAuthMethod.PASSWORD || !MailServerHosts.TryGetIdnHost(mailbox.Host, out var idnHost) || mailbox.Port is < 1 or > 65535 || string.IsNullOrWhiteSpace(mailbox.Username) || string.IsNullOrEmpty(password))
if (!TryGetSocketOptions(mailbox.TransportSecurity, out var socketOptions) || mailbox.AuthMethod is not MailboxAuthMethod.PASSWORD || policy.GetPasswordEncoding(mailbox) is not { TextEncoding: { } textEncoding } passwordEncoding || !MailServerHosts.TryGetIdnHost(mailbox.Host, out var idnHost) || mailbox.Port is < 1 or > 65535 || string.IsNullOrWhiteSpace(mailbox.Username) || string.IsNullOrEmpty(password))
throw new MailboxConnectionException(MailboxConnectionFailure.INVALID_SETTINGS, "The mailbox settings are incomplete, or this version of AI Studio does not know them.");
if (!policy.IsAllowed(mailbox.Host))
@@ -137,9 +140,13 @@ public sealed class ImapMailboxConnector : IAsyncDisposable
throw new MailboxConnectionException(failure, $"Connecting to the IMAP server failed: {failure}.", e);
}
var offersPlain = this.client.AuthenticationMechanisms.Contains(PLAIN_MECHANISM);
if (!TryPrepareSignIn(passwordEncoding, mailbox.Username, password, offersPlain, out var preparedUsername, out var preparedPassword))
throw new MailboxConnectionException(MailboxConnectionFailure.UNSUITABLE_ENCODING, "The encoding of the mailbox cannot carry the username and the password to this IMAP server.");
try
{
await this.SignInAsync(mailbox.Username, password, token);
await this.SignInAsync(textEncoding, preparedUsername, preparedPassword, offersPlain, token);
}
catch (Exception e) when (Classify(e, token) is { } failure)
{
@@ -486,11 +493,41 @@ public sealed class ImapMailboxConnector : IAsyncDisposable
return entity is TextPart textPart ? textPart.Text : null;
}
private async Task SignInAsync(string username, string password, CancellationToken token)
/// <summary>
/// Prepares the username and the password for one sign-in in the encoding of the mailbox, cf. MailServerPolicy.GetPasswordEncoding.
/// </summary>
/// <remarks>
/// A sign-in in bytes which are not the password counts as a failed attempt, just like a wrong
/// password. So nothing may be sent when the encoding cannot carry the username and the password:
/// when it lacks one of their characters, e.g., Windows-1252 has no ẞ, and when the server does not
/// offer PLAIN and the encoding is not UTF-8, since MailKit sends the LOGIN command in UTF-8,
/// whatever encoding it is handed. Text in ASCII has the same bytes in every encoding a mailbox
/// may use, cf. MailboxPasswordEncoding, so it goes through the LOGIN command as well.
/// </remarks>
/// <param name="encoding">The encoding of the mailbox.</param>
/// <param name="username">The username.</param>
/// <param name="password">The password.</param>
/// <param name="offersPlain">Whether the server offers the SASL mechanism PLAIN.</param>
/// <param name="preparedUsername">The username to sign in with.</param>
/// <param name="preparedPassword">The password to sign in with.</param>
/// <returns>Whether the sign-in may go ahead.</returns>
internal static bool TryPrepareSignIn(MailboxPasswordEncoding encoding, string username, string password, bool offersPlain, out string preparedUsername, out string preparedPassword)
{
if (this.client.AuthenticationMechanisms.Contains(PLAIN_MECHANISM))
preparedPassword = password;
if (!encoding.TryPrepare(username, out preparedUsername) || !encoding.TryPrepare(password, out preparedPassword))
return false;
return offersPlain || encoding.IsUtf8 || !MailboxPasswordEncoding.DependsOnEncoding(preparedUsername, preparedPassword);
}
/// <summary>
/// Signs in once, with a username and a password which TryPrepareSignIn prepared.
/// </summary>
private async Task SignInAsync(Encoding textEncoding, string username, string password, bool offersPlain, CancellationToken token)
{
if (offersPlain)
{
await this.client.AuthenticateAsync(new SaslMechanismPlain(username, password), token);
await this.client.AuthenticateAsync(new SaslMechanismPlain(textEncoding, username, password), token);
return;
}
@@ -498,7 +535,8 @@ public sealed class ImapMailboxConnector : IAsyncDisposable
throw new MailboxConnectionException(MailboxConnectionFailure.SERVER_ERROR, "The IMAP server offers no way to sign in with a password.");
//
// With no mechanism left to try, the client signs in with the LOGIN command alone:
// With no mechanism left to try, the client signs in with the LOGIN command alone, always in
// UTF-8, cf. TryPrepareSignIn:
//
this.client.AuthenticationMechanisms.Clear();
await this.client.AuthenticateAsync(username, password, token);