Repository navigation
Verify negotiated TDS 8.0 ALPN in managed SNI strict mode - #4851
priyankatiwari08 wants to merge 2 commits into
Conversation
With Encrypt=Strict the client offers the "tds/8.0" ALPN protocol during the TLS-first handshake but never checked that the server selected it. Fail the handshake when the negotiated application protocol is not "tds/8.0". Addresses Argus finding CF-002 (ADO Bug 48331). Co-authored-by: Copilot App <[email protected]> Copilot-Session: 714af751-d142-4da2-b52c-1eaa231ff72a
There was a problem hiding this comment.
🟡 Changes recommended
The new Windows tests need nonparallel isolation to prevent their process-wide networking-switch changes from disrupting concurrent connections.
1 open finding
What changed in this PR
Hardens managed SNI strict encryption by requiring TLS to negotiate tds/8.0 before sending TDS traffic.
Changes:
- Validates the negotiated protocol after the TLS handshake.
- Adds a localized negotiation-failure message.
- Adds protocol validation and synchronous/asynchronous connection tests.
| File | Description |
|---|---|
src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/ManagedSni/SniTds8AlpnValidationTest.cs |
Tests accepted and rejected negotiation outcomes. |
src/Microsoft.Data.SqlClient/src/Resources/Strings.resx |
Adds the negotiation-failure message. |
src/Microsoft.Data.SqlClient/src/Resources/Strings.Designer.cs |
Exposes the new resource string. |
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ManagedSni/SniHandle.netcore.cs |
Enforces strict-mode protocol validation. |
Files not reviewed (1)
- src/Microsoft.Data.SqlClient/src/Resources/Strings.Designer.cs: Generated file
🧠 Review effort: Balanced
💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Co-authored-by: Copilot App <[email protected]> Copilot-Session: 714af751-d142-4da2-b52c-1eaa231ff72a
There was a problem hiding this comment.
🔵 Needs a closer look
The security-sensitive TLS behavior change needs human confirmation of validation across supported platforms.
0 open findings
1 resolved since last review
Files not reviewed (1)
- src/Microsoft.Data.SqlClient/src/Resources/Strings.Designer.cs: Generated file
🧠 Review effort: Balanced
|
/azp run |
|
Azure Pipelines: Successfully started running 2 pipeline(s). 1 pipeline(s) were filtered out due to trigger conditions. |
| throw new AuthenticationException(StringsHelper.Format( | ||
| Strings.SNI_TDS8_ApplicationProtocolNotNegotiated, | ||
| TdsEnums.TDS8_Protocol, | ||
| negotiated.Protocol.IsEmpty ? string.Empty : negotiated.ToString())); |
There was a problem hiding this comment.
ALPN uses opaque bytes as the protocol identifier, so we can't blindly attempt to emit it via ToString(). It could contain control characters, null-bytes, etc.
| { | ||
| throw new AuthenticationException(StringsHelper.Format( | ||
| Strings.SNI_TDS8_ApplicationProtocolNotNegotiated, | ||
| TdsEnums.TDS8_Protocol, |
There was a problem hiding this comment.
We should use s_tdsProtocols[0] here for the desired value, since that is what the equality check used, and it will ensure consistent formatting with the negotiated value.
| <value>Invalid SQLUserInstance.dll found at the location specified in the registry. Verify that the Local Database Runtime feature of SQL Server Express is properly installed.</value> | ||
| </data> | ||
| <data name="SNI_TDS8_ApplicationProtocolNotNegotiated" xml:space="preserve"> | ||
| <value>The server did not negotiate the required TLS application protocol '{0}' (negotiated: '{1}'). The connection was closed because strict encryption requires TDS 8.0.</value> |
There was a problem hiding this comment.
I would omit the last part of the second sentence, and just say "The connection was closed." The {0} parameter already says what the desired ALP is.
| [InlineData("h2")] | ||
| [InlineData("http/1.1")] | ||
| [InlineData("tds/7.4")] | ||
| [InlineData("TDS/8.0")] |
There was a problem hiding this comment.
Add some control characters, null-bytes, empty string, etc.
| LocalAppContextSwitchesHelper? switches = null; | ||
| if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||
| { | ||
| switches = new LocalAppContextSwitchesHelper { UseManagedNetworking = true }; |
There was a problem hiding this comment.
This is a no-op on non-Windows, so it's harmelss and will simplify to a single using line:
using LocalAppContectSwitchesHelper switches = new(){ UseManagedNetworking = true; };
| /// packet. When it does not, the client must abort before sending any TDS data. | ||
| /// </summary> | ||
| [Theory] | ||
| [InlineData(true, false)] |
There was a problem hiding this comment.
The two true cases seem to wait at least 5 seconds for a connect timeout. Can we minimize this?
| { | ||
| using X509Certificate2 serverCert = CreateServerCertificate(); | ||
| string pinPath = Path.Combine(Path.GetTempPath(), $"alpn-{Guid.NewGuid():N}.cer"); | ||
| File.WriteAllBytes(pinPath, serverCert.Export(X509ContentType.Cert)); |
There was a problem hiding this comment.
We have a TempCertPath helper in UnitTests already, but it's not in a shared place. You may want to move it into its own file and use it here as well. There may also be some helpers to generate server certs that could be re-used here.

Description
With
Encrypt=Strict(TDS 8.0), the client performs the TLS handshake before any TDS traffic and offers the ALPN protocoltds/8.0. Managed SNI setSslClientAuthenticationOptions.ApplicationProtocolsbut never checkedSslStream.NegotiatedApplicationProtocolafter the handshake, so a handshake in which the server selected no ALPN protocol (or a different one) was silently accepted and the client continued with PRELOGIN.This change hardens strict mode:
SniHandle.AuthenticateAsClientAsyncnow calls the newSniHandle.ValidateNegotiatedApplicationProtocolafter the handshake. It throwsAuthenticationExceptionunless the negotiated protocol is exactlytds/8.0.ReportTcpSNIError(..., CertificateValidationErrorCode); Named Pipes:InternalExceptionError). The user gets aSqlExceptionwhoseInnerExceptionexplains thattds/8.0was not negotiated.SNI_TDS8_ApplicationProtocolNotNegotiated.Only the TLS-first (strict) path is affected.
Encrypt=Optional/Mandatory(TDS 7.x, TLS inside PRELOGIN) is unchanged. SQL Server 2022+ and Azure SQL selecttds/8.0in strict mode (verified live below), so this has no impact on compliant servers. The Microsoft JDBC driver added the same check in 11.2.The matching fix for native SNI (Windows default) is being done separately in the native SNI repository.
No public API changes.
Suggested release note (Fixed): In
Encrypt=Strictmode, the managed SNI layer now fails the connection when the server does not negotiate thetds/8.0TLS application protocol (ALPN).Issues
Addresses an internal security review finding (TDS 8.0 ALPN negotiation not verified after handshake).
Testing
New test class
tests/UnitTests/Microsoft/Data/SqlClient/ManagedSni/SniTds8AlpnValidationTest.cs(#if NET):ValidateNegotiatedApplicationProtocol.tds/8.0is accepted. These are rejected: no ALPN,h2,http/1.1,tds/7.4,TDS/8.0.StrictConnection_EnforcesNegotiatedAlpn(serverSelectsTds8, useAsync), covering both syncOpenand asyncOpenAsync:ServerCertificate, and managed SNI forced on Windows.tds/8.0, the client proceeds to send PRELOGIN (0x12).SqlException.InnerExceptionmentionstds/8.0.Test-first check: with the validation call removed, the two "server does not select tds/8.0" e2e cases fail because the client sends PRELOGIN. With the fix, they pass.
Commands and results:
dotnet test src\Microsoft.Data.SqlClient\tests\UnitTests\Microsoft.Data.SqlClient.UnitTests.csproj -f net10.0 --filter "FullyQualifiedName~ManagedSni|FullyQualifiedName~SimulatedServer|FullyQualifiedName~Tds8|FullyQualifiedName~Encrypt": Passed 384/384main, which moved UnitTests tonet462;net10.0): Passed 321/321 eachdotnet build src\Microsoft.Data.SqlClient\src\Microsoft.Data.SqlClient.csproj -f net462: 0 warnings, 0 errorsManual testing:
Encrypt=Strictagainst Azure SQL. The TLS handshake (ALPNtds/8.0) succeeded and the connection continued to PRELOGIN/LOGIN. The server then refused the login only because of its public-network-access policy, which is unrelated.Guidelines
Please review the contribution guidelines before submitting a pull request: