Skip to content

[7.0.2 Cherry-pick] Add TDS token data length bounds checks (#4340)#4358

Open
github-actions[bot] wants to merge 2 commits into
release/7.0from
dev/automation/pr-4340-to-7.0.2
Open

[7.0.2 Cherry-pick] Add TDS token data length bounds checks (#4340)#4358
github-actions[bot] wants to merge 2 commits into
release/7.0from
dev/automation/pr-4340-to-7.0.2

Conversation

@github-actions

@github-actions github-actions Bot commented Jun 11, 2026

Copy link
Copy Markdown

Cherry-pick of #4340 to release/7.0

This is a cherry-pick of #4340 (commit b6c0569) onto the release/7.0 branch for the 7.0.2 hotfix.

Summary

Adds bounds checks on data lengths read from the TDS wire to prevent unbounded byte[] allocation from a malicious server.

All checks throw SQL.ParsingErrorLength(ParsingErrorState.CorruptedTdsStream, ...) when a spoofed length exceeds protocol-reasonable maximums.

Changes

File Change
TdsEnums.cs New constants: MaxTokenDataLength (1 MB), MaxPromoteTransactionLength (64 KB), MaxDateTimeLength (10)
SqlUtil.cs New ParsingErrorLength() helper
TdsParser.cs 7 bounds-check sites: session state total/inner length, fed auth info token length, promote transaction newLength, feature ext ack data length, datetime length, return value non-PLP length, binary/vector length
SqlConnectionInternal.cs Pass loginTimeout into TdsParser for connection-level timeout propagation
GenericTdsServer.cs New FeatureExtAckDataOverride + FeatureExtAckFeatureIdOverride properties for test injection
TdsTokenBoundsTests.cs New — 1,051 lines of tests covering all bounds-check sites
FeatureExtAckBoundsTests.cs New — 163 lines testing feature ext ack bounds
Various Connection*Tests.cs Minor: whitespace fixes, comment updates

Cherry-pick resolution notes

The cherry-pick required manual conflict resolution in TdsParser.cs due to differences between main and release/7.0:

  • Trailing whitespace that was already cleaned on main but still present on release/7.0 (fixed as part of this cherry-pick)
  • The SQLVECTOR case used an older two-line allocation pattern (byte[] b = new byte[length] + out b) which was updated to the inline out byte[] b form consistent with the bounds-check pattern

All substantive logic changes from the original PR are present and verified.

@github-actions github-actions Bot added this to the 7.0.2 milestone Jun 11, 2026
@github-project-automation github-project-automation Bot moved this to To triage in SqlClient Board Jun 11, 2026
@paulmedynski paulmedynski force-pushed the dev/automation/pr-4340-to-7.0.2 branch from aae16d0 to cae627e Compare June 15, 2026 10:46
@paulmedynski paulmedynski changed the title [7.0.2 Cherry-pick - CONFLICTS] Add TDS token data length bounds checks [7.0.2 Cherry-pick] Add TDS token data length bounds checks (#4340) Jun 15, 2026
@paulmedynski paulmedynski moved this from To triage to In progress in SqlClient Board Jun 15, 2026
@paulmedynski paulmedynski added the Code Health 💊 Issues/PRs that are targeted to source code quality improvements. label Jun 15, 2026
@paulmedynski

Copy link
Copy Markdown
Contributor

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 2 pipeline(s).

@paulmedynski paulmedynski marked this pull request as ready for review June 15, 2026 13:56
Copilot AI review requested due to automatic review settings June 15, 2026 13:56
@paulmedynski paulmedynski requested a review from a team as a code owner June 15, 2026 13:56
@paulmedynski paulmedynski enabled auto-merge (squash) June 15, 2026 13:56
@paulmedynski paulmedynski moved this from In progress to In review in SqlClient Board Jun 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.

Pull request overview

Cherry-picks the TDS token-length bounds checking work onto release/7.0 to harden the client against malicious servers sending spoofed lengths that could otherwise trigger unbounded byte[] allocations.

Changes:

  • Adds new protocol-reasonable maximums in TdsEnums and applies new length bounds checks in multiple TDS parsing sites.
  • Introduces a SQL.ParsingErrorLength(..., uint) helper overload to report oversized lengths consistently.
  • Extends the simulated TDS server test infrastructure and adds new unit tests to validate the new bounds checks.

Reviewed changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/Microsoft.Data.SqlClient/tests/UnitTests/SimulatedServerTests/TdsTokenBoundsTests.cs Adds simulated-server tests for multiple new bounds-check sites.
src/Microsoft.Data.SqlClient/tests/UnitTests/SimulatedServerTests/FeatureExtensionNegotiationTests.cs Adds clarification comment for serialized test collection usage.
src/Microsoft.Data.SqlClient/tests/UnitTests/SimulatedServerTests/FeatureExtAckBoundsTests.cs Adds bounds tests for FeatureExtAck token data length.
src/Microsoft.Data.SqlClient/tests/UnitTests/SimulatedServerTests/ConnectionRoutingTestsAzure.cs Whitespace/comment updates.
src/Microsoft.Data.SqlClient/tests/UnitTests/SimulatedServerTests/ConnectionRoutingTests.cs Whitespace/comment updates.
src/Microsoft.Data.SqlClient/tests/UnitTests/SimulatedServerTests/ConnectionReadOnlyRoutingTests.cs Whitespace/comment updates.
src/Microsoft.Data.SqlClient/tests/UnitTests/SimulatedServerTests/ConnectionFailoverTests.cs Whitespace/comment updates.
src/Microsoft.Data.SqlClient/tests/UnitTests/SimulatedServerTests/ConnectionEnhancedRoutingTests.cs Comment update about test collection serialization.
src/Microsoft.Data.SqlClient/tests/tools/TDS/TDS.Servers/GenericTdsServer.cs Adds OnSQLBatchCompleted hook to enable post-login token injection in tests.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParser.cs Adds bounds checks in parser paths to prevent unsafe allocations from spoofed lengths.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsEnums.cs Adds new max-length constants used by parser bounds checks.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlUtil.cs Adds ParsingErrorLength(..., uint) helper overload.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Connection/SqlConnectionInternal.cs Adds bounds check preventing out-of-range copy in SRECOVERY parsing.

@paulmedynski paulmedynski removed their assignment Jun 15, 2026
@paulmedynski

Copy link
Copy Markdown
Contributor

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 2 pipeline(s).

@codecov

codecov Bot commented Jun 16, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 65.79%. Comparing base (778614a) to head (8296c4a).

❗ There is a different number of reports uploaded between BASE (778614a) and HEAD (8296c4a). Click for more details.

HEAD has 1 upload less than BASE
Flag BASE (778614a) HEAD (8296c4a)
CI-SqlClient 1 0
Additional details and impacted files
@@               Coverage Diff               @@
##           release/7.0    #4358      +/-   ##
===============================================
- Coverage        73.11%   65.79%   -7.33%     
===============================================
  Files              280      275       -5     
  Lines            43026    65883   +22857     
===============================================
+ Hits             31457    43345   +11888     
- Misses           11569    22538   +10969     
Flag Coverage Δ
CI-SqlClient ?
PR-SqlClient-Project 65.79% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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

Labels

Code Health 💊 Issues/PRs that are targeted to source code quality improvements.

Projects

Status: In review

Development

Successfully merging this pull request may close these issues.

4 participants