Why Web Security Matters

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…

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:

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:

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:

  1. requireLogin refuses a guest who is not logged in, with status 401.
  2. requireAdmin refuses a regular user who is logged in but is not an admin, with status 403.
  3. 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:

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

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.

What you will learn