designwithfocus

Website security

Cross Site Scripting vs CSRF: Two Attacks, Two Fixes

By Design With Focus Team 6 min read
Two labeled paths, a browser script and a forged request

Short answer

Cross-site scripting runs injected code in the trusted site, while CSRF sends a request the victim did not choose. XSS can read cookies and session tokens because the browser treats the script as the site's own. CSRF uses the victim's credentials to change state, and the attacker does not receive the response. Encode output for XSS. Check a CSRF token on state-changing requests.

Last verified against the sources listed below.

Key takeaways

  • XSS is injected code the browser runs as if the trusted site wrote it.
  • CSRF is a forged request that carries the victim's cookies to a site they use.
  • XSS can expose cookies and session tokens and can rewrite the page.
  • CSRF targets state changes. The attacker does not get the response body.
  • Encode untrusted output for XSS. Validate a CSRF token on the server for changes.

A clear split for cross site scripting vs csrf is who acts. One attack runs a script the browser trusts as the site. The other sends a request the user did not mean to send.

OWASP’s XSS page calls cross-site scripting an injection attack. Malicious script is sent through a site to a different user. The browser executes it because it appears to come from a trusted source. OWASP’s CSRF page calls cross-site request forgery an attack that forces an authenticated user to take an action the attacker chose.

Who does the browser trust?

The browser trusts the site that served the page. In XSS, MDN says the attacker gets that site to execute malicious code as though it were part of the website, which breaks the same-origin boundary.

The code can then do what the site’s own code can do. MDN includes reading or changing the loaded page and local storage, and making HTTP requests with the user’s credentials. OWASP adds cookies and session tokens to that list, and says the script can rewrite the HTML page.

CSRF does not need the attacker to run code inside the target page. MDN’s CSRF page says the attacker tricks the user or the browser into making an HTTP request from a malicious site. The request includes the user’s credentials. The server may treat it as intended.

What is taken or changed?

XSS takes data the browser already holds for the site, and it can change what the page shows. OWASP says the most severe attacks disclose the session cookie so the attacker can hijack the session. The harm is the same whether the script was stored or reflected. The difference is how the script arrives.

CSRF changes state on the server by using the victim’s privileges. OWASP says a normal user might be pushed into a state-changing request such as changing an email address. An administrative account can put the whole application at risk. The attacker does not receive the response, so a plain read of private data is not the usual goal.

Question Cross-site scripting CSRF
Who acts The browser runs injected code as the site The browser sends the victim’s credentials
What is at risk Cookies, session tokens, and page content A state change the user is allowed to make
Main fix Context-specific output encoding A CSRF token checked on the server

There is a special CSRF case OWASP calls login CSRF. The attacker forces a signed-out user to sign in to an account the attacker controls. If the victim later adds personal data, the attacker can sign in and view it. That is still not the same as a script running as the site.

Is csrf vs cross site scripting the same?

No, the bugs are not the same, and the fixes are not interchangeable. XSS is untrusted data treated as code. CSRF is a real user’s request that the user did not intend.

OWASP’s prevention notes say XSS happens when an application puts untrusted data into a page so the browser treats it as code. Untrusted data includes form input, URL parameters, data users stored, and responses from other services. The HTML Living Standard says the same class of input must be validated before use and escaped when displayed.

CSRF depends on a different habit of browsers. OWASP says browser requests automatically include credentials such as the session cookie. The site then cannot tell a forged request from a legitimate one.

HTTPS by itself does not stop that. OWASP says HTTPS should still be in place before other CSRF defenses can be trusted.

Website security covers both bugs as part of keeping a site safe. They are neighbors, not duplicates. A token does not encode HTML. Encoding does not prove the user meant to submit a form.

What is the main fix?

The main fix follows the context. For XSS, start with output encoding. The OWASP XSS prevention cheat sheet says to prefer the framework’s automatic encoding, and to pass each variable through an encoder when you are outside that protection.

The right encoding depends on where the data lands. HTML text, attributes, JavaScript, and CSS are different contexts. OWASP says the wrong encoder can leave a weakness.

If the product must accept user-authored HTML, use a maintained sanitizer. Input checks can enforce a format, and they do not replace encoding.

MDN names a second XSS control. A Content Security Policy can tell the browser which scripts are allowed to run, as a backup if encoding fails. MDN also says JSX in React encodes embedded values by default, and that turning that protection off is a separate, unsafe choice.

How do you stop a forged request?

You stop it by proving the request came from your own page. The CSRF prevention cheat sheet says to use the framework’s built-in protection first. If it has none, add CSRF tokens to state-changing requests and validate them on the server.

OWASP says those tokens should be unique per user session, secret, and unpredictable. The server rejects the request when the token is missing or does not match the session. A token must not be placed in the URL, where it can leak. For sites that target modern browsers, the same sheet allows Fetch metadata, especially Sec-Fetch-Site, with a fallback for older clients.

MDN lists the same token idea, plus two more primary defenses. One is checking Fetch metadata. Another is making state-changing calls from script in a way the browser will not treat as a simple cross-site form post.

SameSite on session cookies is extra protection, not a full substitute. OWASP also says a secret cookie alone fails, because cookies are sent anyway.

What does not fix CSRF?

Several familiar ideas do not fix CSRF, and OWASP lists them. Accepting only POST requests fails because a forged form can still submit. A multi-step flow fails if the attacker can predict each step. Checking the Referer header fails in practice because the header can be absent or spoofed.

Putting the session identifier in the URL is not a fix either. OWASP says that exposes the session id. HTTPS does not, by itself, distinguish a forged authenticated request from a real one.

OWASP’s CSRF sheet also warns that cross-site scripting can defeat CSRF mitigations. Fix the encoding bug first. Then keep the token.

How the internet works explains the request path a browser uses. These two bugs sit on that path in different places: one in the page the browser runs, and one in the request the browser attaches credentials to.

Low-contrast text is a separate problem. The Color Contrast Checker flags text that is hard to read. It does not detect XSS or CSRF, and a passing contrast check says nothing about whether output is encoded.

Frequently asked questions

Does a CSRF attack run a script inside the victim's account?

No. OWASP describes CSRF as an attack that tricks an authenticated user into submitting a request. The browser includes cookies automatically, so the site cannot tell the forged request from a real one. The request inherits the victim's privileges and aims at a state change. The attacker does not receive the response. A script that runs inside the page is the XSS case instead.

What does cross-site scripting steal that CSRF usually does not?

OWASP says an XSS script can access cookies, session tokens, or other sensitive information the browser keeps for that site, and it can rewrite the page. The most severe cases disclose the session cookie so the attacker can take over the account. CSRF is aimed at actions, not at reading the response. Forcing a data read does not help, because the victim's browser receives the answer.

What is the main fix for each attack?

For XSS, OWASP says to use the framework's default output encoding so untrusted data is treated as text, and to use a maintained sanitizer when the site must accept HTML. For CSRF, use built-in protection if the framework has it. Otherwise add a CSRF token to state-changing requests and validate it on the server. MDN also lists Fetch metadata as another CSRF defense, with SameSite as extra help.

Can one bug cancel the other fix?

OWASP's CSRF cheat sheet says cross-site scripting can defeat CSRF mitigations, and that tokens are still essential for sites that use cookies for authentication. A token does not replace output encoding. The HTML Living Standard says untrusted input, such as comments or URL parameters, must be validated before use and escaped when displayed. Cover every place untrusted data enters the page. Missing one place leaves the application vulnerable.

Do modern frameworks remove the need to think about this?

They reduce the chance of a mistake, and they do not erase it. OWASP says modern frameworks have fewer XSS bugs because of templating and auto-escaping, but escape hatches can put HTML on the page without that protection. The CSRF cheat sheet says to use built-in CSRF support first. You still have to know where the framework stops, and you still have to avoid unsafe APIs.

Sources