Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Applications: Properly parse desktop exec keys and stop shelling out #207

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 20 additions & 25 deletions plugins/applications/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use abi_stable::std_types::{ROption, RString, RVec};
use anyrun_plugin::{anyrun_interface::HandleResult, *};
use fuzzy_matcher::FuzzyMatcher;
use scrubber::DesktopEntry;
use scrubber::{DesktopEntry, lower_exec};
use serde::Deserialize;
use std::{env, fs, process::Command};

Expand Down Expand Up @@ -48,17 +48,17 @@ pub fn handler(selection: Match, state: &State) -> HandleResult {
}
})
.unwrap();
let (command, argv) = lower_exec(&entry.exec).unwrap_or_else(
|e| panic!("Unable to parse the exec key `{}`: {}", &entry.exec, e.0)
);

if entry.term {
match &state.config.terminal {
Some(term) => {
if let Err(why) = Command::new("sh")
.arg("-c")
.arg(format!(
"{} {}",
term.command,
term.args.replace("{}", &entry.exec)
))
if let Err(why) = Command::new(&term.command)
.arg(&term.args)
.arg(command)
.args(argv)
.spawn()
{
eprintln!("Error running desktop entry: {}", why);
Expand All @@ -68,23 +68,23 @@ pub fn handler(selection: Match, state: &State) -> HandleResult {
let sensible_terminals = &[
Terminal {
command: "alacritty".to_string(),
args: "-e {}".to_string(),
args: "-e".to_string(),
},
Terminal {
command: "foot".to_string(),
args: "-e \"{}\"".to_string(),
args: "-e".to_string(),
},
Terminal {
command: "kitty".to_string(),
args: "-e \"{}\"".to_string(),
args: "-e".to_string(),
},
Terminal {
command: "wezterm".to_string(),
args: "-e \"{}\"".to_string(),
args: "-e".to_string(),
},
Terminal {
command: "wterm".to_string(),
args: "-e \"{}\"".to_string(),
args: "-e".to_string(),
},
];
for term in sensible_terminals {
Expand All @@ -93,13 +93,10 @@ pub fn handler(selection: Match, state: &State) -> HandleResult {
.output()
.is_ok_and(|output| output.status.success())
{
if let Err(why) = Command::new("sh")
.arg("-c")
.arg(format!(
"{} {}",
term.command,
term.args.replace("{}", &entry.exec)
))
if let Err(why) = Command::new(&term.command)
.arg(&term.args)
.arg(command)
.args(argv)
.spawn()
{
eprintln!("Error running desktop entry: {}", why);
Expand All @@ -112,9 +109,8 @@ pub fn handler(selection: Match, state: &State) -> HandleResult {
} else if let Err(why) = {
let current_dir = &env::current_dir().unwrap();

Command::new("sh")
.arg("-c")
.arg(&entry.exec)
Command::new(command)
.args(argv)
.current_dir(if let Some(path) = &entry.path {
if path.exists() {
path
Expand All @@ -123,8 +119,7 @@ pub fn handler(selection: Match, state: &State) -> HandleResult {
}
} else {
current_dir
})
.spawn()
}).spawn()
} {
eprintln!("Error running desktop entry: {}", why);
}
Expand Down
203 changes: 195 additions & 8 deletions plugins/applications/src/scrubber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,200 @@ const FIELD_CODE_LIST: &[&str] = &[
"%f", "%F", "%u", "%U", "%d", "%D", "%n", "%N", "%i", "%c", "%k", "%v", "%m",
];

// See https://specifications.freedesktop.org/desktop-entry-spec/latest/exec-variables.html
const EXEC_ESCAPE_CHARS: &[char] = &['"', '`', '$', '\\'];

/*
Reserved characters are space (" "), tab, newline, double quote,
single quote ("'"), backslash character ("\"), greater-than sign
(">"), less-than sign ("<"), tilde ("~"), vertical bar ("|"),
ampersand ("&"), semicolon (";"), dollar sign ("$"), asterisk ("*"),
question mark ("?"), hash mark ("#"), parenthesis ("(") and (")") and
backtick character ("`").
*/
const EXEC_RESERVED_CHARS: &[char] = &[
' ', '\t', '\n', '"', '\'', '\\', '>', '<', '~', '|', '&', ';', '$', '*', '?', '#', '(', ')',
'`',
];

// \s, \n, \t, \r, and \\ are valid escapes in Desktop strings
const DESKTOP_STRING_ESCAPES: &[(char, char)] = &[
('s', ' '),
('n', '\n'),
('t', '\t'),
('r', '\r'),
('\\', '\\'),
];

fn get_desktop_string_escapes() -> HashMap<char, char> {
HashMap::from_iter(DESKTOP_STRING_ESCAPES.iter().cloned())
}

#[derive(Debug, Clone)]
pub struct ExecKeyError(pub String);

#[derive(Debug, Clone)]
enum StringEscapeState {
Waiting,
Escape,
}

fn substitute_escapes(s: &str) -> Result<String, ExecKeyError> {
use StringEscapeState::*;

let escapes = get_desktop_string_escapes();
let mut state = Waiting;
let mut out = Vec::<char>::new();
for (i, c) in s.chars().enumerate() {
match state {
Waiting => match c {
'\\' => {
state = Escape;
}
_ => {
out.push(c);
}
},
Escape => match c {
c if escapes.contains_key(&c) => {
out.push(*escapes.get(&c).unwrap());
state = Waiting;
}
_ => {
return Err(ExecKeyError(format!(
"Escaping invalid character {} at position {}",
c, i
)))
}
},
}
}
if let Escape = state {
return Err(ExecKeyError("Dangling escape".to_string()));
}
Ok(out.into_iter().collect())
}

#[derive(Debug, Clone)]
enum ExecKeyState {
Waiting,
Word,
Quoting,
Escape,
}

fn unescape_exec(s: &str) -> Result<Vec<String>, ExecKeyError> {
use ExecKeyState::*;

let mut state = Waiting;
let mut out = Vec::<String>::new();
let mut buffer = Vec::<char>::new();

for (i, c) in s.chars().enumerate() {
match state {
Waiting => {
match c {
'"' => {
state = Quoting;
continue;
}
' ' => continue,
c if EXEC_RESERVED_CHARS.contains(&c) => return Err(ExecKeyError(format!(
"Starting word with reserved character {} at position {}, consider quoting",
c, i
))),
_ => {
state = Word;
}
};
buffer.push(c);
}
Word => match c {
' ' => {
state = Waiting;
out.push(buffer.iter().collect());
buffer.clear();
}
c if EXEC_RESERVED_CHARS.contains(&c) => {
return Err(ExecKeyError(format!(
"Reserved character {} in unquoted word at position {}",
c, i
)))
}
_ => buffer.push(c),
},
Quoting => match c {
'"' => {
out.push(buffer.iter().collect());
buffer.clear();
state = Waiting;
continue;
}
'\\' => state = Escape,
c if EXEC_ESCAPE_CHARS.contains(&c) => {
return Err(ExecKeyError(format!(
"Unescaped character {} in quoted string at position {}",
c, i
)));
}
_ => {
buffer.push(c);
}
},
Escape => match c {
c if EXEC_ESCAPE_CHARS.contains(&c) => {
buffer.push(c);
state = Quoting;
}
_ => {
return Err(ExecKeyError(format!(
"Escaping invalid character {} in quoted string at position {}",
c, i
)))
}
},
}
}
match state {
Waiting => {}
Word => {
out.push(buffer.iter().collect());
buffer.clear();
}
_ => return Err(ExecKeyError("Invalid state at end of exec key".to_string())),
}

Ok(out)
}

/*
1. Substitute general desktop string escapes
2. Unescape EXEC_ESCAPE_CHARS in exec key quoted strings
3. Strip field codes and throw away empty args
*/
pub(crate) fn lower_exec(s: &str) -> Result<(String, Vec<String>), ExecKeyError> {
let subst = substitute_escapes(s)?;
let argvec = unescape_exec(&subst)?;
if let Some((command, argv)) = argvec.split_first() {
let argv_without_fieldcodes = argv
.to_vec()
.into_iter()
.map(|mut c| {
for field_code in FIELD_CODE_LIST.iter() {
c = c.replace(field_code, "");
}
c
})
.filter(|c| {
!c.is_empty()
})
.collect();
return Ok((command.clone(), argv_without_fieldcodes));
} else {
return Err(ExecKeyError("Empty exec key!".to_string()));
}
}

impl DesktopEntry {
fn from_dir_entry(entry: &fs::DirEntry, config: &Config) -> Vec<Self> {
if entry.path().extension() == Some(OsStr::new("desktop")) {
Expand Down Expand Up @@ -68,14 +262,7 @@ impl DesktopEntry {
}
{
Some(DesktopEntry {
exec: {
let mut exec = map.get("Exec")?.to_string();

for field_code in FIELD_CODE_LIST {
exec = exec.replace(field_code, "");
}
exec
},
exec: map.get("Exec")?.to_string(),
path: map.get("Path").map(PathBuf::from),
name: map.get("Name")?.to_string(),
keywords: map
Expand Down