Repository navigation
[6.1.9 Cherry-pick] Connection: Apply ServerCertificate validation to login-only TLS - #5181
Open
github-actions[bot] wants to merge 3 commits into
Open
github-actions[bot] wants to merge 3 commits into
github-actions[bot] wants to merge 3 commits into
Conversation
To resolve, run: git cherry-pick 34ee446
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
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>
Contributor
|
/azp run |
|
Azure Pipelines: Successfully started running 2 pipeline(s). 3 pipeline(s) were filtered out due to trigger conditions. |
paulmedynski
marked this pull request as ready for review
December 4, 2026 15:40
paulmedynski
enabled auto-merge (squash)
December 4, 2026 15:40
Contributor
There was a problem hiding this comment.
🟡 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
reviewed
Dec 4, 2026
paulmedynski
left a comment
Contributor
There was a problem hiding this comment.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Cherry-pick of #5164 (34ee446) into
release/6.1.Conflict resolution
release/6.1keeps separate netcore and netfx copies ofTdsParser, so the cherry-pick conflicted on the sharedTdsParser.cs.TdsParser.csunchanged.ConsumePreLoginHandshakechange tonetcore/src/.../Parser/TdsParser.csandnetfx/src/.../Parser/TdsParser.cs. The netfx copy keeps its existingEncryptionOptions.OPTIONS_MASKcomparisons when deciding whether TLS was negotiated.SniCommon.ValidateSslServerCertificate, so no SNI changes were needed.Additional changes
1. Test infrastructure backport (test tools only, no product code)
The new
SqlConnectionCertificateTestsneed 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.TDSServerEndPoint/TDSServerEndPointConnectionthat the next two patches build on.TdsServerArguments.ExpectTlsHandshakeFailureandTdsServerArguments.TlsFirst.TestServerCertificateunit-test helper, theOnTlsHandshakeStartinghook and related endpoint/login support.Adjustments made while applying them:
TransientTdsErrorTdsServer.cs: takes Stabilize simulated server tests while retaining quarantine #4891's lockedTryReserveError, but keeps 6.1's fixed error class20and 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'sOnPreLoginRequestdocumentation alongside Fix managed SNI TLS handshake ignoring Connect Timeout #5051's newOnTlsHandshakeStartinghook.TestServerCertificate: falls back to theX509Certificate2PFX constructor on net8.0, whereX509CertificateLoaderis unavailable.2. Certificate test adaptation
SqlException.EncryptionInfoandSqlServerCertificateValidationErrors, 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.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.-p:TestTargetOS=Unixnetcoreapp) — all build.SqlConnectionCertificateTests, net8.0 and net9.0 — 42/42 passed in each of 3 runs.category!=failing&category!=flaky&category!=interactive) — 686 passed, 1 failed. The failure,ConnectionFailoverTests.TransientFault_IgnoreServerProvidedFailoverPartner_ShouldConnectToUserProvidedPartner, is the only failure on unmodifiedrelease/6.1in the same environment too.