Every Security Layer I Add Assumes the One Before It Already Failed
Why I stack security layers in a backend only I use: what each one assumes, which mistake it survives, why explaining how it works shouldn't weaken it, and what layering still cannot fix.
There’s a check in my backend that refuses to start if the database user in the config file is postgres. I wrote it knowing it should never fire. The deployment files don’t use that user, the database setup creates a separate role for the application, and I’m the only person who edits any of it.
So why write it? And why, on top of it, row-level security in the database, secrets in files, a scan for leaked credentials before every commit, and a network that only my own devices can reach, for an application whose only user is me?
The case against
The honest objection is that this is overkill. A personal backend isn’t a bank. Every layer is code to write, configuration to keep right, and one more thing that can break at the worst moment. And there’s a quieter version of the objection, the one people rarely say out loud: nobody knows this thing exists, so who would attack it?
The first half deserves a real answer, and I’ll come back to it. The second half I don’t accept. Security that depends on nobody knowing how the system works is called security through obscurity, and it fails the day someone finds out. Kerckhoffs wrote the principle for cryptography in the 19th century, and it holds just as well for a backend: a system should stay secure even if the attacker knows everything about it except the keys. It’s also the reason I can write posts like this one. Explaining why a layer exists shouldn’t weaken it. If it does, the layer was never doing its job.
Layers that prevent, layers that limit
The distinction that made this click for me is that layers don’t all do the same kind of work. Some try to stop a mistake from happening. Others assume it already happened and limit how far it can spread:
| Layer | Kind | The failure it survives |
|---|---|---|
| A network reachable only from my own devices | prevent | a weak spot in the app is still out of reach for strangers |
| Strong authentication in front of everything | prevent | someone on the network still isn’t me |
| An application database role with the minimum rights | limit | a bug or a compromised dependency can’t change the schema or switch off the policies |
| Row-level security | limit | a query that forgot a filter still only sees one user’s rows |
| Secrets in files, never in env vars or logs | limit | a crash report or a log line doesn’t carry the password |
| A secret scan before every commit | prevent | a password pasted into a test doesn’t reach the repository |
The “limit” layers are the ones that feel paranoid, and they’re the ones I’d keep if I had to drop half the list. Prevention fails in ways you don’t see coming. Containment is what decides whether that failure costs a bad afternoon or every row in the database.
Why one layer is never enough
Each layer is designed as if the one before it had already failed. The database role doesn’t assume authentication worked, and row-level security doesn’t assume the backend wrote the right query. That’s the whole idea, and it only works if the layers fail independently.
If they share one point of failure, they’re one layer. Row-level security that depends on the backend setting the user correctly is a real layer against a forgotten filter, and no layer at all against a backend that sets the wrong user. The check against the postgres user exists because row-level security means nothing for a role that bypasses it, which I wrote about in the post on Postgres roles. The layers lean on each other, and the useful exercise is finding out exactly where.
What layering doesn’t fix
Back to the first half of the objection, because it’s partly right.
Layers don’t help if the machine I write the code on is compromised: whoever controls it controls every layer at once. They don’t help against a bug in the one component everything trusts. And they have a cost beyond the time to build them. Each layer is something I have to understand, and a configuration I don’t fully understand is a risk of its own. A layer I set up by copying instructions and never tested is worse than no layer, because it makes me feel safe.
There’s also an asymmetry I don’t have an answer to yet. Every layer I add makes the system harder to attack and harder to debug. So far the trade has been worth it. I don’t know where it stops being worth it, and I’m suspicious of anyone who claims to know in general.
Where I stand
I build the layers because I don’t trust my own code, and that isn’t modesty: I know I make mistakes. A previous post on this site is about an [Authorize] attribute that protected nothing, a layer everyone believed in and nobody had checked. So each layer I add has to answer one question: if the layer before it has already failed, what does this one still save? If the answer is nothing, it doesn’t get built. If the answer is “the data”, it does, even in a backend with a single user.