added bot checks

This commit is contained in:
2025-11-16 23:00:21 +10:00
parent 75d958856a
commit 0bcd6f5e86

View File

@@ -14,14 +14,20 @@ class EmailSubscribe extends Component
public bool $success = false; public bool $success = false;
public string $message = ''; public string $message = '';
public string $trap = ''; public string $trap = '';
public int $renderedAt; // unix timestamp
protected $rules = [ protected $rules = [
'email' => 'required|email|max:255', 'email' => 'required|email|max:255',
]; ];
public function mount()
{
$this->renderedAt = now()->timestamp;
}
public function subscribe(): void public function subscribe(): void
{ {
// Honeypot - if this hidden field is filled, treat as success but do nothing // 1. Honeypot - if this hidden field is filled, treat as success but do nothing
if (! empty($this->trap)) { if (! empty($this->trap)) {
$this->reset(['email', 'trap']); $this->reset(['email', 'trap']);
$this->success = true; $this->success = true;
@@ -29,14 +35,30 @@ class EmailSubscribe extends Component
return; return;
} }
// Simple rate limiting per session // 2. Block submits in first 10 seconds after render
// $attempts = session('subscribe_attempts', 0); if (now()->timestamp - $this->renderedAt < 10) {
// if ($attempts >= 5) { $this->success = false;
// $this->success = false; $this->message = 'That was a bit quick. Please wait a few seconds and try again.';
// $this->message = 'Too many attempts. Please try again in a little while.'; return;
// return; }
// }
// session(['subscribe_attempts' => $attempts + 1]); // 3. Enforce 30 seconds between attempts per session
$lastAttempt = session('subscribe_last_attempt');
if ($lastAttempt && now()->diffInSeconds($lastAttempt) < 30) {
$this->success = false;
$this->message = 'Please wait a little before trying again.';
return;
}
session(['subscribe_last_attempt' => now()]);
// 4. Limit to 5 attempts per session (your existing logic)
$attempts = session('subscribe_attempts', 0);
if ($attempts >= 5) {
$this->success = false;
$this->message = 'Too many attempts. Please try again in a little while.';
return;
}
session(['subscribe_attempts' => $attempts + 1]);
$this->validate(); $this->validate();