I have CMS system where I am using CK Editor to enter data. Now if user types in <script>alert('This is a bad script, data');</script>
then CKEditor does the fair job and encodes it correctly and passes <script>alert('This is a bad script, data')</script>
to server.
But if user goes into browser developer tools (using Inspect element) and adds this inside it as shown in the below screen shot then this is when all the trouble starts. Now after retrieving back from DB when this is displayed in Browser it presents alert box.
So far I have tried many different things one them is
- Encode the contents using AntiXssEncoder [
HttpUtility.HtmlEncode(Contents)
] and then store it in database and when displaying back in browser decode it and display it using MvcHtmlString.Create [MvcHtmlString.Create(HttpUtility.HtmlDecode(Contents))
] or Html.Raw [Html.Raw(Contents)
] as you may expect both of them displays JavaScript alert.
I don't want to replace the <script>
manually thru code as it is not comprehensive solution (search for "And the encoded state:").
So far I have referred many articles (sorry not listing them all here but just adding few as proof to show I have put sincere efforts before writing this question) but none of them have code which shows the answer. May be there is some easy answer and I am not looking in right direction or may be it is not that simple at all and I may need to use something like Content Security Policy.
ASP.Net MVC Html.Raw with AntiXSS protection Is there a risk in using @Html.Raw? http://blog.simontimms.com/2013/01/21/content-security-policy-for-asp-net-mvc/ http://blog.michaelckennedy.net/2012/10/15/understanding-text-encoding-in-asp-net-mvc/
To reproduce what I am saying go to *this url and in the text box type <script>alert('This is a bad script, data');</script>
and click the button.
*This link is from Michael Kennedy's blog