Skip to content

Commit d30f96c

Browse files
asmccasmccjaapmarcus
authored
IPV6 compatible prevent CSRF (hestiacp#3491)
* IPV6 compatible prevent CSRF * IPV6 compatible prevent CSRF. Code style correction * Fix formatting --------- Co-authored-by: asmcc <root@test.hestiacp.com> Co-authored-by: Jaap Marcus <9754650+jaapmarcus@users.noreply.github.com>
1 parent 3347cf9 commit d30f96c

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

web/inc/prevent_csrf.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,19 @@ function prevent_post_csrf() {
4040
if (!empty($_SERVER["REQUEST_METHOD"])) {
4141
if ($_SERVER["REQUEST_METHOD"] === "POST") {
4242
if (!empty($_SERVER["HTTP_HOST"])) {
43-
[$hostname, $port] = explode(":", $_SERVER["HTTP_HOST"] . ":");
44-
if (empty($port)) {
43+
$hostname = preg_replace(
44+
"/(\[?[^]]*\]?):([0-9]{1,5})$/",
45+
"$1",
46+
$_SERVER["HTTP_HOST"],
47+
);
48+
$port_is_defined = preg_match("/\[?[^]]*\]?:[0-9]{1,5}$/", $_SERVER["HTTP_HOST"]);
49+
if ($port_is_defined) {
50+
$port = preg_replace(
51+
"/(\[?[^]]*\]?):([0-9]{1,5})$/",
52+
"$2",
53+
$_SERVER["HTTP_HOST"],
54+
);
55+
} else {
4556
$port = 443;
4657
}
4758
} else {
@@ -74,8 +85,19 @@ function prevent_get_csrf() {
7485
if (!empty($_SERVER["REQUEST_METHOD"])) {
7586
if ($_SERVER["REQUEST_METHOD"] === "GET") {
7687
if (!empty($_SERVER["HTTP_HOST"])) {
77-
[$hostname, $port] = explode(":", $_SERVER["HTTP_HOST"] . ":");
78-
if (empty($port)) {
88+
$hostname = preg_replace(
89+
"/(\[?[^]]*\]?):([0-9]{1,5})$/",
90+
"$1",
91+
$_SERVER["HTTP_HOST"],
92+
);
93+
$port_is_defined = preg_match("/\[?[^]]*\]?:[0-9]{1,5}$/", $_SERVER["HTTP_HOST"]);
94+
if ($port_is_defined) {
95+
$port = preg_replace(
96+
"/(\[?[^]]*\]?):([0-9]{1,5})$/",
97+
"$2",
98+
$_SERVER["HTTP_HOST"],
99+
);
100+
} else {
79101
$port = 443;
80102
}
81103
} else {

0 commit comments

Comments
 (0)