Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid type errors when public key is not retrieved #1446

Merged
merged 1 commit into from
Apr 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel bad about not providing any tests here (which would have caught the issue).
Having this method private and always instantiating SFTP make it slightly difficult to inject a stubbed connection to make sure we cover this behaviour.

Would love having ideas that don't involve using something like https://github.com/shopify/toxiproxy to mess up with the connection we use for tests.

}

$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