Defense in depth: never rely on a single wall
Stack independent layers of protection so that when one fails, and one always does eventually, the next one still stops or slows the attack.
In one sentence
Every control has holes. Line up several independent ones and the holes rarely align: the Swiss cheese model.
The idea
A web application firewall catches most SQL injection payloads, until someone writes a new one. Developers use parameterized queries, until one rushed endpoint doesn't. If either of those is your only protection, one mistake equals a breach.
With layers, the same mistake is a near miss: the payload passes the WAF, but the query is parameterized; or the query is vulnerable, but the database user can't read the passwords table; or it can, but monitoring sees the unusual dump and pages someone.
Typical layers of a web app
| Layer | Examples |
|---|---|
| Edge | CDN, WAF, rate limiting, bot protection |
| Application | Input validation, output encoding, authorization checks, CSRF tokens |
| Data | Parameterized queries, least-privilege DB users, encryption at rest, separate backups |
| Identity | MFA, short sessions, passkeys, admin access from managed devices only |
| Infrastructure | Private networks, security groups, patched images, non-root containers |
| Detection & response | Audit logs, alerts on anomalies, practiced incident runbooks |
Rules of thumb
- Layers must be independent. Two checks that rely on the same input validation function are one layer.
- Prevent, limit, detect. Each threat should ideally have something that stops it, something that limits the damage, and something that notices.
- Don't let layers excuse sloppiness. "The WAF will catch it" is how the WAF becomes the only layer.
Key takeaways
- 01No single control is perfect; stack independent ones.
- 02Cover prevention, damage limitation and detection for each important threat.
- 03Layers only count if they fail independently.
- 04Monitoring and alerts turn a silent breach into a short incident.
Check your understanding
1 of 3
What is the Swiss cheese model?
Related concepts
Least privilege
Assume something will get hacked. Least privilege makes sure that when it does, the attacker gets the keys to one room, not the whole building.
6 min read →SQL injection
If user input is concatenated into a query, a single quote turns data into SQL. Parameterized queries keep data and code apart, so the attacker's text is only ever text.
8 min read →Threat modeling
Threat modeling is answering four questions: what are we building, what can go wrong, what are we going to do about it, and did we do a good job?
8 min read →