Skip to content

Commit b02ec86

Browse files
authored
Changes to API (hestiacp#2325)
1. Validate hash 2. Remove 127.0.0.1 from default allowed ip adresses @numanturle 3. Restrict 127.0.0.1 to be passed trough spoofed headers @numanturle
1 parent f13f15f commit b02ec86

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

bin/v-add-sys-pma-sso

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ fi
109109

110110
$BIN/v-change-sys-config-value 'PHPMYADMIN_KEY' "$phpmyadminkey"
111111

112+
if [ "$(echo $API_ALLOWED_IP | grep 127.0.0.1)" != "127.0.0.1" ]; then
113+
$BIN/v-add-sys-api-ip "127.0.0.1"
114+
fi
115+
112116
#----------------------------------------------------------#
113117
# Logging #
114118
#----------------------------------------------------------#

bin/v-revoke-api-key

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ source_conf "$HESTIA/conf/hestia.conf"
2424

2525
hash=$1
2626

27+
args_usage='HASH'
28+
check_args '1' "$#" "$args_usage"
29+
is_format_valid 'hash'
30+
2731
# Perform verification if read-only mode is enabled
2832
check_hestia_demo_mode
2933

web/api/index.php

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,49 @@
22
define('HESTIA_CMD', '/usr/bin/sudo /usr/local/hestia/bin/');
33
//die("Error: Disabled");
44

5+
function check_local_ip($addr){
6+
if(in_array($addr, array($_SERVER['SERVER_ADDR'], '127.0.0.1'))){
7+
return true;
8+
}else{
9+
return false;
10+
}
11+
}
12+
513
function get_real_user_ip(){
614
$ip = $_SERVER['REMOTE_ADDR'];
7-
if(isset($_SERVER['HTTP_CLIENT_IP'])){
15+
if(isset($_SERVER['HTTP_CLIENT_IP']) && !check_local_ip($_SERVER['HTTP_CLIENT_IP'])) {
16+
if (filter_var($_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP)){
817
$ip = $_SERVER['HTTP_CLIENT_IP'];
18+
}
919
}
10-
if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
20+
21+
if(isset($_SERVER['HTTP_X_FORWARDED_FOR']) && !check_local_ip($_SERVER['HTTP_X_FORWARDED_FOR'])) {
1122
if (filter_var($_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP)){
1223
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
1324
}
1425
}
15-
if(isset($_SERVER['HTTP_FORWARDED_FOR'])){
26+
27+
if(isset($_SERVER['HTTP_FORWARDED_FOR']) && !check_local_ip($_SERVER['HTTP_FORWARDED_FOR'])) {
1628
if (filter_var($_SERVER['HTTP_FORWARDED_FOR'], FILTER_VALIDATE_IP)){
1729
$ip = $_SERVER['HTTP_FORWARDED_FOR'];
1830
}
1931
}
20-
if(isset($_SERVER['HTTP_X_FORWARDED'])){
32+
33+
if(isset($_SERVER['HTTP_X_FORWARDED']) && !check_local_ip($_SERVER['HTTP_X_FORWARDED'])) {
2134
if (filter_var($_SERVER['HTTP_X_FORWARDED'], FILTER_VALIDATE_IP)){
2235
$ip = $_SERVER['HTTP_X_FORWARDED'];
2336
}
2437
}
25-
if(isset($_SERVER['HTTP_FORWARDED'])){
38+
39+
if(isset($_SERVER['HTTP_FORWARDED']) && !check_local_ip($_SERVER['HTTP_FORWARDED'])) {
2640
if (filter_var($_SERVER['HTTP_FORWARDED'], FILTER_VALIDATE_IP)){
2741
$ip = $_SERVER['HTTP_FORWARDED'];
2842
}
2943
}
30-
if(isset($_SERVER['HTTP_CF_CONNECTING_IP'])){
31-
if(!empty($_SERVER['HTTP_CF_CONNECTING_IP'])){
32-
if (filter_var($_SERVER['HTTP_FORWARDED'], FILTER_VALIDATE_IP)){
33-
$ip = $_SERVER['HTTP_CF_CONNECTING_IP'];
34-
}
44+
45+
if(isset($_SERVER['HTTP_CF_CONNECTING_IP']) && !check_local_ip($_SERVER['HTTP_CF_CONNECTING_IP'])) {
46+
if (filter_var($_SERVER['HTTP_CF_CONNECTING_IP'], FILTER_VALIDATE_IP)){
47+
$ip = $_SERVER['HTTP_CF_CONNECTING_IP'];
3548
}
3649
}
3750
return $ip;
@@ -47,7 +60,7 @@ function api($hst_hash, $hst_user, $hst_password, $hst_returncode, $hst_cmd, $hs
4760
}
4861
if ( $settings['config']['API_ALLOWED_IP'] != 'allow-all' ){
4962
$ip_list = explode(',',$settings['config']['API_ALLOWED_IP']);
50-
$ip_list[] = '127.0.0.1';
63+
$ip_list[] = '';
5164
if ( !in_array(get_real_user_ip(), $ip_list)){
5265
echo 'Error: IP is not allowed to connect with API';
5366
exit;

0 commit comments

Comments
 (0)