Visitar URL original
[7.1.2 Cherry-pick] Validate PRELOGIN option offsets before reading option data by github-actions[bot] · Pull Request #4977 · dotnet/SqlClient · GitHub
Skip to content

[7.1.2 Cherry-pick] Validate PRELOGIN option offsets before reading option data - #4977

Merged
priyankatiwari08 merged 1 commit into
release/7.1from
dev/automation/pr-4902-to-7.1.2
Nov 6, 2026
Merged

priyankatiwari08 merged 1 commit into
release/7.1from
dev/automation/pr-4902-to-7.1.2

Conversation

@github-actions

@github-actions github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown

Cherry-pick of #4902 (ff241b6) into release/7.1.

* Validate PRELOGIN option offsets before reading option data

ConsumePreLoginHandshake read the VERSION, ENCRYPT, INSTANCE, MARS and
FEDAUTHREQUIRED option headers and data without checking that they lie
within the received PRELOGIN response. A malformed response could cause an
IndexOutOfRangeException to escape from SqlConnection.Open/OpenAsync.

Read each option header through a helper that verifies the 4-byte header and
the option data it points to are within the response, and report a
CorruptedTdsStream parsing error otherwise. Responses that were previously
accepted are unaffected.

Add simulated-server tests covering out-of-range offsets, data truncated at
the end of the response, and truncated option headers for each option, on
both sync and async open paths, plus a well-formed control.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 7ef2b899-d3ce-43cc-91d5-a321d447d1d4

* Validate declared PRELOGIN option lengths and all option headers

Options the client reads must declare exactly their defined data length
(VERSION 6 bytes; ENCRYPT, INSTANCE, MARS and FEDAUTHREQUIRED 1 byte), so a
short option can no longer be read together with the bytes of the option that
follows it. Every other option header (THREADID, TRACEID, unknown options and
ENCRYPT under TDS 8) is now validated as well: the header and the declared
data must lie within the response.

Rename the helper parameter to expectedBytes and add tests for length
mismatches, a short option overlapping the next option's data, and bounds
violations in options the client does not read.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 7ef2b899-d3ce-43cc-91d5-a321d447d1d4

* Validate PRELOGIN total size and simplify option header parsing

- Read and validate each option header once, before dispatching on the
  option, using ReadPreLoginOptionHeader, which returns the data offset
  and length instead of advancing a ref offset.
- Treat THREADID, TRACEID and unrecognized options in a single branch
  that skips the option after its header is validated; drop Debug.Fail.
- Require the option tokens, headers, data and terminator to describe
  exactly the bytes of the response, rejecting undescribed bytes, data
  claimed by more than one option without a gap, and a missing
  terminator.
- Make the test TDS server write ENCRYPT and TRACEID data only when it
  writes the matching option header.
- Add tests for unknown options and for the total-size check.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 7ef2b899-d3ce-43cc-91d5-a321d447d1d4

---------

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 7ef2b899-d3ce-43cc-91d5-a321d447d1d4
@github-actions github-actions Bot added this to the 7.1.2 milestone Nov 3, 2026
@github-actions
github-actions Bot requested a review from a team as a code owner November 3, 2026 17:39
@github-project-automation github-project-automation Bot moved this to To triage in SqlClient Board Nov 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.

@mdaigle

mdaigle commented Nov 6, 2026

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 Nov 6, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.18310% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 65.44%. Comparing base (be5e575) to head (c838dab).
⚠️ Report is 4 commits behind head on release/7.1.

Files with missing lines Patch % Lines
...t/src/Microsoft/Data/SqlClient/Parser/TdsParser.cs 97.18% 2 Missing ⚠️
Additional details and impacted files
@@               Coverage Diff               @@
##           release/7.1    #4977      +/-   ##
===============================================
- Coverage        66.91%   65.44%   -1.48%     
===============================================
  Files              291      300       +9     
  Lines            45107    68099   +22992     
===============================================
+ Hits             30182    44564   +14382     
- Misses           14925    23535    +8610     
Flag Coverage Δ
CI-SqlClient ?
PR-SqlClient-Project 65.44% <97.18%> (?)

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.

@priyankatiwari08
priyankatiwari08 merged commit 77fc845 into release/7.1 Nov 6, 2026
202 checks passed
@priyankatiwari08
priyankatiwari08 deleted the dev/automation/pr-4902-to-7.1.2 branch November 6, 2026 16:00
@github-project-automation github-project-automation Bot moved this from To triage to Done in SqlClient Board Nov 6, 2026
This was referenced Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants