Merge pull request 'Store answers atomically and limit word cloud input' (#31) from fix/atomic-votes into main
CI / Deploy / test (push) Successful in 52s
CI / Deploy / deploy (push) Successful in 4s

Reviewed-on: #31
This commit was merged in pull request #31.
This commit is contained in:
gnilebein committed 2026-11-08 22:35:42 +01:00
commit 6912d045cf
11 files changed
+224 -50

No files matched your search

+20
View File
@@ -3,7 +3,9 @@ declare(strict_types=1);
namespace App\Controller;
use App\Service\ThrottleService;
use Cake\Controller\Controller;
use Cake\Core\Configure;
use Cake\Event\EventInterface;
use Cake\Http\Response;
@@ -38,6 +40,24 @@ class AppController extends Controller
->withHeader('Referrer-Policy', 'same-origin');
}
/**
* Registers a hit of the client IP in a throttle bucket (see ThrottleService).
*
* @param string $bucket Throttle bucket
* @param string $configKey Configure key holding the limit (0 = unlimited)
* @param int $windowSeconds Length of the window
* @return bool Whether the request is still allowed
*/
protected function throttle(string $bucket, string $configKey, int $windowSeconds = ThrottleService::WINDOW_SECONDS): bool
{
return (new ThrottleService())->attempt(
$bucket,
(string)$this->request->clientIp(),
(int)Configure::read($configKey, 0),
$windowSeconds,
);
}
/**
* Returns a JSON response without going through the view layer.
*
-15
View File
@@ -7,7 +7,6 @@ use App\Captcha\CaptchaFactory;
use App\Mailer\PollMailer;
use App\Model\Entity\Slide;
use App\Model\Table\PollsTable;
use App\Service\ThrottleService;
use Cake\Core\Configure;
use Cake\Http\Response;
use Cake\Log\Log;
@@ -228,20 +227,6 @@ class HomeController extends AppController
}
}
/**
* @param string $bucket Throttle bucket
* @param string $configKey Configure key holding the hourly limit
* @return bool
*/
protected function throttle(string $bucket, string $configKey): bool
{
return (new ThrottleService())->attempt(
$bucket,
(string)$this->request->clientIp(),
(int)Configure::read($configKey, 0),
);
}
/**
* Sends a mail and logs (instead of throwing) transport errors.
*
+7 -1
View File
@@ -118,8 +118,14 @@ class ParticipateController extends AppController
$slide = $stateService->currentSlide($poll);
$submittedSlideId = (int)$this->request->getData('slide_id');
$ajax = $this->request->is('ajax');
$status = 200;
try {
// Generous per IP (a whole audience may share one Wi-Fi), only stops scripted floods.
if (!$this->throttle('vote', 'Plenario.throttle.votesPerMinute', 60)) {
$status = 429;
throw new VoteException(__('Zu viele Antworten in kurzer Zeit. Bitte warte einen Moment und versuche es dann noch einmal.'));
}
if ($slide === null || $slide->id !== $submittedSlideId) {
throw new VoteException(__('Diese Frage ist nicht mehr aktiv.'));
}
@@ -141,7 +147,7 @@ class ParticipateController extends AppController
'ok' => $ok,
'message' => $message,
'state' => $stateService->forParticipant($poll, $this->participantHash($poll)),
]);
], $status);
}
$ok ? $this->Flash->success($message) : $this->Flash->error($message);