Veracode CWE 80 XSS issue with writing to HttpResponse object in c#

984 views Asked by At

Does anybody have any suggestion as to what code I can add to mitigate a Veracode XSS violation that the following code is producing?

        else if (fileBytes != null && fileBytes.Length > 0)
        {
            string htmlContent = System.Web.HttpUtility.HtmlDecode((System.Web.HttpUtility.HtmlEncode(System.Text.Encoding.UTF8.GetString(fileBytes, 0, fileBytes.Length))));
            htmlContent = AntiXssEncoder.HtmlEncode(htmlContent, true);
            fileBytes = Encoding.UTF8.GetBytes(htmlContent);

            Response.Clear();
            MemoryStream ms = new MemoryStream(fileBytes);
            Response.ContentType = "text/plain";
            Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
            Response.Buffer = true;
            ms.WriteTo(Response.OutputStream); // THIS IS CAUSING THE VIOLATION
            Response.End();
        }

I've tried so many different encoding options I can't even keep them all straight in my head. No matter what I do I get the following violation from Veracode.

Improper Neutralization of Script-Related HTML Tags in a Web Page (Basic XSS)

0

There are 0 answers