mirror of
https://github.com/openvk/chandler.git
synced 2024-11-15 03:31:12 +03:00
Add CSRF protection options
This commit is contained in:
parent
3cc25bec51
commit
f64b7ad339
2 changed files with 22 additions and 11 deletions
|
@ -15,4 +15,5 @@ chandler:
|
||||||
|
|
||||||
security:
|
security:
|
||||||
secret: ""
|
secret: ""
|
||||||
|
csrfProtection: "permissive"
|
||||||
sessionDuration: 14
|
sessionDuration: 14
|
||||||
|
|
|
@ -74,19 +74,29 @@ class Router
|
||||||
|
|
||||||
private function setCSRFStatus(Route $route): void
|
private function setCSRFStatus(Route $route): void
|
||||||
{
|
{
|
||||||
$GLOBALS["csrfCheck"] = false;
|
if(CHANDLER_ROOT_CONF["security"]["csrfProtection"] === "disabled") {
|
||||||
|
$GLOBALS["csrfCheck"] = true;
|
||||||
$hash = ($_GET["hash"] ?? ($_POST["hash"] ?? false));
|
} else {
|
||||||
if($hash !== false) {
|
$GLOBALS["csrfCheck"] = false;
|
||||||
$data = explode("#", $hash);
|
|
||||||
|
|
||||||
try {
|
$hash = ($_GET["hash"] ?? ($_POST["hash"] ?? false));
|
||||||
if(!isset($data[0]) || !isset($data[1])) throw new \SodiumException;
|
if($hash !== false) {
|
||||||
[$hash, $nonce] = $data;
|
$data = explode("#", $hash);
|
||||||
|
|
||||||
if(sodium_memcmp($this->makeCSRFToken($route, hex2bin($nonce)), "$hash#$nonce") === 0)
|
try {
|
||||||
$GLOBALS["csrfCheck"] = parse_url($_SERVER["HTTP_REFERER"], PHP_URL_HOST) === parse_url($_SERVER["HTTP_HOST"], PHP_URL_HOST);
|
if(!isset($data[0]) || !isset($data[1])) throw new \SodiumException;
|
||||||
} catch(\SodiumException $ex) {}
|
[$hash, $nonce] = $data;
|
||||||
|
|
||||||
|
if(sodium_memcmp($this->makeCSRFToken($route, hex2bin($nonce)), "$hash#$nonce") === 0) {
|
||||||
|
if(CHANDLER_ROOT_CONF["security"]["csrfProtection"] === "permissive")
|
||||||
|
$GLOBALS["csrfCheck"] = true;
|
||||||
|
else if(CHANDLER_ROOT_CONF["security"]["csrfProtection"] === "strict")
|
||||||
|
$GLOBALS["csrfCheck"] = parse_url($_SERVER["HTTP_REFERER"], PHP_URL_HOST) === $_SERVER["HTTP_HOST"];
|
||||||
|
else
|
||||||
|
trigger_error("Bad value for chandler.security.csrfProtection: disabled, permissive or strict expected.", E_USER_ERROR);
|
||||||
|
}
|
||||||
|
} catch(\SodiumException $ex) {}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$GLOBALS["csrfToken"] = $this->makeCSRFToken($route, openssl_random_pseudo_bytes(4));
|
$GLOBALS["csrfToken"] = $this->makeCSRFToken($route, openssl_random_pseudo_bytes(4));
|
||||||
|
|
Loading…
Reference in a new issue