Search by

alexandrebulete / ddd-antispam-bundle

Alexandre Buleté

Spam checks for public forms, without a third party: trap field, signed timing token, rate limit per address, rules on links and banned words — Google reCAPTCHA v3 optional.

Package info

github.com/AlexandreBulete/ddd-antispam-bundle

Type:symfony-bundle

pkg:composer/alexandrebulete/ddd-antispam-bundle

Statistics

Installs: 75

Dependents: 2

Suggesters: 0

Stars: 0

Open Issues: 0

1.0.0 2026-11-26 14:53 UTC

This package is auto-updated.

Last update: 2026-11-26 16:09:26 UTC


README

Spam checks for the public forms of a Symfony site — a contact form, comments — without a third party by default:

  • a trap field a person never fills;
  • a signed timing token: a person needs a few seconds to write, a robot posts at once or replays an old form; the signature covers the form's purpose, so a token taken from one form does not pass another;
  • a rate limit per address, one limiter per form;
  • Google reCAPTCHA v3, invisible, when enabled — on top of the others;
  • rules on the text: a number of links, banned words.

Installation

composer require alexandrebulete/ddd-antispam-bundle

Guarding a form

$builder->add('antispam', AntispamType::class, ['purpose' => 'comment']);
$policy = new SpamPolicy('comment', $commentLimiter, minSeconds: 3, maxAge: 86400);
$verdict = $spamGuard->judge(Submission::fromForm($form->get('antispam'), $request), $policy);
Verdict Meaning What to do
Clean passed every check keep
Suspicious reCAPTCHA scored it low (or could not be reached) keep, flagged for a person
Robot trap filled, too fast, forged or replayed drop, and answer as if it went through
TooMany too many submissions from this address refuse, and say so

$commentLimiter is a RateLimiterFactoryInterface: declare one per form under framework.rate_limiter.

Render the form with form_rest() (or form_row(form.antispam)): the trap hides itself. With reCAPTCHA, include @DddAntispam/recaptcha.html.twig once per page, after the forms.

Rules on the text

$reasons = ContentInspector::reasons($text, new ContentRules(maxLinks: 2, bannedWords: ['casino']));

The reasons ("3 links", banned word "casino") are for a moderator: a text that breaks a rule is held, not dropped. Banned words match whole words, accents and case aside.

Configuration

antispam:
    recaptcha:
        enabled: false
        site_key: '%env(RECAPTCHA_SITE_KEY)%'
        secret: '%env(RECAPTCHA_SECRET)%'
        min_score: 0.5

reCAPTCHA loads a Google script and sends Google the visitor's address: mention it in the privacy policy.

Development

composer qa    # phpstan (max, strict) + phpunit

License

MIT