Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix: handle untracked file in policy command quoting

Steven Enamakelf922ab211d7f
diff --git a/crates/openhuman-core/src/security/policy/policy_command/quoting.rs b/crates/openhuman-core/src/security/policy/policy_command/quoting.rs
new file mode 100644
index 0000000000..239fa6c4fa
--- /dev/null
+++ b/crates/openhuman-core/src/security/policy/policy_command/quoting.rs
@@ -0,0 +1,282 @@
+//! Quote-aware shell-string scanning: splitting a command into `;`/`|`/`&&`/
+//! `||`-separated segments, and detecting unquoted control characters
+//! (`&`, arbitrary redirects) without being fooled by quoted literals.
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+enum QuoteState {
+ None,
+ Single,
+ Double,
+}
+
+/// Split a shell command into sub-commands by unquoted separators.
+///
+/// Separators:
+/// - `;` and newline
+/// - `|`
+/// - `&&`, `||`
+///
+/// Characters inside single or double quotes are treated as literals, so
+/// `sqlite3 db "SELECT 1; SELECT 2;"` remains a single segment.
+pub(in crate::security::policy) fn split_unquoted_segments(command: &str) -> Vec<String> {
+ let mut segments = Vec::new();
+ let mut current = String::new();
+ let mut quote = QuoteState::None;
+ let mut escaped = false;
+ let mut chars = command.chars().peekable();
+
+ let push_segment = |segments: &mut Vec<String>, current: &mut String| {
+ let trimmed = current.trim();
+ if !trimmed.is_empty() {
+ segments.push(trimmed.to_string());
+ }
+ current.clear();
+ };
+
+ while let Some(ch) = chars.next() {
+ match quote {
+ QuoteState::Single => {
+ if ch == '\'' {
+ quote = QuoteState::None;
+ }
+ current.push(ch);
+ }
+ QuoteState::Double => {
+ if escaped {
+ escaped = false;
+ current.push(ch);
+ continue;
+ }
+ if ch == '\\' {
+ escaped = true;
+ current.push(ch);
+ continue;
+ }
+ if ch == '"' {
+ quote = QuoteState::None;
+ }
+ current.push(ch);
+ }
+ QuoteState::None => {
+ if escaped {
+ escaped = false;
+ current.push(ch);
+ continue;
+ }
+ if ch == '\\' {
+ escaped = true;
+ current.push(ch);
+ continue;
+ }
+
+ match ch {
+ '\'' => {
+ quote = QuoteState::Single;
+ current.push(ch);
+ }
+ '"' => {
+ quote = QuoteState::Double;
+ current.push(ch);
+ }
+ ';' | '\n' => push_segment(&mut segments, &mut current),
+ '|' => {
+ if chars.next_if_eq(&'|').is_some() {
+ // Consume full `||`; both characters are separators.
+ }
+ push_segment(&mut segments, &mut current);
+ }
+ '&' => {
+ if chars.next_if_eq(&'&').is_some() {
+ // `&&` is a separator; single `&` is handled separately.
+ push_segment(&mut segments, &mut current);
+ } else {
+ current.push(ch);
+ }
+ }
+ _ => current.push(ch),
+ }
+ }
+ }
+ }
+
+ let trimmed = current.trim();
+ if !trimmed.is_empty() {
+ segments.push(trimmed.to_string());
+ }
+
+ segments
+}
+
+/// Detect a single unquoted `&` operator (background/chain). `&&` is allowed.
+///
+/// We treat any standalone `&` as unsafe in policy validation because it can
+/// chain hidden sub-commands and escape foreground timeout expectations.
+pub(in crate::security::policy) fn contains_unquoted_single_ampersand(command: &str) -> bool {
+ let mut quote = QuoteState::None;
+ let mut escaped = false;
+ let mut chars = command.chars().peekable();
+
+ while let Some(ch) = chars.next() {
+ match quote {
+ QuoteState::Single => {
+ if ch == '\'' {
+ quote = QuoteState::None;
+ }
+ }
+ QuoteState::Double => {
+ if escaped {
+ escaped = false;
+ continue;
+ }
+ if ch == '\\' {
+ escaped = true;
+ continue;
+ }
+ if ch == '"' {
+ quote = QuoteState::None;
+ }
+ }
+ QuoteState::None => {
+ if escaped {
+ escaped = false;
+ continue;
+ }
+ if ch == '\\' {
+ escaped = true;
+ continue;
+ }
+ match ch {
+ '\'' => quote = QuoteState::Single,
+ '"' => quote = QuoteState::Double,
+ '&' if chars.next_if_eq(&'&').is_none() => {
+ return true;
+ }
+ _ => {}
+ }
+ }
+ }
+ }
+
+ false
+}
+
+/// Like [`contains_unquoted_single_ampersand`] but ignores file-descriptor
+/// duplication redirects, where the `&` is part of a redirect operator rather
+/// than a background/separator: `2>&1`, `>&2` (prev char `>`), and `&>file`
+/// (next char `>`). Used by [`super::classification::has_hidden_execution`] so
+/// a benign `… 2>&1` — which `classify_command` already accounts for as a
+/// `Write` redirect — is not mistaken for a backgrounded command and
+/// hard-blocked after the human approved it. A standalone `&` (e.g. `cmd &`,
+/// `a & b`) still returns true, since it can run a second command
+/// `classify_command` wouldn't see.
+pub(super) fn contains_unquoted_background_ampersand(command: &str) -> bool {
+ let mut quote = QuoteState::None;
+ let mut escaped = false;
+ let mut prev = '\0';
+ let mut chars = command.chars().peekable();
+
+ while let Some(ch) = chars.next() {
+ match quote {
+ QuoteState::Single => {
+ if ch == '\'' {
+ quote = QuoteState::None;
+ }
+ }
+ QuoteState::Double => {
+ if escaped {
+ escaped = false;
+ prev = ch;
+ continue;
+ }
+ if ch == '\\' {
+ escaped = true;
+ prev = ch;
+ continue;
+ }
+ if ch == '"' {
+ quote = QuoteState::None;
+ }
+ }
+ QuoteState::None => {
+ if escaped {
+ escaped = false;
+ prev = ch;
+ continue;
+ }
+ if ch == '\\' {
+ escaped = true;
+ prev = ch;
+ continue;
+ }
+ match ch {
+ '\'' => quote = QuoteState::Single,
+ '"' => quote = QuoteState::Double,
+ '&' => {
+ if chars.next_if_eq(&'&').is_some() {
+ // `&&` logical AND — consume both, not background.
+ } else {
+ let next = chars.peek().copied().unwrap_or('\0');
+ // Skip fd-dup redirects: `2>&1`/`>&2` (prev `>`) and
+ // `&>file` (next `>`).
+ if prev != '>' && next != '>' {
+ return true;
+ }
+ }
+ }
+ _ => {}
+ }
+ }
+ }
+ prev = ch;
+ }
+
+ false
+}
+
+/// Detect an unquoted character in a shell command.
+pub(in crate::security::policy) fn contains_unquoted_char(command: &str, target: char) -> bool {
+ let mut quote = QuoteState::None;
+ let mut escaped = false;
+
+ for ch in command.chars() {
+ match quote {
+ QuoteState::Single => {
+ if ch == '\'' {
+ quote = QuoteState::None;
+ }
+ }
+ QuoteState::Double => {
+ if escaped {
+ escaped = false;
+ continue;
+ }
+ if ch == '\\' {
+ escaped = true;
+ continue;
+ }
+ if ch == '"' {
+ quote = QuoteState::None;
+ continue;
+ }
+ }
+ QuoteState::None => {
+ if escaped {
+ escaped = false;
+ continue;
+ }
+ if ch == '\\' {
+ escaped = true;
+ continue;
+ }
+ match ch {
+ '\'' => quote = QuoteState::Single,
+ '"' => quote = QuoteState::Double,
+ _ if ch == target => return true,
+ _ => {}
+ }
+ }
+ }
+ }
+
+ false
+}