forked from hestiacp/hestiacp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprevent_csrf.php
More file actions
151 lines (144 loc) · 3.95 KB
/
prevent_csrf.php
File metadata and controls
151 lines (144 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
$check_csrf = true;
if (
$_SERVER["SCRIPT_FILENAME"] == "/usr/local/hestia/web/inc/mail-wrapper.php" ||
$_SERVER["SCRIPT_FILENAME"] == "/usr/local/hestia//web/inc/mail-wrapper.php"
) {
$check_csrf = false;
} // execute only from CLI
if (
$_SERVER["SCRIPT_FILENAME"] == "/usr/local/hestia/web/reset/mail/index.php" ||
$_SERVER["SCRIPT_FILENAME"] == "/usr/local/hestia/web//reset/mail/index.php"
) {
$check_csrf = false;
} // Localhost only
if (
$_SERVER["SCRIPT_FILENAME"] == "/usr/local/hestia/web/api/index.php" ||
$_SERVER["SCRIPT_FILENAME"] == "/usr/local/hestia/web//api/index.php"
) {
$check_csrf = false;
} // Own check
if (substr($_SERVER["SCRIPT_FILENAME"], 0, 22) == "/usr/local/hestia/bin/") {
$check_csrf = false;
}
function checkStrictness($level) {
if ($level >= $_SESSION["POLICY_CSRF_STRICTNESS"]) {
return true;
} else {
http_response_code(400);
echo "<h1>Potential CSRF use detected</h1>\n" .
"<p>Please disable any plugins/add-ons inside your browser or contact your system administrator. If you are the system administrator you can run v-change-sys-config-value 'POLICY_CSRF_STRICTNESS' '0' as root to disable this check.<p>" .
"<p>If you followed a bookmark or an static link please <a href='/'>navigate to root</a>";
die();
}
}
function prevent_post_csrf() {
if (!empty($_SERVER["REQUEST_METHOD"])) {
if ($_SERVER["REQUEST_METHOD"] === "POST") {
if (!empty($_SERVER["HTTP_HOST"])) {
$hostname = preg_replace(
"/(\[?[^]]*\]?):([0-9]{1,5})$/",
"$1",
$_SERVER["HTTP_HOST"],
);
$port_is_defined = preg_match("/\[?[^]]*\]?:[0-9]{1,5}$/", $_SERVER["HTTP_HOST"]);
if ($port_is_defined) {
$port = preg_replace(
"/(\[?[^]]*\]?):([0-9]{1,5})$/",
"$2",
$_SERVER["HTTP_HOST"],
);
} else {
$port = 443;
}
} else {
$hostname = gethostname();
$port = 443;
}
if (isset($_SERVER["HTTP_ORIGIN"])) {
$origin_host = parse_url($_SERVER["HTTP_ORIGIN"], PHP_URL_HOST);
if (
strcmp($origin_host, gethostname()) === 0 &&
in_array($port, ["443", $_SERVER["SERVER_PORT"]])
) {
return checkStrictness(2);
} else {
if (
strcmp($origin_host, $hostname) === 0 &&
in_array($port, ["443", $_SERVER["SERVER_PORT"]])
) {
return checkStrictness(1);
} else {
return checkStrictness(0);
}
}
}
}
}
}
function prevent_get_csrf() {
if (!empty($_SERVER["REQUEST_METHOD"])) {
if ($_SERVER["REQUEST_METHOD"] === "GET") {
if (!empty($_SERVER["HTTP_HOST"])) {
$hostname = preg_replace(
"/(\[?[^]]*\]?):([0-9]{1,5})$/",
"$1",
$_SERVER["HTTP_HOST"],
);
$port_is_defined = preg_match("/\[?[^]]*\]?:[0-9]{1,5}$/", $_SERVER["HTTP_HOST"]);
if ($port_is_defined) {
$port = preg_replace(
"/(\[?[^]]*\]?):([0-9]{1,5})$/",
"$2",
$_SERVER["HTTP_HOST"],
);
} else {
$port = 443;
}
} else {
$hostname = gethostname();
$port = 443;
}
//list of possible entries route and these should never be blocked
if (
in_array($_SERVER["DOCUMENT_URI"], [
"/list/user/index.php",
"/login/index.php",
"/list/web/index.php",
"/list/dns/index.php",
"/list/mail/index.php",
"/list/db/index.php",
"/list/cron/index.php",
"/list/backup/index.php",
"/reset/index.php",
])
) {
return true;
}
if (isset($_SERVER["HTTP_REFERER"])) {
$referrer_host = parse_url($_SERVER["HTTP_REFERER"], PHP_URL_HOST);
if (
strcmp($referrer_host, gethostname()) === 0 &&
in_array($port, ["443", $_SERVER["SERVER_PORT"]])
) {
return checkStrictness(2);
} else {
if (
strcmp($referrer_host, $hostname) === 0 &&
in_array($port, ["443", $_SERVER["SERVER_PORT"]])
) {
return checkStrictness(1);
} else {
return checkStrictness(0);
}
}
} else {
return checkStrictness(0);
}
}
}
}
if ($check_csrf == true) {
prevent_post_csrf();
prevent_get_csrf();
}