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
### Work Item / Issue Reference
<!--
IMPORTANT: Please follow the PR template guidelines below.
For mssql-python maintainers: Insert your ADO Work Item ID below (e.g.
AB#37452)
For external contributors: Insert Github Issue number below (e.g. #149)
Only one reference is required - either GitHub issue OR ADO Work Item.
-->
<!-- mssql-python maintainers: ADO Work Item -->
>
[AB#39049](https://sqlclientdrivers.visualstudio.com/c6d89619-62de-46a0-8b46-70b92a84d85e/_workitems/edit/39049)
<!-- External contributors: GitHub Issue -->
> GitHub Issue: #250
-------------------------------------------------------------------
### Summary
This pull request introduces significant improvements to encoding and
decoding handling in the MSSQL Python driver, focusing on thread safety,
security, and robustness. The main changes include stricter validation
and enforcement of encoding rules for SQL_WCHAR types, making
encoding/decoding settings thread-safe, and updating cursor methods to
consistently use these settings. This ensures correct handling of
Unicode data, prevents ambiguous encoding scenarios, and improves
reliability in multi-threaded environments.
**Encoding and Decoding Validation & Enforcement**
* Enforced strict validation so that only `'utf-16le'` and `'utf-16be'`
encodings are accepted for SQL_WCHAR, explicitly rejecting `'utf-16'`
with BOM due to byte order ambiguity. Programming errors are raised if
invalid encodings are used, both in `setencoding` and `setdecoding`
methods.
* Added validation to ensure encoding names only contain safe characters
and are of reasonable length, preventing security issues and
denial-of-service attacks.
**Thread Safety**
* Introduced a re-entrant lock (`_encoding_lock`) to protect encoding
and decoding settings, making `setencoding`, `setdecoding`,
`getencoding`, and `getdecoding` thread-safe and preventing race
conditions.
**Cursor Integration**
* Updated cursor methods (`execute`, `executemany`, `fetchone`,
`fetchmany`, `fetchall`) to retrieve encoding and decoding settings from
the connection and pass them to low-level bindings, ensuring consistent
Unicode handling throughout query execution and result fetching
**Error Handling and Logging**
* Improved error handling in cursor encoding/decoding retrieval, logging
warnings if settings cannot be accessed due to database errors and
falling back to safe defaults.
**Bindings Interface Update**
* Updated the C++ binding for parameter encoding to accept an explicit
encoding argument, supporting the new encoding flow from Python.
0 commit comments