You have probably read a news story like this: the data of millions of an app's users leaked and was sold on a forum. Usually it is not the work of a genius hacker. The cause is often simple: one admin page that someone forgot to lock, or one form that accepted whatever the user typed without checking it. An ordinary developer wrote that code, someone like you.
So web security starts with the code you write. Your job is to make that code hard for other people to misuse. Think of a house. A door that closes is not enough. You also add a lock, curtains and lights, and you hope a neighbour watches it too. One protection is easy to get past. Many protections make a burglar give up. In a web app, the doors are endpoints: addresses on the server (the computer that runs your app) that can be opened from outside, such as /admin. Every endpoint is one way in.
Why this matters to you
Every day, automatic programs called bots scan the internet for security gaps. A bot checks big and small apps alike. If there is a gap, it will find it. The damage is real:
- user data leaks,
- the company is fined and loses people's trust,
- you, who wrote the code, have to deal with it too.
That is why security is not an extra feature to add later. It is part of how we write every line of code.
You will often see one term: vulnerability. A vulnerability is a gap or weakness in software that an attacker (someone who breaks in on purpose) can use to get in without permission or to damage the system. When you hear "this app has a vulnerability", it means a door was left unlocked.
CIA Triad: three things we protect
Information security protects three things. Their short name is CIA. It is not the intelligence agency, but the first letters of three words:
- Confidentiality: only people who are allowed can see the data. Other people's passwords, chats and card numbers must stay hidden. We protect it with encryption (the data is scrambled, and only someone with the key can read it) and access control (rules about who may open what).
- Integrity: nobody can change data in secret. Balances, prices and transaction results must be exactly what they should be. We protect it with hashing (a fingerprint of the data: change the data a little, and the fingerprint changes too) and digital signatures (proof that the data really comes from its sender).
- Availability: the system works when people need it. If someone floods a server with requests (messages from browsers) until it stops working, users can't use it. That is a security problem too.
So the letter C stands for Confidentiality.
The developer is the first door
Look at the admin endpoint below. All it does is res.send(adminPanel): it sends the admin panel and checks nothing. Before you read on, guess: who can open it?
// VULNERABLE: anyone who knows the URL can open the admin panel
app.get('/admin', (req, res) => {
res.send(adminPanel)
})
The answer: everyone. The name of this problem is: there is no authentication, which means nothing checks who is asking. Anyone who types /admin in a browser gets the panel right away. They don't need to log in, and they don't need to be an admin.
The fixed version adds two checks before the panel is sent:
// SAFE: check login first, then check whether they are an admin
app.get('/admin', requireLogin, requireAdmin, (req, res) => {
res.send(adminPanel)
})
The functions run in order, from left to right:
requireLoginrefuses a guest who is not logged in, with status 401.requireAdminrefuses a regular user who is logged in but is not an admin, with status 403.- The panel is sent only when both checks pass.
requireLogin checks who you are. That is called authentication. requireAdmin checks whether you are allowed to open this page. That is called authorization.
Defence in depth: don't rely on one layer
Defence in depth means using several layers of protection. If one layer fails, the next one still holds.
Try it in the Lab. There, a guest sends the request GET /admin, and you can switch on four layers one at a time: requireLogin, requireAdmin, rateLimit and auditLog. With every layer off, the guest reaches the admin data right away. As soon as requireLogin is on, the guest stops at that layer.
What each layer does:
requireLoginandrequireAdminguard access.rateLimitlimits the number of tries, so nobody can keep guessing passwords again and again.auditLogrefuses nothing, but it records every refused attempt. If someone attacks, you will know.
No system is one hundred percent safe. So the goal is to make an attack cost far more than what the attacker could gain.
⚠️ Beliefs that get apps broken into
- "A small app won't be attacked." Wrong. Bots scan addresses automatically. To a bot, a small app and a famous app are the same.
- An endpoint with no check. Hiding the admin URL does not protect it, because an address can be guessed. Always add authentication and authorization.
- Trusting raw user input. Treat input (data from outside: forms, URLs, headers and cookies) as dangerous until you have checked it.
- Keeping secrets inside the code. A password or API key written in the code can be uploaded to a public repo, for example on GitHub, and stay leaked forever. Keep it in an environment variable (a value stored outside the code, in the server's settings).
TL;DR: Web security starts with every line of code you write, because bots (automatic programs) look for gaps every day and the damage from a leak is real. A vulnerability is a gap an attacker can use. The CIA Triad is the three things we protect: Confidentiality, Integrity and Availability. An endpoint (an address on the server) like GET /admin with no check has no authentication (no check of who is asking), so anyone can get in. Add requireLogin, then requireAdmin. Defence in depth means using many layers: if one fails, another still holds.
⚡ Bots don't pick their victims
Guess first: is a small, unknown app safe from attacks?
No. Bots (automatic programs) scan addresses on the internet every day. If there is a gap, they find it.
Meaning: A gap in software that an attacker can use is called a vulnerability. Security is not an extra feature. It is how we write every line of code.
⚡ Three things we protect
Meaning: The CIA Triad. Confidentiality: only people who are allowed can see the data. Integrity: nobody can change data in secret. Availability: the system works when people need it.
⚡ An endpoint with no check
Write:
app.get('/admin', (req, res) => {
res.send(adminPanel)
})
Guess first: who can open this /admin?
Everyone. There is no authentication (no check of who is asking), so anyone who types /admin gets the panel right away.
Meaning: This endpoint (an address on the server) checks nothing before res.send sends the panel. Hiding the admin URL does not protect it.
⚡ Two doors, in layers
Write:
app.get('/admin', requireLogin, requireAdmin, (req, res) => {
res.send(adminPanel)
})
Meaning: requireLogin refuses a guest (not logged in) with a 401: it checks who you are. requireAdmin refuses a non-admin with a 403: it checks whether you are allowed. Using several layers like this is called defence in depth: if one fails, another still holds.