-
Notifications
You must be signed in to change notification settings - Fork 315
Description
Describe the bug
When connecting to a non-default instance from a Linux client, Microsoft.Data.SqlClient 2.1 and later ignores the instance name and instead connects to the default instance. Obviously this is a problem as it raises the possibility of executing code on an instance that you didn't know you were connected to.
To reproduce
Setup a Windows server with two SQL Server instances. Let's call them server\defaultinstance
and server\nondefaultinstance
. I have SQL Server Profiler open with two traces running - one on each instance.
Using dotnet new
create a console application using the following code and reference Microsoft.Data.SqlClient
2.1.1.
using Microsoft.Data.SqlClient;
using System;
using System.IO;
namespace test
{
class Program
{
static void Main(string[] args)
{
var commandText = @"SELECT 1";
var connectionString = new SqlConnectionStringBuilder() {
InitialCatalog = "master",
DataSource = @"server\nondefaultinstance",
UserID = "sa",
Password = "redacted",
IntegratedSecurity = false
};
try {
Console.WriteLine($"Executing SQL - {commandText}");
using (var connection = new SqlConnection(connectionString.ToString()))
{
connection.Open();
var command = new SqlCommand(commandText, connection);
command.ExecuteNonQuery();
}
}
catch (Exception e)
{
Console.WriteLine("Caught an exception");
Console.WriteLine(e.Message);
}
finally
{
Console.WriteLine("End of program");
}
}
}
}
I'm using an Ubuntu terminal in WSL2. In the terminal, change to the project directory and run dotnet run
.
Expected behavior
To see a batch with SELECT 1
appear in the trace for server\nondefaultinstance
Actual behavior
A batch with SELECT 1
appears in the trace for server\defaultinstance
Further technical details
Microsoft.Data.SqlClient version: 2.1.1 (but 2.1.0 is also affected)
.NET target: 5.0
SQL Server version: Have tested on 2017 and 2019
Operating system: Server 2019 for running the two SQL Server instances, Ubuntu 20.04 for running the application