Skip to content

Commit 5352581

Browse files
committed
fix(NODE-3924): Change to throw at connect time
1 parent c454e37 commit 5352581

File tree

4 files changed

+40
-48
lines changed

4 files changed

+40
-48
lines changed

src/connection_string.ts

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,30 +1096,10 @@ export const OPTIONS = {
10961096
}
10971097
},
10981098
tlsCAFile: {
1099-
transform({ name, values: [value] }) {
1100-
if (typeof value !== 'string') {
1101-
throw new MongoParseError(`${name} must be of type string`);
1102-
}
1103-
1104-
if (value.length === 0) {
1105-
throw new MongoParseError(`${name} must be have non-zero length`);
1106-
}
1107-
1108-
return value;
1109-
}
1099+
type: 'string'
11101100
},
11111101
tlsCertificateKeyFile: {
1112-
transform({ name, values: [value] }) {
1113-
if (typeof value !== 'string') {
1114-
throw new MongoParseError(`${name} must be of type string`);
1115-
}
1116-
1117-
if (value.length === 0) {
1118-
throw new MongoParseError(`${name} must be have non-zero length`);
1119-
}
1120-
1121-
return value;
1122-
}
1102+
type: 'string'
11231103
},
11241104
tlsCertificateKeyFilePassword: {
11251105
target: 'passphrase',

src/mongo_client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,10 +435,10 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> {
435435
const options = this[kOptions];
436436

437437
if (options.tls) {
438-
if (options.tlsCAFile) {
438+
if (typeof options.tlsCAFile === 'string') {
439439
options.ca ??= await fs.readFile(options.tlsCAFile, { encoding: 'utf8' });
440440
}
441-
if (options.tlsCertificateKeyFile) {
441+
if (typeof options.tlsCertificateKeyFile === 'string') {
442442
options.key ??= await fs.readFile(options.tlsCertificateKeyFile, { encoding: 'utf8' });
443443
}
444444
}

test/manual/tls_support.test.ts

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ describe('TLS Support', function () {
3737

3838
context('when tls filepaths are provided', () => {
3939
let client: MongoClient;
40+
afterEach(async () => {
41+
if (client) await client.close();
42+
});
43+
4044
context('when tls filepaths have length > 0', () => {
4145
beforeEach(async () => {
4246
client = new MongoClient(CONNECTION_STRING, tlsSettings);
4347
});
4448

45-
afterEach(async () => {
46-
if (client) await client.close();
47-
});
48-
4949
it('should read in files async at connect time', async () => {
5050
expect(client.options).property('tlsCAFile', TLS_CA_FILE);
5151
expect(client.options).property('tlsCertificateKeyFile', TLS_CERT_KEY_FILE);
@@ -73,6 +73,38 @@ describe('TLS Support', function () {
7373
});
7474
});
7575
});
76+
77+
context('when tlsCAFile has length === 0', () => {
78+
beforeEach(() => {
79+
client = new MongoClient(CONNECTION_STRING, {
80+
tls: true,
81+
tlsCAFile: '',
82+
tlsCertificateKeyFile: TLS_CERT_KEY_FILE
83+
});
84+
});
85+
86+
it('should throw an error at connect time', async () => {
87+
const err = await client.connect().catch(e => e);
88+
89+
expect(err).to.be.instanceof(Error);
90+
});
91+
});
92+
93+
context('when tlsCertificateKeyFile has length === 0', () => {
94+
beforeEach(() => {
95+
client = new MongoClient(CONNECTION_STRING, {
96+
tls: true,
97+
tlsCAFile: TLS_CA_FILE,
98+
tlsCertificateKeyFile: ''
99+
});
100+
});
101+
102+
it('should throw an error at connect time', async () => {
103+
const err = await client.connect().catch(e => e);
104+
105+
expect(err).to.be.instanceof(Error);
106+
});
107+
});
76108
});
77109
});
78110

test/unit/mongo_client.test.js

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -455,26 +455,6 @@ describe('MongoOptions', function () {
455455
expect(optionsUndefined.checkServerIdentity).to.equal(undefined);
456456
});
457457

458-
context('when tlsCAPath is provided as an empty string', function () {
459-
it('throws MongoParseError', function () {
460-
expect(() => {
461-
parseOptions('mongodb://localhost:27017/?tls=true', {
462-
tlsCAFile: ''
463-
});
464-
}).to.throw(MongoParseError);
465-
});
466-
});
467-
468-
context('when tlsCertificateKeyFile is provided as an empty string', function () {
469-
it('throws MongoParseError', function () {
470-
expect(() => {
471-
parseOptions('mongodb://localhost:27017/?tls=true', {
472-
tlsCertificateKeyFile: ''
473-
});
474-
}).to.throw(MongoParseError);
475-
});
476-
});
477-
478458
describe('compressors', function () {
479459
it('can be set when passed in as an array in the options object', function () {
480460
const clientViaOpt = new MongoClient('mongodb://localhost', {

0 commit comments

Comments
 (0)