Skip to content
IT glossary
SecurityUpdated December 3, 2026

XSS

Cross-site scripting (XSS) is a web security flaw that lets untrusted code run in someone else's browser as part of a trusted website. The code can interact with the page and act within that user's session.

Also known as: межсайтовый скриптинг

What XSS means

Think of a website that displays a profile name. The name is data, not an instruction for the browser. XSS becomes possible when an application puts untrusted data somewhere the browser can interpret it as executable code. The name is historical: a second website isn't required. What matters is that the code runs with the privileges of the vulnerable page, so it may read data shown in the interface, change what the user sees, or make requests through the user's session.

Where the flaw happens

Stored XSS involves data the application keeps and later renders unsafely. A comment, for instance, might be saved once and displayed to many readers. With reflected XSS, the server includes data from the current request in its response without treating it safely for that output context. A search results page that echoes the search term is a common place to review. In DOM-based XSS, client-side code passes untrusted data to a browser API that interprets it as markup or code. The server doesn't have to embed that data in its HTML response. These labels can overlap: saved data can reach an unsafe browser API through client-side code.

Choose an output method that matches the data

Most values, such as a display name or notification message, only need to be displayed as text. For an ordinary HTML element, assign the value to textContent. The browser displays the characters rather than parsing them as HTML.

const message = document.querySelector('#notification');
message.textContent = notification.message;

For server-rendered HTML, keep template auto-escaping enabled and check what context it covers. Escaping for HTML text isn't a general-purpose defense for JavaScript or URLs. For example, adding a user-provided destination to a link also requires checking that its URL scheme is allowed. If a feature genuinely needs user-authored HTML, use a maintained sanitizer configured to keep only the elements and attributes the feature needs. A frontend framework's default escaping helps with ordinary text rendering, but it doesn't make raw-HTML APIs safe.

What to explain in an interview

In a code review, start with three questions: Who controls the value? Where does it end up? How will the browser interpret it there? Explain the fix at that point, then describe the role of backup controls. A strict Content Security Policy (CSP) can restrict script execution, but it doesn't repair unsafe rendering. An HttpOnly cookie can't be read directly by JavaScript, but the browser can still attach it to eligible requests made by code running on the page.