Skip to content

Commit

Permalink
fix(NODE-3921): error on invalid TLS option combinations (#3405)
Browse files Browse the repository at this point in the history
  • Loading branch information
biniona-mongodb committed Oct 4, 2022
1 parent dc62bcb commit 1a550df
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 19 deletions.
17 changes: 8 additions & 9 deletions src/connection_string.ts
Expand Up @@ -29,7 +29,6 @@ import { ReadConcern, ReadConcernLevel } from './read_concern';
import { ReadPreference, ReadPreferenceMode } from './read_preference';
import type { TagSet } from './sdam/server_description';
import {
AnyOptions,
DEFAULT_PK_FACTORY,
emitWarning,
emitWarningOnce,
Expand Down Expand Up @@ -157,14 +156,14 @@ export async function resolveSRVRecord(options: MongoOptions): Promise<HostAddre
/**
* Checks if TLS options are valid
*
* @param options - The options used for options parsing
* @throws MongoParseError if TLS options are invalid
* @param allOptions - All options provided by user or included in default options map
* @throws MongoAPIError if TLS options are invalid
*/
export function checkTLSOptions(options: AnyOptions): void {
if (!options) return;
function checkTLSOptions(allOptions: CaseInsensitiveMap): void {
if (!allOptions) return;
const check = (a: string, b: string) => {
if (Reflect.has(options, a) && Reflect.has(options, b)) {
throw new MongoParseError(`The '${a}' option cannot be used with '${b}'`);
if (allOptions.has(a) && allOptions.has(b)) {
throw new MongoAPIError(`The '${a}' option cannot be used with the '${b}' option`);
}
};
check('tlsInsecure', 'tlsAllowInvalidCertificates');
Expand Down Expand Up @@ -360,6 +359,8 @@ export function parseOptions(
}
}

checkTLSOptions(allOptions);

const unsupportedOptions = setDifference(
allKeys,
Array.from(Object.keys(OPTIONS)).map(s => s.toLowerCase())
Expand Down Expand Up @@ -427,8 +428,6 @@ export function parseOptions(
mongoOptions.dbName = 'test';
}

checkTLSOptions(mongoOptions);

if (options.promiseLibrary) {
PromiseProvider.set(options.promiseLibrary);
}
Expand Down
10 changes: 0 additions & 10 deletions test/unit/assorted/uri_options.spec.test.ts
Expand Up @@ -10,16 +10,6 @@ describe('URI option spec tests', function () {
// Skipped because this does not apply to Node
'Valid options specific to single-threaded drivers are parsed correctly',

// TODO(NODE-3921): fix tls option validation
'tlsInsecure and tlsAllowInvalidCertificates both present (and true) raises an error',
'tlsInsecure and tlsAllowInvalidCertificates both present (and false) raises an error',
'tlsAllowInvalidCertificates and tlsInsecure both present (and true) raises an error',
'tlsAllowInvalidCertificates and tlsInsecure both present (and false) raises an error',
'tlsAllowInvalidHostnames and tlsInsecure both present (and true) raises an error',
'tlsAllowInvalidHostnames and tlsInsecure both present (and false) raises an error',
'tlsInsecure and tlsAllowInvalidHostnames both present (and true) raises an error',
'tlsInsecure and tlsAllowInvalidHostnames both present (and false) raises an error',

// TODO(NODE-3922): have not implemented option support
'tlsDisableCertificateRevocationCheck can be set to true',
'tlsDisableCertificateRevocationCheck can be set to false',
Expand Down

0 comments on commit 1a550df

Please sign in to comment.