Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions packages/arg-parser/src/arg-mapper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ describe('arg-mapper.mapCliToDriver', () => {

context('when cli args have password', () => {
it('maps to auth object', () => {
expect(optionsTest({ password: 'aphextwin' })).to.deep.equal({
cs: 'mongodb://:aphextwin@localhost/'
expect(optionsTest({ password: 'aphextwin', username: 'x' })).to.deep.equal({
cs: 'mongodb://x:aphextwin@localhost/'
});
});
});
Expand Down Expand Up @@ -340,4 +340,21 @@ describe('arg-mapper.mapCliToDriver', () => {
});
});
});

context('when password is provided without username', () => {
const cliOptions: CliOptions = { password: '1234' };

it('throws a helpful error', () => {
expect(() => optionsTest(cliOptions)).to.throw(
"[COMMON-10001] Invalid connection information: Password specified but no username provided (did you mean '--port' instead of '-p'?)");
});
});
context('when password is provided without username and --port is already specified', () => {
const cliOptions: CliOptions = { password: '1234', port: '12345' };

it('throws a helpful error', () => {
expect(() => optionsTest(cliOptions)).to.throw(
'[COMMON-10001] Invalid connection information: Password specified but no username provided');
});
});
});
23 changes: 23 additions & 0 deletions packages/arg-parser/src/arg-mapper.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CommonErrors, MongoshInvalidInputError } from '@mongosh/errors';
import type { CliOptions, ConnectionInfo } from './';
import type { DevtoolsConnectOptions } from '@mongodb-js/devtools-connect';
import { ConnectionString, CommaAndColonSeparatedRecord } from 'mongodb-connection-string-url';
Expand Down Expand Up @@ -152,6 +153,8 @@ export function mapCliToDriver(options: Readonly<CliOptions>, i: Readonly<Connec
}
}

validateConnectionInfoAfterArgMapping(i, options);

return i;
}

Expand All @@ -167,4 +170,24 @@ function mapGSSAPIHostnameCanonicalization(value: string): AuthMechanismProps['C
return value as AuthMechanismProps['CANONICALIZE_HOST_NAME'];
}

function validateConnectionInfoAfterArgMapping(info: ConnectionInfo, originalOptions: CliOptions): void {
if (!info.connectionString) {
return;
}
// Provide a better error message for some cases in which applying
// command line options to the connection string can make it invalid.
const connectionString = new ConnectionString(info.connectionString, { looseValidation: true });
if (connectionString.password && !connectionString.username) {
let text = 'Invalid connection information: Password specified but no username provided';
if (originalOptions.password && !originalOptions.port) {
text += " (did you mean '--port' instead of '-p'?)";
}
throw new MongoshInvalidInputError(text, CommonErrors.InvalidArgument);
}
// Just make sure the result ultimately parses with strict validation.
// eslint-disable-next-line no-new
new ConnectionString(info.connectionString, { looseValidation: false });
}


export default mapCliToDriver;