From d6af6bb2f22f0a8e119745626179edb4ed0b8a18 Mon Sep 17 00:00:00 2001 From: Eduarda Scharnhorst Date: Thu, 19 Nov 2026 16:29:58 +0000 Subject: [PATCH 1/2] fix(compass-collection): harden quoting of connection string in mock data generator mongosh command --- .../mongosh-command.spec.ts | 41 +++++++++++++++++++ .../mongosh-command.ts | 9 +++- 2 files changed, 49 insertions(+), 1 deletion(-) 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..1924b232cab 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,45 @@ describe('getMongoshCommand', () => { ); }); } + + const encodings = [ + ['$(value)', '%24(value)'], + ['`value`', '%60value%60'], + ['";value', '%22;value'], + ['\\', '%5C'], + ['\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..eb5608899b0 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,13 @@ import ConnectionString, { } from 'mongodb-connection-string-url'; import { DEFAULT_CONNECTION_STRING_FALLBACK } from './constants'; +// Percent-encodes characters that bash/zsh, PowerShell or cmd.exe treat +// specially inside double quotes. The argument stays literal in every shell +// and drivers decode it back to the same URI. +function encodeShell(uri: string) { + return uri.replace(/["$`\\]|[^\x20-\x7e]/gu, encodeURIComponent); +} + export function getMongoshCommand(connectionInfo: ConnectionInfo) { const isAtlas = !!connectionInfo.atlasMetadata; const uri = @@ -42,7 +49,7 @@ export function getMongoshCommand(connectionInfo: ConnectionInfo) { const command = [ 'mongosh', - `"${connectionString}"`, + `"${encodeShell(connectionString)}"`, ...(needsUsername ? ['--username', '""'] : []), '--file', 'mockdatascript.js', From 36fe01a4a1bb4e15c6fac650cf437c120545525d Mon Sep 17 00:00:00 2001 From: Eduarda Scharnhorst Date: Thu, 19 Nov 2026 16:47:17 +0000 Subject: [PATCH 2/2] fix(mongosh-command): add encoding for '!' character in shell command --- .../mock-data-generator-modal/mongosh-command.spec.ts | 1 + .../mock-data-generator-modal/mongosh-command.ts | 11 +++++++---- 2 files changed, 8 insertions(+), 4 deletions(-) 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 1924b232cab..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 @@ -23,6 +23,7 @@ describe('getMongoshCommand', () => { ['`value`', '%60value%60'], ['";value', '%22;value'], ['\\', '%5C'], + ['!value', '%21value'], ['\u201Cvalue\u201D', '%E2%80%9Cvalue%E2%80%9D'], [';', ';'], ["it's", "it's"], 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 eb5608899b0..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,11 +4,14 @@ import ConnectionString, { } from 'mongodb-connection-string-url'; import { DEFAULT_CONNECTION_STRING_FALLBACK } from './constants'; -// Percent-encodes characters that bash/zsh, PowerShell or cmd.exe treat -// specially inside double quotes. The argument stays literal in every shell -// and drivers decode it back to the same URI. +// 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, encodeURIComponent); + return uri.replace(/["$`\\!]|[^\x20-\x7e]/gu, (char) => + // encodeURIComponent leaves "!" as is. + char === '!' ? '%21' : encodeURIComponent(char) + ); } export function getMongoshCommand(connectionInfo: ConnectionInfo) {