diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index 169be55..a9a861a 100644 --- a/src/Controller/HomeController.php +++ b/src/Controller/HomeController.php @@ -18,6 +18,12 @@ use Throwable; */ class HomeController extends AppController { + /** + * Name of the invisible honeypot field in the "create poll" form + * (see templates/element/honeypot.php). Must stay empty. + */ + public const HONEYPOT_FIELD = 'website'; + protected PollsTable $Polls; /** @@ -56,7 +62,9 @@ class HomeController extends AppController $poll = $this->Polls->newEmptyEntity(); $poll = $this->Polls->patchEntity($poll, (array)$this->request->getData(), ['fields' => ['title', 'email']]); - if (!$captcha->verify($this->request)) { + if ($this->isHoneypotFilled()) { + $this->Flash->error(__('Die Umfrage konnte nicht angelegt werden.')); + } elseif (!$captcha->verify($this->request)) { $this->Flash->error(__('Die Sicherheitsabfrage war nicht korrekt. Bitte versuche es erneut.')); } elseif (!$poll->hasErrors() && !$this->throttle('create', 'Plenario.throttle.createPerHour')) { $this->Flash->error(__('Zu viele Umfragen in kurzer Zeit. Bitte versuche es später noch einmal.')); @@ -88,6 +96,19 @@ class HomeController extends AppController return null; } + /** + * True if the invisible honeypot field was filled in, i.e. the request + * most likely comes from a bot. + * + * @return bool + */ + protected function isHoneypotFilled(): bool + { + $value = $this->request->getData(self::HONEYPOT_FIELD); + + return $value !== null && $value !== ''; + } + /** * Confirmation page after creating a poll. * diff --git a/templates/Home/index.php b/templates/Home/index.php index aeb1603..83dbc73 100644 --- a/templates/Home/index.php +++ b/templates/Home/index.php @@ -30,6 +30,7 @@ $this->assign('title', __('Live-Umfragen ohne Login')); 'required' => true, 'autocomplete' => 'email', ]) ?> + element('honeypot') ?> element('captcha', ['captcha' => $captcha]) ?>

Form->button(__('Umfrage erstellen'), ['class' => 'button button--primary']) ?> diff --git a/templates/element/honeypot.php b/templates/element/honeypot.php new file mode 100644 index 0000000..0978fe9 --- /dev/null +++ b/templates/element/honeypot.php @@ -0,0 +1,16 @@ + + diff --git a/tests/TestCase/Controller/HomeControllerTest.php b/tests/TestCase/Controller/HomeControllerTest.php index 8b1af5d..e630d21 100644 --- a/tests/TestCase/Controller/HomeControllerTest.php +++ b/tests/TestCase/Controller/HomeControllerTest.php @@ -60,6 +60,44 @@ class HomeControllerTest extends TestCase $this->assertResponseNotContains('/admin/', 'Admin link must not be shown when disabled'); } + public function testIndexRendersHiddenHoneypot(): void + { + $this->get('/'); + $this->assertResponseContains('class="hp-field" aria-hidden="true"'); + $this->assertResponseContains('name="website"'); + $this->assertResponseContains('tabindex="-1"'); + } + + public function testCreateRejectsFilledHoneypot(): void + { + $this->session(['Captcha.math.create' => 7]); + $this->post('/create', [ + 'title' => 'Retro', + 'email' => 'owner@example.org', + 'captcha_answer' => '7', + 'website' => 'https://spam.example', + ]); + + $this->assertResponseOk(); + $this->assertResponseContains('Die Umfrage konnte nicht angelegt werden.'); + $this->assertSame(0, $this->pollsTable()->find()->count()); + $this->assertNoMailSent(); + } + + public function testCreateAcceptsEmptyHoneypot(): void + { + $this->session(['Captcha.math.create' => 7]); + $this->post('/create', [ + 'title' => 'Retro', + 'email' => 'owner@example.org', + 'captcha_answer' => '7', + 'website' => '', + ]); + + $this->assertRedirect(['_name' => 'created']); + $this->assertSame(1, $this->pollsTable()->find()->count()); + } + public function testCreateRejectsWrongCaptcha(): void { $this->session(['Captcha.math.create' => 7]); diff --git a/webroot/css/plenario.css b/webroot/css/plenario.css index 26ac3af..b444af5 100644 --- a/webroot/css/plenario.css +++ b/webroot/css/plenario.css @@ -73,6 +73,8 @@ h3 { font-size: 1.05rem; margin-top: 1.6em; } .hint--error { color: var(--danger); } .sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; } [hidden] { display: none !important; } +/* Honeypot: off-screen instead of display:none, so bots still see and fill it. */ +.hp-field { position: absolute; left: -10000px; top: auto; width: 1px; height: 1px; overflow: hidden; } /* ---------------------------------------------------------------- layout */ .container { width: 100%; max-width: 1120px; margin: 0 auto; padding: 0 16px; }