Blocking attackers inside your Symfony app, without banning your mail provider
Every public Symfony site gets probed all day. Scanners ask for /.env, /.git/config, /wp-login.php. Others try SQL injection, directory traversal, ${jndi: in a header. Most of it goes nowhere, but you only know that afterwards, and only if you look.
The usual answers each have a gap. Reading the web server logs every morning: nobody does it. fail2ban works on log files, outside the application, and knows nothing about your routes, your firewall or which user was signed in. A hosted WAF sees all your traffic and costs money. I wanted something in between, inside the app, where you know which route answered and which status code actually went out. That became Sentinelle, an MIT-licensed bundle that runs on all my sites.
composer require acencyril/sentinelle-bundle
What it does
It records every request, not only the suspicious ones. An attempt is recognised by contrast with ordinary traffic: three 404s are a dead link or a scan, and you can only tell with the page views next to them.
Each request is classified from its path, query string and response code: page view, not found, access denied, probe, exploitation attempt. Critical patterns (code execution, SQL injection, traversal, Log4Shell, PHP deserialisation) block at once. Probes for sensitive files alert in bursts: one probe is noise, fifteen in ten minutes is a scan. Ten access denials in ten minutes is brute force.
Blocks go up step by step: 24 hours on the first strike, 7 days on the second, permanent on the third. Most scanning addresses are compromised machines or recycled cloud addresses, so the first block is short; an address that keeps coming back is doing it on purpose.
The rule that made me write it: never block what you depend on
An address once showed up on my dashboard, flagged after a 401 on a webhook. One click would have blocked it. It belonged to a mail provider: the site's inbound mail would have died silently. The 401 came from a signing key that had not reached the container yet, a deployment issue, not an attack.
So Sentinelle enforces three safeguards:
- Every address carries its owner's name, from reverse DNS. You never decide on a string of digits.
- Critical providers cannot be blocked, automatically or by hand. The button refuses and says why.
- Some paths never trigger a block. A webhook answering 401 while a key is being deployed must not get the caller banned.
# config/packages/sentinelle.yaml
sentinelle:
dry_run: true # detect, log, alert, block nothing: start here
never_block:
ips: '%env(default::SENTINELLE_ALLOWLIST)%' # at least your own outbound address
paths: ['/api/webhook/', '/stripe/callback']
providers: ['.my-signature-provider.com'] # on top of the bundle's own list
A protection that breaks a working part of the site destroys more than it defends.
An address is not a person
Many people share one public address: an office network, a school, a mobile carrier putting thousands of phones behind a few addresses. One infected machine probes your site, the address gets blocked, and everyone behind it is locked out.
So a blocked address that asks for a page from a browser gets a small page instead of a bare 403, with one button. It runs a light proof of work in plain JavaScript, a second or two of hashing, with no third-party captcha and nothing loaded from outside. Solved, the browser gets a signed cookie bound to the address, valid 24 hours, that lets that browser through and nothing else. Sensitive paths stay closed for everyone, pass or not. Scripts don't run the page and don't keep the cookie, so they stay out.
Emails only when something got out
A public site is scanned constantly. With one email per scanner you get a hundred "blocked, nothing served" alerts a week, you learn to ignore them, and the one that matters looks like the other hundred.
Since 0.6, only one kind of alert is emailed at once by default: a breach, meaning a sensitive path (.env, .git, keys, dumps) answered with a 2xx, or a secret found in a response. Not "someone tried": the answer went out. Everything else is still detected, blocked and logged, then counted in one daily digest (sentinelle:digest). A quiet day sends nothing.
A few design decisions
- Logging costs the visitor nothing. It happens on
kernel.terminate, after the response is sent, which is also the only moment the status code is known. - Blocking happens before the router, at priority 300. A banned address costs no route resolution, no session, no database query.
- The anti-flood quota stops writing, not counting. A 155-request scan in 16 seconds does not write 155 rows, but the counters keep running, so the burst threshold still fires.
- Detection patterns can be added, not removed. Whatever you make configurable you make accidentally disableable.
- It fails open. Cache down, database unreachable, mail not sent: the request goes through and the error goes to the logs. A missed block is less serious than a site that turns everyone away.
- Secrets are redacted before writing:
?token=…is stored astoken=***.
It also serves /.well-known/security.txt (RFC 9116) with its expiry date kept fresh, and sends your Content-Security-Policy with a nonce on every page.
What it does not do
It does not inspect request bodies: expensive, and often personal data you should not store. It does not replace your web server, which is still the best place to stop the crudest probes before PHP. And it does not fix an application flaw; it tells you someone is looking for one.
Try it safely
The Flex recipe registers the bundle and creates the config and routes. Then:
php bin/console doctrine:migrations:diff && php bin/console doctrine:migrations:migrate
php bin/console sentinelle:check # cache, tables, allowlist, alert recipient
Schedule sentinelle:purge and sentinelle:digest once a day, and if you run behind nginx, Traefik or a load balancer, set trusted_proxies first, or every visitor will share the proxy's address. Leave dry_run: true for a few days and watch the dashboard: every block that would have happened is logged with its duration. When you agree with what it would have done, switch it off.
The README has the full configuration, in English and French, and the changelog what changed in each version. Issues and feedback are welcome, especially from teams running it behind a proxy or a CDN.
Read next
Votre site a un assistant de discussion. Qui l'a poussé à la faute ?
Le test gratuit LiaScan repère l'assistant de discussion chargé par votre page d'accueil, sans jamais lui écrire. Pourquoi c'est l'agent IA le plus exposé du site.
in FrenchLire la pensée d'une IA ne suffit plus pour la juger
Le raisonnement des IA devient plus dur à surveiller, et une triche peut se cacher d'un contrôle qui lit le texte. Pourquoi je juge les actes d'un agent, pas ses mots.
in FrenchQuand une IA croit que rien n'est réel
Une IA qui se croit testée ne se tient pas toujours mieux : elle peut aussi franchir une ligne en se disant que ce n'est pas réel. Ce que j'ai changé dans mes tests.