Skip to content

fix(compass-collection): harden quoting of connection string in mock data generator COMPASS-11390 - #8714

Open
dudaschar wants to merge 2 commits into
mainfrom
COMPASS-11390
Open

dudaschar wants to merge 2 commits into
mainfrom
COMPASS-11390

Conversation

@dudaschar

@dudaschar dudaschar commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

Description

The mongosh command 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/--password flow).

Added unit tests with exact expected output for each encoded character in the host, database name and query.

Checklist

  • New tests and/or benchmarks are included
  • Documentation is changed or added
  • If this change updates the UI, screenshots/videos are added and a design review is requested
  • If this change could impact the load on the MongoDB cluster, please describe the expected and worst case impact
  • I have signed the MongoDB Contributor License Agreement (https://www.mongodb.com/legal/contributor-agreement)

Motivation and Context

  • Bugfix
  • New feature
  • Dependency update
  • Misc

Open Questions

Dependents

Types of changes

  • Backport Needed
  • Patch (non-breaking change which fixes an issue)
  • Minor (non-breaking change which adds functionality)
  • Major (fix or feature that would cause existing functionality to change)

@dudaschar dudaschar self-assigned this Nov 19, 2026
@dudaschar
dudaschar requested a review from a team as a code owner November 19, 2026 16:36
@dudaschar
dudaschar requested review from ivandevp and a balanced review from Copilot November 19, 2026 16:36
@dudaschar dudaschar changed the title fix(compass-collection): harden quoting of connection string in mock data generator mongosh command fix(compass-collection): harden quoting of connection string in mock data generator COMPASS-11390 Nov 19, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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);

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can't be fixed with parsing and the focus is to escape commands, env names are fine if kept.

@ivandevp ivandevp left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

// 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) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 🤔

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants