Skip to content
BENIBIX Learn

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.

Development🔐 Security

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.

Example
Emma's blog shows comments as raw HTML. Someone posts a comment with a script tag, and it runs for every reader who opens the post.
Why it matters
A script on your page can do anything the user can. One comment box can expose every account.
Full entry
Development🔐 Security

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.

Example
Jack is logged in to his shop admin. He opens a link from an email, and a hidden form on that page changes his account email to the attacker's.
Why it matters
It turns your own login against you. Any action a user can do by a click can be forged this way.
Full entry

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.