Skip to content

Commit 7078a9d

Browse files
committed
Removed unnesseray validation + add support for other styles of keys / formats
1 parent 0682fce commit 7078a9d

File tree

1 file changed

+2
-45
lines changed

1 file changed

+2
-45
lines changed

web/add/key/index.php

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,6 @@
55
// Main include
66
include($_SERVER['DOCUMENT_ROOT']."/inc/main.php");
77

8-
//check for valid format ssh key. Doesn't check it is working!
9-
//https://gist.github.com/jupeter/3248095
10-
function validateKey($value)
11-
{
12-
$key_parts = explode(' ', $value, 3);
13-
if (count($key_parts) < 2) {
14-
return false;
15-
}
16-
if (count($key_parts) > 3) {
17-
return false;
18-
}
19-
20-
$algorithm = $key_parts[0];
21-
$key = $key_parts[1];
22-
23-
if (!in_array($algorithm, array('ssh-rsa', 'ssh-dss'))) {
24-
return false;
25-
}
26-
27-
$key_base64_decoded = base64_decode($key, true);
28-
if ($key_base64_decoded == FALSE) {
29-
return false;
30-
}
31-
32-
$check = base64_decode(substr($key,0,16));
33-
$check = preg_replace("/[^\w\-]/","", $check);
34-
35-
if((string) $check !== (string) $algorithm) {
36-
return false;
37-
}
38-
return true;
39-
}
40-
418
// Check POST request
429
if (!empty($_POST['ok'])) {
4310
// Check token
@@ -51,38 +18,30 @@ function validateKey($value)
5118
}
5219

5320
if(!$_SESSION['error_msg']){
54-
switch ($_POST['v_key']){
55-
default:
21+
if($_POST){
5622
//key if key already exisits
5723
exec (HESTIA_CMD . "v-list-user-ssh-key ".$user." json", $output, $return_var);
5824
$data = json_decode(implode('', $output), true);
25+
unset($output);
5926
$keylist = array();
6027
foreach($data as $key => $value){
6128
$idlist[] = trim($data[$key]['ID']);
6229
$keylist[] = trim($data[$key]['KEY']);
6330
}
64-
65-
if(!validateKey($_POST['v_key'])){
66-
$_SESSION['error_msg'] = _('SSH KEY is invalid');
67-
break;
68-
}
6931

7032
$v_key_parts = explode(' ',$_POST['v_key']);
7133
$key_id = trim($v_key_parts[2]);
7234
if($v_key_parts[2] == ''){
7335
$_SESSION['error_msg'] = _('SSH KEY is invalid');
74-
break;
7536
}
7637

7738
//for deleting / revoking key the last part user@domain is used therefore needs to be unique
7839
//maybe consider adding random generated message or even an human read able string set by user?
7940
if(in_array($v_key_parts[2], $idlist)){
8041
$_SESSION['error_msg'] = _('SSH KEY already exists');
81-
break;
8242
}
8343
if(in_array($v_key_parts[1], $keylist)){
8444
$_SESSION['error_msg'] = _('SSH KEY already exists');
85-
break;
8645
}
8746
$v_key = escapeshellarg(trim($_POST['v_key']));
8847
}
@@ -92,9 +51,7 @@ function validateKey($value)
9251
exec (HESTIA_CMD."v-add-user-ssh-key ".$user." ".$v_key, $output, $return_var);
9352
check_return_code($return_var,$output);
9453
}
95-
9654
unset($output);
97-
9855
// Flush field values on success
9956
if (empty($_SESSION['error_msg'])) {
10057
$_SESSION['ok_msg'] = _('SSH KEY created');

0 commit comments

Comments
 (0)