Skip to content

Commit e4917d1

Browse files
committed
[SecImprove] Validate keys real location when adding sftp key
1 parent 5e06cc5 commit e4917d1

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

bin/v-add-user-sftp-key

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ PRVKEY_FILE="$HOMEDIR/$user/.ssh/hst-filemanager-key"
3434
PUBKEY_FILE="$HOMEDIR/$user/.ssh/hst-filemanager-key.pub"
3535
AUTHKEY_FILE="$HOMEDIR/$user/.ssh/authorized_keys"
3636

37-
[ -L "$PRVKEY_FILE" ] && check_result $E_FORBIDEN "Private key file cannot be a symlink"
38-
[ -L "$PUBKEY_FILE" ] && check_result $E_FORBIDEN "Public key file cannot be a symlink"
39-
[ -L "$AUTHKEY_FILE" ] && check_result $E_FORBIDEN "Authorized keys file cannot be a symlink"
37+
[ -z "$(readlink -f "$PRVKEY_FILE" | egrep "^$HOMEDIR/$user/.ssh/")" ] && check_result $E_FORBIDEN "Invalid private key file path"
38+
[ -z "$(readlink -f "$PUBKEY_FILE" | egrep "^$HOMEDIR/$user/.ssh/")" ] && check_result $E_FORBIDEN "Invalid public key file path"
39+
[ -z "$(readlink -f "$AUTHKEY_FILE" | egrep "^$HOMEDIR/$user/.ssh/")" ] && check_result $E_FORBIDEN "Invalid authorized keys path"
4040

4141
if [ ! -f "${PRVKEY_FILE}" ]; then
4242

@@ -63,7 +63,7 @@ if [ ! -f "${AUTHKEY_FILE}" ] || [ "$new_pubkey" = true ]; then
6363
fi
6464

6565
# make sure authorized_keys is ending with EOL
66-
sed -i '$a\' "${AUTHKEY_FILE}"
66+
[ -f "${AUTHKEY_FILE}" ] && sed -i '$a\' "${AUTHKEY_FILE}"
6767

6868
echo "from=\"127.0.0.1\",command=\"internal-sftp\",restrict ${pubkey_str} TS:${now} ${pubkey_desc}" >> "${AUTHKEY_FILE}"
6969

0 commit comments

Comments
 (0)