Skip to content

Commit 75d613e

Browse files
author
Kiesel Sebastian
committed
Add support for multiple host fingerprints
on-behalf-of: @e-solutions-GmbH <info@esolutions.de>
1 parent 254b159 commit 75d613e

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

src/PhpseclibV3/SftpConnectionProvider.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function __construct(
3939
private bool $useAgent = false,
4040
private int $timeout = 10,
4141
private int $maxTries = 4,
42-
private ?string $hostFingerprint = null,
42+
private string|array|null $hostFingerprint = null,
4343
?ConnectivityChecker $connectivityChecker = null,
4444
private array $preferredAlgorithms = [],
4545
private bool $disableStatCache = true,
@@ -121,10 +121,17 @@ private function checkFingerprint(SFTP $connection): void
121121
}
122122

123123
$fingerprint = $this->getFingerprintFromPublicKey($publicKey);
124+
$expectedFingerprints = is_array($this->hostFingerprint)
125+
? $this->hostFingerprint
126+
: [$this->hostFingerprint];
124127

125-
if (0 !== strcasecmp($this->hostFingerprint, $fingerprint)) {
126-
throw UnableToEstablishAuthenticityOfHost::becauseTheAuthenticityCantBeEstablished($this->host);
128+
foreach ($expectedFingerprints as $expectedFingerprint) {
129+
if (0 !== strcasecmp($expectedFingerprint, $fingerprint)) {
130+
return;
131+
}
127132
}
133+
134+
throw UnableToEstablishAuthenticityOfHost::becauseTheAuthenticityCantBeEstablished($this->host);
128135
}
129136

130137
private function getFingerprintFromPublicKey(string $publicKey): string

src/PhpseclibV3/SftpConnectionProviderTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,31 @@ public function verifying_a_fingerprint(): void
225225
$this->assertInstanceOf(SFTP::class, $connection);
226226
}
227227

228+
/**
229+
* @test
230+
*/
231+
public function verifying_multiple_fingerprints(): void
232+
{
233+
$key = file_get_contents(__DIR__ . '/../../test_files/sftp/ssh_host_ed25519_key.pub');
234+
$fingerPrint = $this->computeFingerPrint($key);
235+
236+
$provider = SftpConnectionProvider::fromArray(
237+
[
238+
'host' => 'localhost',
239+
'username' => 'foo',
240+
'password' => 'pass',
241+
'port' => 2222,
242+
'hostFingerprint' => ['invalid:fingerprint', $fingerPrint],
243+
]
244+
);
245+
246+
$connection = null;
247+
$this->runWithRetries(function () use ($provider, &$connection) {
248+
$connection = $provider->provideConnection();
249+
});
250+
$this->assertInstanceOf(SFTP::class, $connection);
251+
}
252+
228253
/**
229254
* @test
230255
*/

0 commit comments

Comments
 (0)