
BRanpariya644525 (Community Member) asked a question.
Below is my existing Java base code and as you can see I am simply downloading files using output stream.
I am only using the request parameter to get browser detail from the header to generate the filename and again I am decoding it.
So now I don't understand how the below code
outputStream.write(data, 0, readed);
can leads to an XSS issue?
And how I can reproduce this issue to understand it better?
OutputStream outputStream = httpResponse.getOutputStream();
InputStream fileInputStream = generateInputStreamObj(user, attachmentObj, false, httpRequest, servletContext, ipAddress);
String userAgent = httpRequest.getHeader("User-Agent");
String encodedFileName = generateFileName(userAgent, attachmentObj);
int downloadBufferSize = Integer.parseInt(CacheManagement.getInstance().getSystemPropertyByAlias().get("DOWNLOAD_BUFFER_SIZE").getPropertyValue());
httpResponse.setContentType("application/x-download");
httpResponse.setHeader("Content-Length", String.valueOf(attachmentObj.getDocumentSize()));
httpResponse.setHeader("Content-Disposition", "attachment; filename=\"" + encodedFileName + "\"");
httpResponse.setBufferSize(downloadBufferSize);
bufferInputStream = new BufferedInputStream(fileInputStream);
byte[] data = new byte[downloadBufferSize];
int readed = 0;
while ((readed = bufferInputStream.read(data)) != -1)
{
outputStream.write(data, 0, readed);
}
I check Stack overflow and Veracode answers but it doesn't help me. Please guide me to reproduce the issue in local and solve it.
.png)
Hi @BRanpariya644525 (Community Member),
Thank you for your question. The Veracode Static Analysis engine raised this flaw due to it detecting some untrusted data being output via outputStream.write(). The untrusted data could have come from one or more of a number of places such as the user agent from the HTTP request headers, or from the user, attachmentObj, httpRequest, servletContext or ipAddress variables. I cannot see the implementation of generateInputStreamObj or generateFileName in the code snippet above to answer in any great detail, however anywhere that data enters your application (e.g. perhaps the user variable is populated from data from a database) we will consider that data untrusted.
The scan engine is looking for evidence of Cross-Site Scripting (CWE-80) cleansing. Typically this can be achieved by using one of the supported cleansing functions from this list: https://help.veracode.com/r/review_cleansers. For this purpose the application is instructing the browser to download a file and not render HTML. You have mitigated this flaw by setting the Content-Type header to a non-HTML value and by including the Content-Disposition header with the attachment property. Please note that the content type should be the exact MIME type for the file, e.g. "application/pdf" rather than "application/x-download". You may chose to propose a mitigation by design to address this flaw detailing how these two HTTP headers mitigate the CWE-80 flaw.
I hope that answers your question.
Thanks,
Anthony Fielding