diff --git a/packages/compass-collection/src/components/mock-data-generator-modal/mongosh-command.spec.ts b/packages/compass-collection/src/components/mock-data-generator-modal/mongosh-command.spec.ts index 13f5d7d6efa..eae47447e88 100644 --- a/packages/compass-collection/src/components/mock-data-generator-modal/mongosh-command.spec.ts +++ b/packages/compass-collection/src/components/mock-data-generator-modal/mongosh-command.spec.ts @@ -17,4 +17,46 @@ describe('getMongoshCommand', () => { ); }); } + + const encodings = [ + ['$(value)', '%24(value)'], + ['`value`', '%60value%60'], + ['";value', '%22;value'], + ['\\', '%5C'], + ['!value', '%21value'], + ['\u201Cvalue\u201D', '%E2%80%9Cvalue%E2%80%9D'], + [';', ';'], + ["it's", "it's"], + ]; + + for (const [input, encoded] of encodings) { + it(`encodes ${JSON.stringify(input)} as ${encoded}`, () => { + const getCommand = (connectionString: string) => + getMongoshCommand({ + id: 'test', + connectionOptions: { connectionString }, + }).command; + + expect(getCommand(`mongodb://h${input}:27017/`)).to.equal( + `mongosh "mongodb://h${encoded}:27017/" --file mockdatascript.js` + ); + expect(getCommand(`mongodb://localhost/db${input}`)).to.equal( + `mongosh "mongodb://localhost/db${encoded}" --file mockdatascript.js` + ); + expect(getCommand(`mongodb://localhost/?appName=${input}`)).to.equal( + `mongosh "mongodb://localhost/?appName=${encoded}" --file mockdatascript.js` + ); + }); + } + + it('round-trips to the same URI after decoding', () => { + const { command } = getMongoshCommand({ + id: 'test', + connectionOptions: { + connectionString: 'mongodb://localhost/db?appName=$(x)%60\\', + }, + }); + const quoted = command.match(/^mongosh "([^"]*)"/)?.[1] as string; + expect(new URL(quoted).searchParams.get('appName')).to.equal('$(x)`\\'); + }); }); diff --git a/packages/compass-collection/src/components/mock-data-generator-modal/mongosh-command.ts b/packages/compass-collection/src/components/mock-data-generator-modal/mongosh-command.ts index d6f0f78dd79..b799abaf4d9 100644 --- a/packages/compass-collection/src/components/mock-data-generator-modal/mongosh-command.ts +++ b/packages/compass-collection/src/components/mock-data-generator-modal/mongosh-command.ts @@ -4,6 +4,16 @@ import ConnectionString, { } from 'mongodb-connection-string-url'; import { DEFAULT_CONNECTION_STRING_FALLBACK } from './constants'; +// Percent-encodes characters that bash/zsh and PowerShell treat specially +// inside double quotes, so the argument stays literal and drivers decode it +// back to the same URI. +function encodeShell(uri: string) { + return uri.replace(/["$`\\!]|[^\x20-\x7e]/gu, (char) => + // encodeURIComponent leaves "!" as is. + char === '!' ? '%21' : encodeURIComponent(char) + ); +} + export function getMongoshCommand(connectionInfo: ConnectionInfo) { const isAtlas = !!connectionInfo.atlasMetadata; const uri = @@ -42,7 +52,7 @@ export function getMongoshCommand(connectionInfo: ConnectionInfo) { const command = [ 'mongosh', - `"${connectionString}"`, + `"${encodeShell(connectionString)}"`, ...(needsUsername ? ['--username', '""'] : []), '--file', 'mockdatascript.js',