spring-integration: [SFTP] [v6 Regression] ResourceKnownHostsServerKeyVerifier does not match by IP

In what version(s) of Spring Integration are you seeing this issue?

  • 6.0.5 (Spring Boot 3.0.6)
  • Still in the main branch

Describe the bug

When using a DefaultSftpSessionFactory with knownHostsResource set, the known_hosts are matched only by hostname, and not by IP.

Even if host is set to an IP address in the configuration, only its reverse DNS is matched.

Exception from the log (redacted)

o.a.s.client.session.ClientSessionImpl   : exceptionCaught(ClientSessionImpl[username@1-113-0-203.static-ip.example.com/203.0.113.1:2222])[state=Opened] SshException: Server key did not validate

To Reproduce

Configure a DefaultSftpSessionFactory, with host set to an IP address, and knownHostsResource set to a classpath known_host file with a public key configured for the IP.

Expected behavior

The known_hosts should be matched with the IP from the host config param, like it did in Spring Integration SFTP < 6.

Analyzing a bit more

ResourceKnownHostsServerKeyVerifier.resolveHostNetworkIdentities() calls SshdSocketAddress.toSshdSocketAddress, which always fetches the hostname from the connect address’s IP.

This behavior appears to be copied from Apache Mina’s KnownHostsServerKeyVerifier. It might well be on purpose on their side to only use hostnames, but this is a regression from the behavior in previous versions of Spring Integration SFTP.

Maybe ResourceKnownHostsServerKeyVerifier.resolveHostNetworkIdentities() should (in addition?) return the raw address from clientSession.getConnectAddress()?

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 1
  • Comments: 17 (9 by maintainers)

Most upvoted comments

@artembilan I just encountered a similar issue with my local integration tests. Not sure if it is the issue the original poster had, but it looks like @laguiar might at least be running into the same problem.

I believe my issue is related to this https://github.com/spring-projects/spring-integration/blob/4d33ea093c0bf3cdb01ffe07d17e3d60c51bfcfc/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/DefaultSftpSessionFactory.java#L363-L367

Previously, if you specified both setAllowUnknownKeys as true and the setKnownHostsResource the key verification would be skipped regardless of what setKnownHostsResource was.

        // this session factory configuration works with 5.x.x but breaks with 6.x.x
        sessionFactory.setAllowUnknownKeys(true);
        sessionFactory.setKnownHostsResource(knownHostsFile); // knownHostsFile pointing to /dev/null

But now if the knownHosts file is ever not null, the allowUnknownKeys configuration is ignored.

For my case, the integration tests specified the knownHostsFile as /dev/null and allow unknown keys as true as we don’t really care for the validations in our local integration tests. Setting the knownHostsFile to be null removed the error.

It’s possible that users have a bad knownHosts file and are relying on setAllowUnknownKeys