
PSousa989544 (Community Member) asked a question.
Hi everybody,
I have a code used for iterate the List and add some files in a zip file.
I'm using StreamWriter inside de using quote, but Veracode is always reporting CWE - 404: Improper Resource Shutdown or Release
Using quotes always closes and disposes the object after the end of the process. Why Veracode doesn't recognize these quotes. See my code on below:
IEnumerable<ArquivoDTO> notas = await ObterNotas(codigosParticionados);
foreach (ArquivoDTO nota in notas)
{
ZipArchiveEntry archiveEntry = zipArchive.CreateEntry($"{nota.Name}.xml", CompressionLevel.Optimal);
using (StreamWriter writer = new StreamWriter(archiveEntry.Open()))
{
await writer.WriteAsync(nota.Xml);
}
}
These flaw can be a false positive?
.png)
The USING keyword provides the necessary controls for ensuring the resource is disposed of in synchronous code. The platform is not recognize this USING because the use of asynchronous behavior. The actual implementation would instantiate the StreamWriter and then pass it to the thread. When an exception happens it is dealt with in the thread and closes the resource within that thread. It is this designed separation for responsibility that the platform cannot detect. As the try..catch that is actually being performed is not within the location of the creation of the object but within a separate thread. You'll need to mitigate the issue by design and document the using functionality is providing protection.
Thank you for your explanation to does help. But as a developer a simple example to show how to implement what you are describing would go 1000 times further. What you are explaining is academic and not actual. So now I have to go out and find the solution that matches your academic explanation. You would save so much time so many developers using your tool if you would proved real examples.
even when using the:
await using(..){} your code throws this error. Which is the answer to your statement above
Hi @JGray865962 (Community Member),
I'm happy to clarify the advice @Dennis, Principal Security Consultant (Veracode) provided with respect to the question asked on 17th July 2019.
The static analysis engine identified a CWE-404 flaw due to the use the using statement and because the code is asynchronous, it was unable to detect that if the resource had been released correctly. As such the scanner errs on the side of caution by reporting the flaw. At this present time the only way to address this case (specifically CWE-404 flaws identified when using await/async) is to mitigate the flaw by design. You can find more information about how to mitigate flaws in the Help Centre here: https://help.veracode.com/reader/DGHxSJy3Gn3gtuSIN2jkRQ/~p4MSKOS8F8X8h0KwFTKoQ.
Please note that in an upcoming release to the platform we will be making some changes to how we report CWE-404 which should make it easier for you to address this category of flaw.
I hope that explanation helps clear that up for you, but if not please do reply.
Many thanks,
Anthony Fielding
Jason D. Gray Sr
Principal Architect
[signature_1098286837]
[cid:8B402C8A-DA40-48D5-B9AB-0B45B8AD4061@hsd1.co.comcast.net]<https://www.facebook.com/Aureus-Tech-Systems-223981211075059/> [cid:C8BCFD1B-4EFB-4E1C-8FD1-FFEA27E216CD@hsd1.co.comcast.net] <https://twitter.com/aureusts> [cid:18EB1960-40CC-4AC9-B74A-FA4F299EFDF8@hsd1.co.comcast.net] <https://www.linkedin.com/company/aureus-tech-systems-llc>
O (972) 869-7678 : C (512) 921 -5620
[Certified SAFe® 4 Practitioner]
Certified SAFe® 4 Practitioner
Issued By Scaled Agile Inc<https://www.youracclaim.com/org/scaled-agile>
Hi Jason,
If you are using .NET with await and async and you are sure the resources are being closed correctly, then the way to move forward would be to mitigate the flaw by design.
Alternatively you may consider speaking with your security team to consider altering the application profile so that CWE-404 flaws do not violate the rules of the policy, if this results in sub-optimal flaw findings. Note however that valid instances of CWE-404 could be missed in doing this so there is some risk to accept in this approach.
Please also note that at this time we have initial support for .NET Core 3.1 as per the Help Centre article https://help.veracode.com/reader/4EKhlLSMHm5jC8P8j3XccQ/YP2ecJQmr9vE7~AkYNZJlg. This means that Veracode has implemented initial coverage for a new version of a language or platform or a new framework. Support for new functionality in this framework, language, or platform version is not fully implemented and may affect the quality of your scan results.
Thanks,
Anthony Fielding
Thanks for sharing.