mirror of
https://github.com/luckfox-eng29/kvm.git
synced 2026-01-18 03:28:19 +01:00
feat: validate ssh public key before saving (#794)
* feat: validate ssh public key before saving * fix: TestValidSSHKeyTypes
This commit is contained in:
29
jsonrpc.go
29
jsonrpc.go
@@ -17,6 +17,7 @@ import (
|
||||
"go.bug.st/serial"
|
||||
|
||||
"github.com/jetkvm/kvm/internal/usbgadget"
|
||||
"github.com/jetkvm/kvm/internal/utils"
|
||||
)
|
||||
|
||||
type JSONRPCRequest struct {
|
||||
@@ -429,21 +430,27 @@ func rpcGetSSHKeyState() (string, error) {
|
||||
}
|
||||
|
||||
func rpcSetSSHKeyState(sshKey string) error {
|
||||
if sshKey != "" {
|
||||
// Create directory if it doesn't exist
|
||||
if err := os.MkdirAll(sshKeyDir, 0700); err != nil {
|
||||
return fmt.Errorf("failed to create SSH key directory: %w", err)
|
||||
}
|
||||
|
||||
// Write SSH key to file
|
||||
if err := os.WriteFile(sshKeyFile, []byte(sshKey), 0600); err != nil {
|
||||
return fmt.Errorf("failed to write SSH key: %w", err)
|
||||
}
|
||||
} else {
|
||||
if sshKey == "" {
|
||||
// Remove SSH key file if empty string is provided
|
||||
if err := os.Remove(sshKeyFile); err != nil && !os.IsNotExist(err) {
|
||||
return fmt.Errorf("failed to remove SSH key file: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Validate SSH key
|
||||
if err := utils.ValidateSSHKey(sshKey); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Create directory if it doesn't exist
|
||||
if err := os.MkdirAll(sshKeyDir, 0700); err != nil {
|
||||
return fmt.Errorf("failed to create SSH key directory: %w", err)
|
||||
}
|
||||
|
||||
// Write SSH key to file
|
||||
if err := os.WriteFile(sshKeyFile, []byte(sshKey), 0600); err != nil {
|
||||
return fmt.Errorf("failed to write SSH key: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user