Skip to content

Commit a769cf8

Browse files
authored
fix(NODE-3166): allowInvalidHostnames and allowInvalidCertificates flags are ignored (#2784)
1 parent 76b110e commit a769cf8

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

src/connection_string.ts

+14-4
Original file line numberDiff line numberDiff line change
@@ -938,10 +938,18 @@ export const OPTIONS = {
938938
type: 'boolean'
939939
},
940940
tlsAllowInvalidCertificates: {
941-
type: 'boolean'
941+
target: 'rejectUnauthorized',
942+
transform({ name, values: [value] }) {
943+
// allowInvalidCertificates is the inverse of rejectUnauthorized
944+
return !getBoolean(name, value);
945+
}
942946
},
943947
tlsAllowInvalidHostnames: {
944-
type: 'boolean'
948+
target: 'checkServerIdentity',
949+
transform({ name, values: [value] }) {
950+
// tlsAllowInvalidHostnames means setting the checkServerIdentity function to a noop
951+
return getBoolean(name, value) ? () => undefined : undefined;
952+
}
945953
},
946954
tlsCAFile: {
947955
target: 'ca',
@@ -969,10 +977,12 @@ export const OPTIONS = {
969977
transform({ name, options, values: [value] }) {
970978
const tlsInsecure = getBoolean(name, value);
971979
if (tlsInsecure) {
972-
options.checkServerIdentity = undefined;
980+
options.checkServerIdentity = () => undefined;
973981
options.rejectUnauthorized = false;
974982
} else {
975-
options.checkServerIdentity = options.tlsAllowInvalidHostnames ? undefined : (true as any);
983+
options.checkServerIdentity = options.tlsAllowInvalidHostnames
984+
? () => undefined
985+
: undefined;
976986
options.rejectUnauthorized = options.tlsAllowInvalidCertificates ? false : true;
977987
}
978988
return tlsInsecure;

test/unit/mongo_client_options.test.js

+40
Original file line numberDiff line numberDiff line change
@@ -296,4 +296,44 @@ describe('MongoOptions', function () {
296296
expect(options.credentials.username).to.equal('USERNAME');
297297
expect(options.credentials.password).to.equal('PASSWORD');
298298
});
299+
300+
it('transforms tlsAllowInvalidCertificates and tlsAllowInvalidHostnames correctly', function () {
301+
const optionsTrue = parseOptions('mongodb://localhost/', {
302+
tlsAllowInvalidCertificates: true,
303+
tlsAllowInvalidHostnames: true
304+
});
305+
expect(optionsTrue.rejectUnauthorized).to.equal(false);
306+
expect(optionsTrue.checkServerIdentity).to.be.a('function');
307+
expect(optionsTrue.checkServerIdentity()).to.equal(undefined);
308+
309+
const optionsFalse = parseOptions('mongodb://localhost/', {
310+
tlsAllowInvalidCertificates: false,
311+
tlsAllowInvalidHostnames: false
312+
});
313+
expect(optionsFalse.rejectUnauthorized).to.equal(true);
314+
expect(optionsFalse.checkServerIdentity).to.equal(undefined);
315+
316+
const optionsUndefined = parseOptions('mongodb://localhost/');
317+
expect(optionsUndefined.rejectUnauthorized).to.equal(undefined);
318+
expect(optionsUndefined.checkServerIdentity).to.equal(undefined);
319+
});
320+
321+
it('transforms tlsInsecure correctly', function () {
322+
const optionsTrue = parseOptions('mongodb://localhost/', {
323+
tlsInsecure: true
324+
});
325+
expect(optionsTrue.rejectUnauthorized).to.equal(false);
326+
expect(optionsTrue.checkServerIdentity).to.be.a('function');
327+
expect(optionsTrue.checkServerIdentity()).to.equal(undefined);
328+
329+
const optionsFalse = parseOptions('mongodb://localhost/', {
330+
tlsInsecure: false
331+
});
332+
expect(optionsFalse.rejectUnauthorized).to.equal(true);
333+
expect(optionsFalse.checkServerIdentity).to.equal(undefined);
334+
335+
const optionsUndefined = parseOptions('mongodb://localhost/');
336+
expect(optionsUndefined.rejectUnauthorized).to.equal(undefined);
337+
expect(optionsUndefined.checkServerIdentity).to.equal(undefined);
338+
});
299339
});

0 commit comments

Comments
 (0)