fix(mssql): sanitize brackets in connection-URL userinfo before parse - #2447
Conversation
mssql:// URLs with '[' or ']' in credentials (e.g. mssql://user:p[a]ss@host/db) raised ValueError from urlparse's IPv6 netloc check, so such connections could never be opened. Mirror the MySQL connector's userinfo sanitiser (_parse_mysql_connection_url) so bracketed credentials parse while legitimate bracketed IPv6 hosts still work.
WalkthroughThis PR adds a helper function to pre-sanitize MSSQL connection URLs by escaping bracket characters within the userinfo segment before URL parsing, preventing misinterpretation as IPv6 delimiters. Two unit tests validate credential bracket handling and legitimate IPv6 host parsing. ChangesMSSQL URL Parsing Fix
Estimated code review effort: 2 (Simple) | ~10 minutes Sequence Diagram(s)sequenceDiagram
participant Caller
participant ConnectMssqlFromUrl as _connect_mssql_from_url
participant ParseHelper as _parse_mssql_connection_url
participant Urlparse as urllib.parse.urlparse
participant PyodbcConnect as _connect_mssql_pyodbc
Caller->>ConnectMssqlFromUrl: connection_url
ConnectMssqlFromUrl->>ParseHelper: raw connection_url
ParseHelper->>ParseHelper: escape [ ] in userinfo segment
ParseHelper->>Urlparse: sanitized URL
Urlparse-->>ParseHelper: parsed components
ParseHelper-->>ConnectMssqlFromUrl: parsed URL result
ConnectMssqlFromUrl->>ConnectMssqlFromUrl: validate scheme/host/database
ConnectMssqlFromUrl->>PyodbcConnect: build connection with unquoted UID/PWD
PyodbcConnect-->>Caller: connection
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
core/wren/src/wren/connector/mssql.py (1)
340-369: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚖️ Poor tradeoffExtract the shared userinfo bracket-sanitization helper.
The MSSQL and MySQL parsers use the same bracket-escaping logic; moving it into a shared URL utility would keep the two implementations from drifting.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@core/wren/src/wren/connector/mssql.py` around lines 340 - 369, The MSSQL URL parser `_parse_mssql_connection_url` duplicates the same userinfo bracket-escaping logic already used by the MySQL parser, so extract that sanitization into a shared URL utility and have both parsers call it. Keep the parsing behavior in `_parse_mssql_connection_url` the same, but move the common `userinfo` sanitization/authority-splitting logic into a reusable helper to prevent the MSSQL and MySQL implementations from drifting.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@core/wren/src/wren/connector/mssql.py`:
- Around line 340-369: The MSSQL URL parser `_parse_mssql_connection_url`
duplicates the same userinfo bracket-escaping logic already used by the MySQL
parser, so extract that sanitization into a shared URL utility and have both
parsers call it. Keep the parsing behavior in `_parse_mssql_connection_url` the
same, but move the common `userinfo` sanitization/authority-splitting logic into
a reusable helper to prevent the MSSQL and MySQL implementations from drifting.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: e89ae179-0696-4ee2-ad4f-e466ea57f402
📒 Files selected for processing (2)
core/wren/src/wren/connector/mssql.pycore/wren/tests/unit/test_mssql_connection.py
Summary
mssql://connection URLs containing[or]in the userinfo (username/password) crashed withValueErrorfromurlparse's IPv6 netloc check.Motivation
_connect_mssql_from_urlcalledurlparse(connection_url)directly. Python'surlparsetreats[/]anywhere in the netloc as IPv6 host delimiters, so a URL likemssql://user:p[a]ss@host:1433/dbraisesValueError: 'host' does not appear to be an IPv4 or IPv6 addressbefore any connection is attempted — the credential can never be used.The MySQL connector already solved this exact problem with
_parse_mysql_connection_url, which escapes brackets in the userinfo segment only. This change mirrors that helper for MSSQL (_parse_mssql_connection_url), keeping valid IPv6 hosts (mssql://user:pass@[::1]:1433/db) intact.Verification
Regression test proof (fails without the fix):
The
ipv6test passes both with and without the fix, confirming no regression for legitimate bracketed IPv6 hosts.Apache-2.0 path (
core/**). One commit, two files.Summary by CodeRabbit
Bug Fixes
[or]are parsed correctly.Tests