Skip to content

Verify negotiated TDS 8.0 ALPN in managed SNI strict mode - #4851

Draft
priyankatiwari08 wants to merge 2 commits into
mainfrom
dev/automation/fix-tds8-alpn-validation
Draft

priyankatiwari08 wants to merge 2 commits into
mainfrom
dev/automation/fix-tds8-alpn-validation

Conversation

@priyankatiwari08

Copy link
Copy Markdown
Contributor

Description

With Encrypt=Strict (TDS 8.0), the client performs the TLS handshake before any TDS traffic and offers the ALPN protocol tds/8.0. Managed SNI set SslClientAuthenticationOptions.ApplicationProtocols but never checked SslStream.NegotiatedApplicationProtocol after 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.AuthenticateAsClientAsync now calls the new SniHandle.ValidateNegotiatedApplicationProtocol after the handshake. It throws AuthenticationException unless the negotiated protocol is exactly tds/8.0.
  • The exception follows the existing TLS failure path (TCP: ReportTcpSNIError(..., CertificateValidationErrorCode); Named Pipes: InternalExceptionError). The user gets a SqlException whose InnerException explains that tds/8.0 was not negotiated.
  • New resource string 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 select tds/8.0 in 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=Strict mode, the managed SNI layer now fails the connection when the server does not negotiate the tds/8.0 TLS 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):

  • Unit tests for ValidateNegotiatedApplicationProtocol. tds/8.0 is accepted. These are rejected: no ALPN, h2, http/1.1, tds/7.4, TDS/8.0.
  • End-to-end StrictConnection_EnforcesNegotiatedAlpn(serverSelectsTds8, useAsync), covering both sync Open and async OpenAsync:
    • Setup: an in-process TLS-first fake server with a self-signed cert pinned via ServerCertificate, and managed SNI forced on Windows.
    • When the server selects tds/8.0, the client proceeds to send PRELOGIN (0x12).
    • When the server selects no ALPN, the client sends nothing and the SqlException.InnerException mentions tds/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/384
  • Same filter on net8.0 / net9.0 (before rebasing onto current main, which moved UnitTests to net462;net10.0): Passed 321/321 each
  • Same e2e filter without the fix (validation call removed): Failed 2/4 (expected; the no-ALPN cases)
  • dotnet build src\Microsoft.Data.SqlClient\src\Microsoft.Data.SqlClient.csproj -f net462: 0 warnings, 0 errors

Manual testing:

  • Managed SNI + Encrypt=Strict against Azure SQL. The TLS handshake (ALPN tds/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:

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
@priyankatiwari08
priyankatiwari08 requested a review from a team as a code owner October 15, 2026 09:26
Copilot AI balanced review requested due to automatic review settings October 15, 2026 09:26
@github-project-automation github-project-automation Bot moved this to To triage in SqlClient Board Oct 15, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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
Copilot AI balanced review requested due to automatic review settings October 15, 2026 10:29

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 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

@paulmedynski

Copy link
Copy Markdown
Contributor

/azp run

@azure-pipelines

Copy link
Copy Markdown
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()));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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,

@paulmedynski paulmedynski Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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")]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add some control characters, null-bytes, empty string, etc.

LocalAppContextSwitchesHelper? switches = null;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
switches = new LocalAppContextSwitchesHelper { UseManagedNetworking = true };

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@priyankatiwari08
priyankatiwari08 marked this pull request as draft October 15, 2026 16:08
@mdaigle mdaigle moved this from To triage to In progress in SqlClient Board Oct 20, 2026
@cheenamalhotra cheenamalhotra added the Localization Needed PRs that contain changes that would need localization label Nov 6, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Localization Needed PRs that contain changes that would need localization

Projects

Status: In progress

Development

Successfully merging this pull request may close these issues.

6 participants