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:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user