Skip to content

Expert · Web attacks and their fixes · topic 23 of 35 · 20 min

CSRF and IDOR

Explanation

CSRF tricks a logged-in user's browser into making a request they did not intend; the fix is anti-CSRF tokens and SameSite cookies. IDOR lets a user reach another user's data by changing an ID; the fix is to check ownership on the server for every request.

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.

Think of it like this: Imagine your signature is automatically stamped on any letter that leaves your house. A trickster slips a pre-written letter into your outbox; it goes out with your stamp. CSRF slips a request into your browser; your session cookie is the stamp.
  • 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.

javascript
// 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);
});
The fix for IDOR: an ownership check on the server for every request.

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.

Careful: IDOR testing reads other records, which may be real personal data. On authorised engagements, prove the flaw with the minimum needed (for example, that a different record is returned) and never collect or keep the data. Out of scope or without permission, this is illegal access.

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 = forged request

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)

Task 1 · Question

CSRF abuses the fact that the browser…

Task 2 · Question

The primary fix for IDOR is…

Task 3 · Code · Python

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

Question 1

1.An anti-CSRF token works because…

Question 2

2.A SameSite=Strict cookie is…

Question 3

3.IDOR is a type of…

Question 4

4.Relying on IDs being hard to guess is…

Read more (free)

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.

From XSS and CSRF: attacks through the browser

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.

From Authorization: who can do what

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.

From A full-stack security review

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. 1 more topic in “Web attacks and their fixes” before its exam: SSRF and authentication flaws.
  2. Module 7 exam: web attacks and fixes · 13 questions · 22 min, without help. 4 topics of this module still to finish first.
  3. Then the next module, Deeper lab skills: passwords, privilege, Metasploit, wireless. 35 topics left in the course before the final exam.

← Previous: Cross-site scripting (XSS)