Skip to content

Commit 38782ca

Browse files
authored
update ssh key adding logic (#214)
1 parent 03934e0 commit 38782ca

File tree

2 files changed

+33
-40
lines changed

2 files changed

+33
-40
lines changed

resources/lib/UnitySite.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,24 @@ public static function forbidden($message)
6565
self::die();
6666
}
6767

68-
public static function removeTrailingWhitespace($arr)
68+
public static function arrayGetOrBadRequest(array $array, ...$keys)
6969
{
70-
$out = array();
71-
foreach ($arr as $str) {
72-
$new_string = rtrim($str);
73-
array_push($out, $new_string);
70+
$cursor = $array;
71+
$keysTraversed = [];
72+
foreach ($keys as $key) {
73+
array_push($keysTraversed, $key);
74+
if (!isset($cursor[$key])) {
75+
self::badRequest("array key not found: " . json_encode($keysTraversed));
76+
}
77+
$cursor = $cursor[$key];
7478
}
79+
return $cursor;
80+
}
7581

76-
return $out;
82+
public static function alert(string $message)
83+
{
84+
// json_encode escapes quotes
85+
echo "<script type='text/javascript'>alert(" . json_encode($message) . ");</script>";
7786
}
7887

7988
public static function testValidSSHKey($key_str)

webroot/panel/account.php

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,51 +6,35 @@
66

77
require_once $LOC_HEADER;
88

9-
$invalid_ssh_dialogue = "<script type='text/javascript'>
10-
alert('Invalid SSH key. Please verify your public key file is valid.');
11-
</script>";
12-
139
if ($_SERVER['REQUEST_METHOD'] == "POST") {
14-
switch ($_POST["form_type"]) {
10+
switch (UnitySite::arrayGetOrBadRequest($_POST, "form_type")) {
1511
case "addKey":
16-
$added_keys = array();
17-
18-
switch ($_POST["add_type"]) {
12+
$keys = array();
13+
switch (UnitySite::arrayGetOrBadRequest($_POST, "add_type")) {
1914
case "paste":
20-
$key = $_POST["key"];
21-
if (UnitySite::testValidSSHKey($key)) {
22-
array_push($added_keys, $key);
23-
} else {
24-
echo $invalid_ssh_dialogue;
25-
}
15+
array_push($keys, UnitySite::arrayGetOrBadRequest($_POST, "key"));
2616
break;
2717
case "import":
28-
$keyfile = $_FILES["keyfile"]["tmp_name"];
29-
$key = file_get_contents($keyfile);
30-
if (UnitySite::testValidSSHKey($key)) {
31-
array_push($added_keys, $key);
32-
} else {
33-
echo $invalid_ssh_dialogue;
34-
}
18+
$keyPath = UnitySite::arrayGetOrBadRequest($_FILES, "keyfile", "tmp_name");
19+
$key = file_get_contents($keyPath);
20+
array_push($keys, $key);
3521
break;
3622
case "generate":
37-
array_push($added_keys, $_POST["gen_key"]);
23+
array_push($keys, UnitySite::arrayGetOrBadRequest($_POST, "gen_key"));
3824
break;
3925
case "github":
40-
$gh_user = $_POST["gh_user"];
41-
$keys = $GITHUB->getSshPublicKeys($gh_user);
42-
foreach ($keys as $key) {
43-
if (UnitySite::testValidSSHKey($key)) {
44-
array_push($added_keys, $key);
45-
}
46-
}
26+
$githubUsername = UnitySite::arrayGetOrBadRequest($_POST, "gh_user");
27+
$githubKeys = $GITHUB->getSshPublicKeys($githubUsername);
28+
$keys = array_merge($keys, $githubKeys);
4729
break;
4830
}
49-
50-
if (!empty($added_keys)) {
51-
$added_keys = UnitySite::removeTrailingWhitespace($added_keys);
52-
$totalKeys = array_merge($USER->getSSHKeys(), $added_keys);
53-
$USER->setSSHKeys($totalKeys, $OPERATOR);
31+
if (!empty($keys)) {
32+
$keys = array_map("trim", $keys);
33+
$validKeys = array_filter($keys, ["UnityWebPortal\lib\UnitySite", "testValidSSHKey"]);
34+
$USER->setSSHKeys(array_merge($USER->getSSHKeys(), $validKeys));
35+
if (count($keys) != count($validKeys)) {
36+
UnitySite::alert("invalid SSH key");
37+
}
5438
}
5539
break;
5640
case "delKey":

0 commit comments

Comments
 (0)