Skip to content
engineering workbench.by StudyPort Academy
Learning path
Advanced18 min

CSRF, XSS, and cookie security

Understand the idea

XSS runs the attacker's script on your origin. CSRF runs the victim's browser at your origin.

The idea

is the forged note. A title stored as text and later written as HTML is parsed as markup. The img tag in the run is split so the lesson source stays text. After escaping, the angle bracket is gone and the browser would show the characters instead of fetching an image that runs script. That script is your origin. It can read the DOM and localStorage. It cannot read an HttpOnly cookie, which is why the token lives there, and why escaping is still required: the script can act as the user without stealing the cookie. is the mailed form. The browser would attach the cookie on its own. SameSite=Lax does not attach it to a cross-site POST, which is the first print. A same-site POST still carries the cookie, so the form token has to match the cookie token. A mismatch is the block. Secure means the cookie is not sent on plain HTTP. None of these replace the ownership check.

What changes

Two attacks, two directions
AttackWhat actually movesWhat stops it
XSSAttacker's markup runs as your originEscape text, and do not put tokens in localStorage
CSRFVictim's browser sends their cookie to youSameSite=Lax on cross-site POST, plus a token on the requests you accept
Cookie theft via scriptdocument.cookieHttpOnly, so that read is empty

The command

Escape the title, withhold the cross-site cookie

3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.

The mistake

Check the result

The title line is escaped. The cross-site POST is browser withheld cookie. The same-site POST with matching tokens is allowed. The same-site POST with nope versus abc is csrf blocked.

Guided challenge

Compare the form token

The token check is currently skipped, so a missing form token is allowed. Return csrf blocked when formToken and cookieToken differ.

Compare the form token

3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.

EXPECTED RESULT

escaped
browser withheld cookie
allowed
csrf blocked

Hints 0/2

    Free exploration

    Change this example and run it again. Each run is a new process. Nothing is saved.

    Your variation

    3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01What does XSS get that a cross-site form does not?
    02What does SameSite=Lax change for a cross-site POST?

    Ready to call this one understood?

    Mark this lesson complete to earn 200 XP. You can always revisit it.

    Sign in to save progress
    Next lesson