Repository navigation
feat: send the instance name in PRELOGIN and allow a port with an instance name - #1856
Draft
arthurschreiber wants to merge 3 commits into
Draft
arthurschreiber wants to merge 3 commits into
arthurschreiber wants to merge 3 commits into
Conversation
…tance name Port the remaining parts of #1491 to current master: - Send the configured instance name (or, after a redirect, the routing target's instance name) in the PRELOGIN `INSTOPT` option, and fail the connection with `EINSTANCE` if the server rejects it. - Allow `port` and `instanceName` to be set together. The port is then used directly, without a SQL Server Browser lookup, and the instance name is still sent for the server to verify. Co-authored-by: MichaelSun90 <47126816+MichaelSun90@users.noreply.github.com> Co-authored-by: mShan0 <96149598+mShan0@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UtKcuXMU8g8KvcVkzpxu8q
Match what Microsoft.Data.SqlClient and the MS-TDS spec do with `INSTOPT`: - Send the instance name only when the port was looked up via the SQL Server Browser. With an explicit port, or after a redirect, send an empty instance name, so connections that work today keep working. SqlClient's managed networking never sends an instance name, and mssql-jdbc doesn't send `INSTOPT` at all. - Don't send `MSSQLSERVER`, which SQL Server 2008 R2 and older reject, or names that aren't printable ASCII, as the server compares them in its own code page. - Treat only `0x01` as a mismatch, like SqlClient, and report it as `EINSTLOOKUP` instead of a new `EINSTANCE` code, since it means the Browser returned a port of a different instance. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UtKcuXMU8g8KvcVkzpxu8q
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1856 +/- ##
==========================================
+ Coverage 85.24% 85.43% +0.19%
==========================================
Files 97 97
Lines 4778 4787 +9
Branches 896 900 +4
==========================================
+ Hits 4073 4090 +17
+ Misses 447 441 -6
+ Partials 258 256 -2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
…iew-dependencies-uiz27p-instance-name
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This ports the parts of #1491 (by @MichaelSun90 and @mShan0) that haven't landed yet onto current master. The routing part of #1491 (keeping the instance from a redirect target and sending
server\instancein LOGIN7) already landed in #1668.The PRELOGIN part was later narrowed to match what Microsoft.Data.SqlClient does. See "Findings" below.
Changes
porttogether withinstanceName. This combination used to throwPort and instanceName are mutually exclusive. Now both are accepted: the port is used directly, no SQL Server Browser lookup takes place, and the instance name is ignored. This is how SqlClient treatsserver\instance,port. With onlyinstanceName, the port is looked up as before. With neither, the port defaults to 1433.Instance name in PRELOGIN, to verify a Browser lookup. When the port was looked up via the SQL Server Browser, the instance name is sent in the PRELOGIN
INSTOPToption, null-terminated. If the server answers0x01(it is a different instance), the connection fails with aConnectionErrorwith codeEINSTLOOKUP. The message names the server, the port and the instance.In every other case an empty instance name (
0x00) is still sent, as before. The server always accepts an empty name. These cases are:portMSSQLSERVER, in any caseCompared with #1491:
INSTOPTresponse is read as the single status byte the protocol defines, not as a string.0x01counts as a mismatch, as in SqlClient.EINSTLOOKUPcode instead of a newEINSTANCEcode.instance-lookup.tsandsender.tsare left out.Findings
Sources:
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/.src/main/java/com/microsoft/sqlserver/jdbc/.Spec. MS-TDS 2.2.6.5 PRELOGIN:
0if the string is empty, or if it matches the instance name or "MSSQLServer" case-insensitively, using the server's locale. Otherwise it answers1.1.a. Which instance name do drivers send?
ManagedSni/SniProxy.netcore.csline 68:CreateConnectionHandlesetsinstanceName = new byte[1]and never fills it.DataSourceparsing ignores the instance when a port is given, and rejectsserver\mssqlserver.NativeSni/SNIHandle.csline 37 passes a 256-byte buffer that the native code fills. What it writes for a default instance, with an explicit port, or after a redirect can't be read from the source.Parser/TdsParser.cslines 1336–1347, inConsumePreLoginHandshake: the comment gives the purpose of the check. A mismatch means "the cached info we used to connect is not valid", in other words a stale SSRP (Browser) result.INSTOPTat all.SQLServerConnection.javalines 4593–4598: its PRELOGIN request has only VERSION, ENCRYPTION, TRACEID and optionally FEDAUTHREQUIRED.MSSQLSERVER.b. Encoding. The spec says MBCS, compared in the server's locale. The client can't know the server's code page, so only printable-ASCII names are sent. ASCII encodes the same way in the relevant code pages. Any other name is sent empty, which the server always accepts. In SqlClient, the encoding is done in native SNI and isn't visible.
c. Response byte, and retries.
0x01(Parser/TdsParser.cslines 1336–1347). tedious now does the same.Parser/TdsParser.cslines 575–636). The retry opens a new connection withflushCache: true, so the SSRP lookup runs again, and it sends the instance name again. If that also fails, it throwsSQL.InstanceFailure()("Instance failure.").d. Error code. An instance name is now only sent to verify a Browser result, so a mismatch means the lookup returned the port of a different instance. The existing
EINSTLOOKUPcode fits that, so no new code is added. SqlClient reports it as a genericInvalidOperationException.Remaining behavior change
When
instanceNameis set without aport, tedious now sends that name. A connection fails withEINSTLOOKUPif the server at the looked-up port says it is a different instance. Before this change, such a connection would have gone on to log in to whichever instance was listening there.Coverage against a real server
SQL2017. It connects through a Browser lookup with no port (test/config.appveyor.ts), which is exactly the path that now sendsINSTOPT. The suite passes on Node.js 22, 24 and 26 fordcd0ffe(build).setup-sqlserver. Adding a named instance there would mean installing a second SQL Server instance and starting the SQL Browser on the Windows runners. That is a larger CI change and isn't part of this PR.Testing
Unit tests:
prelogin-payload-test.ts: encoding the instance name, and reading a rejecting response.connection-config-validation.ts: port defaults, andporttogether withinstanceName.connection-instance-name-test.ts(new), against a mock server with a stubbed Browser lookup:0x01fails withEINSTLOOKUPMSSQLSERVER,mssqlserverand a non-ASCII name are sent emptyrerouting-test.ts: after a redirect, an empty instance name is sent.Locally,
npm test(754 passing) andnpm run lint(eslint + tsc) pass. In CI, all GitHub Actions jobs and AppVeyor are green ondcd0ffe.