liveBook · Manning

Cross-site request forgery

As explained in Grokking Web Application Security

Cross-site request forgery is a web attack that causes a user's browser to send an unintended authenticated request to a site.

Cross-site request forgery (CSRF) tricks a signed-in user’s browser into sending a request that performs an action the user did not intend. A malicious link can arrive with the user’s cookies attached, so the site treats it as that user’s request. The result can range from unwanted social-media posts to account deletion, payments, or disclosure of personal information. section 2.14 section 6.8

The central risk is confusing a request’s authentication with its intent. Browsers commonly send cookies automatically, while a user may only have clicked a disguised link or followed a redirect. CSRF is therefore an attack on users through the browser rather than a direct attempt to access the server. section 2.14 section 6.0

A comment form that becomes a worm

A comment form using GET can put the comment text in its URL. An attacker can hide that URL behind an innocent-looking link; each victim who follows it posts the attacker’s comment, which can prompt more people to follow the same link. This self-replicating comment is a worm. section 6.8

<form action="/comment/new" method="get">
 <textarea name="comment" 
           placeholder="What's going on?"></textarea>
 <button type="submit">Submit</button>
</form>

This form uses GET for an action that creates a comment, making its effect reproducible through a crafted URL. section 6.8

Reduce CSRF exposure by making request semantics and cookie rules work together:

  • Keep GET requests free of side effects. State-changing actions should use methods such as POST, PUT, or DELETE rather than GET. section 2.14 section 6.18
  • Use antiforgery tokens for CSRF protection. section 6.18
  • Set SameSite on sensitive cookies. SameSite=Strict prevents cookies on cross-site requests; SameSite=Lax allows them on cross-site GET requests but not POST, PUT, or DELETE. section 2.14
  • Explicitly send the SameSite header even though modern browsers default unspecified cookies to Lax, because older browsers may not. section 2.14

How Grokking Web Application Security covers it

How deep it goes
Explains CSRF through a GET comment-worm scenario and gives practical defenses, without detailing antiforgery-token implementation.
Examples
Uses a baking-forum GET comment form, a shortened redirect link, a self-replicating worm, and sensitive actions such as payments and deletions.
What it assumes
Introduces CSRF through links, cookies, and familiar HTTP methods without assuming specialist security knowledge.
What you can do after
Apply it by keeping GET side-effect-free, using antiforgery tokens, and setting SameSite on sensitive cookies.
How it connects
Connects CSRF with cookies, SameSite, HTTP GET, POST, PUT, DELETE, antiforgery tokens, and browser security.

Also explained in

Compare how 5 books explain cross-site request forgery →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Web Application Security. The section links point to the parts of the book it draws on.