security: add CSRF protection and harden core handling
Add CSRF token validation and include tokens in authentication, website manager, and GrapesJS forms and requests. Harden login with password hash verification and session ID regeneration, validate database identifiers and charset, and secure upload MIME detection and filename generation. Update nullable type declarations and PHP syntax across core contracts and modules, switch the Docker image to PHP 8.4, and move the theme script asset to the end of the page. Refresh the PageBuilderBody distribution assets.
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
namespace Plugi\Core;
|
||||
|
||||
class Csrf
|
||||
{
|
||||
/**
|
||||
* Session key for storing CSRF tokens.
|
||||
*/
|
||||
private const SESSION_KEY = 'phpb_csrf_tokens';
|
||||
|
||||
/**
|
||||
* Generate a new CSRF token and store it in the session.
|
||||
*
|
||||
* @return string The generated token
|
||||
*/
|
||||
public static function generateToken(): string
|
||||
{
|
||||
if (session_status() === PHP_SESSION_NONE) {
|
||||
throw new \RuntimeException('Session must be started before generating CSRF tokens.');
|
||||
}
|
||||
|
||||
if (!isset($_SESSION[self::SESSION_KEY])) {
|
||||
$_SESSION[self::SESSION_KEY] = [];
|
||||
}
|
||||
|
||||
$token = bin2hex(random_bytes(32));
|
||||
$_SESSION[self::SESSION_KEY][$token] = time();
|
||||
self::cleanupExpiredTokens();
|
||||
return $token;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate a CSRF token against the session-stored tokens.
|
||||
*
|
||||
* @param string $token The token to validate
|
||||
* @return bool True if the token is valid, false otherwise
|
||||
*/
|
||||
public static function validateToken(string $token): bool
|
||||
{
|
||||
if (empty($token) || !isset($_SESSION[self::SESSION_KEY])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Use hash_equals for timing-safe comparison
|
||||
foreach ($_SESSION[self::SESSION_KEY] as $storedToken => $timestamp) {
|
||||
if (hash_equals($storedToken, $token)) {
|
||||
// Do not remove the token — it is reusable for AJAX requests.
|
||||
// Tokens expire after 1 hour via cleanupExpiredTokens().
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current CSRF token for use in forms.
|
||||
* Generates a new one if none exists.
|
||||
*
|
||||
* @return string The CSRF token
|
||||
*/
|
||||
public static function getToken(): string
|
||||
{
|
||||
if (session_status() === PHP_SESSION_NONE) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (!isset($_SESSION[self::SESSION_KEY]) || empty($_SESSION[self::SESSION_KEY])) {
|
||||
return self::generateToken();
|
||||
}
|
||||
|
||||
// Return the most recent token
|
||||
$keys = array_keys($_SESSION[self::SESSION_KEY]);
|
||||
return end($keys);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove expired tokens from the session.
|
||||
* Tokens expire after 1 hour by default.
|
||||
*/
|
||||
private static function cleanupExpiredTokens(): void
|
||||
{
|
||||
if (!isset($_SESSION[self::SESSION_KEY])) {
|
||||
return;
|
||||
}
|
||||
|
||||
$now = time();
|
||||
$maxAge = 3600; // 1 hour
|
||||
|
||||
foreach ($_SESSION[self::SESSION_KEY] as $token => $timestamp) {
|
||||
if ($now - $timestamp > $maxAge) {
|
||||
unset($_SESSION[self::SESSION_KEY][$token]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user