Repository navigation
feat: add fail-closed command inspection with Veltra #118
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
Open
its-amann
wants to merge
3
commits into
browser-use:main
Choose a base branch
from
its-amann:feat/pre-execution-command-guard-6119
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| //! Fail-closed command inspection, independent of approval policy and its cache. | ||
|
|
||
| use std::process::Stdio; | ||
| use std::sync::Arc; | ||
| use std::time::Duration; | ||
|
|
||
| use tokio::io::{AsyncReadExt, AsyncWriteExt}; | ||
| use tokio::process::Command; | ||
|
|
||
| use crate::hooks::{HookDecision, HookInput}; | ||
|
|
||
| use super::runtime::ToolError; | ||
|
|
||
| pub(super) fn from_env() -> Option<Arc<Vec<String>>> { | ||
| std::env::var_os("BUT_COMMAND_GUARD").map(|raw| { | ||
| // Invalid configuration stays enabled and fails closed at execution time. | ||
| let command: Vec<String> = raw | ||
| .to_str() | ||
| .and_then(|value| serde_json::from_str(value).ok()) | ||
| .unwrap_or_default(); | ||
| if command.first().is_none_or(String::is_empty) { | ||
| eprintln!("warning: invalid BUT_COMMAND_GUARD; expected a nonempty JSON argv array; commands will be denied"); | ||
| } | ||
| Arc::new(command) | ||
| }) | ||
| } | ||
|
|
||
| pub(super) async fn inspect(command: &[String], input: HookInput) -> Result<(), ToolError> { | ||
| let denied = |reason: &str| { | ||
| eprintln!("warning: command guard: {reason}"); | ||
| ToolError::Rejected("Command blocked by pre-execution guard.".into()) | ||
| }; | ||
| let program = command | ||
| .first() | ||
| .filter(|program| !program.is_empty()) | ||
| .ok_or_else(|| denied("invalid checker configuration"))?; | ||
| let payload = serde_json::to_vec(&input).map_err(|_| denied("cannot encode checker input"))?; | ||
| let mut child = Command::new(program) | ||
| .args(&command[1..]) | ||
| .stdin(Stdio::piped()) | ||
| .stdout(Stdio::piped()) | ||
| // Checkers can echo sensitive command text; only report fixed failure categories. | ||
| .stderr(Stdio::null()) | ||
| .kill_on_drop(true) | ||
| .spawn() | ||
| .map_err(|_| denied("cannot start checker"))?; | ||
|
|
||
| tokio::time::timeout(Duration::from_secs(5), async { | ||
| let mut stdin = child | ||
| .stdin | ||
| .take() | ||
| .ok_or_else(|| denied("checker stdin unavailable"))?; | ||
| stdin | ||
| .write_all(&payload) | ||
| .await | ||
| .map_err(|_| denied("cannot write checker input"))?; | ||
| drop(stdin); | ||
|
|
||
| let mut output = Vec::new(); | ||
| child | ||
| .stdout | ||
| .take() | ||
| .ok_or_else(|| denied("checker stdout unavailable"))? | ||
| .take(8 * 1024 + 1) | ||
| .read_to_end(&mut output) | ||
| .await | ||
| .map_err(|_| denied("cannot read checker output"))?; | ||
| if output.len() > 8 * 1024 { | ||
| return Err(denied("checker output exceeds 8 KiB")); | ||
| } | ||
| if !child | ||
| .wait() | ||
| .await | ||
| .map_err(|_| denied("cannot wait for checker"))? | ||
| .success() | ||
| { | ||
| return Err(denied("checker exited unsuccessfully")); | ||
| } | ||
| let decision: HookDecision = | ||
| serde_json::from_slice(&output).map_err(|_| denied("invalid checker response"))?; | ||
| if decision.r#continue != Some(true) { | ||
| return Err(denied("checker did not explicitly allow execution")); | ||
| } | ||
| Ok(()) | ||
| }) | ||
| .await | ||
| .map_err(|_| denied("checker timed out"))? | ||
| } | ||
|
|
||
| #[cfg(all(test, unix))] | ||
| mod tests { | ||
| use super::inspect; | ||
| use crate::hooks::{HookEvent, HookInput}; | ||
|
|
||
| #[tokio::test] | ||
| async fn command_guard_requires_a_successful_explicit_allow_decision() { | ||
| for script in [ | ||
| "cat >/dev/null; printf '{\"continue\":true}'; exit 1", | ||
| "cat >/dev/null; printf '{}'", | ||
| "cat >/dev/null; printf '{\"continue\":\"true\"}'", | ||
| "cat >/dev/null; printf 'not json'", | ||
| ] { | ||
| let result = inspect( | ||
| &["sh".into(), "-c".into(), script.into()], | ||
| HookInput::new(HookEvent::PreToolUse), | ||
| ) | ||
| .await; | ||
| assert!(result.is_err(), "checker must fail closed: {script}"); | ||
| } | ||
| for command in [ | ||
| vec![], | ||
| vec!["/nonexistent/browser-use-command-guard".into()], | ||
| ] { | ||
| assert!(inspect(&command, HookInput::new(HookEvent::PreToolUse)) | ||
| .await | ||
| .is_err()); | ||
| } | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn command_guard_enforces_the_output_byte_limit_on_valid_json() { | ||
| for size in [8 * 1024, 8 * 1024 + 1] { | ||
| let mut output = String::from("{\"continue\":true}"); | ||
| output.push_str(&" ".repeat(size - output.len())); | ||
| assert!(serde_json::from_str::<crate::hooks::HookDecision>(&output).is_ok()); | ||
| let result = inspect( | ||
| &[ | ||
| "sh".into(), | ||
| "-c".into(), | ||
| "cat >/dev/null; printf '%s' \"$1\"".into(), | ||
| "guard".into(), | ||
| output, | ||
| ], | ||
| HookInput::new(HookEvent::PreToolUse), | ||
| ) | ||
| .await; | ||
| assert_eq!(result.is_ok(), size == 8 * 1024); | ||
| } | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn command_guard_times_out_without_allowing_execution() { | ||
| let result = tokio::time::timeout( | ||
| std::time::Duration::from_secs(7), | ||
| inspect( | ||
| &[ | ||
| "sh".into(), | ||
| "-c".into(), | ||
| "cat >/dev/null; exec sleep 30".into(), | ||
| ], | ||
| HookInput::new(HookEvent::PreToolUse), | ||
| ), | ||
| ) | ||
| .await | ||
| .expect("checker must honor the five-second timeout"); | ||
| assert!(result.is_err()); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.