Repository navigation
fix: correct semicolon handling in parse() and quoting in build() - #299
Merged
Merged
Conversation
A `;;` in an unquoted value was read as an escaped `;`. So `Server=h;;Database=d` gave `server` the value `h;Database=d`, and the `Database` pair was lost. A `;` at the start of a key stayed in the key, so `;Server=h` gave the key `;server`. In the SqlClient connection string syntax, `;` separates pairs and extra semicolons before or between pairs are skipped. A value that contains `;` must be quoted. Outside a quoted value, doubling only escapes `=` in a key (`==`). The parser now ends an unquoted value at the first `;`, and skips whitespace and `;` before a key. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The parser added the first character of a key or value to the buffer without checking if it was a terminator. So `Key=;Encrypt=true` gave `key` the value `;Encrypt=true`, and the `Encrypt` pair was lost. A key that starts with an escaped `=` was also misread: `==Key=Value` gave the key `=` with the value `Key=Value`. The first character now goes through the same checks as the others. An empty value is now collected as an empty string. An empty key, for example in `=Value`, now throws "Malformed connection string", as SqlClient does. Before, the parser silently dropped such a pair, or joined it with the next pair when one followed (`=Value;Next=x` gave the key `=value;next`). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…uote
`build()` wrote these values without quotes, so `parse()` did not read
them back correctly. The parser trims unquoted values, so ` secret `
came back as `secret`. The parser also reads a leading `'` or `"` as an
opening quote. So `'abc` threw "Connection string terminated
unexpectedly", and `"x"` came back as `x`.
`build()` now wraps these values in `{...}`, as it does for values that
contain other special characters.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`build()` wrote a key that contains `=` as it was, and `parse()` ended
the key at the first `=`. For example, `build({ 'a=b': '1' })` gave
`a=b=1`, which parsed as the key `a` with the value `b=1`.
`build()` now writes each `=` in a key as `==`. The parser reads `==`
in a key as a literal `=`.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
🔵 Needs a closer look
Core parse()/build() behavior changes would automatically reach node-mssql consumers on ^1.0.0, requiring maintainers to confirm whether a patch release is semver-compatible.
No open issues 🎉
What changed in this PR
Corrects connection-string parsing and serialization to address #298 and the empty-value issue in #279.
Changes:
- Fixes repeated semicolons, empty values, and escaped
=characters in keys; rejects empty keys. - Preserves whitespace and leading quotes in built values, and escapes
=in keys. - Adds parser regression cases and builder round-trip tests.
| File | Description |
|---|---|
test/parser/fixture/connection-strings.json |
Adds separator, empty-value, and escaped-key fixtures. |
test/parser/connection-string.test.ts |
Tests rejection of empty keys. |
test/builder/builder.test.ts |
Tests value quoting, key escaping, and round trips. |
src/parser/connection-string-parser.ts |
Corrects separator and first-character handling. |
src/builder/index.ts |
Quotes sensitive values and escapes = in keys. |
Review effort: Max
|
🎉 This PR is included in version 1.1.1 🎉 The release is available on: Your semantic-release bot 📦🚀 |
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 PR fixes the parser and builder problems reported in #298. It also covers item 1 of #279, because the cause is in the same part of the parser.
Changes
parse()skips repeated semicolons between pairs. Before,;;in an unquoted value was read as an escaped;, soServer=h;;Database=dgaveserverthe valueh;Database=d. A leading;also stayed in the first key. Now;always ends an unquoted value, and whitespace and extra;before a key are skipped, as in SqlClient. Doubling is still an escape for=in keys (==) and for closing quotes inside quoted values.parse()checks the first character of a key or value. Before, the first character skipped the terminator checks. SoKey=;Encrypt=truegavekeythe value;Encrypt=true, and==Key=Valuegave the key=. An empty value is now'', and a key can start with==. An empty key, for example in=Value, now throwsMalformed connection string, as SqlClient does. Before, that pair was dropped, or joined with the next pair.build()quotes values with leading or trailing whitespace, or with a leading'or". These values now round-trip throughparse(). For example,build({ Password: ' secret ' })givesPassword={ secret }.build()escapes=in keys as==. For example,build({ 'a=b': '1' })givesa==b=1, which parses back to the keya=b.Behaviour changes
These changes follow the SqlClient syntax that the README links to, but they can change results for existing input:
;;in an unquoted value no longer gives a literal;. A value that contains;must be quoted, for examplePassword={a;b}.parse()now throws on an empty key.build()now keeps the quotes on a value that is already wrapped in'...'or"...". For example,build({ Authentication: '"Active Directory Default"' })givesAuthentication={"Active Directory Default"}, which parses back with the quotes.Not in this PR
(#279: the{a}already-quoted check, the\|\(pattern inneedsQuotes, and boolean coercion.build()writes: ODBC{}quotes or SqlClient'and"quotes.build()does not check keys that the syntax can't represent, such as an empty key or a key that starts with;.Testing
masterand pass on this branch.npm run lint,npm run test:coverageandnpm run buildpass on each commit. The tests pass on Node 20, 22 and 24.parse()with a port of SqlClient'sDbConnectionOptions.GetKeyValuePairfor every string up to 7 characters over two small alphabets of separators, quotes and letters (7.8 million strings). There are no differences, apart from three known ones: this library accepts{}quotes, accepts an unquoted value that ends in a quote, and drops a last key that has no=. The same check onmastergives 126,744 differences for strings up to 6 characters.{a}case in Parser swallows the pair after an empty value; builder mis-quotes brace-wrapped values and a bare(#279 and keys that the syntax can't represent.Closes #298
🤖 Generated with Claude Code