
KK000149 (Community Member) asked a question.
In my application, for two methods I am getting a CWE 15 error. For these two methods, I am sending the filename as the input parameter and I am using OleDbConnection and assigning Data Source as file path. I put validation to the filename and filepath but still, it is showing an error. My code is
public ActionResult _InvalidEmployee(string fileName)
{
if (string.IsNullOrEmpty(fileName) || !CommonFunctions.IsValidFileName(fileName) ||!CommonFunctions.IsValidFileExtension(fileName))
{
return Json(new { errorMsg = "InvalidFormat" }, JsonRequestBehavior.AllowGet);
}
var regex = new System.Text.RegularExpressions.Regex(@"\.\.|\||\/", RegexOptions.None, TimeSpan.FromMilliseconds(100));
if (!regex.IsMatch(fileName))
{
string filePath = string.Empty;
DirectoryInfo dirInfo = new DirectoryInfo(Server.MapPath(ConfigurationManager.AppSettings["fileUploadPath"]));
string dirPath = dirInfo.FullName;
filePath = dirInfo.FullName + @"\" + fileName;
string fileFullPath = Path.GetFullPath(filePath);
OleDbConnection objConnection = new OleDbConnection();
if (fileFullPath.StartsWith(dirPath) && System.IO.File.Exists(fileFullPath))
{
try
{
string connKey = @"upload";
string connection = ConfigurationManager.ConnectionStrings[connKey].ConnectionString.Replace(@"file", fileFullPath);
objConnection.ConnectionString = connection;
objConnection.Open();
string tableName = GetSheetName(objConnection);
string query = string.Format(CultureInfo.InvariantCulture, "select FName,LName,Email from {0} where FirstName <> '' or LastName <> '' or Email <> '', tableName);
using (OleDbDataAdapter objOleDbDataAdapter = new OleDbDataAdapter(query, objConnection))
{
objOleDbDataAdapter.TableMappings.Add("Table", "CandidateStatus");
objOleDbDataAdapter.Fill(objCandidateList);
}
}
catch (Exception ex)
{
if (ex is OleDbException)
return Json(new { errorMsg = "InvalidFormat" }, JsonRequestBehavior.AllowGet);
else if (ex is ArgumentException)
return Json(new { errorMsg = "DuplicateEmailId" }, JsonRequestBehavior.AllowGet);
else
return Json(new { errorMsg = ex.Message }, JsonRequestBehavior.AllowGet);
}
finally
{
if (objConnection != null)
{
objConnection.Close();
objConnection = null;
if (System.IO.File.Exists(fileFullPath))
System.IO.File.Delete(fileFullPath);
}
}
}
else
{
return Json(new { errorMsg = "InvalidFormat" }, JsonRequestBehavior.AllowGet);
}
}
else
{
return Json(new { errorMsg = "InvalidFormat" }, JsonRequestBehavior.AllowGet);
}
}
.png)
Hi @KK000149 (Community Member)
If you're receiving CWE-15 errors for methods where you're passing filenames as input parameters and using OleDbConnection with the file path as the Data Source, it suggests that the input provided (the filename) might not be adequately validated or sanitized. To fix CWE-15 errors in your application, you should focus on ensuring that the filename input is properly validated and sanitized before using it to construct file paths or system resources.
1/ Implement strict input validation for the filename parameter. Before constructing file paths or using them as part of system resources like OleDbConnection, sanitize the filename to remove any potentially harmful characters or sequences.
2/ Instead of directly concatenating user-provided filenames with paths to create file paths, consider using safer constructs like Path.Combine or DirectoryInfo to construct file paths.
3/ In addition, you can implement a Allow-list approach. I would recommend the below article :
https://community.veracode.com/s/article/allowlist-helps-fix-cwe
If you still have questions I would recommend you schedule a consultation call to discuss.
You can check out this knowledge article (https://community.veracode.com/s/article/How-to-schedule-a-consultation-call) on how to schedule a consultation call with us.
Regards,
Kashif
Hi @Kashif, Security Consultant (Veracode inc)
Thanks for your reply.
I have validated filename using CommonFunctions.IsValidFileName(fileName). In that function, I wrote below code
public static bool IsValidFileName(string fileName)
{
var regex = new System.Text.RegularExpressions.Regex(@"\.\.|\||\/", RegexOptions.None, TimeSpan.FromMilliseconds(100));
if(!regex.IsMatch(fileName))
{
// list of characters that are not allowed in the file name
char[] invalidChars = { '[', ']', '*', '@', '#', '\'', '=', '"' };
// Remove invalid characters from the file name
return fileName.IndexOfAny(invalidChars) == -1;
}
else
{
return false;
}
}
And with OleDbConnection, I have used OleDbConnectionStringBuilder for preparing connection string.
OleDbConnection objConnection = new OleDbConnection();
OleDbConnectionStringBuilder builder = new OleDbConnectionStringBuilder();
try
{
string connKey = @"upload";
if (Path.GetExtension(fileFullPath) == ".xlsx")
connKey = @"upload7";
builder.ConnectionString = ConfigurationManager.ConnectionStrings[connKey].ConnectionString;
builder["Data Source"] = fileFullPath;
objConnection.ConnectionString = builder.ConnectionString;
objConnection.Open();
string tableName = GetSheetName(objConnection);
string query = string.Format(CultureInfo.InvariantCulture, "select FirstName,LastName,Email,Status from {0} where FirstName <> '' or LastName <> '' or Email <> '' or Status <> ''", tableName);
using (OleDbDataAdapter objOleDbDataAdapter = new OleDbDataAdapter(query, objConnection))
{
objOleDbDataAdapter.TableMappings.Add("Table", "CandidateStatus");
objOleDbDataAdapter.Fill(objCandidateList);
}
}
and as per your suggestion, I have prepared filepath using Path.combine method.
Still issue present. Is anything else required? Please help me.
Thanks in advance.
Hi @KK000149 (Community Member)
You can consider raising mitigation by design if you have already implemented the validation approach.
Regards
Kashif