Skip to content

Commit dc33cc3

Browse files
almailerspring-builds
authored andcommitted
GH-10188: Close ClientSession in DefaultSftpSF on auth fail
Fixes: #10188 * Prevent a connection leak due to `ClientSession` going out of scope in the `DefaultSftpSessionFactory.getSession()` when connecting is successful but authentication fails. Signed-off-by: alastair Mailer <[email protected]> (cherry picked from commit 851001c)
1 parent 27e5fca commit dc33cc3

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/DefaultSftpSessionFactory.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
* @author Christian Tzolov
8080
* @author Adama Sorho
8181
* @author Darryl Smith
82+
* @author Alastair Mailer
8283
*
8384
* @since 2.0
8485
*/
@@ -351,7 +352,13 @@ private ClientSession initClientSession() throws IOException {
351352
.verify(verifyTimeout)
352353
.getSession();
353354

354-
clientSession.auth().verify(verifyTimeout);
355+
try {
356+
clientSession.auth().verify(verifyTimeout);
357+
}
358+
catch (IOException ex) {
359+
clientSession.close();
360+
throw ex;
361+
}
355362

356363
return clientSession;
357364
}

spring-integration-sftp/src/test/java/org/springframework/integration/sftp/session/SftpSessionFactoryTests.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import org.springframework.core.task.SimpleAsyncTaskExecutor;
5353

5454
import static org.assertj.core.api.Assertions.assertThat;
55+
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
5556
import static org.assertj.core.api.Assertions.assertThatNoException;
5657
import static org.assertj.core.api.Assertions.fail;
5758
import static org.awaitility.Awaitility.await;
@@ -61,17 +62,18 @@
6162
* @author Artem Bilan
6263
* @author Auke Zaaiman
6364
* @author Darryl Smith
65+
* @author Alastair Mailer
6466
*
6567
* @since 3.0.2
6668
*/
67-
public class SftpSessionFactoryTests {
69+
class SftpSessionFactoryTests {
6870

6971
/*
7072
* Verify the socket is closed if the channel.connect() fails.
7173
* INT-3305
7274
*/
7375
@Test
74-
public void testConnectFailSocketOpen() throws Exception {
76+
void testConnectFailSocketOpen() throws Exception {
7577
try (SshServer server = SshServer.setUpDefaultServer()) {
7678
server.setPasswordAuthenticator((arg0, arg1, arg2) -> true);
7779
server.setPort(0);
@@ -118,7 +120,7 @@ public void testConnectFailSocketOpen() throws Exception {
118120
}
119121

120122
@Test
121-
public void concurrentGetSessionDoesntCauseFailure() throws Exception {
123+
void concurrentGetSessionDoesntCauseFailure() throws Exception {
122124
try (SshServer server = SshServer.setUpDefaultServer()) {
123125
server.setPasswordAuthenticator((arg0, arg1, arg2) -> true);
124126
server.setPort(0);
@@ -335,4 +337,33 @@ protected SftpClient createSftpClient(ClientSession clientSession,
335337
}
336338
}
337339

340+
/*
341+
* Verify the socket is closed if authentication fails.
342+
*/
343+
@Test
344+
void noClientSessionLeakOnAuthError() throws Exception {
345+
try (SshServer server = SshServer.setUpDefaultServer()) {
346+
server.setPasswordAuthenticator((arg0, arg1, arg2) -> false);
347+
server.setPort(0);
348+
server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("hostkey.ser").toPath()));
349+
server.setSubsystemFactories(Collections.singletonList(new SftpSubsystemFactory()));
350+
server.start();
351+
352+
DefaultSftpSessionFactory sftpSessionFactory = new DefaultSftpSessionFactory();
353+
sftpSessionFactory.setHost("localhost");
354+
sftpSessionFactory.setPort(server.getPort());
355+
sftpSessionFactory.setUser("user");
356+
sftpSessionFactory.setAllowUnknownKeys(true);
357+
358+
assertThatIllegalStateException()
359+
.isThrownBy(sftpSessionFactory::getSession)
360+
.withCauseInstanceOf(SshException.class)
361+
.withStackTraceContaining("No more authentication methods available");
362+
363+
await().untilAsserted(() -> assertThat(server.getActiveSessions()).hasSize(0));
364+
365+
sftpSessionFactory.destroy();
366+
}
367+
}
368+
338369
}

0 commit comments

Comments
 (0)