Store answers atomically and limit word cloud input
- Run the checks and the insert of VoteService::submit() in one transaction with the slide row locked, so double clicks or retries cannot store a vote twice (also covers the Q&A upvote budget and word cloud duplicates). - Limit word cloud terms per request and per participant, and throttle answers per IP and minute (THROTTLE_VOTES_PER_MINUTE, 429 with message). - Treat array values in text fields as empty instead of storing "Array". - Move the throttle helper to AppController for reuse. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
8b0e82d806
commit
9c532d164f
11 files changed
+224
-50
No files matched your search
@@ -7,7 +7,8 @@ use Cake\Cache\Cache;
|
||||
|
||||
/**
|
||||
* Very small fixed-window rate limiter backed by the "throttle" cache config.
|
||||
* Used to limit poll creation and recovery mails per client IP.
|
||||
* Used to limit poll creation and recovery mails per client IP (per hour) and
|
||||
* participant answers per client IP (per minute).
|
||||
*/
|
||||
class ThrottleService
|
||||
{
|
||||
@@ -19,9 +20,10 @@ class ThrottleService
|
||||
* @param string $bucket Logical action, e.g. "create"
|
||||
* @param string $identifier Client identifier, e.g. IP address
|
||||
* @param int $limit Allowed hits per window; <= 0 disables throttling
|
||||
* @param int $windowSeconds Length of the window
|
||||
* @return bool
|
||||
*/
|
||||
public function attempt(string $bucket, string $identifier, int $limit): bool
|
||||
public function attempt(string $bucket, string $identifier, int $limit, int $windowSeconds = self::WINDOW_SECONDS): bool
|
||||
{
|
||||
if ($limit <= 0) {
|
||||
return true;
|
||||
@@ -31,7 +33,7 @@ class ThrottleService
|
||||
$now = time();
|
||||
/** @var array{start: int, hits: int}|null $state */
|
||||
$state = Cache::read($key, 'throttle');
|
||||
if (!is_array($state) || $state['start'] + self::WINDOW_SECONDS <= $now) {
|
||||
if (!is_array($state) || $state['start'] + $windowSeconds <= $now) {
|
||||
$state = ['start' => $now, 'hits' => 0];
|
||||
}
|
||||
if ($state['hits'] >= $limit) {
|
||||
|
||||
Reference in new issue
Block a user