alexandrebulete / ddd-antispam-bundle
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
Requires
- php: ^8.4
- psr/clock: ^1.0
- psr/log: ^3.0
- symfony/config: ^7.0 || ^8.0
- symfony/dependency-injection: ^7.0 || ^8.0
- symfony/form: ^7.0 || ^8.0
- symfony/framework-bundle: ^7.0 || ^8.0
- symfony/http-client-contracts: ^3.0
- symfony/http-foundation: ^7.0 || ^8.0
- symfony/http-kernel: ^7.0 || ^8.0
- symfony/options-resolver: ^7.0 || ^8.0
- symfony/rate-limiter: ^7.3 || ^8.0
- symfony/string: ^7.0 || ^8.0
- twig/twig: ^3.10
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: ^12.0 || ^13.0
- symfony/clock: ^7.0 || ^8.0
- symfony/http-client: ^7.0 || ^8.0
Suggests
- symfony/http-client: Needed by the optional reCAPTCHA v3 check
Provides
None
Conflicts
None
Replaces
None
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