diff --git a/resources/lib/UnitySite.php b/resources/lib/UnitySite.php index c888c662..10c334f9 100644 --- a/resources/lib/UnitySite.php +++ b/resources/lib/UnitySite.php @@ -2,6 +2,8 @@ namespace UnityWebPortal\lib; +use phpseclib3\Crypt\PublicKeyLoader; + class UnitySite { public static function redirect($destination) @@ -50,4 +52,14 @@ public static function getConfig($conf_path) $arr = parse_ini_file($conf_path, true); return $arr; } + + public static function testValidSSHKey($key_str) + { + try { + PublicKeyLoader::load($key_str); + return true; + } catch (\Exception $e) { + return false; + } + } } diff --git a/webroot/js/ajax/ssh_validate.php b/webroot/js/ajax/ssh_validate.php new file mode 100644 index 00000000..7180defb --- /dev/null +++ b/webroot/js/ajax/ssh_validate.php @@ -0,0 +1,12 @@ + +alert('Invalid SSH key. Please verify your public key file is valid.'); +"; + if ($_SERVER['REQUEST_METHOD'] == "POST") { switch ($_POST["form_type"]) { case "addKey": @@ -13,10 +17,21 @@ switch ($_POST["add_type"]) { case "paste": - array_push($added_keys, $_POST["key"]); + $key = $_POST["key"]; + if (UnitySite::testValidSSHKey($key)) { + array_push($added_keys, $key); + } else { + echo $invalid_ssh_dialogue; + } break; case "import": - array_push($added_keys, file_get_contents($_FILES['keyfile']['tmp_name'])); + $keyfile = $_FILES["keyfile"]["tmp_name"]; + $key = file_get_contents($keyfile); + if (UnitySite::testValidSSHKey($key)) { + array_push($added_keys, $key); + } else { + echo $invalid_ssh_dialogue; + } break; case "generate": array_push($added_keys, $_POST["gen_key"]); diff --git a/webroot/panel/modal/new_key.php b/webroot/panel/modal/new_key.php index 9f63d736..96ada521 100644 --- a/webroot/panel/modal/new_key.php +++ b/webroot/panel/modal/new_key.php @@ -32,7 +32,7 @@