What is cross-site request forgery (CSRF)?

January 29, 2024

Cross-site request forgery

You have probably heard someone say, “Don’t click that link, it might give you a virus!” What could go wrong just from following a link?

Consider a real incident from 2007. David opened a message in Gmail and followed a link. A few days later, he discovered that someone had stolen his domain name. After considerable effort he recovered it and learned that the theft could be traced back to that click.

csrf_attack0alt

  1. David had already signed in to Gmail. His browser held the cookies associated with his session, so Gmail recognized subsequent requests from that browser.
  2. While that session was active, David followed a link to another site. In the incident described here, a change he had never authorized caused Gmail to forward his mail to an address controlled by someone else.
  3. The loss of control over his mail also compromised account recovery at the domain registrar. His domain was transferred to the other party.

CSRF stands for cross-site request forgery. A third-party site causes a browser to make a request that uses the visitor’s existing login session at another site. The receiving site can mistake that request for an action the user intended.

A typical CSRF incident involves the following sequence of events:

  1. A visitor signs in to site A. The browser retains the session cookie.
  2. The visitor is drawn to site B.
  3. The browser receives a request originating at B but addressed to A. Depending on the cookie settings, the browser can include credentials for A.
  4. A recognizes the visitor’s credentials and mistakes possession of those credentials for evidence that the visitor intended the action.
  5. A carries out the action under the visitor’s account.
  6. The visitor may remain unaware that someone else caused the action.

Characteristics of CSRF

  • The request generally originates on another website. The receiving site cannot control what that site initiates.
  • CSRF abuses credentials the victim already holds to submit an action in their name. Its immediate effect is an unauthorized action.
  • The other party need never learn the victim’s login credentials. The browser supplies them.
  • Browsers can initiate cross-site requests in several ways, including through image URLs, hyperlinks, CORS requests, and form submissions.

Reposted from

© 2024 borui. All rights reserved. This content may be freely reproduced, displayed, modified, or distributed with proper attribution to borui and a link to the article: borui (2024-01-29). What is cross-site request forgery (CSRF)?. https://caiborui.com/blog/2024-01-29-zh-%E4%BB%80%E4%B9%88%E6%98%AFCSRF%E6%94%BB%E5%87%BB-entrans

@misc{borui2024-75eaae5d,
  author = {borui},
  title = {{What is cross-site request forgery (CSRF)?}},
  year = {2024},
  publisher = {borui's blog},
  url = {https://caiborui.com/blog/2024-01-29-zh-%E4%BB%80%E4%B9%88%E6%98%AFCSRF%E6%94%BB%E5%87%BB-entrans}
}