Cross-Site Request Forgery via XSS
What you'll learn: How attackers combine XSS and CSRF techniques to force authenticated victims to unknowingly perform actions on their behalf.
The Power Combo Attack
You've learned about XSS (executing malicious scripts in a victim's browser) and understand CSRF as a separate attack type. Now we see how XSS makes CSRF defenses nearly useless by creating the ultimate one-two punch.
Here's how it works: Imagine you're logged into your banking site. Normally, CSRF tokens protect you—the bank includes a random, unpredictable token with every sensitive action to verify requests are legitimate. But what if an attacker has XSS on that same banking site?
Through XSS, the attacker's JavaScript runs in the context of the trusted domain. This means the script can:
- Read the CSRF token directly from the page (something external sites can't do due to same-origin policy)
- Craft authenticated requests with the stolen token
- Submit forms or API calls on your behalf—transferring money, changing passwords, deleting data
Why This is So Dangerous
Traditional CSRF defenses rely on attackers being external—unable to read page content due to browser security. XSS breaks this assumption. The attacker's code runs with your full privileges, as if it were the legitimate website's own JavaScript.
Think of it like this: CSRF is someone forging your signature from outside your house. CSRF-via-XSS is someone sneaking into your house, watching you sign documents, then forging your signature with your own pen while you're still sitting there.
The victim sees and trusts the legitimate website—they have no idea malicious actions are occurring in the background.
Key Takeaway: XSS transforms CSRF from a theoretical risk into a practical nightmare by giving attackers the ability to bypass token-based protections and execute authenticated actions silently within the victim's session context.