fix: MONGOSH-2914 properly sanitize HTTP output restrict unsafe file path access and escape shell arguments to prevent #2552
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary of Fixes
Detailed Jira Ticket 馃師MONGOSH-2914
packages/build/src/download-center/config.spec.ts
User input (
req.url
) was being written directly to the HTTP response usingres.end(req.url);
, making the application vulnerable to reflected cross-site scripting (XSS). A malicious actor could craft a specially formed URL containing HTML or JavaScript, which would be reflected back and executed in a victim鈥檚 browser.Fix:
escape-html
library.This ensures all HTML-sensitive characters are encoded safely before being returned in the response.
Prevents reflected XSS attacks while preserving the intended functionality of the HTTP handler.
packages/snippet-manager/src/snippet-manager.spec.ts
File paths were being constructed using unvalidated user input (
req.url
) via:This approach allowed potential directory traversal, enabling access to files outside the intended
fixtures
directory if crafted paths such as../../
were provided.Introduced a strict validation check using
path.resolve()
and prefix comparison.Ensured that only paths within the intended
fixtures
root are served.Updated the file path construction to:
Additional Adjustments:
fixturesRoot
once outside the request handler for reuse.Eliminates directory traversal risks and ensures that only authorized fixture files can be accessed or read during test operations.
packages/editor/src/editor.ts
The code dynamically constructed a shell command with:
When
shell: true
is enabled, unescaped input in the command arguments can lead to shell injection, where special characters in the file name could alter the executed command or inject arbitrary commands.Fix:
Imported the
shell-quote
module for secure shell argument escaping.Escaped the filename argument before passing it to
spawn
.Updated the code to:
Alternatively, to quote both command and arguments:
Prevents command injection while preserving the expected shell command behavior.
All user-influenced paths and arguments are now properly escaped before execution.
This fixes request improves the overall security posture of mongosh by introducing: