Skip to content

Commit c675fb9

Browse files
committed
improve ssh key validation
1 parent a1679de commit c675fb9

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

resources/lib/UnitySite.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ public static function getGithubKeys($username)
4949

5050
public static function testValidSSHKey($key_str)
5151
{
52+
$key_str = trim($key_str);
53+
if ($key_str == ""){
54+
return false;
55+
}
56+
// PHP warning when key_str is digits: Attempt to read property "keys" on int
57+
if (preg_match("/^[0-9]+$/", $key_str)) {
58+
return false;
59+
}
60+
// PHP warning when key_str is JSON: Undefined property: stdClass::$keys
61+
if (!is_null(@json_decode($key_str))){
62+
return false;
63+
}
5264
try {
5365
PublicKeyLoader::load($key_str);
5466
return true;

webroot/js/ajax/ssh_validate.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22

33
require "../../../resources/autoload.php";
44

5+
use UnityWebPortal\lib\UnitySite;
56
use phpseclib3\Crypt\PublicKeyLoader;
67

7-
try {
8-
PublicKeyLoader::load($_POST['key'], $password = false);
9-
echo "true";
10-
} catch (Exception $e) {
11-
echo "false";
12-
}
8+
echo (UnitySite::testValidSSHKey($_POST["key"]) ? "true" : "false");

0 commit comments

Comments
 (0)