Add option to disable passive argument injection in shell extension. Fix #513
This commit is contained in:
parent
be0dd8a2cb
commit
1431145afa
|
@ -25,11 +25,8 @@ use std::collections::HashMap;
|
||||||
use std::process::{Command, Output};
|
use std::process::{Command, Output};
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref POS_ARG_REGEX: Regex = if cfg!(target_os = "windows") {
|
static ref UNIX_POS_ARG_REGEX: Regex = Regex::new("\\$(?P<pos>\\d+)").unwrap();
|
||||||
Regex::new("%(?P<pos>\\d+)").unwrap()
|
static ref WIN_POS_ARG_REGEX: Regex = Regex::new("%(?P<pos>\\d+)").unwrap();
|
||||||
} else {
|
|
||||||
Regex::new("\\$(?P<pos>\\d+)").unwrap()
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum Shell {
|
pub enum Shell {
|
||||||
|
@ -121,6 +118,18 @@ impl Shell {
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_arg_regex(&self) -> &Regex {
|
||||||
|
let regex = match self {
|
||||||
|
Shell::Cmd | Shell::Powershell => {
|
||||||
|
&*WIN_POS_ARG_REGEX
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
&*UNIX_POS_ARG_REGEX
|
||||||
|
}
|
||||||
|
};
|
||||||
|
regex
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Shell {
|
impl Default for Shell {
|
||||||
|
@ -162,20 +171,13 @@ impl super::Extension for ShellExtension {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
let original_cmd = cmd.unwrap().as_str().unwrap();
|
let inject_args = params
|
||||||
|
.get(&Value::from("inject_args"))
|
||||||
|
.unwrap_or(&Value::from(false))
|
||||||
|
.as_bool()
|
||||||
|
.unwrap_or(false);
|
||||||
|
|
||||||
// Render positional parameters in args
|
let original_cmd = cmd.unwrap().as_str().unwrap();
|
||||||
let cmd = POS_ARG_REGEX
|
|
||||||
.replace_all(&original_cmd, |caps: &Captures| {
|
|
||||||
let position_str = caps.name("pos").unwrap().as_str();
|
|
||||||
let position = position_str.parse::<i32>().unwrap_or(-1);
|
|
||||||
if position >= 0 && position < args.len() as i32 {
|
|
||||||
args[position as usize].to_owned()
|
|
||||||
} else {
|
|
||||||
"".to_owned()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.to_string();
|
|
||||||
|
|
||||||
let shell_param = params.get(&Value::from("shell"));
|
let shell_param = params.get(&Value::from("shell"));
|
||||||
let shell = if let Some(shell_param) = shell_param {
|
let shell = if let Some(shell_param) = shell_param {
|
||||||
|
@ -192,6 +194,23 @@ impl super::Extension for ShellExtension {
|
||||||
Shell::default()
|
Shell::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Render positional parameters in args
|
||||||
|
let cmd = if inject_args {
|
||||||
|
shell.get_arg_regex()
|
||||||
|
.replace_all(&original_cmd, |caps: &Captures| {
|
||||||
|
let position_str = caps.name("pos").unwrap().as_str();
|
||||||
|
let position = position_str.parse::<i32>().unwrap_or(-1);
|
||||||
|
if position >= 0 && position < args.len() as i32 {
|
||||||
|
args[position as usize].to_owned()
|
||||||
|
} else {
|
||||||
|
"".to_owned()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.to_string()
|
||||||
|
} else {
|
||||||
|
original_cmd.to_owned()
|
||||||
|
};
|
||||||
|
|
||||||
let env_variables = super::utils::convert_to_env_variables(&vars);
|
let env_variables = super::utils::convert_to_env_variables(&vars);
|
||||||
|
|
||||||
let output = shell.execute_cmd(&cmd, &env_variables);
|
let output = shell.execute_cmd(&cmd, &env_variables);
|
||||||
|
@ -348,6 +367,7 @@ mod tests {
|
||||||
fn test_shell_args_unix() {
|
fn test_shell_args_unix() {
|
||||||
let mut params = Mapping::new();
|
let mut params = Mapping::new();
|
||||||
params.insert(Value::from("cmd"), Value::from("echo $0"));
|
params.insert(Value::from("cmd"), Value::from("echo $0"));
|
||||||
|
params.insert(Value::from("inject_args"), Value::from(true));
|
||||||
|
|
||||||
let extension = ShellExtension::new();
|
let extension = ShellExtension::new();
|
||||||
let output = extension.calculate(¶ms, &vec!["hello".to_owned()], &HashMap::new());
|
let output = extension.calculate(¶ms, &vec!["hello".to_owned()], &HashMap::new());
|
||||||
|
@ -357,11 +377,26 @@ mod tests {
|
||||||
assert_eq!(output.unwrap(), ExtensionResult::Single("hello".to_owned()));
|
assert_eq!(output.unwrap(), ExtensionResult::Single("hello".to_owned()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[cfg(not(target_os = "windows"))]
|
||||||
|
fn test_shell_no_default_inject_args_unix() {
|
||||||
|
let mut params = Mapping::new();
|
||||||
|
params.insert(Value::from("cmd"), Value::from("echo 'hey friend' | awk '{ print $2 }'"));
|
||||||
|
|
||||||
|
let extension = ShellExtension::new();
|
||||||
|
let output = extension.calculate(¶ms, &vec!["hello".to_owned()], &HashMap::new());
|
||||||
|
|
||||||
|
assert!(output.is_some());
|
||||||
|
|
||||||
|
assert_eq!(output.unwrap(), ExtensionResult::Single("friend".to_owned()));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(target_os = "windows")]
|
#[cfg(target_os = "windows")]
|
||||||
fn test_shell_args_windows() {
|
fn test_shell_args_windows() {
|
||||||
let mut params = Mapping::new();
|
let mut params = Mapping::new();
|
||||||
params.insert(Value::from("cmd"), Value::from("echo %0"));
|
params.insert(Value::from("cmd"), Value::from("echo %0"));
|
||||||
|
params.insert(Value::from("inject_args"), Value::from(true));
|
||||||
|
|
||||||
let extension = ShellExtension::new();
|
let extension = ShellExtension::new();
|
||||||
let output = extension.calculate(¶ms, &vec!["hello".to_owned()], &HashMap::new());
|
let output = extension.calculate(¶ms, &vec!["hello".to_owned()], &HashMap::new());
|
||||||
|
|
|
@ -330,7 +330,7 @@ fn default_markdown() -> Option<String> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
fn default_paragraph() -> bool {
|
fn default_paragraph() -> bool {
|
||||||
true
|
false
|
||||||
}
|
}
|
||||||
fn default_html() -> Option<String> {
|
fn default_html() -> Option<String> {
|
||||||
None
|
None
|
||||||
|
|
Loading…
Reference in New Issue
Block a user