Fix STARTTLS bug in SmtpTransport

This commit is contained in:
cketti 2022-05-01 01:31:46 +02:00
parent 0a05802843
commit 1280f43cba
3 changed files with 43 additions and 3 deletions

View file

@ -130,15 +130,16 @@ class SmtpTransport(
if (extensions.containsKey("STARTTLS")) {
executeCommand("STARTTLS")
this.socket = trustedSocketFactory.createSocket(
val tlsSocket = trustedSocketFactory.createSocket(
socket,
host,
port,
clientCertificateAlias
)
inputStream = PeekableInputStream(BufferedInputStream(socket.getInputStream(), 1024))
this.socket = tlsSocket
inputStream = PeekableInputStream(BufferedInputStream(tlsSocket.getInputStream(), 1024))
responseParser = SmtpResponseParser(logger, inputStream!!)
outputStream = BufferedOutputStream(socket.getOutputStream(), 1024)
outputStream = BufferedOutputStream(tlsSocket.getOutputStream(), 1024)
// Now resend the EHLO. Required by RFC2487 Sec. 5.2, and more specifically, Exim.
extensions = sendHello(helloName)

View file

@ -68,6 +68,11 @@ public class MockSmtpServer {
interactions.add(new ExpectedCommand(command));
}
public void startTls() {
checkServerNotRunning();
interactions.add(new UpgradeToTls());
}
public void closeConnection() {
checkServerNotRunning();
interactions.add(new CloseConnection());
@ -212,6 +217,9 @@ public class MockSmtpServer {
}
}
private static class UpgradeToTls implements SmtpInteraction {
}
private static class CloseConnection implements SmtpInteraction {
}
@ -303,6 +311,8 @@ public class MockSmtpServer {
readExpectedCommand((ExpectedCommand) interaction);
} else if (interaction instanceof CannedResponse) {
writeCannedResponse((CannedResponse) interaction);
} else if (interaction instanceof UpgradeToTls) {
upgradeToTls(socket);
} else if (interaction instanceof CloseConnection) {
clientSocket.close();
}

View file

@ -567,6 +567,35 @@ class SmtpTransportTest {
server.verifyInteractionCompleted()
}
@Test
fun `open() with STARTTLS`() {
val server = MockSmtpServer().apply {
output("220 localhost Simple Mail Transfer Service Ready")
expect("EHLO [127.0.0.1]")
output("250-localhost Hello 127.0.0.1")
output("250-STARTTLS")
output("250 HELP")
expect("STARTTLS")
output("220 Ready to start TLS")
startTls()
expect("EHLO [127.0.0.1]")
output("250-localhost Hello 127.0.0.1")
output("250 AUTH PLAIN LOGIN")
expect("AUTH PLAIN AHVzZXIAcGFzc3dvcmQ=")
output("235 2.7.0 Authentication successful")
}
val transport = startServerAndCreateSmtpTransport(
server,
authenticationType = AuthType.PLAIN,
connectionSecurity = ConnectionSecurity.STARTTLS_REQUIRED
)
transport.open()
server.verifyConnectionStillOpen()
server.verifyInteractionCompleted()
}
@Test
fun `sendMessage() without address to send to should not open connection`() {
val message = MimeMessage()