Skip to content

[6.1.9 Cherry-pick] Connection: Apply ServerCertificate validation to login-only TLS - #5181

Open
github-actions[bot] wants to merge 3 commits into
release/6.1from
dev/automation/pr-5164-to-6.1.9
Open

github-actions[bot] wants to merge 3 commits into
release/6.1from
dev/automation/pr-5164-to-6.1.9

Conversation

@github-actions

@github-actions github-actions Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Cherry-pick of #5164 (34ee446) into release/6.1.

Conflict resolution

release/6.1 keeps separate netcore and netfx copies of TdsParser, so the cherry-pick conflicted on the shared TdsParser.cs.

  • Kept the 6.1 shared TdsParser.cs unchanged.
  • Applied the ConsumePreLoginHandshake change to netcore/src/.../Parser/TdsParser.cs and netfx/src/.../Parser/TdsParser.cs. The netfx copy keeps its existing EncryptionOptions.OPTIONS_MASK comparisons when deciding whether TLS was negotiated.
  • Managed SNI on 6.1 already passes the configured certificate file to SniCommon.ValidateSslServerCertificate, so no SNI changes were needed.
  • The documentation and ManualTests matrix changes applied cleanly.

Additional changes

1. Test infrastructure backport (test tools only, no product code)

The new SqlConnectionCertificateTests need simulated-server TLS support that isn't on 6.1. A separate commit applies only the test-tool portions of the following PRs. Their product changes and other test changes are not included.

Adjustments made while applying them:

  • TransientTdsErrorTdsServer.cs: takes Stabilize simulated server tests while retaining quarantine #4891's locked TryReserveError, but keeps 6.1's fixed error class 20 and its existing LOGIN7 counting. ErrorClass, and a LOGIN7 counter that derived servers can update, aren't on 6.1. No 6.1 test checks LOGIN7 counts against this server.
  • GenericTdsServer.cs: kept 6.1's OnPreLoginRequest documentation alongside Fix managed SNI TLS handshake ignoring Connect Timeout #5051's new OnTlsHandshakeStarting hook.
  • TestServerCertificate: falls back to the X509Certificate2 PFX constructor on net8.0, where X509CertificateLoader is unavailable.

2. Certificate test adaptation

  • Removed two assertions on SqlException.EncryptionInfo and SqlServerCertificateValidationErrors, which are main-only APIs. The remaining assertions still verify that a mismatched pin is rejected before login: fatal error class 20, one pre-login, and zero LOGIN7 packets.
  • Removed one nullable annotation, because 6.1's unit tests have no nullable context.

Testing

  • dotnet build build.proj -t:BuildNetCore — passed.
  • dotnet build src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj -p:OSGroup=Windows_NT — passed. This compiles netfx; netfx tests were not run on Linux.
  • Unit, Functional and ManualTesting test projects (-p:TestTargetOS=Unixnetcoreapp) — all build.
  • SqlConnectionCertificateTests, net8.0 and net9.0 — 42/42 passed in each of 3 runs.
  • Full unit suite, net8.0 (category!=failing&category!=flaky&category!=interactive) — 686 passed, 1 failed. The failure, ConnectionFailoverTests.TransientFault_IgnoreServerProvidedFailoverPartner_ShouldConnectToUserProvidedPartner, is the only failure on unmodified release/6.1 in the same environment too.

To resolve, run:  git cherry-pick 34ee446
@github-actions github-actions Bot added this to the 6.1.9 milestone Dec 3, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

paulmedynski and others added 2 commits December 4, 2026 11:11
Brings the TDS test-server support required by the certificate validation tests, applying only the test-tool portions of: #4467 (endpoint lifecycle changes that the later patches build on), #4891 (TLS-first framing and expected handshake failures) and #5051 (TestServerCertificate helper and related endpoint/login support).

TransientTdsErrorTdsServer keeps 6.1's fixed error class 20 and does not count rejected LOGIN7 requests, because TransientTdsErrorTdsServerArguments.ErrorClass and a derived-class-accessible LOGIN7 counter are not on release/6.1. TestServerCertificate falls back to the X509Certificate2 PFX constructor on net8.0.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
When ServerCertificate is configured and TrustServerCertificate is false, validate the server certificate for login-only TLS negotiated with Encrypt=Optional, before login is sent. A configured certificate with no negotiated TLS now fails instead of being ignored. Matching certificates, TrustServerCertificate and connections without ServerCertificate are unchanged.

[6.1] Applied the ConsumePreLoginHandshake change to the separate netcore and netfx TdsParser copies (netfx keeps its OPTIONS_MASK comparisons). Certificate tests: removed assertions on main-only SqlException.EncryptionInfo/SqlServerCertificateValidationErrors, and a nullable annotation (no nullable context on 6.1).

(cherry picked from commit 34ee446)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@paulmedynski paulmedynski changed the title [6.1.9 Cherry-pick - CONFLICTS] Connection: Apply ServerCertificate validation to login-only TLS [6.1.9 Cherry-pick] Connection: Apply ServerCertificate validation to login-only TLS Dec 4, 2026
@paulmedynski

Copy link
Copy Markdown
Contributor

/azp run

@paulmedynski paulmedynski moved this from To triage to In review in SqlClient Board Dec 4, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 2 pipeline(s).
3 pipeline(s) were filtered out due to trigger conditions.

@paulmedynski
paulmedynski marked this pull request as ready for review December 4, 2026 15:40
@paulmedynski
paulmedynski requested a review from a team as a code owner December 4, 2026 15:40
Copilot AI balanced review requested due to automatic review settings December 4, 2026 15:40
@paulmedynski
paulmedynski enabled auto-merge (squash) December 4, 2026 15:40

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

Expected certificate-rejection tests leave ExpectTlsHandshakeFailure disabled, allowing TLS authentication errors to escape during server disposal instead of being handled as expected failures.

1 open issue
What changed in this PR

Backports explicit server-certificate validation for login-only TLS to release/6.1, with matching changes in both parser implementations.

Changes:

  • Validates configured certificate pins before login and rejects connections when required validation cannot use TLS.
  • Adds certificate-validation tests and backports simulator TLS and lifecycle support.
  • Documents Optional encryption behavior and the trust override.
File Description
src/​Microsoft.Data.SqlClient/​tests/​UnitTests/​Utilities/​TestServerCertificate.cs Creates temporary server certificates.
src/​Microsoft.Data.SqlClient/​tests/​UnitTests/​Utilities/​ServerCertificateFiles.cs Supplies certificate pin fixtures.
src/​Microsoft.Data.SqlClient/​tests/​UnitTests/​Microsoft/​Data/​SqlClient/​SqlConnectionCertificateTests.cs Tests certificate validation across encryption modes.
src/​Microsoft.Data.SqlClient/​tests/​tools/​TDS/​TDS/​TDSVersion.cs Defines TDS 8 version constants.
src/​Microsoft.Data.SqlClient/​tests/​tools/​TDS/​TDS/​TDSUtilities.cs Handles omitted encryption metadata.
src/​Microsoft.Data.SqlClient/​tests/​tools/​TDS/​TDS/​Login7/​TDSLogin7Token.cs Supports TDS 8 LOGIN7 encoding.
src/​Microsoft.Data.SqlClient/​tests/​tools/​TDS/​TDS.Servers/​TransientTdsErrorTdsServer.cs Reserves simulated errors atomically.
src/​Microsoft.Data.SqlClient/​tests/​tools/​TDS/​TDS.Servers/​TransientDelayTdsServer.cs Makes delays synchronized and cancellable.
src/​Microsoft.Data.SqlClient/​tests/​tools/​TDS/​TDS.Servers/​TdsServerArguments.cs Adds TLS simulation options.
src/​Microsoft.Data.SqlClient/​tests/​tools/​TDS/​TDS.Servers/​GenericTdsServer.cs Passes TLS options and exposes handshake hook.
src/​Microsoft.Data.SqlClient/​tests/​tools/​TDS/​TDS.EndPoint/​TDSServerParser.cs Supports TLS-first handshakes.
src/​Microsoft.Data.SqlClient/​tests/​tools/​TDS/​TDS.EndPoint/​TDSServerEndPointConnection.cs Improves worker lifecycle and failure reporting.
src/​Microsoft.Data.SqlClient/​tests/​tools/​TDS/​TDS.EndPoint/​TDSServerEndPoint.cs Coordinates connection shutdown and errors.
src/​Microsoft.Data.SqlClient/​tests/​tools/​TDS/​TDS.EndPoint/​TDSParser.cs Handles TLS framing and expected rejection.
src/​Microsoft.Data.SqlClient/​tests/​tools/​TDS/​TDS.EndPoint/​ITDSServer.cs Declares handshake-start hook.
src/​Microsoft.Data.SqlClient/​tests/​ManualTests/​DataCommon/​ConnectionTestParametersData.cs Extends certificate-validation matrix.
src/​Microsoft.Data.SqlClient/​netfx/​src/​Microsoft/​Data/​SqlClient/​Parser/​TdsParser.cs Enforces certificate pins on .NET Framework.
src/​Microsoft.Data.SqlClient/​netcore/​src/​Microsoft/​Data/​SqlClient/​Parser/​TdsParser.cs Enforces certificate pins on .NET Core.
doc/​snippets/​Microsoft.Data.SqlClient/​SqlConnectionStringBuilder.xml Explains login-only TLS validation.

Review effort: Balanced

// Explicit pins also apply to Optional login-only TLS.
new(TDSPreLoginTokenEncryptionType.Off, SqlConnectionEncryptOption.Optional, false, s_fullPathToCer, _empty, true),
new(TDSPreLoginTokenEncryptionType.Off, SqlConnectionEncryptOption.Optional, true, s_fullPathToCer, _empty, true),
new(TDSPreLoginTokenEncryptionType.Off, SqlConnectionEncryptOption.Optional, false, s_mismatchedcert, _empty, false),

@paulmedynski paulmedynski 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.

Original backports look good. Leaving approval to other reviewers, due to the inclusion of other backports for test infrastructure.

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

None yet

Projects

Status: In review

Development

Successfully merging this pull request may close these issues.

3 participants