What Is Cross-Site Request Forgery (CSRF), and How Can You Prevent It? (Explained Clearly)
CompTIA Security+ SY0-801: Cybersecurity Fundamentals
Learn how a CSRF attack uses an active login to make unwanted changes, how tokens and SameSite cookies help prevent it, and what to check in audit logs.
👉 Master your Security+ skills with Pluralsight here: Pluralsight
You’ll learn how cross-site request forgery (CSRF) uses an active login to trigger unwanted account changes and how CSRF tokens and SameSite cookies help prevent them. This beginner-friendly Security+ guide also shows what to look for in audit logs when a change wasn’t authorized.
Chapters:
0:00 Intro
0:13 How does cross-site request forgery work?
0:59 Why doesn’t the browser just block CSRF?
2:14 What actually prevents CSRF?
3:18 Outro
Affiliate notice: Some links may earn a commission. Thanks for your support.
- A valid session identifies the account, not the user’s intent. In classic CSRF, a signed-in browser can send an unintended request with its session cookie attached, without anyone stealing the cookie.
- CORS alone doesn’t prevent CSRF. A browser can send some cross-origin requests even when the originating site can’t read the response.
- Validate CSRF tokens before changing data. Use your framework’s built-in protection on state-changing routes, keep
GETread-only, and treatSameSitecookies as an extra layer, not a replacement for validation. - Trace unexpected changes from audit events to request logs. Match the account and timestamp, then check the route, HTTP method, validation result, and whether the change took effect. Log validation outcomes, not raw tokens or cookies.
- 👉 Put your cybersecurity knowledge into practice with Pluralsight: build skills in guided labs, get feedback, and use practice exams to focus your certification prep.
Cross-site request forgery (CSRF, also called XSRF) tricks a browser into sending a request the user didn’t intend. In a classic CSRF attack, the browser is already signed in to a trusted application and automatically attaches a valid session cookie. If the application treats that cookie as enough reason to carry out a change, it can act without the user’s approval.
The distinction is simple: a valid session tells the application which account is signed in, not whether the user intended this particular action.
How a CSRF attack works: The shipping address example
Imagine you’re signed in to an online store. The store uses a session cookie so your browser doesn’t have to ask you to sign in again on every page. While you’re still signed in, another site causes your browser to send the store a request to change your shipping address.
If the browser’s cookie rules allow it, the browser attaches your real session cookie to the request . The other site doesn’t need to see or steal the cookie. The store checks that you’re signed in and that your account can edit its own address. Both checks pass. But if the store doesn’t check for evidence that the request came through an expected application flow, it may save an address you never approved.
FAQ
Quick answers to the questions people usually ask about this topic.
Do REST APIs need CSRF protection?
Do REST APIs need CSRF protection?
Yes, if a browser can authenticate requests that change data using a cookie it attaches automatically. An API that accepts only a bearer token explicitly added to the Authorization header is generally not vulnerable to classic CSRF. Review the credentials each endpoint actually accepts, including any fallback to cookie authentication.
Do HTTPS and HttpOnly cookies prevent CSRF?
Do HTTPS and HttpOnly cookies prevent CSRF?
No. HTTPS protects requests in transit, and HttpOnly prevents page scripts from reading the cookie. Neither tells the browser whether you intended a request or stops it from attaching an eligible cookie. Use server-side CSRF validation and an appropriate SameSite setting. See the OWASP CSRF Prevention Cheat Sheet.
Does logging out prevent CSRF or undo an unauthorized change?
Does logging out prevent CSRF or undo an unauthorized change?
Logging out can prevent later requests from using that session if the application invalidates it on the server. It won't undo a change that's already been saved. If you notice an unauthorized change, review recent account activity, correct the change if possible, and report it to the service.
What should a regression test check after a CSRF fix?
What should a regression test check after a CSRF fix?
Check that a legitimate request still succeeds, then confirm that requests with missing or invalid CSRF proof are rejected. Verify that the underlying data stays unchanged in both rejection cases. An error response alone doesn't prove the server rejected the request before saving the change.
How can I practice applying CSRF concepts while studying for Security+?
How can I practice applying CSRF concepts while studying for Security+?
If you're ready to move beyond reviewing definitions, explore guided security labs and assessments with Pluralsight. Use the feedback to identify which web security concepts need more practice, then use practice exams to plan your next study session.
Comments
Add a comment...Pinned by @jaymayor
@jaymayor👉 Master your Security+ skills with Pluralsight here: https://www.jaymayor.com/go/security-plus
Some links may earn a commission. Thanks for your support.
