Skip to content

Commit 5f5ccc6

Browse files
committed
[SecImprove] Validate real auth keys location when adding or deleting ssh key. Add required args and some cosmetic changes
1 parent e4917d1 commit 5f5ccc6

File tree

3 files changed

+28
-20
lines changed

3 files changed

+28
-20
lines changed

bin/v-add-user-ssh-key

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ source $HESTIA/conf/hestia.conf
2424
# Verifications #
2525
#----------------------------------------------------------#
2626

27+
check_args '2' "$#" 'USER SSH_PUBLIC_KEY'
2728
is_format_valid 'user'
2829
is_object_valid 'user' 'USER' "$user"
2930
is_object_unsuspended 'user' 'USER' "$user"
@@ -37,22 +38,23 @@ check_hestia_demo_mode
3738

3839
# Reading user values
3940
source $USER_DATA/user.conf
41+
42+
AUTHKEY_FILE="$HOMEDIR/$user/.ssh/authorized_keys"
43+
[ -z "$(readlink -f "$AUTHKEY_FILE" | egrep "^$HOMEDIR/$user/.ssh/")" ] && check_result $E_FORBIDEN "Invalid authorized keys path"
44+
4045
#check if file exits
41-
AUTHKEY_FILE=$HOMEDIR/$user/.ssh/authorized_keys
4246
if [ ! -f "$AUTHKEY_FILE" ]; then
43-
touch "$AUTHKEY_FILE"
44-
chown ${user}: "${AUTHKEY_FILE}"
47+
v-add-fs-file "$user" "${AUTHKEY_FILE}"
4548
fi
46-
TEMP=$(mktemp)
47-
echo "$key" >> "$TEMP"
48-
ssh-keygen -l -f "$TEMP"
49-
if [ ! $? -eq 0 ]; then
50-
rm "$TEMP"
51-
exit
49+
50+
[ -z "$key" ] && check_result $E_NOTEXIST "Empty ssh public key"
51+
52+
if ! echo "$key" | ssh-keygen -l -f - > /dev/null 2>&1; then
53+
check_result $E_PARSING "Validating user private key"
5254
fi
53-
rm "$TEMP"
55+
5456
# make sure authorized_keys is ending with EOL
55-
sed -i '$a\' "${AUTHKEY_FILE}"
57+
[ -f "${AUTHKEY_FILE}" ] && sed -i '$a\' "${AUTHKEY_FILE}"
5658

5759
#append key to file
5860
echo "$key" >> "$AUTHKEY_FILE"
@@ -62,7 +64,7 @@ echo "$key" >> "$AUTHKEY_FILE"
6264
#----------------------------------------------------------#
6365

6466
# Logging
65-
log_history "added ssh-key $user"
67+
log_history "Added ssh-key $user"
6668
log_event "$OK" "$ARGUMENTS"
6769

6870
exit

bin/v-delete-user-ssh-key

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,18 @@ source $HESTIA/conf/hestia.conf
2323
# Verifications #
2424
#----------------------------------------------------------#
2525

26+
check_args '2' "$#" 'USER KEYID'
2627
is_format_valid 'user'
2728
is_object_valid 'user' 'USER' "$user"
2829
is_object_unsuspended 'user' 'USER' "$user"
2930

3031
source $USER_DATA/user.conf
3132

32-
FILE=$HOMEDIR/$user/.ssh/authorized_keys
33-
if [ ! -f "$FILE" ]; then
34-
exit;
33+
AUTHKEY_FILE="$HOMEDIR/$user/.ssh/authorized_keys"
34+
[ -z "$(readlink -f "$AUTHKEY_FILE" | egrep "^$HOMEDIR/$user/.ssh/")" ] && check_result $E_FORBIDEN "Invalid authorized keys path"
35+
36+
if [ ! -f "$AUTHKEY_FILE" ]; then
37+
exit
3538
fi
3639

3740
# Perform verification if read-only mode is enabled
@@ -41,14 +44,14 @@ check_hestia_demo_mode
4144
# Action #
4245
#----------------------------------------------------------#
4346

44-
sed -i "/${keyid}/d" "$FILE"
47+
sed -i "/${keyid}/d" "$AUTHKEY_FILE"
4548

4649
#----------------------------------------------------------#
4750
# Hestia #
4851
#----------------------------------------------------------#
4952

5053
# Logging
51-
log_history "DELETE ssh-key $user"
54+
log_history "Deleted ssh-key $user"
5255
log_event "$OK" "$ARGUMENTS"
5356

5457
exit

bin/v-list-user-ssh-key

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ csv_list() {
7474
# Verifications #
7575
#----------------------------------------------------------#
7676

77+
check_args '1' "$#" 'USER'
7778
is_format_valid 'user'
7879
is_object_valid 'user' 'USER' "$user"
7980
is_object_unsuspended 'user' 'USER' "$user"
@@ -82,9 +83,11 @@ is_object_unsuspended 'user' 'USER' "$user"
8283
# Action #
8384
#----------------------------------------------------------#
8485

85-
#check if file exsists
86+
AUTHKEY_FILE="$HOMEDIR/$user/.ssh/authorized_keys"
87+
[ -z "$(readlink -f "$AUTHKEY_FILE" | egrep "^$HOMEDIR/$user/.ssh/")" ] && check_result $E_FORBIDEN "Invalid authorized keys path"
8688

87-
if [ ! -f "$HOMEDIR/$user/.ssh/authorized_keys" ]; then
89+
#check if file exsists
90+
if [ ! -f "$AUTHKEY_FILE" ]; then
8891
exit
8992
fi
9093

@@ -96,7 +99,7 @@ while read l; do
9699
(( $first_loop )) && keys="$sshkey" || keys="$keys"$'\n'"$sshkey"
97100
unset first_loop
98101
fi
99-
done < "$HOMEDIR/$user/.ssh/authorized_keys"
102+
done < "$AUTHKEY_FILE"
100103

101104

102105
# Listing data

0 commit comments

Comments
 (0)