Repository navigation
fix(compass-collection): harden quoting of connection string in mock data generator COMPASS-11390 - #8714
fix(compass-collection): harden quoting of connection string in mock data generator COMPASS-11390#8714dudaschar wants to merge 2 commits into
Conversation
…data generator mongosh command
There was a problem hiding this comment.
🟡 Changes recommended
The generated mongosh command can alter URI arguments because cmd.exe expands percent-delimited environment references and interactive Bash expands exclamation-mark history references, even inside double quotes.
2 open issues
What changed in this PR
This PR hardens connection-string handling in the Mock Data Generator’s copyable mongosh command. The change is focused, but cross-shell correctness remains the main concern.
Changes:
- Adds percent-encoding before quoting the connection string.
- Adds exact-output tests for special characters and a URI-decoding check.
| File | Description |
|---|---|
| packages/compass-collection/src/components/mock-data-generator-modal/mongosh-command.ts | Encodes selected characters in the generated command’s connection string. |
| packages/compass-collection/src/components/mock-data-generator-modal/mongosh-command.spec.ts | Tests encoded output across URI components and query-value decoding. |
Review effort: Balanced
| // 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); |
There was a problem hiding this comment.
This can't be fixed with parsing and the focus is to escape commands, env names are fine if kept.
| // 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) { |
There was a problem hiding this comment.
nit: Maybe encodeShell is a bit vague name for what it's actually doing. Maybe something like escapeForShellDoubleQuotes can describe better what's doing here 🤔


Description
The
mongoshcommand shown in the Mock Data Generator modal embeds the connection string in double quotes. This change percent-encodes characters that bash/zsh, PowerShell and cmd.exe treat specially inside double quotes (",$,`,\, control and non-ASCII characters), so the pasted command is always interpreted literally. Drivers decode percent-encoding, so the connection string means the same thing, and the command format is otherwise unchanged (including the Atlas--username/--passwordflow).Added unit tests with exact expected output for each encoded character in the host, database name and query.
Checklist
Motivation and Context
Open Questions
Dependents
Types of changes