Fix: Make sure invalid private keys can not be added

This commit is contained in:
peaklabs-dev
2024-09-16 13:02:48 +02:00
parent f9b7841572
commit 02017334e5
2 changed files with 31 additions and 14 deletions

View File

@@ -59,13 +59,11 @@ class Create extends Component
{
if ($updateProperty === 'value') {
try {
$this->publicKey = PublicKeyLoader::load($this->$updateProperty)->getPublicKey()->toString('OpenSSH', ['comment' => '']);
$key = PublicKeyLoader::load($this->$updateProperty);
$this->publicKey = $key->getPublicKey()->toString('OpenSSH', ['comment' => '']);
} catch (\Throwable $e) {
if ($this->$updateProperty === '') {
$this->publicKey = '';
} else {
$this->publicKey = 'Invalid private key';
}
$this->publicKey = '';
$this->addError('value', 'Invalid private key');
}
}
$this->validateOnly($updateProperty);
@@ -73,7 +71,21 @@ class Create extends Component
public function createPrivateKey()
{
$this->validate();
$this->validate([
'name' => 'required|string',
'value' => [
'required',
'string',
function ($attribute, $value, $fail) {
try {
PublicKeyLoader::load($value);
} catch (\Throwable $e) {
$fail('The private key is invalid.');
}
},
],
]);
try {
$this->value = trim($this->value);
if (! str_ends_with($this->value, "\n")) {