Skip to content

Commit 526c73f

Browse files
authored
fix(NODE-3413): accept tls=false in mongodb+srv connection strings (#2886)
1 parent 3d490dc commit 526c73f

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

src/connection_string.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,9 @@ export function parseOptions(
232232
if (isSRV) {
233233
// SRV Record is resolved upon connecting
234234
mongoOptions.srvHost = hosts[0];
235-
options.tls = true;
235+
if (!url.searchParams.has('tls') && !url.searchParams.has('ssl')) {
236+
options.tls = true;
237+
}
236238
}
237239

238240
const urlOptions = new CaseInsensitiveMap();

test/unit/core/mongodb_srv.test.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,14 @@ describe('mongodb+srv', function () {
3838
expect(result).to.exist;
3939
// Implicit SRV options must be set.
4040
expect(options.directConnection).to.be.false;
41-
expect(options.tls).to.be.true;
4241
const testOptions = test[1].options;
42+
if (testOptions && 'tls' in testOptions) {
43+
expect(options).to.have.property('tls', testOptions.tls);
44+
} else if (testOptions && 'ssl' in testOptions) {
45+
expect(options).to.have.property('tls', testOptions.ssl);
46+
} else {
47+
expect(options.tls).to.be.true;
48+
}
4349
if (testOptions && testOptions.replicaSet) {
4450
expect(options).to.have.property('replicaSet', testOptions.replicaSet);
4551
}

test/unit/mongo_client_options.test.js

+13
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,19 @@ describe('MongoOptions', function () {
229229
it('srvHost saved to options for later resolution', function () {
230230
const options = parseOptions('mongodb+srv://server.example.com/');
231231
expect(options).has.property('srvHost', 'server.example.com');
232+
expect(options).has.property('tls', true);
233+
});
234+
235+
it('ssl= can be used to set tls=false', function () {
236+
const options = parseOptions('mongodb+srv://server.example.com/?ssl=false');
237+
expect(options).has.property('srvHost', 'server.example.com');
238+
expect(options).has.property('tls', false);
239+
});
240+
241+
it('tls= can be used to set tls=false', function () {
242+
const options = parseOptions('mongodb+srv://server.example.com/?tls=false');
243+
expect(options).has.property('srvHost', 'server.example.com');
244+
expect(options).has.property('tls', false);
232245
});
233246

234247
it('supports ReadPreference option in url', function () {

0 commit comments

Comments
 (0)