XSS vs CSRF
Cross-site scripting and cross-site request forgery both abuse the trust between you and a site, in opposite ways. XSS puts the attacker's script on the real site, where it runs as if the site wrote it. CSRF stays on the attacker's page. It tricks your browser into sending a request to the real site. Safe output stops XSS. Tokens and SameSite cookies stop CSRF.
Cross-Site Scripting (XSS)
An attack that hides a script in a page. It runs for everyone who visits.
Cross-site scripting gets a bad script to run on pages others visit. It happens when a site shows user text, like a comment, without making it safe first. The browser then treats the text as code. The script runs as if the site wrote it, so it can steal session cookies or act as the user. The fix is to escape all user text before showing it.
Cross-Site Request Forgery (CSRF)
An attack that tricks your browser. It sends a request you never meant to send.
Cross-site request forgery tricks your browser into sending a request. You are logged in to your bank in one tab. A bad page in another tab quietly sends a form to the bank. Your browser adds your login cookie, so the bank thinks it was you. The fix is a secret token on every form, plus cookies marked SameSite.
In one line
- Cross-Site Scripting (XSS)
- An attack that hides a script in a page. It runs for everyone who visits.
- Cross-Site Request Forgery (CSRF)
- An attack that tricks your browser. It sends a request you never meant to send.