Veracode report is showing a SQL injection flaw for the below query.
private const string DropDatabaseTemplate = @"DROP DATABASE [{0}]";
ExecuteNonQuery(connection, string.Format(DropDatabaseTemplate, databaseName));
private static int ExecuteNonQuery(SqlConnection connection, string commandText)
{
using (var command = new SqlCommand(commandText, connection))
{
return command.ExecuteNonQuery();
}
}
they suggested using parameterized prepared statements. What would be my approach to remove this security vulnerability
Thanks in advance.
Ans : You can simply avoid security vulnerability with this
private static void ExecuteNonQuery(SqlConnection connection, string commandText)
{
using (var command = new SqlCommand("exec sp_executesql @sqlCommandText", connection))
{
command.Prepare();
command.Parameters.Add("@sqlCommandText", SqlDbType.NVarChar);
command.Parameters["@sqlCommandText"].Value = commandText;
command.ExecuteNonQuery();
}
}