
User16188492502227878163 (Community Member) asked a question.

User16188492502227878163 (Community Member) asked a question.
Ask the Community
Get answers, share a use case, discuss your favorite features, or get input from the community.
By clicking “Accept All Cookies”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts.
.png)
These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. All information these cookies collect is aggregated and therefore anonymous. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
These cookies may be set through our site by our advertising partners. They may be used by those companies to build a profile of your interests and show you relevant adverts on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. If you do not allow these cookies, you will experience less targeted advertising.
Hi @User16188492502227878163 (Community Member),
Veracode Static Analysis reports CWE 73 (External Control of File Name or Path) when it can detect that data coming from outside the application, such as an HTTP request, a file, or even your database, is being used to access a file path. The concern is that an attacker might be able to manipulate the file path and e.g. access or delete arbitrary files. Please refer to this knowledge article on how to deal with this CWE: https://community.veracode.com/s/article/how-do-i-fix-cwe-73-external-control-of-file-name-or-path-in-java
Thank you,
Florian Walter
@Florian, Security Consultant
Thank you so much for the answer. can I get how to write the code in c# to prevent this flaw.
Waiting for your reply.
@User16188492502227878163 (Community Member)
Without knowing more about your particular use case, it is impossible to give customized advice. The only way that would make Veracode Static Analysis automatically close CWE 73 is using hardcoded values or validation of the input against an allow-list of hardcoded values. If that is not possible, we recommend dynamic validation of inputs using regular expressions or, in more complex cases, canonicalizing the input to validate the whole path.
The advice is the same for Java and .NET, and the Community article I shared earlier also has a C#.NET example. Moreover, we have a whole page describing how to deal with CWE 73 in .NET here: https://www.veracode.com/security/dotnet/cwe-73 .
Thank you,
Florian Walter
Hi,
{
string fname= System.IO.Path.GetFileName(Request.Files["xyz"].FileName);
if (fileExtension == ".csv")
{
fileLocation = Server.MapPath(ConfigKeys.xyz) + Path.GetFileName(fname);
}
if (System.IO.File.Exists(fileLocation))
{
System.IO.File.Delete(fileLocation);
}
Request.Files["bsy"].SaveAs(fileLocation);
System.IO.File.Copy(fileLocation, extrnfl, true);
}
here is my code something like this here at line no System.IO.File.Copy(fileLocation, extrnfl, true); also I am getting same flaw. Please help.
Hi @User16188492502227878163 (Community Member) ,
The variable `fileLocation` consists of 2 dynamic parts, `ConfigKeys.xyz` and `fname`. While the former appears to come from a config file and might be reasonably safe in a locked-down environment, the latter comes from the request and certainly needs strong validation. If your use case only allows a set of possible file names, I recommend creating an allow-list and validating that `fname` is one of these values. Otherwise, I recommend applying dynamic validation in form of a regex. Ideally, you would validate that `fname` is alphanumeric if that is applicable to your use case. A possible regex for this would be "^[a-zA-Z0-9]{1,50}$" (without the double quotes). This regex allows between 1 and 50 alphanumeric characters.
Please note that the only remediation Veracode Static Analysis accepts for CWE 73 is a hardcoded path or validation against a strict allow-list. This means that, once proper validation is in place, you may need to propose a mitigation (https://help.veracode.com/r/improve_mitigation) and contact your security team for mitigation review.
Thank you,
Florian Walter