In XSS, an attacker gets their own JavaScript to run in another user's browser, on your site. For example, someone posts a comment containing <script>...</script>. If your page shows comments without encoding, every visitor's browser runs that script. It can steal session data, change the page or perform actions as the user.
How to prevent it:
Encode output according to where it appears (HTML, attribute, JavaScript, URL). Razor and most modern frameworks HTML-encode by default. Be careful with Html.Raw, innerHTML and v-html/dangerouslySetInnerHTML.
If you must allow some HTML (rich text), sanitise it with a trusted library such as HtmlSanitizer or DOMPurify, using an allow-list.
Add a Content Security Policy (CSP) header to limit which scripts can run.
Mark session cookies HttpOnly, so scripts cannot read them.
Validate input too, but remember that output encoding is the main defence.
Fix every place that prints user-supplied data, and retest with the scanner.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In XSS, an attacker gets their own JavaScript to run in another user's browser, on your site. For example, someone posts a comment containing
<script>...</script>. If your page shows comments without encoding, every visitor's browser runs that script. It can steal session data, change the page or perform actions as the user.How to prevent it:
Html.Raw,innerHTMLandv-html/dangerouslySetInnerHTML.Fix every place that prints user-supplied data, and retest with the scanner.