Expert · Web attacks and their fixes · topic 23 of 35 · 20 min
CSRF and IDOR
Explanation
CSRF: riding on a logged-in session
Cross-site request forgery (CSRF) abuses the fact that a browser automatically sends your cookies with every request to a site. If you are logged in to your bank and then visit a malicious page, that page can quietly make your browser send a request to the bank — say, a transfer — and your cookie rides along, so the bank thinks you meant it.
- Fix: anti-CSRF tokens — a secret, per-session value the server includes in its forms and checks on submission. A malicious site cannot know the token, so its forged request fails.
- Fix: SameSite cookies — set cookies to SameSite=Lax or Strict so the browser does not send them on cross-site requests.
- Detection: requests that change state but lack a valid token, or come with an unexpected Origin/Referer.
IDOR: changing the ID
Insecure direct object reference (IDOR) is a kind of broken access control. The app uses an identifier (like ?id=7) to fetch a record, but does not check that the record belongs to the logged-in user. Change 7 to 8 and you see someone else's data. It is one of the most common and serious real-world flaws, and one of the easiest to find — which you did in the Burp lesson.
// Vulnerable: returns any order by id
app.get('/api/orders/:id', requireLogin, async (req, res) => {
res.json(await db.getOrder(req.params.id));
});
// Fixed: check the order belongs to this user
app.get('/api/orders/:id', requireLogin, async (req, res) => {
const order = await db.getOrder(req.params.id);
if (!order || order.userId !== req.user.id) return res.status(404).end();
res.json(order);
});Detection and defence
Detection for IDOR: logs showing one account accessing many different record IDs, especially sequential ones. Defence: enforce access control on the server for every object, every time; never rely on the ID being hard to guess; and consider using identifiers that are not sequential (like random UUIDs) as a small extra hurdle — but the real fix is always the ownership check.
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
CSRF and IDOR · 1/7
CSRF tricks a logged-in browser into sending a request the user did not intend.
Prefer a video by a teacher? Find free videos on “csrf and idor explained with fixes web security” on YouTube ↗
In real life: Two flaws, two fixes
Testing a lab app, a learner finds that a "change email" form has no CSRF token, so a crafted page could change a victim's email while they are logged in — a CSRF flaw. She also finds GET /invoice?id=1002 returns any invoice regardless of owner — an IDOR flaw. She writes both up: add an anti-CSRF token and SameSite=Lax for the first; add a server-side ownership check for the second. Each attack is paired with its exact fix.
Practice (0/3 done)
CSRF abuses the fact that the browser…
The primary fix for IDOR is…
Model the IDOR fix. Write can_access(user_id, record_owner_id) that returns True only when the two ids are equal (the user owns the record), else False.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- SSRF and authentication flawsWeb attacks and their fixes · same module
- XSS and CSRF: attacks through the browserBackend Development · Backend security
- Authorization: who can do whatBackend Development · Authentication and authorization
- Sessions and cookiesBackend Development · Authentication and authorization
- A full-stack security reviewFull-Stack Development · Performance, security and testing
- Front-end security: HTTPS, XSS, CSP and secretsFrontend Web Development · Fast, accessible, findable and safe: quality for real sites
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
An anti-CSRF token works because…
Answer: A malicious site cannot know the secret token
The forged request cannot include the unknown token, so it fails.
A SameSite=Strict cookie is…
Answer: Not sent on cross-site requests
SameSite limits when the cookie is sent across sites.
IDOR is a type of…
Answer: Broken access control
IDOR is broken access control: missing ownership checks.
Relying on IDs being hard to guess is…
Answer: Not enough; you still need an ownership check
Obscure IDs are a weak hurdle; the server must still check ownership.
In a CSRF attack, what does the attacker use?Related
Answer: Your browser automatically sending your logged-in cookie
CSRF works because the browser attaches your cookie to a request that another site started.
What is IDOR?Related
Answer: Trusting an id from the request without checking the user may access that record
Insecure Direct Object Reference: changing /orders/1043 to /orders/1044 shows someone else's data.
What must the server do when a user logs out?Related
Answer: Destroy the session in the session store
Deleting the session on the server makes any copy of the old session id useless.
From Sessions and cookies
Which cookie setting stops JavaScript on the page from reading the session cookie?Related
Answer: HttpOnly
HttpOnly hides the cookie from document.cookie, so an XSS script cannot steal it. Secure means HTTPS only; SameSite helps against CSRF.
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 1 more topic in “Web attacks and their fixes” before its exam: SSRF and authentication flaws.
- Module 7 exam: web attacks and fixes · 13 questions · 22 min, without help. 4 topics of this module still to finish first.
- Then the next module, Deeper lab skills: passwords, privilege, Metasploit, wireless. 35 topics left in the course before the final exam.