Skip to content
Open

mimi #93

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Tests

on:
push:
branches: [main]
pull_request:
branches: [main]
workflow_dispatch:

permissions:
contents: read

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- name: Install dependencies
run: npm ci
- name: Run tests
run: npm test -- --runInBand
104 changes: 48 additions & 56 deletions Flow.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# Flow — Simulating the CSRF and DoS Attacks

This project ships a **deliberately vulnerable** feature: **Maintenance Requests**
(`/maintenance`). Any logged-in resident can file a request to fix something in a room.
Two things are wrong with it:
This project includes a **Maintenance Requests** feature (`/maintenance`). Any logged-in
resident can file a request to fix something in a room. This flow verifies defenses against
CSRF and request flooding:

1. **CSRF (Cross-Site Request Forgery).** The app authenticates every request with a
**JWT stored in an httpOnly cookie**. The browser attaches that cookie **automatically**
to *every* request to the API — including requests triggered by a completely different
website. So an attacker's page can forge a state-changing request **in the victim's name**
without ever seeing their password or token.
2. **DoS (Denial of Service).** There is **no rate limiting** anywhere, so a single client
can flood the endpoints with unlimited requests and exhaust the server.
website, so state-changing requests also require a session-bound CSRF token that an attacker
page cannot read or guess.
2. **DoS (Denial of Service).** Maintenance endpoints are rate-limited so one client cannot
make unlimited requests in a short time.

Follow the steps to reproduce both, then read *Why it works*, then run the tests.
Follow the steps to verify both protections, then read *Why it works*, then run the tests.

---

Expand All @@ -31,6 +31,7 @@ PORT=4000
MONGO_URI=<your MongoDB connection string>
JWT_SECRET=<any long random string>
JWT_EXPIRES_IN=7d
CSRF_SECRET=<a different long random string>
```

Start the app:
Expand All @@ -50,9 +51,8 @@ Open the client at **http://localhost:5175**.
and note the `token` cookie. It is **HttpOnly** (JavaScript cannot read it) — but the
browser still sends it on every request to the API.
3. Click **Maintenance** and file a request (e.g. room `B2-104`, *plumbing*, "Leaking tap").
This is a legitimate action; the forged one is about to look just like it.

**Leave this browser logged in.** Both attacks rely on the victim's active session.
**Leave this browser logged in.** The demo attempts both attacks against the active session.

---

Expand All @@ -71,74 +71,66 @@ npm start # Attacker site on http://localhost:1337

---

## 3. Spring the CSRF trap
## 3. Verify the CSRF protection

In the **same browser** where you are logged in as the victim, open:

```
http://localhost:1337
```

It looks like a harmless "You won a prize!" page. But on load it **auto-submits a hidden
form** that POSTs to `http://localhost:4000/api/maintenance` with a forged request
(`description=Forged by attacker via CSRF`), targeting a hidden iframe so you see nothing.
It looks like a harmless "You won a prize!" page. On load it auto-submits a hidden form that
attempts to POST a forged maintenance request, targeting a hidden iframe.

Now go back to the app tab and **refresh the Maintenance page**. A request you never made —
`Forged by attacker via CSRF` for room `Z9-666` — is now filed **in your name**. The server
could not tell it apart from a real click.
The POST should be rejected with `403` because the form cannot provide the session's CSRF token.
Go back to the app and refresh Maintenance: no request for room `Z9-666` should appear.

> Want to watch it happen? Open DevTools → **Network** on the attacker page before it loads,
> and you will see the cross-site `POST` to `/api/maintenance` return `201`, with your
> `token` cookie attached under **Request Headers → Cookie**.
In DevTools → **Network**, verify that the POST is refused and does not create a request. CORS
only controls whether an origin may read a response; it does not prevent the browser from sending
the form. The anti-CSRF token is the proof the server checks.

---

## 4. Spring the DoS flood

Still on `http://localhost:1337`, click **"Claim my prize"**. The page fires **hundreds of
`fetch` requests** at `http://localhost:4000/api/maintenance` straight from the victim's
browser.
Still on `http://localhost:1337`, click **"Claim my prize"**. The page fires many `fetch` requests
at `http://localhost:4000/api/maintenance` from the victim's browser.

Open DevTools → **Network** and watch: every request returns `200` and is served in full.
There is nothing stopping one client (or a crowd of victims lured to this page) from
hammering the API until it falls over. **No request is ever refused.**
Open DevTools → **Network** and verify that early requests return `200`, then excess requests
return `429 Too Many Requests`.

---

## 5. Why it works (the vulnerable code path)
## 5. Why the protections work

**CSRF**

1. **Auth is a cookie the browser sends for you** — `server/src/middleware/auth.js` reads the
JWT from `req.cookies.token`. The victim's browser attaches it to the attacker's forged
request automatically; the server cannot tell the difference between that and a real click.
2. **The write trusts the cookie alone** — `server/src/routes/maintenance.js` guards `POST /`
with only `requireAuth`. There is **no proof the request came from our own app**: no
anti-CSRF token.
3. **The body is a plain form** — `server/src/app.js` parses `urlencoded` bodies, so a bare
HTML `<form>` POST (which needs no JavaScript and triggers no CORS preflight) is enough.
4. **CORS does not save you.** CORS only blocks the attacker from *reading* the response; the
browser still *sends* the request and the server still *acts on it*. CSRF is about the
write succeeding, not about reading the reply.
1. **Auth is a cookie the browser sends automatically** — so cookie-authenticated state-changing
requests need additional proof.
2. **The token is session-bound** — `csrf-csrf` signs it using `CSRF_SECRET` and binds it to the
auth cookie. The client reads it from the authenticated `/api/csrf-token` response and sends it
in `x-csrf-token`. An attacker page cannot read that response or set the required header on a
plain HTML form.
3. **Safe methods and pre-auth routes differ** — `GET`, `HEAD`, and `OPTIONS` are ignored by CSRF
protection; login and registration are exempt because there is no session yet. All other
state-changing requests require the token.
4. **CORS does not prevent sending forms.** It only controls whether the attacker can read the
response. The token check prevents the server from acting on the forged write.

> ### Why `SameSite` doesn't save you here
> The auth cookie is `SameSite=Lax`. You might expect that to block a cross-site POST — and
> across true different sites (`evil.com` → `yourbank.com`) it largely would. But the attacker
> page (`localhost:1337`) and the API (`localhost:4000`) are the **same site**: the port is not
> part of a site's identity, only the registrable domain is. So the cookie is sent. This is a
> deliberate lesson: **`SameSite` is a useful layer but not a substitute for a real anti-CSRF
> token.**
> page (`localhost:1337`) and the API (`localhost:4000`) are the **same site**: ports do not
> distinguish sites. The browser may therefore send the `SameSite=Lax` auth cookie with the
> forged request. SameSite is useful defense-in-depth, not a substitute for a CSRF token.

**DoS**

5. **No rate limiting** — `server/src/routes/maintenance.js` applies no limiter, so
`maintenanceController.getAllRequests` runs for *every* request, no matter how many arrive
from one client in how short a time. Each one hits the database.
5. **Rate limiting** — `server/src/routes/maintenance.js` applies a per-IP limit to all
maintenance endpoints, returning `429` after 30 requests in a 15-minute window.

The fixes: require a secret, per-session **CSRF token** on every state-changing request (a
value the attacker's site cannot read or guess), and **rate-limit** the feature so a flood is
refused with `429`. The solution project does this with the `csrf-csrf` and
`express-rate-limit` packages.
Login and registration remain available before a session exists. Configure a separate,
cryptographically random `CSRF_SECRET`; do not reuse the JWT secret.

---

Expand All @@ -151,14 +143,14 @@ npm install # first time only
npm test
```

What to expect **in this vulnerable project**:
The CSRF and DoS tests verify the protected behavior:

- The tests describe the **secure** behavior the feature *should* have: a forged request with
no CSRF token is rejected (server), a flood is throttled to `429` (server), and the client
attaches a CSRF token to mutating requests (client).
- So here they are **expected to FAIL** (the suite is red). That red result is the starting
point — your job is to make them green by adding CSRF protection and rate limiting. The
solution project shows them all passing.
- a forged write without a CSRF token is rejected and does not change data;
- repeated requests to maintenance endpoints are throttled with `429`; and
- the client attaches its CSRF token to mutating requests.

The tests should pass once dependencies are installed and `CSRF_SECRET` is configured in the
server environment (the tests set their own value).

Notes:

Expand Down
49 changes: 16 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ PORT=4000
MONGO_URI=mongodb://tasks:pass1234@ac-j3acrgb-shard-00-00.lueesfz.mongodb.net:27017,ac-j3acrgb-shard-00-01.lueesfz.mongodb.net:27017,ac-j3acrgb-shard-00-02.lueesfz.mongodb.net:27017/?ssl=true&replicaSet=atlas-6to6iy-shard-0&authSource=admin&appName=Cluster0
JWT_SECRET=4a498d90a0f923ae4b3f1f65571ff25985d17e2f3286e8e90383e449f68a40a8
JWT_EXPIRES_IN=7d
CSRF_SECRET=<a different long, random secret>
```

## What was added on top of Task 3
Expand All @@ -87,8 +88,11 @@ JWT_EXPIRES_IN=7d
**httpOnly cookie** (`authController.js`), and `middleware/auth.js` reads the token from
`req.cookies.token`. A `POST /api/auth/logout` route clears the cookie. The browser now sends
the session automatically on every request — which is exactly the precondition for `CSRF`.
- `app.js` now mounts `cookie-parser`, `express.urlencoded` (so plain HTML-form bodies are
parsed), and CORS with `credentials: true` locked to the client origin.
- `app.js` mounts `cookie-parser`, `express.urlencoded`, and CORS locked to the client origin.
- Mutating API requests require a signed, session-bound CSRF token from `GET /api/csrf-token`.
Login and registration remain exempt because no authenticated session exists yet. The client
adds the token header automatically; maintenance endpoints are limited to 30 requests per
client per 15 minutes.
- Added a new model called **`MaintenanceRequest`** with the attributes: `roomNumber`, `category`,
`description`, `status`, `author`.
- Added the **Maintenance controller** with the following routes:
Expand All @@ -113,10 +117,8 @@ JWT_EXPIRES_IN=7d

For this you will **not** need Postman — a `CSRF` attack is carried out by a webpage, not a crafted
API client. The `attacker/` folder is a small, separate Express server that plays the role of a
malicious site the victim visits. Look through the `Flow.md` Markdown file for the detailed steps
to simulate both attacks (register a victim, run the attacker server, open its link, and watch a
maintenance request get silently filed in the victim's name — then click through and watch the same
page flood the API with requests).
malicious site the victim visits. Follow [Flow.md](./Flow.md) to verify that its forged write is
rejected and that its flood eventually receives `429 Too Many Requests`.

Please note that the demo works over `http://localhost` because the attacker's port
(`localhost:1337`) and the API's port (`localhost:4000`) are the **same site** — a cookie's
Expand All @@ -131,42 +133,23 @@ file). After reading the file, read the next section.

<summary>View Task</summary>

The attack in the `Flow.md` file works for two independent reasons.
The demonstration in `Flow.md` shows two attacks that the application now blocks.

First, the write endpoint `POST /api/maintenance` (`maintenanceController.js` +
`routes/maintenance.js`) trusts the session cookie **alone**: there is no proof the request
actually originated from our own app — no anti-`CSRF` token, no check of intent. Since the browser
attaches that cookie to any cross-site request, any page the victim visits can forge a request in
their name.
First, state-changing requests require a signed, session-bound anti-CSRF token in addition to the
session cookie. `GET /api/csrf-token` issues it to authenticated clients, and `csrf-csrf` rejects
forged writes without the matching `x-csrf-token` header.

Second, none of the maintenance endpoints place **any limit on how often one client may call
them**, so the attacker's page can flood `GET /api/maintenance` with unlimited requests and
exhaust the server — a `DoS`.
Second, `express-rate-limit` caps each client's maintenance traffic at 30 requests per 15 minutes.
Excess calls receive `429 Too Many Requests`.

**Fix both.** Make the maintenance feature safe while keeping its legitimate behavior, then re-run
the attacks in `Flow.md` and confirm they now fail (the forged request should return `403` and
leave the data unchanged, and the flood should be refused with `429`).

- **CSRF:** issue a secret, per-session **CSRF token** that the attacker's site cannot read or
guess; require that token on every state-changing request; leave "safe" `GET` requests and the
pre-auth routes (login/register, which run before a session exists) exempt; and echo the token
from the client on each write. Use the **`csrf-csrf`** package (the double-submit-cookie pattern;
the old `csurf` is deprecated). You are expected to understand **why** `CORS` and `SameSite`
cookies do not by themselves close this hole, and to think carefully about which requests need
protection and which must stay exempt.
- **DoS:** throttle the feature so one client cannot flood it. Use the **`express-rate-limit`**
package to return `429 Too Many Requests` once a client exceeds a sensible limit in a time
window.

A test suite ships with the project. Run it from the project root:
A test suite is available through the project setup. Run it from the project root:

```bash
npm install # first time only
npm test
```

The `CSRF` and `DoS` tests describe the **secure** behavior and are **expected to FAIL** in this
project (the suite is red). Your job is to make them green by applying the fix.
The CSRF and DoS tests verify the protections above and should pass.

### Custom Configurations

Expand Down
5 changes: 5 additions & 0 deletions babel.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
presets: [
['@babel/preset-env', { targets: { node: 'current' } }]
]
}
41 changes: 37 additions & 4 deletions client/src/api.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,43 @@
import axios from 'axios'

// `withCredentials: true` makes the browser send the httpOnly auth cookie on
// every request to the API — including requests triggered by a different site.
// That convenience is exactly what leaves this app open to CSRF: there is no
// second proof that a state-changing request actually came from our own UI.
export const api = axios.create({
baseURL: 'http://localhost:4000/api',
withCredentials: true
})

const safeMethods = new Set(['get', 'head', 'options'])
const preAuthPaths = new Set(['/auth/login', '/auth/register'])
const sessionPaths = new Set([...preAuthPaths, '/auth/logout'])
let csrfTokenPromise

api.interceptors.request.use(async config => {
const method = (config.method || 'get').toLowerCase()
const path = (config.url || '').split('?')[0]

if (safeMethods.has(method) || preAuthPaths.has(path)) return config

if (!csrfTokenPromise) {
csrfTokenPromise = api.get('/csrf-token')
.then(res => {
const token = res.data?.csrfToken
if (typeof token !== 'string' || !token) {
throw new Error('CSRF token endpoint returned no token')
}
return token
})
.catch(error => {
csrfTokenPromise = undefined
throw error
})
}

const csrfToken = await csrfTokenPromise
config.headers.set('x-csrf-token', csrfToken)
return config
})

api.interceptors.response.use(response => {
const path = (response.config.url || '').split('?')[0]
if (sessionPaths.has(path)) csrfTokenPromise = undefined
return response
})
20 changes: 20 additions & 0 deletions jest.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module.exports = {
projects: [
{
displayName: 'server',
testEnvironment: 'node',
testMatch: ['<rootDir>/tests/server/**/*.test.js'],
transform: {
'^.+\\.js$': 'babel-jest'
}
},
{
displayName: 'client',
testEnvironment: 'jsdom',
testMatch: ['<rootDir>/tests/client/**/*.test.js'],
transform: {
'^.+\\.js$': 'babel-jest'
}
}
]
}
Loading