You're making some mistakes.
If you're accepting HTML-formatted text from the rich-text editor, you cannot call Html.Encode
, or it will encode all of the HTML tags, and you'll see raw markup instead of formatted text.
However, you still need to protect against XSS.
In other words, if the user enters the following HTML:
<b>Hello!</b>
<script>alert('XSS!');</script>
You want to keep the <b>
tag, but drop (not encode) the <script>
tag.
Similarly, you need to drop inline event attributes (like onmouseover
) and Javascript URLs (like <a href="javascript:alert('XSS!');>Dancing Bunnies!</a>
)
You should run the user's HTML through a strict XML parser and maintain a strict white-list of tags and attributes when saving the content.