From 37504da666caffda98a6dc4b7221846519f06709 Mon Sep 17 00:00:00 2001 From: Dan Hensby Date: Thu, 19 Nov 2026 14:50:24 +0000 Subject: [PATCH] fix: throw for a connection string number value that is not a number The connection string library reads a number value with parseInt, so a value such as "abc" gave NaN. For Max Pool Size, Min Pool Size, Packet Size and Connection Timeout, the connect then failed with a misleading error from tarn, the socket or the connect timer. Request Timeout used the default without a warning on tedious and had no timeout on msnodesqlv8. Connection Timeout also had no timeout on msnodesqlv8, and ConnectRetryInterval retried after 1 ms. The parser now throws an error that names the key and the value. The check applies only to the number keys that the parser uses, and only to values that parseInt cannot read, so "30s" still gives 30. An empty value still behaves the same as a left-out key. Co-Authored-By: Claude Opus 5.5 --- README.md | 2 ++ lib/base/connection-pool.js | 20 ++++++++++++------ test/common/unit.js | 42 +++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 84d0cf5d..f3103d52 100644 --- a/README.md +++ b/README.md @@ -663,6 +663,8 @@ Complete list of pool options can be found [here](https://github.com/vincit/tarn In addition to configuration object there is an option to pass config as a connection string. Connection strings are supported. +The number keys `Connection Timeout`, `ConnectRetryInterval`, `Max Pool Size`, `Min Pool Size`, `Packet Size` and `Request Timeout` must have a value that starts with a number. For a value such as `abc`, the parser throws an error that names the key and the value. + ##### Classic Connection String ###### Standard configuration using tedious driver diff --git a/lib/base/connection-pool.js b/lib/base/connection-pool.js index e11be7f6..23037556 100644 --- a/lib/base/connection-pool.js +++ b/lib/base/connection-pool.js @@ -148,6 +148,14 @@ class ConnectionPool extends EventEmitter { if (value === undefined) { return config } + // a number value that parseInt cannot read is a mistake, so a key that is used throws for it + const number = () => { + if (Number.isNaN(value)) { + const prop = [key, ...(NODE_MSSQL_SCHEMA[key].aliases ?? [])].find(k => result.has(k)) + throw new Error(`Invalid value for connection string key "${prop}": "${result.get(prop)}" is not a number.`) + } + return value + } switch (key) { case 'application name': break @@ -169,7 +177,7 @@ class ConnectionPool extends EventEmitter { break case 'connection timeout': Object.assign(config, { - connectionTimeout: value * 1000 + connectionTimeout: number() * 1000 }) break case 'connection lifetime': @@ -178,7 +186,7 @@ class ConnectionPool extends EventEmitter { break case 'connectretryinterval': Object.assign(config.options, { - connectionRetryInterval: value * 1000 + connectionRetryInterval: number() * 1000 }) break case 'context connection': @@ -257,12 +265,12 @@ class ConnectionPool extends EventEmitter { break case 'max pool size': Object.assign(config.pool, { - max: value + max: number() }) break case 'min pool size': Object.assign(config.pool, { - min: value + min: number() }) break case 'msi endpoint': @@ -286,7 +294,7 @@ class ConnectionPool extends EventEmitter { break case 'packet size': Object.assign(config.options, { - packetSize: value + packetSize: number() }) break case 'password': @@ -355,7 +363,7 @@ class ConnectionPool extends EventEmitter { break case 'request timeout': Object.assign(config, { - requestTimeout: parseInt(value, 10) + requestTimeout: number() }) break case 'stream': diff --git a/test/common/unit.js b/test/common/unit.js index a1d4a95d..7fb4663f 100644 --- a/test/common/unit.js +++ b/test/common/unit.js @@ -940,6 +940,48 @@ describe('connection string empty values', () => { }) }) +describe('connection string number values that are not a number', () => { + const keys = [ + 'Connection Timeout', + 'ConnectRetryInterval', + 'Max Pool Size', + 'Min Pool Size', + 'Packet Size', + 'Request Timeout' + ] + + for (const key of keys) { + it(`throws for ${key}=abc`, () => { + assert.throws(() => BasePool._parseConnectionString(`Server=localhost;${key}=abc`), { + name: 'Error', + message: `Invalid value for connection string key "${key.toLowerCase()}": "abc" is not a number.` + }) + }) + } + + it('ignores a number key that is not used', () => { + const expected = BasePool._parseConnectionString('Server=localhost') + assert.deepStrictEqual(BasePool._parseConnectionString('Server=localhost;Connection Lifetime=abc'), expected) + }) + + it('names the alias that the connection string uses', () => { + assert.throws(() => BasePool._parseConnectionString('Server=localhost;Connect Timeout=abc'), { + message: 'Invalid value for connection string key "connect timeout": "abc" is not a number.' + }) + }) + + it('throws from the ConnectionPool constructor', () => { + assert.throws(() => new ConnectionPool('Server=localhost;Max Pool Size=undefined'), { + message: 'Invalid value for connection string key "max pool size": "undefined" is not a number.' + }) + }) + + it('parses a value that starts with a number as that number', () => { + const config = BasePool._parseConnectionString('Server=localhost;Connection Timeout=30s') + assert.strictEqual(config.connectionTimeout, 30000) + }) +}) + describe('connection string auth - base', () => { it('parses basic login', () => { const config = BasePool._parseConnectionString('Server=database.test.com;Database=test;User Id=test;Password=admin')