Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 38 ยท Common Vulnerability Workflows

CSRF and CORS Misconfiguration Testing

Learn how to test CSRF defences (tokens, SameSite, origin checks) and CORS headers with Burp Repeater, and evaluate a CORS policy with a small Python model.

Two browser-security mechanisms are often misunderstood and often misconfigured: CSRF protection and CORS. They are related because both concern requests that cross from one website to another, but they solve opposite problems. CSRF defences stop a hostile page from making your browser do things. CORS controls which other sites may read responses. This lesson shows how to test each with Burp.

Burp screens are labelled example output; the Python blocks are offline stand-ins. Test only authorised systems.

CSRF in one minute

Cross-site request forgery works because browsers attach cookies automatically. If you are logged in to bank.example, a request to bank.example/transfer sent from another site's page still carries your session cookie. If the server accepts it with no further proof, the attacker's page can make you transfer money without ever seeing the response.

For CSRF to be possible, three things must hold:

  1. A state-changing action (change email, transfer, delete).
  2. Cookie-based (automatic) authentication.
  3. No unpredictable value the attacker cannot know in the request.

Defences to look for

Defence How it works Test
Anti-CSRF token A random per-session value in a hidden field or header, checked on every unsafe request Remove it, change it, use another user's token
SameSite cookies The browser omits the cookie on cross-site requests (Lax for most, Strict stricter) Inspect Set-Cookie; check browser default behaviour
Origin / Referer check Server verifies the request came from its own site Remove or change the headers
Custom header requirement X-Requested-With or a JSON content type forces a CORS preflight Try a form-encoded body instead of JSON
Re-authentication Password or MFA for sensitive actions Check the sensitive flows

CSRF testing workflow

  1. Find a state-changing request in Proxy history (POST /account/email).
  2. Send it to Repeater and confirm it works.
  3. Test the token: remove it, blank it, change one character, use a token from another test account, reuse an old token. A secure server rejects all of these.
  4. Test the method: change POST to GET; some frameworks accept either and a GET is far easier to trigger cross-site.
  5. Test headers: delete Referer and Origin; change them to another domain.
  6. Check cookie attributes: is SameSite set? Without it modern browsers default to Lax-like behaviour, but a Lax cookie is still sent on top-level GET navigation, so state change on GET stays dangerous.
  7. Burp Suite Professional can generate a proof-of-concept HTML form (right-click, Engagement tools, Generate CSRF PoC). Open it in a browser logged in as your test user to confirm. Do this only for your own accounts.
TEXT
Example (labelled): CSRF token tests on POST /account/email
Original request                      -> 200 "Email updated"
Token removed                         -> 200 "Email updated"     <-- no enforcement
Token changed                         -> 200 "Email updated"     <-- not validated
Verdict: CSRF token is present but ignored.

A frequent finding is exactly this: a token that is generated but never checked, or checked only when present.

A token validator model

This stand-in compares a correct validator against one with common mistakes, running the same test list you would run in Repeater.

Python CSRF token checks: correct and flawed (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The two buggy validators fail different rows; recognising which row fails tells you what the programmer got wrong.

CORS in one minute

Browsers enforce the same-origin policy: a script on a.example cannot read responses from b.example. CORS (cross-origin resource sharing) is a controlled relaxation: b.example sends Access-Control-Allow-Origin headers naming the origins allowed to read its responses. A server that is too generous lets any website's script read data using the victim's cookies.

Important headers:

  • Access-Control-Allow-Origin (ACAO): which origin may read.
  • Access-Control-Allow-Credentials: true: allow cookies to be sent and the response to be read.
  • Access-Control-Allow-Methods, -Headers: for preflight requests.

The dangerous combination is a reflected, attacker-controlled origin plus credentials. The browser forbids Access-Control-Allow-Origin: * together with credentials, so careless servers copy the incoming Origin into the header instead.

CORS testing workflow

  1. Find an authenticated API request that returns sensitive data.
  2. In Repeater, add Origin: https://evil.example and send. Look at the response headers.
  3. If ACAO echoes your origin and Allow-Credentials: true, any site can read this data from a logged-in victim.
  4. Try variations: Origin: null (sandboxed iframes send this), a sub-domain of the real domain (https://x.target.example), the real domain with a suffix (https://target.example.evil.example), and a prefix (https://eviltarget.example). Loose regex or endsWith matching accepts these.
  5. Note whether HTTP origins are trusted for an HTTPS API.
  6. Check preflight (OPTIONS) responses for overly permissive methods and headers.
TEXT
Example (labelled): CORS probe
> GET /api/profile   Origin: https://evil.example
< Access-Control-Allow-Origin: https://evil.example
< Access-Control-Allow-Credentials: true
Verdict: arbitrary origin reflected with credentials -> readable cross-site

An origin matcher model

Python Allow-list matching: strict versus sloppy (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Only the first origin should be allowed. The endswith check wrongly approves eviltarget.example, which an attacker can register.

Fixes

  • CSRF: use the framework's built-in token middleware, validate on every unsafe method, tie tokens to the session, set cookies SameSite=Lax or Strict, never use GET for changes, and re-authenticate for critical actions.
  • CORS: keep a strict allow-list of exact origins, never reflect the Origin header, do not allow null, avoid credentials unless required, and do not rely on CORS for server-side security (it only guides browsers).

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why does CSRF work?

    With cookie authentication, a request triggered by another site still carries the victim's session.
  2. 2. A form's CSRF token is removed and the request still succeeds. What does that show?

    An enforced token causes a rejection when missing or wrong.
  3. 3. Which CORS response is dangerous?

    Reflecting arbitrary origins with credentials lets any site read authenticated responses.
  4. 4. What does CORS control?

    CORS relaxes the same-origin policy for reading responses; it is not a server-side access control.
  5. 5. Which origin check is safest?

    Exact matching avoids suffix, prefix and sub-domain tricks.

Summary

  • CSRF abuses automatic cookies; defend with validated, session-bound tokens, SameSite cookies, origin checks, and no state change on GET.
  • Test CSRF by removing, altering and swapping tokens, switching methods, and altering headers.
  • CORS governs cross-origin reads; test by sending crafted Origin headers and watching ACAO and Allow-Credentials.
  • Use exact allow-lists, never reflect Origin, and never treat CORS as authentication.
  • The Python blocks are offline stand-ins that mirror the checks.

Next: SSRF and out-of-band testing with Collaborator