Add honeypot field to poll creation form
CI / Deploy / test (push) Successful in 41s
CI / Deploy / deploy (push) Skipped

An invisible "website" field is rendered off-screen in the create form.
If a bot fills it in, the poll is not created and no mail is sent.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
gnilebeinandClaude Opus 5.5 committed 2026-11-07 14:34:55 +01:00
1 parent 04cbc5bcd3
commit ffe93a3256
5 files changed
+79 -1

No files matched your search

+22 -1
View File
@@ -18,6 +18,12 @@ use Throwable;
*/ */
class HomeController extends AppController 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; protected PollsTable $Polls;
/** /**
@@ -56,7 +62,9 @@ class HomeController extends AppController
$poll = $this->Polls->newEmptyEntity(); $poll = $this->Polls->newEmptyEntity();
$poll = $this->Polls->patchEntity($poll, (array)$this->request->getData(), ['fields' => ['title', 'email']]); $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.')); $this->Flash->error(__('Die Sicherheitsabfrage war nicht korrekt. Bitte versuche es erneut.'));
} elseif (!$poll->hasErrors() && !$this->throttle('create', 'Plenario.throttle.createPerHour')) { } 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.')); $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; 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. * Confirmation page after creating a poll.
* *
+1
View File
@@ -30,6 +30,7 @@ $this->assign('title', __('Live-Umfragen ohne Login'));
'required' => true, 'required' => true,
'autocomplete' => 'email', 'autocomplete' => 'email',
]) ?> ]) ?>
<?= $this->element('honeypot') ?>
<?= $this->element('captcha', ['captcha' => $captcha]) ?> <?= $this->element('captcha', ['captcha' => $captcha]) ?>
<p class="hint"><?= __('Wir schicken dir einen geheimen Admin-Link. Die Adresse wird nur dafür verwendet.') ?></p> <p class="hint"><?= __('Wir schicken dir einen geheimen Admin-Link. Die Adresse wird nur dafür verwendet.') ?></p>
<?= $this->Form->button(__('Umfrage erstellen'), ['class' => 'button button--primary']) ?> <?= $this->Form->button(__('Umfrage erstellen'), ['class' => 'button button--primary']) ?>
+16
View File
@@ -0,0 +1,16 @@
<?php
/**
* Honeypot field against spam bots. Humans never see or reach it (moved
* off-screen, hidden from screen readers, not focusable); bots that fill in
* every field get their request rejected by the controller.
*
* @var \App\View\AppView $this
*/
use App\Controller\HomeController;
?>
<div class="hp-field" aria-hidden="true">
<label for="hp-website"><?= __('Bitte dieses Feld leer lassen') ?></label>
<input type="text" id="hp-website" name="<?= h(HomeController::HONEYPOT_FIELD) ?>" value=""
tabindex="-1" autocomplete="off">
</div>
@@ -60,6 +60,44 @@ class HomeControllerTest extends TestCase
$this->assertResponseNotContains('/admin/', 'Admin link must not be shown when disabled'); $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 public function testCreateRejectsWrongCaptcha(): void
{ {
$this->session(['Captcha.math.create' => 7]); $this->session(['Captcha.math.create' => 7]);
+2
View File
@@ -73,6 +73,8 @@ h3 { font-size: 1.05rem; margin-top: 1.6em; }
.hint--error { color: var(--danger); } .hint--error { color: var(--danger); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; } .sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
[hidden] { display: none !important; } [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 */ /* ---------------------------------------------------------------- layout */
.container { width: 100%; max-width: 1120px; margin: 0 auto; padding: 0 16px; } .container { width: 100%; max-width: 1120px; margin: 0 auto; padding: 0 16px; }