You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
FIX: Validate access-token buffers before ODBC calls - #923
Prevent malformed access-token buffers from crashing the ODBC driver or bypassing validation on pooled connections, using shared native validation before pool acquisition and ODBC attribute setting.
Validate the four-byte little-endian length header, exact non-empty/even payload length, and ODBC signed size limit before forwarding the token.
Validate explicitly supplied tokens before acquiring a pooled connection, so empty bytes or bytearrays cannot reuse a token-free connection without validation.
Preserve connection-owned token buffers and accept correctly packed opaque tokens without JWT parsing. Deferred token factories remain lazy and their results are validated when invoked.
Add bounded subprocess regressions for public connections, direct native calls, deferred token factories, and warmed token-free pools; document the raw-token contract.
Validation
Windows x64 native Release rebuild using CMake/Ninja: passed.
Rust provider: .venv\Scripts\python.exe -m pytest tests\test_008_auth.py -k 'access_token_structure_in_subprocess or empty_access_token_rejected_before_pool_checkout or TestRawAccessTokenPoolKey' -q -p no:cacheprovider — 52 passed.
Warmed-pool regression: all four empty-bytes/empty-bytearray public/native combinations reproduced the bypass before this update; all now reject cleanly on both providers and verify a subsequent healthy checkout. Empty buffers are also covered in the non-pooled matrix.
Initial-fix 24-case isolated matrix per provider: all nine former Microsoft crashes prevented; 16 malformed buffers cleanly rejected per provider; no crashes or timeouts.
.venv\Scripts\python.exe -m pre_commit run black-check --all-files --hook-stage pre-push — passed.
Limitations: Validation used Windows x64, Python 3.13, and SQL Server 2019 LocalDB. Actual Entra credential rejection, other platforms/architectures, and the full repository suite were not verified. This change validates buffer structure, not credential validity.
Unix / SQL Server 2022: Python 3.12.3, x86_64, SQL 16.0.4325.4; 5 paired comparisons and 1 warmup.
Unix / SQL Server 2025: Python 3.12.3, x86_64, SQL 17.0.5065.9; 5 paired comparisons and 1 warmup.
A consistent change requires more than 20% median paired movement, at least 1 ms between the median runtimes, and at least 80% of pairs exceeding the relative threshold in the same direction. A slowdown without enough pair agreement is reported as inconsistent.
The displayed change is the median of paired before-and-after ratios. It is not recalculated from the two displayed median runtimes.
Both revisions use profiling-enabled builds on the same agent and database, with alternating order and discarded warmups. Results are diagnostic and do not represent production-wheel latency.
Raw samples and logs are attached to the ADO run as profiler-* artifacts.
Adds defensive access-token buffer validation at the shared C++ attribute boundary to address the ODBC crashes reported in #278, without parsing JWTs or checking credential validity.
Changes:
Checks the length header, payload size and alignment, and ODBC’s signed size limit.
Adds bounded subprocess regressions for public connections, native connections, and deferred token factories.
Documents the raw-token format and validation behavior.
Reported runtime validation covers Windows x64, Python 3.13, and SQL Server 2019 LocalDB; other platforms remain unverified.
File
Description
tests/test_008_auth.py
Adds isolated malformed and correctly packed token probes.
README.md
Documents the raw-token structure and validation limits.
mssql_python/pybind/connection/connection.cpp
Validates token buffers before forwarding the attribute to ODBC.
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
Token validation can be bypassed during pooled connection reuse through same-token refreshes or nonstandard attribute keys, allowing inputs rejected on pool misses to succeed.
Near-expiry pool reuse bypasses this validator if a factory returns a str whose UTF-8 bytes match the stored token (connection_pool.cpp:163–169). The connection can then be reused with refreshed expiry even though the same factory output is rejected on a pool miss. Validate token attributes in both return paths of invokeTokenFactory() and add a regression for same-token refresh.
Use attribute-key conversion when validating pool hits
The pre-checkout lookup does not recognize every key that applyAttrsBefore() treats as attribute 1256: that method converts each key with py::cast<int> (lines 651–669), whereas this lookup uses dictionary equality. For example, an int subclass with value 1256 that compares unequal to plain integers can carry an empty token without triggering this check. A warmed pool then returns a token-free connection, although a pool miss rejects the same attributes. Iterate and convert keys using the same rules as applyAttrsBefore() before validating, so accepted attribute keys cannot bypass validation on pool hits.
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
The pooled-refresh path can accept a factory’s string token without validating the required bytes/bytearray type, leaving a validation bypass that needs a fix and regression coverage.
Deferred factory tokens are not validated on the near-expiry, same-token pool reuse path (connection_pool.cpp:164–169). A factory can return a Python str whose UTF-8 bytes match the stored token and a future expiry; the pool accepts it without calling setAttribute, bypassing the new bytes/bytearray requirement. Validate token attributes inside invokeTokenFactory before returning either supported result form, and add a pooled-refresh regression for this non-binary result.
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
Expiry-aware pool reuse can bypass validation and accept a non-binary factory token, requiring validation inside invokeTokenFactory and a near-expiry pooled regression test.
Validate both token factory return formats before comparison, normalize native attribute-key validation, and name the token header size. Add isolated refresh and key-conversion regressions with valid binary controls.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
Native crash-prevention changes require maintainer-confirmed runtime validation on macOS and Linux, as reported runs cover only Windows x64 and this review cannot run native probes.
Install checksum-pinned LCOV 2.6 to fix Python function-record HTML rendering, cover hit and unhit functions, and allow the full macOS suite to finish.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
The new regression test is not version-gated, so ordinary full-suite runs with Bash and LCOV 2.4 fail even though only the dedicated coverage job installs the fixed renderer.
Update the profiler pipeline contract for the intentional 120-minute macOS budget so the shared test no longer fails on every platform.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
The new test runs with any installed genhtml, including incompatible LCOV 2.4, causing failures outside the dedicated coverage job that pins version 2.6.
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
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.
Work Item / Issue Reference
Summary
Prevent malformed access-token buffers from crashing the ODBC driver or bypassing validation on pooled connections, using shared native validation before pool acquisition and ODBC attribute setting.
Validation
.venv\Scripts\python.exe -m pytest tests\test_008_auth.py -q -p no:cacheprovider— 249 passed..venv\Scripts\python.exe -m pytest tests\test_008_auth.py -k 'access_token_structure_in_subprocess or empty_access_token_rejected_before_pool_checkout or TestRawAccessTokenPoolKey' -q -p no:cacheprovider— 52 passed..venv\Scripts\python.exe -m pre_commit run black-check --all-files --hook-stage pre-push— passed.Limitations: Validation used Windows x64, Python 3.13, and SQL Server 2019 LocalDB. Actual Entra credential rejection, other platforms/architectures, and the full repository suite were not verified. This change validates buffer structure, not credential validity.