---
title: "What is XSS and how do I prevent it?"  
description: "What is XSS and how do I prevent it?"  
author: "Khoa Vu"  
published: 2026-11-19  
updated: 2026-11-19  
canonical: https://www.mindstick.com/forum/163077/what-is-xss-and-how-do-i-prevent-it  
category: "Cloud and Security"  
tags: ["xss", "cross-site scripting", "web security", "input validation", "OWASP"]  
reading_time: 1 minute  

---

# What is XSS and how do I prevent it?

A security scan reported cross-site scripting on our comment page. What is it, and how do we fix it?

## Replies

### Reply by Poulami Sarkar

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.


---

Original Source: https://www.mindstick.com/forum/163077/what-is-xss-and-how-do-i-prevent-it

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
