What is cross-site scripting?
DEFINITION
Cross-site scripting (XSS) is a vulnerability where a website displays untrusted input as code, letting an attacker run malicious scripts in other visitors' browsers.
Cross-site scripting, usually shortened to XSS, occurs when a web page includes data from users, such as comments, profile names or URL parameters, without properly escaping it. If that data contains script code, the visitor's browser runs it as if it came from the trusted site itself.
There are three main types. Stored XSS saves the malicious input in the database, so it affects everyone who views that content. Reflected XSS bounces input from a crafted link straight back into the page. DOM-based XSS happens entirely in front-end JavaScript that writes untrusted data into the page unsafely.
Why XSS matters
Because the script runs with the site's authority, it can act as the logged-in user: read page content, submit forms, change account settings, or redirect people to phishing pages. On sites with admin dashboards, an XSS flaw that reaches an administrator's browser can lead to a full takeover of the site.
How to prevent XSS
The core defense is context-aware output encoding: treat all user-supplied data as text and escape it for the place it appears, whether HTML, an attribute, a URL or JavaScript. Modern frameworks such as React escape content by default, so the main risks come from features that bypass that protection, like inserting raw HTML. For example, a blog that allows formatted comments should run them through a trusted HTML sanitizer library that keeps only safe tags. Add a Content Security Policy (CSP) header to restrict which scripts can run, and mark session cookies as HttpOnly so scripts cannot read them.
Best practices
- Rely on your framework's automatic escaping and avoid raw HTML insertion wherever possible.
- Sanitize any user-supplied rich text with a maintained library before displaying it.
- Deploy a Content Security Policy that blocks inline scripts and untrusted sources.
- Set session cookies with the HttpOnly, Secure and SameSite flags.
Common mistakes
- Trying to block XSS with a handful of hand-written filters for words like script.
- Trusting data from your own database as safe just because it was already stored.
- Escaping for HTML but placing the data inside JavaScript or a URL, where different rules apply.
Frequently asked questions
What is the difference between XSS and SQL injection?
Both are injection flaws caused by mixing untrusted input with code, but they target different places. SQL injection attacks the database on the server, aiming to read or change stored data. XSS attacks other users' browsers, aiming to run scripts in their sessions. Each needs its own defenses.
Does React prevent cross-site scripting?
React escapes values rendered in JSX by default, which prevents many XSS issues. Risks remain when developers use features that insert raw HTML, place untrusted data into links that can contain javascript: URLs, or use third-party components that handle HTML unsafely. Code review and a Content Security Policy still matter.
What is a Content Security Policy?
A Content Security Policy is an HTTP header that tells the browser which sources of scripts, styles, images and other content are allowed on a page. A strict policy can stop injected scripts from running even if an XSS flaw exists, making it a strong second layer of defense.
Need help with this?
Cybersecurity by VYZEON — Proteggete la vostra azienda, i vostri dati e i vostri clienti.
Fixed packages from $199. Written scope, no surprises.
