Skip to content

Commit 1ad4af3

Browse files
Suppress TLS security warning with Encrypt=false by new AppContext switch (#1457)
1 parent fe38d26 commit 1ad4af3

File tree

4 files changed

+44
-8
lines changed

4 files changed

+44
-8
lines changed

BUILDGUIDE.md

+6
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,12 @@ TLS 1.3 has been excluded due to the fact that the driver lacks full support. To
319319

320320
`Switch.Microsoft.Data.SqlClient.EnableSecureProtocolsByOS`
321321

322+
## Suppressing TLS security warning
323+
324+
When connecting to a server, if a protocol lower than TLS 1.2 is negotiated, a security warning is output to the console. This warning can be suppressed on SQL connections with `Encrypt = false` by enabling the following AppContext switch on application startup:
325+
326+
`Switch.Microsoft.Data.SqlClient.SuppressInsecureTLSWarning`
327+
322328
## Debugging SqlClient on Linux from Windows
323329

324330
For enhanced developer experience, we support debugging SqlClient on Linux from Windows, using the project "**Microsoft.Data.SqlClient.DockerLinuxTest**" that requires "Container Tools" to be enabled in Visual Studio. You may import configuration: [VS19Components.vsconfig](./tools/vsconfig/VS19Components.vsconfig) if not enabled already.

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs

+10-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
using System.Diagnostics;
1111
using System.Globalization;
1212
using System.IO;
13-
using System.Reflection;
1413
using System.Security.Authentication;
1514
using System.Text;
1615
using System.Threading;
@@ -960,8 +959,16 @@ private PreLoginHandshakeStatus ConsumePreLoginHandshake(bool encrypt, bool trus
960959
string warningMessage = protocol.GetProtocolWarning();
961960
if (!string.IsNullOrEmpty(warningMessage))
962961
{
963-
// This logs console warning of insecure protocol in use.
964-
_logger.LogWarning(GetType().Name, MethodBase.GetCurrentMethod().Name, warningMessage);
962+
if (!encrypt && LocalAppContextSwitches.SuppressInsecureTLSWarning)
963+
{
964+
// Skip console warning
965+
SqlClientEventSource.Log.TryTraceEvent("<sc|{0}|{1}|{2}>{3}", nameof(TdsParser), nameof(ConsumePreLoginHandshake), SqlClientLogger.LogLevel.Warning, warningMessage);
966+
}
967+
else
968+
{
969+
// This logs console warning of insecure protocol in use.
970+
_logger.LogWarning(nameof(TdsParser), nameof(ConsumePreLoginHandshake), warningMessage);
971+
}
965972
}
966973

967974
// create a new packet encryption changes the internal packet size

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs

+10-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
using System.Diagnostics;
1010
using System.Globalization;
1111
using System.IO;
12-
using System.Reflection;
1312
using System.Runtime.CompilerServices;
1413
using System.Runtime.InteropServices;
1514
using System.Security.Cryptography.X509Certificates;
@@ -1339,8 +1338,16 @@ private PreLoginHandshakeStatus ConsumePreLoginHandshake(SqlAuthenticationMethod
13391338
string warningMessage = SslProtocolsHelper.GetProtocolWarning(protocolVersion);
13401339
if (!string.IsNullOrEmpty(warningMessage))
13411340
{
1342-
// This logs console warning of insecure protocol in use.
1343-
_logger.LogWarning(GetType().Name, MethodBase.GetCurrentMethod().Name, warningMessage);
1341+
if (!encrypt && LocalAppContextSwitches.SuppressInsecureTLSWarning)
1342+
{
1343+
// Skip console warning
1344+
SqlClientEventSource.Log.TryTraceEvent("<sc|{0}|{1}|{2}>{3}", nameof(TdsParser), nameof(ConsumePreLoginHandshake), SqlClientLogger.LogLevel.Warning, warningMessage);
1345+
}
1346+
else
1347+
{
1348+
// This logs console warning of insecure protocol in use.
1349+
_logger.LogWarning(nameof(TdsParser), nameof(ConsumePreLoginHandshake), warningMessage);
1350+
}
13441351
}
13451352

13461353
// Validate server certificate

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/LocalAppContextSwitches.cs

+18-2
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ internal static partial class LocalAppContextSwitches
1414
internal const string MakeReadAsyncBlockingString = @"Switch.Microsoft.Data.SqlClient.MakeReadAsyncBlocking";
1515
internal const string LegacyRowVersionNullString = @"Switch.Microsoft.Data.SqlClient.LegacyRowVersionNullBehavior";
1616
internal const string UseSystemDefaultSecureProtocolsString = @"Switch.Microsoft.Data.SqlClient.UseSystemDefaultSecureProtocols";
17+
internal const string SuppressInsecureTLSWarningString = @"Switch.Microsoft.Data.SqlClient.SuppressInsecureTLSWarning";
1718

18-
private static bool _makeReadAsyncBlocking;
19+
private static bool s_makeReadAsyncBlocking;
1920
private static bool? s_LegacyRowVersionNullBehavior;
2021
private static bool? s_UseSystemDefaultSecureProtocols;
22+
private static bool? s_SuppressInsecureTLSWarning;
2123

2224
#if !NETFRAMEWORK
2325
static LocalAppContextSwitches()
@@ -35,12 +37,26 @@ static LocalAppContextSwitches()
3537
}
3638
#endif
3739

40+
public static bool SuppressInsecureTLSWarning
41+
{
42+
get
43+
{
44+
if (s_SuppressInsecureTLSWarning is null)
45+
{
46+
bool result;
47+
result = AppContext.TryGetSwitch(SuppressInsecureTLSWarningString, out result) ? result : false;
48+
s_SuppressInsecureTLSWarning = result;
49+
}
50+
return s_SuppressInsecureTLSWarning.Value;
51+
}
52+
}
53+
3854
public static bool MakeReadAsyncBlocking
3955
{
4056
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4157
get
4258
{
43-
return AppContext.TryGetSwitch(MakeReadAsyncBlockingString, out _makeReadAsyncBlocking) ? _makeReadAsyncBlocking : false;
59+
return AppContext.TryGetSwitch(MakeReadAsyncBlockingString, out s_makeReadAsyncBlocking) ? s_makeReadAsyncBlocking : false;
4460
}
4561
}
4662

0 commit comments

Comments
 (0)