Implement CSRF token by default

Implement CSRF protection on CMS for postback handling
This commit is contained in:
Samuel Georges 2017-10-30 08:59:19 +11:00
parent 08989ff40a
commit 4a6e0e1e0e
2 changed files with 30 additions and 1 deletions

View File

@ -332,7 +332,7 @@ return [
|
*/
'enableCsrfProtection' => false,
'enableCsrfProtection' => true,
/*
|--------------------------------------------------------------------------

View File

@ -337,6 +337,7 @@ class Controller
if (
$useAjax &&
($handler = post('_handler')) &&
($this->verifyCsrfToken()) &&
($handlerResponse = $this->runAjaxHandler($handler)) &&
$handlerResponse !== true
) {
@ -1355,4 +1356,32 @@ class Controller
}
}
}
//
// Security
//
/**
* Checks the request data / headers for a valid CSRF token.
* Returns false if a valid token is not found. Override this
* method to disable the check.
* @return bool
*/
protected function verifyCsrfToken()
{
if (!Config::get('cms.enableCsrfProtection')) {
return true;
}
if (in_array(Request::method(), ['HEAD', 'GET', 'OPTIONS'])) {
return true;
}
$token = Request::input('_token') ?: Request::header('X-CSRF-TOKEN');
return hash_equals(
Session::token(),
$token
);
}
}