Merge pull request 'Add honeypot field to poll creation form' (#5) from feature/create-honeypot into main
Reviewed-on: #5
This commit was merged in pull request #5.
This commit is contained in:
commit
5eecf28933
5 files changed
+79
-1
No files matched your search
@@ -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.
|
||||
*
|
||||
|
||||
@@ -30,6 +30,7 @@ $this->assign('title', __('Live-Umfragen ohne Login'));
|
||||
'required' => true,
|
||||
'autocomplete' => 'email',
|
||||
]) ?>
|
||||
<?= $this->element('honeypot') ?>
|
||||
<?= $this->element('captcha', ['captcha' => $captcha]) ?>
|
||||
<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']) ?>
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
|
||||
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]);
|
||||
|
||||
@@ -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; }
|
||||
|
||||
Reference in new issue
Block a user