Course contentsShow
← Computer Science
Lesson 2472 of 2,87255. Web SecurityFree lesson

State-Changing Operations and Safe Methods

Ensuring GET requests are idempotent and never cause state changes to prevent CSRF via simple link/image attacks.

State-Changing Operations and Safe Methods

You'll learn why GET requests must never modify data and how this design principle prevents trivial CSRF attacks.

The Core Problem

In web applications, HTTP methods have specific intended purposes. Safe methods like GET and HEAD should only retrieve information—never create, modify, or delete data. State-changing operations (creating accounts, transferring money, deleting records) should always use methods like POST, PUT, PATCH, or DELETE.

Why does this matter for security? Because GET requests are the easiest to trigger without user consent. An attacker can embed them in:

  • Image tags: <img src="https://bank.com/transfer?to=attacker&amount=1000">
  • Simple links: Clicking an innocent-looking URL executes the request
  • CSS backgrounds, iframes, or any resource reference

If your application performs state changes via GET, an attacker can execute CSRF attacks with nothing more than convincing a logged-in user to visit a malicious page. No JavaScript required, no form needed—just a single HTML tag.

The Safe Design Pattern

Idempotent and read-only GET: Your GET endpoints should be safe to call multiple times with identical results and zero side effects. A user refreshing a page, a search engine crawler, or a prefetching browser should never accidentally trigger account deletions.

State changes require POST or other non-safe methods: These require intentional form submissions or AJAX requests, which can be protected with CSRF tokens, proper headers, and the SameSite cookie attribute.

Think of it like the difference between looking at your bank balance (safe, repeatable) versus transferring money (requires explicit action and verification).

Key Takeaway:
Always use safe HTTP methods (GET, HEAD) for read-only operations and reserve POST/PUT/DELETE for state changes—this architectural decision prevents entire classes of trivial CSRF exploits.