4

Our client uses Veracode scanning tool to scan ASP.NET Application. We have solved many flaws except for the below.

Improper Neutralization of CRLF Sequences in HTTP Headers ('HTTP Response Splitting')
(CWE ID 113)(1 flaw) in the line  

HttpContext.Current.Response.AddHeader("Content-Disposition", contentDisposition);

This is the corresponding code:

public static void DownloadFile(string fileName, byte[] dByteData, bool isNoOpen = false)
        {

            byte[] fileContents = new byte[] { };
            string contentDisposition = string.Empty;
            fileContents = dByteData;
            if (string.IsNullOrWhiteSpace(fileName))
            {
                return;
            }
            fileName = fileName.Replace("\n", "").Replace("\r", "");
            string contentType = "application/*.".Replace("\n", "").Replace("\r", "");
            contentDisposition = "attachment; filename=\"" + HttpContext.Current.Server.UrlPathEncode(fileName) + "\"";//While Downloading file - file name comes with junk characters
            contentDisposition= contentDisposition.Replace("\n", "").Replace("\r", "");
            HttpContext.Current.Response.Buffer = true;
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.Charset = "";
            HttpContext.Current.Response.ContentType = contentType;
            if (isNoOpen)
            {
                HttpContext.Current.Response.AddHeader("X-Download-Options", "noopen");
            }
            HttpContext.Current.Response.AddHeader("Content-Disposition", contentDisposition);
            HttpContext.Current.Response.AddHeader("Content-Length", fileContents.Length.ToString());
            HttpContext.Current.Response.BinaryWrite(fileContents.ToArray());

            HttpContext.Current.Response.End();
            HttpContext.Current.Response.Flush();
            HttpContext.Current.ApplicationInstance.CompleteRequest();
        }

External Control of File Name or Path (CWE ID 73)

if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }

It shows error in File.Delete line. We have tried sanitizing filepath and also used Path.GetFullpath but to vain only.

Joachim Sauer
  • 278,207
  • 54
  • 523
  • 586

4 Answers4

1

You can get more detailed information about the flaw origin by the call stack analysis (it is available at the Triage flaws section of the Application build scan result in the Veracode Analysis Center). Some Veracode flaws origins are difficult to understand without this information.

MaSEL
  • 505
  • 1
  • 5
  • 19
1

Very often tools like Veracode do not understand the fact that you have sanitised your content. It seems to be missing your Replace() calls. I'd mark this finding as a false positive and move on.

Vitaly Osipov
  • 986
  • 5
  • 14
0

For External Control of File Name or Path (CWE ID 73):

Validate filePath with somethings like:

public ValidatePath(string path) {
    var invalidPathCharacters = System.IO.Path.GetInvalidPathChars();
    foreach (var a in path)
    {
        if (invalidPathCharacters.Contains(a))
        {
            throw new Exception($"Character {a} is an invalid path character for path {path}");
        }
    }
}

Veracode was satisfied in our last scan.

vich
  • 11,638
  • 13
  • 46
  • 60
0

Use veracode filepathcleanser attribute. see https://help.veracode.com/reader/DGHxSJy3Gn3gtuSIN2jkRQ/CWbscOAsMPyIXqASkLRTnw