Skip to content

Commit

Permalink
Avoid type errors when public key is not retrieved
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lcobucci committed Apr 14, 2022
1 parent 24d5142 commit ec4d8a8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/PhpseclibV2/SftpConnectionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
7 changes: 6 additions & 1 deletion src/PhpseclibV3/SftpConnectionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down

0 comments on commit ec4d8a8

Please sign in to comment.