Implemented: editor uploads are constrained to safe image files inside

the upload tree, and public form submissions are
size-bounded and rate-limited
This commit is contained in:
2026-11-28 18:25:41 +01:00
parent cf721b04b0
commit 345a99cf32
11 changed files with 417 additions and 23 deletions
@@ -7,22 +7,44 @@ use Plugi\Extensions\Form\Finishers\FinishersRepository;
class SubmissionHandler
{
public const MAX_REQUEST_BYTES = 65536;
public const MAX_FIELD_BYTES = 16384;
public const MAX_FIELDS = 100;
private $formsRepository;
private $finishersRepository;
private $rateLimiter;
public function __construct($formsRepository = null, $finishersRepository = null)
public function __construct($formsRepository = null, $finishersRepository = null, $rateLimiter = null)
{
$this->formsRepository = $formsRepository ?? new DefinitionsRepository();
$this->finishersRepository = $finishersRepository ?? new FinishersRepository();
$this->rateLimiter = $rateLimiter ?? new SubmissionRateLimiter();
}
public function handle($formId, string $rawData): array
{
if (strlen($rawData) > self::MAX_REQUEST_BYTES) {
return $this->failure(413, ['The submission is too large.']);
}
$formId = filter_var($formId, FILTER_VALIDATE_INT);
if ($formId === false || $formId < 1) {
return $this->failure(400, ['A valid form id is required.']);
}
$clientIp = $_SERVER['REMOTE_ADDR'] ?? '';
if (!is_string($clientIp) || filter_var($clientIp, FILTER_VALIDATE_IP) === false) {
$clientIp = 'unknown';
}
try {
if (!$this->rateLimiter->consume($formId, $clientIp)) {
return $this->failure(429, ['Too many submissions. Please try again later.']);
}
} catch (\Throwable $exception) {
return $this->failure(503, ['Submissions are temporarily unavailable.']);
}
try {
$form = $this->formsRepository->findWithId($formId);
} catch (\Throwable $exception) {
@@ -125,6 +147,10 @@ class SubmissionHandler
$knownFields = [];
$supportedTypes = ['text', 'email', 'textarea', 'checkbox', 'radio', 'select'];
if (count($submittedFields) > self::MAX_FIELDS) {
return ['The submission contains too many fields.'];
}
foreach ($formFields as $field) {
if (!is_array($field) || !isset($field['name']) || !is_string($field['name']) || trim($field['name']) === '' || !isset($field['label']) || !is_string($field['label']) || !isset($field['type']) || !is_string($field['type']) || !in_array($field['type'], $supportedTypes, true)) {
return ['The form contains an invalid field configuration.'];
@@ -160,6 +186,20 @@ class SubmissionHandler
if (!is_string($name) || !array_key_exists($name, $knownFields)) {
return ['The submission contains an unknown field.'];
}
$value = $submittedFields[$name];
if (is_string($value) && strlen($value) > self::MAX_FIELD_BYTES) {
return ['A submitted field is too large.'];
}
if (is_array($value)) {
if (count($value) > self::MAX_FIELDS) {
return ['A submitted field contains too many values.'];
}
foreach ($value as $item) {
if (is_string($item) && strlen($item) > self::MAX_FIELD_BYTES) {
return ['A submitted field is too large.'];
}
}
}
}
foreach ($knownFields as $name => $field) {
@@ -0,0 +1,79 @@
<?php
namespace Plugi\Extensions\Form;
use RuntimeException;
class SubmissionRateLimiter
{
private const MAX_SUBMISSIONS = 5;
private const WINDOW_SECONDS = 600;
private $db;
private string $table;
public function __construct($db = null)
{
global $phpb_db;
$this->db = $db ?? $phpb_db ?? null;
$prefix = preg_replace('/[^a-zA-Z0-9_]/', '', $_ENV['DATABASE_PREFIX'] ?? '');
$this->table = '`' . $prefix . 'ext__form_submission_limits`';
}
public function consume(int $formId, string $clientIp): bool
{
if (!is_object($this->db)) {
throw new RuntimeException('The submission rate limit store is unavailable.');
}
$now = time();
$ipHash = hash('sha256', $clientIp);
if (!$this->db->query(
"INSERT IGNORE INTO {$this->table} (formId, ipHash, startedAt, submissions) VALUES (?, ?, ?, 0)",
[$formId, $ipHash, $now]
)) {
throw new RuntimeException('The submission rate limit could not be initialized.');
}
$allowed = $this->db->transaction(function () use ($formId, $ipHash, $now): bool {
$rows = $this->db->select(
"SELECT startedAt, submissions FROM {$this->table} WHERE formId = ? AND ipHash = ? FOR UPDATE",
[$formId, $ipHash]
);
if (empty($rows)) {
throw new RuntimeException('The submission rate limit could not be loaded.');
}
$startedAt = (int) $rows[0]['startedAt'];
$submissions = (int) $rows[0]['submissions'];
if ($now - $startedAt >= self::WINDOW_SECONDS) {
$startedAt = $now;
$submissions = 0;
}
if ($submissions >= self::MAX_SUBMISSIONS) {
return false;
}
if (!$this->db->query(
"UPDATE {$this->table} SET startedAt = ?, submissions = ? WHERE formId = ? AND ipHash = ?",
[$startedAt, $submissions + 1, $formId, $ipHash]
)) {
throw new RuntimeException('The submission rate limit could not be updated.');
}
return true;
});
if ($allowed && random_int(1, 100) === 1) {
try {
$this->db->query(
"DELETE FROM {$this->table} WHERE startedAt < ?",
[$now - (self::WINDOW_SECONDS * 2)]
);
} catch (\Throwable $exception) {
}
}
return $allowed;
}
}