From 041c0427bb53901a208c363dd32209ad75ca4cf3 Mon Sep 17 00:00:00 2001 From: Luca Rinaldi Date: Tue, 28 Jan 2020 19:55:16 +0100 Subject: [PATCH] fix: escape "$" character to avoid bash interpreting (#884) --- src/context.rs | 2 +- src/module.rs | 19 +++++++++---------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/context.rs b/src/context.rs index 460ede159..ce2002066 100644 --- a/src/context.rs +++ b/src/context.rs @@ -270,7 +270,7 @@ fn get_current_branch(repository: &Repository) -> Option { shorthand.map(std::string::ToString::to_string) } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] pub enum Shell { Bash, Fish, diff --git a/src/module.rs b/src/module.rs index f09c6e5df..1768d6b2f 100644 --- a/src/module.rs +++ b/src/module.rs @@ -191,18 +191,17 @@ fn ansi_strings_modified(ansi_strings: Vec, shell: Shell) -> Vec x.to_string(), } } - MAYBE_ESCAPE_END => { - if escaped { - escaped = false; - match shell { - Shell::Bash => String::from("m\u{5c}\u{5d}"), // => m\] - Shell::Zsh => String::from("m\u{25}\u{7d}"), // => m%} - _ => x.to_string(), - } - } else { - x.to_string() + MAYBE_ESCAPE_END if escaped => { + escaped = false; + match shell { + Shell::Bash => String::from("m\u{5c}\u{5d}"), // => m\] + Shell::Zsh => String::from("m\u{25}\u{7d}"), // => m%} + _ => x.to_string(), } } + // escape the $ character to avoid $() code injection on bash shell, + // see #658 for more + '$' if Shell::Bash == shell => String::from("\\$"), _ => x.to_string(), }) .collect();