Skip to content

[7.0.4 Cherry-pick] Validate PRELOGIN option offsets before reading option data - #4978

Open
github-actions[bot] wants to merge 1 commit into
release/7.0from
dev/automation/pr-4902-to-7.0.4
Open

github-actions[bot] wants to merge 1 commit into
release/7.0from
dev/automation/pr-4902-to-7.0.4

Conversation

@github-actions

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

Copy link
Copy Markdown

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

* 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.0.4 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.

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: To triage

Development

Successfully merging this pull request may close these issues.

1 participant