Reflected_XSS_All_Clients (C# sanitize issue)

542 Views Asked by At

I am using the tool Checkmarx to scan code for security vulnerabilities. One particular one is "Reflected XSS All Clients". The general fix to sanitize this is to use HttpUtility.UrlEncode or HttpUtility.HtmlEncode. I have come across some code that either one of these functions breaks the code since it strips out the tags which in this case are needed for a redirect. This is not code I have written, but scanning for a client. Any ideas on another way to fix this vulnerability?

        private string GetRedirectForm(string url, string response)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<html>");
            sb.AppendFormat("<body onload='document.forms[0].submit()'>");
            sb.AppendFormat("<form action='{0}' method='post'>", url);
            sb.AppendFormat("<input type='hidden' name='SAMLResponse' value='{0}'>", response);
            sb.Append("</form>");
            sb.Append("</body>");
            sb.Append("</html>");
            return HttpUtility.UrlEncode( sb.ToString());
        }

1

There are 1 best solutions below

0
Kolzar On

I think you have two options:

  1. you can use the HtmlSanitizer NuGet package in ASP.NET Core.
  1. But you can encode the HTML before rendering it:
@Html.Raw(Html.Encode(Model.Html))