From ec4d8a8730fe53f3066e0244a4ad0e946785a8c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Cobucci?= Date: Thu, 14 Apr 2022 12:10:55 +0200 Subject: [PATCH 1/2] Avoid type errors when public key is not retrieved When dealing with unstable connections, phpseclib may return `false` when retrieving the public key. That causes `null` to be passed down to `base64_decode()` and triggers a TypeError due to strict types. This exits early, preventing triggering the problem. However, testing is a bit tricky since we need an unstable connection. --- src/PhpseclibV2/SftpConnectionProvider.php | 7 ++++++- src/PhpseclibV3/SftpConnectionProvider.php | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/PhpseclibV2/SftpConnectionProvider.php b/src/PhpseclibV2/SftpConnectionProvider.php index efd8ab5d1..c04a12ede 100644 --- a/src/PhpseclibV2/SftpConnectionProvider.php +++ b/src/PhpseclibV2/SftpConnectionProvider.php @@ -143,7 +143,12 @@ private function checkFingerprint(SFTP $connection): void return; } - $publicKey = $connection->getServerPublicHostKey() ?: 'no-public-key'; + $publicKey = $connection->getServerPublicHostKey(); + + if ($publicKey === false) { + throw UnableToEstablishAuthenticityOfHost::becauseTheAuthenticityCantBeEstablished($this->host); + } + $fingerprint = $this->getFingerprintFromPublicKey($publicKey); if (0 !== strcasecmp($this->hostFingerprint, $fingerprint)) { diff --git a/src/PhpseclibV3/SftpConnectionProvider.php b/src/PhpseclibV3/SftpConnectionProvider.php index 4dda92093..082e6d3fb 100644 --- a/src/PhpseclibV3/SftpConnectionProvider.php +++ b/src/PhpseclibV3/SftpConnectionProvider.php @@ -150,7 +150,12 @@ private function checkFingerprint(SFTP $connection): void return; } - $publicKey = $connection->getServerPublicHostKey() ?: 'no-public-key'; + $publicKey = $connection->getServerPublicHostKey(); + + if ($publicKey === false) { + throw UnableToEstablishAuthenticityOfHost::becauseTheAuthenticityCantBeEstablished($this->host); + } + $fingerprint = $this->getFingerprintFromPublicKey($publicKey); if (0 !== strcasecmp($this->hostFingerprint, $fingerprint)) { From 837431df8650d9d941d5fdb137181fbb2145da9e Mon Sep 17 00:00:00 2001 From: Frank de Jonge Date: Thu, 14 Apr 2022 16:56:29 +0200 Subject: [PATCH 2/2] Updated changelog for release --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b9fea1f07..11f6dbb39 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Version 2.x Changelog +## 2.4.4 - 2022-04-14 + +### Fixed + +- [SFTP v2] Avoid type errors when public key is not retrieved (#1446) +- [SFTP v3] Avoid type errors when public key is not retrieved (#1446) + ## 2.4.3 - 2022-02-16 ### Fixed