#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Substitution {
pub start: usize,
pub end: usize,
pub inner: String,
pub quoted: bool,
}
pub fn substitutions(src: &str) -> Vec<Substitution> {
let chars: Vec<char> = src.chars().collect();
let mut found = Vec::new();
let mut i = 0;
let mut in_single = false;
let mut in_double = false;
while i < chars.len() {
let c = chars[i];
if in_single {
if c == '\'' {
in_single = false;
}
i += 1;
continue;
}
match c {
'\\' => {
i += 2;
continue;
}
'\'' if !in_double => {
in_single = true;
i += 1;
continue;
}
'"' => {
in_double = !in_double;
i += 1;
continue;
}
'`' => {
if let Some(end) = find_backtick_end(&chars, i + 1) {
let inner: String = chars[i + 1..end].iter().collect();
found.push(Substitution {
start: i,
end: end + 1,
inner,
quoted: in_double,
});
i = end + 1;
continue;
}
i += 1;
continue;
}
'$' if i + 1 < chars.len() && chars[i + 1] == '(' => {
if i + 2 < chars.len() && chars[i + 2] == '(' {
i += 3;
continue;
}
if let Some(end) = find_paren_end(&chars, i + 2) {
let inner: String = chars[i + 2..end].iter().collect();
found.push(Substitution {
start: i,
end: end + 1,
inner,
quoted: in_double,
});
i = end + 1;
continue;
}
i += 1;
continue;
}
_ => {}
}
i += 1;
}
found
}
fn find_backtick_end(chars: &[char], from: usize) -> Option<usize> {
let mut i = from;
while i < chars.len() {
match chars[i] {
'\\' => i += 2,
'`' => return Some(i),
_ => i += 1,
}
}
None
}
fn find_paren_end(chars: &[char], from: usize) -> Option<usize> {
let mut depth = 1usize;
let mut i = from;
let mut in_single = false;
let mut in_double = false;
while i < chars.len() {
let c = chars[i];
if in_single {
if c == '\'' {
in_single = false;
}
i += 1;
continue;
}
match c {
'\\' => {
i += 2;
continue;
}
'\'' if !in_double => in_single = true,
'"' => in_double = !in_double,
'(' if !in_double => depth += 1,
')' if !in_double => {
depth -= 1;
if depth == 0 {
return Some(i);
}
}
_ => {}
}
i += 1;
}
None
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ForeignScript {
pub interpreter: String,
pub word: String,
}
const SCRIPT_INTERPRETERS: &[&str] = &["pwsh", "powershell"];
#[must_use]
pub fn foreign_script_expansion(src: &str) -> Option<ForeignScript> {
let mut words = split_words(src);
while words
.first()
.is_some_and(|w| matches!(w.as_str(), "&" | "command") || is_assignment(w))
{
words.remove(0);
}
let interpreter = script_interpreter(words.first()?)?;
for (i, word) in words.iter().enumerate().skip(1) {
let flag = word.trim_start_matches(['-', '/']).to_ascii_lowercase();
if !matches!(flag.as_str(), "command" | "c" | "commandwithargs") {
continue;
}
let Some(argument) = words.get(i + 1) else {
continue;
};
if argument.starts_with('"') && (argument.contains('$') || argument.contains('`')) {
return Some(ForeignScript {
interpreter,
word: argument.clone(),
});
}
}
None
}
fn is_assignment(word: &str) -> bool {
let Some((name, _)) = word.split_once('=') else {
return false;
};
!name.is_empty()
&& name
.chars()
.next()
.is_some_and(|c| c.is_ascii_alphabetic() || c == '_')
&& name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')
}
fn script_interpreter(word: &str) -> Option<String> {
let name = word
.rsplit(['/', '\\'])
.next()
.unwrap_or(word)
.trim_matches('"')
.to_ascii_lowercase();
let name = name.strip_suffix(".exe").unwrap_or(name.as_str());
SCRIPT_INTERPRETERS
.contains(&name)
.then(|| name.to_string())
}
fn split_words(src: &str) -> Vec<String> {
let chars: Vec<char> = src.chars().collect();
let mut words = Vec::new();
let mut i = 0;
let mut in_single = false;
let mut in_double = false;
while i < chars.len() {
while i < chars.len() && chars[i].is_whitespace() && !in_single && !in_double {
i += 1;
}
if i >= chars.len() {
break;
}
let start = i;
while i < chars.len() {
let c = chars[i];
if in_single {
if c == '\'' {
in_single = false;
}
i += 1;
continue;
}
match c {
'\\' => i = (i + 2).min(chars.len()),
'\'' if !in_double => {
in_single = true;
i += 1;
}
'"' => {
in_double = !in_double;
i += 1;
}
c if c.is_whitespace() && !in_double => break,
_ => i += 1,
}
}
words.push(chars[start..i].iter().collect());
if i == start {
i += 1;
}
}
words
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn finds_a_substitution() {
let subs = substitutions("echo $(date +%s) done");
assert_eq!(subs.len(), 1);
assert_eq!(subs[0].inner, "date +%s");
assert!(!subs[0].quoted);
}
#[test]
fn ignores_single_quotes_and_arithmetic() {
assert!(substitutions("echo '$(date)'").is_empty());
assert!(substitutions("echo $((1 + 2))").is_empty());
}
#[test]
fn handles_nesting_and_quotes() {
let subs = substitutions(r#"echo "$(echo "$(date)")" x"#);
assert_eq!(subs.len(), 1);
assert_eq!(subs[0].inner, r#"echo "$(date)""#);
assert!(subs[0].quoted);
}
#[test]
fn finds_backticks() {
let subs = substitutions("echo `date`");
assert_eq!(subs.len(), 1);
assert_eq!(subs[0].inner, "date");
}
#[test]
fn finds_a_script_bash_would_expand_first() {
let hit = foreign_script_expansion(
r#"pwsh -NoProfile -Command "Get-CimInstance X | Select-Object @{n='GB';e={$_.Capacity/1GB}}""#,
)
.expect("a hit");
assert_eq!(hit.interpreter, "pwsh");
assert!(hit.word.contains("$_"));
}
#[test]
fn accepts_single_quoted_and_file_scripts() {
assert!(foreign_script_expansion(r#"pwsh -Command 'Get-Item $_.Name'"#).is_none());
assert!(foreign_script_expansion("pwsh -NoProfile -File build.ps1").is_none());
assert!(foreign_script_expansion(r#"powershell.exe -Command "echo plain""#).is_none());
}
#[test]
fn ignores_other_programs_and_mentions() {
assert!(foreign_script_expansion(r#"node -e "console.log($x)""#).is_none());
assert!(foreign_script_expansion(r#"echo pwsh -Command "$x""#).is_none());
}
#[test]
fn the_shape_table() {
let cases: &[(&str, bool)] = &[
(r#"pwsh -Command "$env:PATH""#, true),
(r#"pwsh.exe -Command "$env:PATH""#, true),
(r#"powershell -Command "$env:PATH""#, true),
(r#"powershell.exe -NoProfile -Command "$env:PATH""#, true),
(r#""C:/Program Files/PowerShell/7/pwsh.exe" -Command "$env:PATH""#, true),
(r#"& "C:/Program Files/PowerShell/7/pwsh.exe" -Command "$env:PATH""#, true),
(r#"command pwsh -Command "$env:PATH""#, true),
(r#"NO_COLOR=1 pwsh -Command "$env:PATH""#, true),
(r#"pwsh -c "Get-Item $_.Name""#, true),
(r#"pwsh -CommandWithArgs "Get-Item $_.Name" x"#, true),
("pwsh -Command \"`git rev-parse HEAD`\"", true),
(r#"pwsh -Command 'Get-Item $_.Name'"#, false),
(r#"pwsh -NoProfile -Command "Get-Item .\file.txt""#, false),
(r#"pwsh -NoProfile -File build.ps1"#, false),
(r#"pwsh -NoProfile -EncodedCommand RQBjAGgAbwA="#, false),
(r#"cmd /c "echo %PATH%""#, false),
(r#"node -e "console.log($x)""#, false),
(r#"pwsh --version"#, false),
(r#"echo 'pwsh -Command "$x"'"#, false),
];
for (source, expected) in cases {
let hit = foreign_script_expansion(source);
assert_eq!(
hit.is_some(),
*expected,
"{source:?} → {hit:?} (expected {expected})"
);
if let Some(hit) = hit {
assert!(
source.contains(&hit.word),
"{source:?}: the matched word is not in the source: {hit:?}"
);
}
}
}
#[test]
fn fuzz_the_scanner_survives_random_junk() {
const TOKENS: &[&str] = &[
"pwsh", "powershell.exe", "-Command", "-c", "-File", "-EncodedCommand", "\"", "'", "$",
"$_", "$env:X", "`", "(", ")", "{", "}", "@", " ", " ", "\n", "\\", "|", "&&", ";",
"Get-Item", "echo", "&", "command", "X=1", "-NoProfile", "a", "=", "'", "\"",
];
let rounds: usize = std::env::var("AGSHELL_FUZZ_ROUNDS")
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(4_000);
let mut state: u64 = 0x5f37_5b4d_2a19_c7e1;
let mut next = move || {
state ^= state << 13;
state ^= state >> 7;
state ^= state << 17;
state
};
for round in 0..rounds {
let len = (next() % 14) as usize;
let mut source = String::new();
for _ in 0..len {
source.push_str(TOKENS[(next() % TOKENS.len() as u64) as usize]);
}
let first = foreign_script_expansion(&source);
assert_eq!(
first,
foreign_script_expansion(&source),
"round {round}: not deterministic for {source:?}"
);
if let Some(hit) = first {
assert!(
hit.word.starts_with('"'),
"round {round}: a match that is not a double-quoted argument: {source:?} → {hit:?}"
);
assert!(
hit.word.contains('$') || hit.word.contains('`'),
"round {round}: a match with nothing to expand: {source:?} → {hit:?}"
);
assert!(
source.contains(&hit.word),
"round {round}: matched text is not in the source: {source:?} → {hit:?}"
);
assert!(
hit.interpreter == "pwsh" || hit.interpreter == "powershell",
"round {round}: {hit:?}"
);
}
}
}
}