Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent connections to SQL 7.0 & 2000 #2839

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4315,25 +4315,6 @@ private TdsOperationStatus TryProcessLoginAck(TdsParserStateObject stateObj, out
// information provided by S. Ashwin
switch (majorMinor)
{
case TdsEnums.SQL70OR2000_MAJOR << 24 | TdsEnums.DEFAULT_MINOR: // 7.0 & 2000 RTM
// note that 7.0 and 2000 can only be distinguished by the increment
switch (increment)
{
case TdsEnums.SQL2000_INCREMENT:
_is2000 = true;
break;
case TdsEnums.SQL70_INCREMENT:
// no flag will be set
break;
default:
throw SQL.InvalidTDSVersion();
}
break;
case TdsEnums.SQL2000SP1_MAJOR << 24 | TdsEnums.SQL2000SP1_MINOR: // 2000 SP1
if (increment != TdsEnums.SQL2000SP1_INCREMENT)
{ throw SQL.InvalidTDSVersion(); }
_is2000SP1 = true;
break;
case TdsEnums.SQL2005_MAJOR << 24 | TdsEnums.SQL2005_RTM_MINOR: // 2005
if (increment != TdsEnums.SQL2005_INCREMENT)
{ throw SQL.InvalidTDSVersion(); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -492,5 +492,37 @@ public void ConnectionTestAccessTokenCallbackCombinations()
});
}
}

[Theory]
[InlineData(9, 0, 2047)] // SQL Server 2005
[InlineData(10, 0, 2531)] // SQL Server 2008
[InlineData(10, 50, 2500)] // SQL Server 2008 R2
[InlineData(11, 0, 3000)] // SQL Server 2012-2022
public void ConnectionTestPermittedVersion(int major, int minor, int build)
{
Version simulatedServerVersion = new Version(major, minor, build);
using TestTdsServer server = TestTdsServer.StartTestServer(serverVersion: simulatedServerVersion);
using SqlConnection conn = new SqlConnection(server.ConnectionString);

conn.Open();
Assert.Equal(ConnectionState.Open, conn.State);

Version returnedServerVersion = Version.Parse(conn.ServerVersion);

Assert.Equal(simulatedServerVersion, returnedServerVersion);
}

[Theory]
[InlineData(7, 0, 623)] // SQL Server 7.0
[InlineData(8, 0, 194)] // SQL Server 2000 RTM
[InlineData(8, 0, 384)] // SQL Server 2000 SP1
public void ConnectionTestDeniedVersion(int major, int minor, int build)
{
Version simulatedServerVersion = new Version(major, minor, build);
using TestTdsServer server = TestTdsServer.StartTestServer(serverVersion: simulatedServerVersion);
using SqlConnection conn = new SqlConnection(server.ConnectionString);

Assert.Throws<InvalidOperationException>(() => conn.Open());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public TestTdsServer(QueryEngine engine, TDSServerArguments args) : base(args)
Engine = engine;
}

public static TestTdsServer StartServerWithQueryEngine(QueryEngine engine, bool enableFedAuth = false, bool enableLog = false, int connectionTimeout = DefaultConnectionTimeout, bool excludeEncryption = false, [CallerMemberName] string methodName = "")
public static TestTdsServer StartServerWithQueryEngine(QueryEngine engine, bool enableFedAuth = false, bool enableLog = false, int connectionTimeout = DefaultConnectionTimeout, bool excludeEncryption = false, Version serverVersion = null, [CallerMemberName] string methodName = "")
{
TDSServerArguments args = new TDSServerArguments()
{
Expand All @@ -40,6 +40,10 @@ public static TestTdsServer StartServerWithQueryEngine(QueryEngine engine, bool
{
args.Encryption = SqlServer.TDS.PreLogin.TDSPreLoginTokenEncryptionType.None;
}
if (serverVersion != null)
{
args.ServerVersion = serverVersion;
}

TestTdsServer server = engine == null ? new TestTdsServer(args) : new TestTdsServer(engine, args);
server._endpoint = new TDSServerEndPoint(server) { ServerEndPoint = new IPEndPoint(IPAddress.Any, 0) };
Expand All @@ -57,9 +61,9 @@ public static TestTdsServer StartServerWithQueryEngine(QueryEngine engine, bool
return server;
}

public static TestTdsServer StartTestServer(bool enableFedAuth = false, bool enableLog = false, int connectionTimeout = DefaultConnectionTimeout, bool excludeEncryption = false, [CallerMemberName] string methodName = "")
public static TestTdsServer StartTestServer(bool enableFedAuth = false, bool enableLog = false, int connectionTimeout = DefaultConnectionTimeout, bool excludeEncryption = false, Version serverVersion = null, [CallerMemberName] string methodName = "")
{
return StartServerWithQueryEngine(null, enableFedAuth, enableLog, connectionTimeout, excludeEncryption, methodName);
return StartServerWithQueryEngine(null, enableFedAuth, enableLog, connectionTimeout, excludeEncryption, serverVersion, methodName);
}

public void Dispose() => _endpoint?.Stop();
Expand Down
20 changes: 19 additions & 1 deletion src/Microsoft.Data.SqlClient/tests/tools/TDS/TDS/TDSVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ namespace Microsoft.SqlServer.TDS
/// </summary>
public static class TDSVersion
{
/// <summary>
/// 7.0 (Sphinx) TDS version
/// </summary>
public static Version SqlServer7_0 = new Version(7, 0, 0, 0);

/// <summary>
/// 2000 (Shiloh) TDS version
/// </summary>
public static Version SqlServer2000 = new Version(7, 1, 0, 1);

/// <summary>
/// 2005 (Yukon) TDS version
/// </summary>
Expand Down Expand Up @@ -46,6 +56,14 @@ public static Version GetTDSVersion(Version buildVersion)
{
return SqlServer2005;
}
else if (buildVersion.Major == 8)
{
return SqlServer2000;
}
else if (buildVersion.Major == 7)
{
return SqlServer7_0;
}
else
{
// Not supported TDS version
Expand Down Expand Up @@ -79,7 +97,7 @@ public static Version Resolve(Version tdsServer, Version tdsClient)
/// </summary>
public static bool IsSupported(Version tdsVersion)
{
return tdsVersion >= SqlServer2005 && tdsVersion <= SqlServer2012;
return tdsVersion >= SqlServer7_0 && tdsVersion <= SqlServer2012;
}
}
}
Loading