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