We are building on top of the Dorm Room Booking variant from Task 3. Previously we
implemented JWT authentication; now the session is stored in a cookie and a new
maintenance requests feature has been added. Let us first start with some context.
A CSRF attack is a security vulnerability where a malicious website causes a victim's browser
to send a state-changing request to an application the victim is already logged into —
without the victim's knowledge or consent. The attacker never sees the victim's password or
session token; they simply abuse the fact that the browser automatically attaches the victim's
credentials (most commonly a session cookie) to every request bound for that application,
whoever triggered it.
Unlike injection or XSS, the attacker runs no code inside the target's origin. They do not
read any of the victim's data either — a CSRF attack is "blind." It only needs the victim's
request to succeed, riding on their ambient, authenticated session. That is why any sensitive,
state-changing endpoint authenticated only by a cookie is a target.
- An authenticated session in a cookie: The victim logs into the application. The server hands the browser a session cookie, which the browser stores and then sends on every subsequent request to that site — automatically.
- A forged cross-site request: The victim visits a page the attacker controls (a phishing link, a forum post, a malicious ad). That page quietly issues a state-changing request to the target application — classically a hidden, auto-submitting HTML form.
- The browser attaches the cookie: Because the request is bound for the target site, the browser attaches the victim's session cookie. The server sees a valid session and executes the request as if the victim intended it — changing data on their behalf.
A DoS attack is a security vulnerability where an attacker makes an application slow or
unavailable to its legitimate users by overwhelming it with more work than it can handle. There
is no session to hijack and nothing is changed on anyone's behalf; the goal is simply to exhaust
a finite resource — CPU, memory, database connections, network bandwidth — by sending far more
requests, or far more expensive requests, than the server was provisioned for.
The attacker needs no credentials and reads no data; they only need the requests to keep arriving. Any endpoint that does real work on every call — a database query, a file read, a hash computation — and places no limit on how often one client may call it is a target. The simplest form is a flood: a single client (or a crowd of them) hammers the endpoint as fast as it can.
- An endpoint that does work and has no limit: The server exposes a route that runs a query (or other real work) on every call, and never caps how many calls one client may make in a given window.
- A flood of requests: The attacker fires hundreds or thousands of requests in a short burst — from a script, or even from JavaScript on a webpage running in the browsers of many lured visitors.
- The server tries to serve them all: With nothing to refuse the excess, every request competes for the same CPU, memory and database connections. Legitimate users' requests queue behind the flood and time out — the service is effectively down.
npm install # Install concurrently and the dependencies of the projects
npm run install:all # For safety
npm run dev # API on :4000, client on :5175Create the server/.env file.
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=7dView details
- Auth moved from the
Authorizationheader to a cookie. Instead of returning theJWTin the response body for the client to keep inlocalStorage,login/registernow set it in an httpOnly cookie (authController.js), andmiddleware/auth.jsreads the token fromreq.cookies.token. APOST /api/auth/logoutroute clears the cookie. The browser now sends the session automatically on every request — which is exactly the precondition forCSRF. app.jsnow mountscookie-parser,express.urlencoded(so plain HTML-form bodies are parsed), and CORS withcredentials: truelocked to the client origin.- Added a new model called
MaintenanceRequestwith the attributes:roomNumber,category,description,status,author. - Added the Maintenance controller with the following routes:
GET /api/maintenance: returns all maintenance requests (public).GET /api/maintenance/:id: returns a single maintenance request (public).POST /api/maintenance: files a new maintenance request for the logged-in user. Filing a request is a sensitive, state-changing action done in the user's name.PATCH /api/maintenance/:id,DELETE /api/maintenance/:id: update or delete your own request (others' requests return403).
api.jsnow sends requests withwithCredentials: true(so the auth cookie rides along) and no longer stores a token inlocalStorage.AuthContextrestores the session viaGET /api/auth/me.- Added the
Maintenance.jsxpage (and a Maintenance nav link) for viewing and filing maintenance requests.
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).
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
SameSite attribute keys off the registrable domain, not the port, so the SameSite=Lax auth
cookie is still sent. This is a deliberate lesson: SameSite is a useful layer but not a
complete defense, which is why a real anti-CSRF token is required (details in the Flow.md
file). After reading the file, read the next section.
View Task
The attack in the Flow.md file works for two independent reasons.
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.
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.
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"
GETrequests 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 thecsrf-csrfpackage (the double-submit-cookie pattern; the oldcsurfis deprecated). You are expected to understand whyCORSandSameSitecookies 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-limitpackage to return429 Too Many Requestsonce a client exceeds a sensible limit in a time window.
A test suite covers the maintenance route's CSRF checks and rate limit, plus the client's CSRF header interceptor. Run it from the project root:
npm install # first time only
npm testThe tests use mocked authentication and maintenance handlers, so they do not need a database.
They verify that invalid CSRF requests are rejected, valid tokens are accepted, safe GET
requests remain available, excessive maintenance requests receive 429, and the client sends
the CSRF header on mutating requests.
Sometimes when working in teams, the team may have custom configurations (like automatic tasks) that run immediately after booting up VSCode. The reason these automatic tasks run on startup is because of a folder called .vscode; this folder store custom configurations for VSCode and it is normally present on the C-Drive, assuming a Windows user. However, that folder can also be present in project directories, like the one you are currently using. Before cloning any GitHub Repository that has automatic takes or custom configurations in general you should always verify these configurations before cloning, since they might contain malicious code that targets your machine. For example, I could have a backdoor open that periodically queries my server, attacker server, and I could, at any point, send a command that shuts down your PC, among other options. The second part of this task for you is to find the piece of code that is creating the dialog box issue and inform your TA afterwards.