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:
2026-11-24 09:26:15 +00:00
parent d6c06b82d5
commit 2b73242d04
34 changed files with 311 additions and 61 deletions
+36 -12
View File
@@ -22,19 +22,28 @@ class DB
*/
protected PDO $pdo;
/**
* @var string $databaseName
*/
protected string $databaseName;
/**
* DB constructor.
*
*/
public function __construct()
{
// Validate database charset against a whitelist to prevent SQL injection
$allowedCharsets = ['utf8mb4', 'utf8', 'latin1', 'ascii', 'utf8mb3'];
$charset = in_array($_ENV['DATABASE_CHARSET'], $allowedCharsets, true) ? $_ENV['DATABASE_CHARSET'] : 'utf8mb4';
$this->pdo = new PDO(
$_ENV['DATABASE_DRIVER'] . ':host=' . $_ENV['DATABASE_HOST'] . ';dbname=' . $_ENV['DATABASE_DATABASE'] . ";options='--client_encoding=" . $_ENV['DATABASE_CHARSET'] . "'",
$_ENV['DATABASE_DRIVER'] . ':host=' . $_ENV['DATABASE_HOST'] . ';dbname=' . $_ENV['DATABASE_DATABASE'] . ";options='--client_encoding=" . $charset . "'",
$_ENV['DATABASE_USERNAME'],
$_ENV['DATABASE_PASSWORD'],
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
);
$this->pdo->exec("set names " . $_ENV['DATABASE_CHARSET']);
$this->pdo->exec("set names " . $charset);
$this->databaseName = $_ENV['DATABASE_DATABASE'];
}
/**
@@ -57,14 +66,19 @@ class DB
public function all(string $table, array|string $columns = '*'): array
{
try {
// Sanitize table name to prevent SQL injection
$sanitizedTable = phpb_remove_non_alpha_numeric($table);
if (empty($sanitizedTable)) {
throw new PDOException('Invalid table name.');
}
if (is_array($columns)) {
foreach ($columns as &$column) {
$column = preg_replace('/[^a-zA-Z_]*/', '', $column);
}
$columns = implode(',', $columns);
$stmt = $this->pdo->prepare("SELECT $columns FROM $table");
$stmt = $this->pdo->prepare("SELECT $columns FROM $sanitizedTable");
} else {
$stmt = $this->pdo->prepare("SELECT * FROM $table");
$stmt = $this->pdo->prepare("SELECT * FROM $sanitizedTable");
}
$stmt->execute();
return $stmt->fetchAll();
@@ -86,7 +100,8 @@ class DB
*/
public function findWithId(string $table, $id)
{
$stmt = $this->pdo->prepare("SELECT * FROM {$table} WHERE id = ?");
$sanitizedTable = phpb_remove_non_alpha_numeric($table);
$stmt = $this->pdo->prepare("SELECT * FROM {$sanitizedTable} WHERE id = ?");
$stmt->execute([$id]);
return $stmt->fetchAll();
}
@@ -152,13 +167,13 @@ class DB
// check if table with the name $table exists. if not, execute $query.'
try {
$keys = $this->select(
"SELECT CONSTRAINT_NAME FROM information_schema.TABLE_CONSTRAINTS WHERE CONSTRAINT_NAME = ? AND TABLE_SCHEMA = 'plugi' AND TABLE_NAME = ? AND CONSTRAINT_TYPE = 'UNIQUE'",
[$key, $tableName]
"SELECT CONSTRAINT_NAME FROM information_schema.TABLE_CONSTRAINTS WHERE CONSTRAINT_NAME = ? AND TABLE_SCHEMA = ? AND TABLE_NAME = ? AND CONSTRAINT_TYPE = 'UNIQUE'",
[$key, $this->databaseName, $tableName]
);
if (count($keys) === 1) return true;
return false;
} catch (Exception $e) {
print_r($e->getMessage());
// Do not expose database error details to the user
return false;
}
}
@@ -176,7 +191,7 @@ class DB
}
public function getDBDefinitionHash() {
return sha1(json_encode($this->getDBDefinition()));
return hash('sha256', json_encode($this->getDBDefinition()));
}
public function diffDB() {
@@ -207,15 +222,24 @@ class DB
foreach ($diff as $table => $tableDiff) {
$tableName = $_ENV['DATABASE_PREFIX'] . phpb_remove_non_alpha_numeric($table);
if($tableDiff === false){
$this->query("CREATE TABLE {$tableName} (`id` int NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3");
$this->query("CREATE TABLE {$tableName} (`id` int NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4");
$this->query("ALTER TABLE {$tableName} ADD PRIMARY KEY (`id`)");
$this->query("ALTER TABLE {$tableName} MODIFY `id` int NOT NULL AUTO_INCREMENT");
} elseif (is_array($tableDiff)) {
foreach ($tableDiff['columns'] as $column => $columnDefinition) {
$this->query("ALTER TABLE {$tableName} ADD {$column} {$columnDefinition}");
// Validate column name to prevent SQL injection
$sanitizedColumn = phpb_remove_non_alpha_numeric($column);
if (empty($sanitizedColumn)) continue;
// Validate column definition against a safe pattern
if (!preg_match('/^[A-Z\s(),0-9_`]+$/', $columnDefinition)) continue;
$this->query("ALTER TABLE {$tableName} ADD {$sanitizedColumn} {$columnDefinition}");
}
foreach ($tableDiff['uniqueKeys'] as $key => $keyDefinition) {
$this->query("ALTER TABLE {$tableName} ADD UNIQUE KEY {$key} ($keyDefinition)");
// Validate key name and definition
$sanitizedKey = phpb_remove_non_alpha_numeric($key);
if (empty($sanitizedKey)) continue;
if (!preg_match('/^[A-Z,`\s_]+$/', $keyDefinition)) continue;
$this->query("ALTER TABLE {$tableName} ADD UNIQUE KEY {$sanitizedKey} ($keyDefinition)");
}
}
}