Repository navigation
Validate TDS parser prerequisites and invalidate malformed responses - #4941
paulmedynski wants to merge 2 commits into
Conversation
Reject missing row metadata, unmatched alternate metadata, and unavailable SSPI providers through the existing corrupted-stream failure path. Guard reader transitions and add simulated-server and compatibility coverage. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🔵 Needs a closer look
Parser-driven connection teardown changes need compatibility validation because SQL Server tests could not connect and Windows/native-SNI and net462 runtime paths remain unverified.
No open issues 🎉
What changed in this PR
This PR hardens SqlClient’s TDS response handling by validating required metadata and authentication state before consuming tokens. Malformed responses use the existing corrupted-stream error path and invalidate the connection.
Changes:
- Add prerequisite checks for ROW, NBCROW, ALTROW, and SSPI processing, including reader result transitions.
- Centralize connection invalidation and let alternate-metadata consumers reject unmatched identifiers.
- Add 48 simulated-server regressions and four real-server compatibility cases covering sync/async operations and valid result processing.
The PR reports passing simulated-server tests and cross-builds; real-server compatibility and additional platform runtime validation remain outstanding.
| File | Description |
|---|---|
src/Microsoft.Data.SqlClient/tests/UnitTests/SimulatedServerTests/ParserPrerequisiteTests.cs |
Tests malformed responses, pool recovery, valid results, and packet continuation. |
src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ConnectivityTests/ParserResultProcessingTest.cs |
Adds real-server coverage for nullable rows, result skipping, and connection reuse. |
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDataReader.cs |
Validates alternate metadata during reader result transitions. |
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Parser/Tokens/TdsAltMetadataCollection.cs |
Removes the Debug-only lookup failure so consumers handle missing metadata. |
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Parser/TdsParser.cs |
Adds token prerequisite checks and shared corrupted-stream rejection. |
Review effort: Balanced
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4941 +/- ##
==========================================
- Coverage 67.68% 66.23% -1.46%
==========================================
Files 311 305 -6
Lines 45249 68310 +23061
==========================================
+ Hits 30629 45248 +14619
- Misses 14620 23062 +8442
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🔵 Needs a closer look
Core TDS rejection now tears down connections across login, command, and reader paths, requiring maintainer validation because CI evidence is absent and real-server, supported-runtime, and SNI compatibility remain unverified.
No open issues 🎉
Review effort: Balanced
benrr101
left a comment
There was a problem hiding this comment.
I think the tests need a bit of work (which is why I submitted #4948) - but overall the change looks good. I'm glad we can just outright reject malformed packets (and hopefully no one demands we put it behind a context switch)
| } | ||
| } | ||
|
|
||
| // @TODO: Debug fail usage here isn't ideal. |
There was a problem hiding this comment.
NB: wheeeee, someone is fixing my todo's 😜
| [InlineData(true, true, true, false)] | ||
| [InlineData(true, true, false, true)] | ||
| [InlineData(true, true, true, true)] | ||
| public async Task AlternateRowWithoutMatchingMetadata_ThrowsParsingError( |
There was a problem hiding this comment.
I'd like to see this broken up a bit more. Let's have a separate test for using a reader vs not using a reader.
| [InlineData(false, true)] | ||
| [InlineData(true, false)] | ||
| [InlineData(true, true)] | ||
| public async Task ValidMetadata_PreservesResultProcessing(bool useReader, bool useAsync) |
There was a problem hiding this comment.
Same request applies here. Reader tests should get their own test
| [InlineData(false, true)] | ||
| [InlineData(true, false)] | ||
| [InlineData(true, true)] | ||
| public async Task AlternateRowIdAcrossPackets_PreservesValidation(bool knownId, bool useAsync) |
There was a problem hiding this comment.
This test is basically doing a positive and negative test in the same test. Please split these into separate tests.
|
Sorry @paulmedynski I meant that to be "Request Changes" |
Description
Validate the metadata and authentication prerequisites needed to consume TDS tokens. Recognizing a token type alone does not establish that its required state exists. Reject the affected malformed responses through the existing corrupted-stream error path instead of dereferencing missing state or using unrelated result metadata.
TryHasMoreResultsand validate collection/identifier state inTryNextResult.NextResult/NextResultAsyncreject missing or unmatched state instead of throwing an incidental null-reference error or retaining primary-result metadata.Public API and compatibility
Open/OpenAsync, command execution, and reader transitions now produce the existingInvalidOperationExceptionparsing error (CorruptedTdsStream, internal state 18) and invalidate the connection, rather than incidental null-reference failures or unrelated metadata fallback.SqlExceptioncontract or blanket exception conversion. Applications catching onlySqlExceptionwill not catch this parsing error.Testing