Skip to content

smtps:// email connection string without a port connects to port 25 with no TLS #2697

Open

Description

@ejsmith

SmtpUri sets port 465 for an smtps:// URI with no explicit port, then overwrites it with 25 a few lines later. Because the encryption default is derived from the port, the mailer then connects to port 25 with no TLS. The parsed IsSecure flag is never used.

Where

  • SmtpUri constructor sets Port = uri.IsDefaultPort ? 465 : uri.Port for smtps, then L41 unconditionally runs Port = uri.IsDefaultPort ? 25 : uri.Port. System.Uri has no default port for smtps, so IsDefaultPort is true and Port is -1 whenever the port is omitted.
  • EmailOptions.ReadFromConfiguration copies host, port, user and password but not IsSecure, and defaults SmtpEncryption from the port via GetDefaultSmtpEncryption (25 maps to None).
  • MailKitMailSender connects with SecureSocketOptions.None and then authenticates.

Steps to reproduce

  1. Set EX_ConnectionStrings__Email=smtps://user:password@smtp.example.com (no port) and leave EX_SmtpEncryption unset.
  2. Trigger any email (e.g. a password reset). The mailer connects to smtp.example.com:25 without TLS and sends AUTH with the credentials in the clear, or fails if port 25 is blocked or the server requires TLS.

Expected

smtps://host connects to port 465 with implicit TLS (SslOnConnect). More generally, the smtps scheme should select TLS regardless of port.

Suggested fix

Remove the trailing Port = uri.IsDefaultPort ? 25 : uri.Port; in SmtpUri (the scheme branches already set the port). In EmailOptions, when SmtpEncryption isn't configured and uri.IsSecure is true, default to SSL for port 465 and StartTLS for other ports instead of None. Add SmtpUri tests for smtp://host, smtps://host, and smtps://host:587.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions