ELI5: What are Cross-Site Scripting and Cross-Site Request Forgery?

These are two tricks that bad guys play on websites. Cross-site scripting is like someone taping a fake “Push this button for candy” sign on a school wall — when kids push it, something bad happens. The website accidentally shows the attacker’s trick to everyone who visits. Cross-site request forgery is different — it’s like someone secretly sending a letter to the principal with your name on it, asking to do something you never agreed to. Both attacks trick your web browser into doing things you didn’t mean to do.

Overview

Cross-Site Scripting (XSS) and Cross-Site Request Forgery (CSRF) are client-side web vulnerabilities that exploit the trust relationship between users and web applications. XSS injects malicious scripts into web pages viewed by other users. CSRF tricks authenticated users into performing unintended actions on a web application. Both are critical topics on the Security+ exam and appear in the OWASP Top 10.

Key Concepts

XSS (Cross-Site Scripting)

  • Reflected XSS: Malicious script is included in a URL parameter and reflected back in the server’s response — requires victim to click a crafted link
  • Stored (Persistent) XSS: Malicious script is permanently stored on the target server (e.g., in a forum post) — affects all users who view the content
  • DOM-based XSS: Script executes by modifying the DOM in the victim’s browser without server involvement
  • Impact: Session cookie theft, account hijacking, defacement, keylogging, phishing via injected forms
  • Defenses: Output encoding/escaping, Content Security Policy (CSP) headers, input validation, HttpOnly cookie flag

CSRF (Cross-Site Request Forgery)

  • Mechanism: Attacker crafts a request (e.g., hidden form or image tag) that performs an action using the victim’s authenticated session
  • Example: A hidden image tag that triggers a bank transfer while the victim is logged into their banking site
  • Impact: Unauthorized actions performed as the authenticated user — fund transfers, password changes, data modification
  • Defenses: Anti-CSRF tokens (unique per-session or per-request), SameSite cookie attribute, requiring re-authentication for sensitive actions

Exam Tips

Remember

XSS = attacker injects script into a page that OTHER USERS see (exploits user trust in the site). CSRF = attacker tricks the user’s BROWSER into making requests (exploits site trust in the user). XSS steals data; CSRF performs actions.

  • Stored XSS is more dangerous than reflected XSS because it affects all visitors
  • HttpOnly cookies prevent JavaScript from accessing session cookies (mitigates XSS cookie theft)
  • CSRF tokens must be unique and validated server-side for every state-changing request

Connections

Practice Questions

Scenario

See case-xss-and-csrf for a practical DevOps scenario applying these concepts.