-
Notifications
You must be signed in to change notification settings - Fork 436
Expand file tree
/
Copy pathexecute_bash.rs
More file actions
396 lines (358 loc) · 12.9 KB
/
Copy pathexecute_bash.rs
File metadata and controls
396 lines (358 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
use std::collections::VecDeque;
use std::io::Write;
use std::process::{
ExitStatus,
Stdio,
};
use std::str::from_utf8;
use crossterm::queue;
use crossterm::style::{
self,
Color,
};
use eyre::{
Context as EyreContext,
Result,
};
use serde::Deserialize;
use tokio::io::AsyncBufReadExt;
use tokio::select;
use tracing::error;
use super::super::util::truncate_safe;
use super::{
InvokeOutput,
MAX_TOOL_RESPONSE_SIZE,
OutputKind,
};
use crate::cli::chat::{
CONTINUATION_LINE,
PURPOSE_ARROW,
};
use crate::platform::Context;
const READONLY_COMMANDS: &[&str] = &["ls", "cat", "echo", "pwd", "which", "head", "tail", "find", "grep"];
#[derive(Debug, Clone, Deserialize)]
pub struct ExecuteBash {
pub command: String,
pub summary: Option<String>,
}
impl ExecuteBash {
pub fn requires_acceptance(&self) -> bool {
let Some(args) = shlex::split(&self.command) else {
return true;
};
const DANGEROUS_PATTERNS: &[&str] = &["<(", "$(", "`", ">", "&&", "||", "&", ";"];
if args
.iter()
.any(|arg| DANGEROUS_PATTERNS.iter().any(|p| arg.contains(p)))
{
return true;
}
// Split commands by pipe and check each one
let mut current_cmd = Vec::new();
let mut all_commands = Vec::new();
for arg in args {
if arg == "|" {
if !current_cmd.is_empty() {
all_commands.push(current_cmd);
}
current_cmd = Vec::new();
} else if arg.contains("|") {
// if pipe appears without spacing e.g. `echo myimportantfile|args rm` it won't get
// parsed out, in this case - we want to verify before running
return true;
} else {
current_cmd.push(arg);
}
}
if !current_cmd.is_empty() {
all_commands.push(current_cmd);
}
// Check if each command in the pipe chain starts with a safe command
for cmd_args in all_commands {
match cmd_args.first() {
// Special casing for `find` so that we support most cases while safeguarding
// against unwanted mutations
Some(cmd)
if cmd == "find"
&& cmd_args
.iter()
.any(|arg| arg.contains("-exec") || arg.contains("-delete")) =>
{
return true;
},
Some(cmd) if !READONLY_COMMANDS.contains(&cmd.as_str()) => return true,
None => return true,
_ => (),
}
}
false
}
pub async fn invoke(&self, updates: impl Write) -> Result<InvokeOutput> {
let output = run_command(&self.command, MAX_TOOL_RESPONSE_SIZE / 3, Some(updates)).await?;
let result = serde_json::json!({
"exit_status": output.exit_status.unwrap_or(0).to_string(),
"stdout": output.stdout,
"stderr": output.stderr,
});
Ok(InvokeOutput {
output: OutputKind::Json(result),
})
}
pub fn queue_description(&self, updates: &mut impl Write) -> Result<()> {
queue!(updates, style::Print("I will run the following shell command: "),)?;
// TODO: Could use graphemes for a better heuristic
if self.command.len() > 20 {
queue!(updates, style::Print("\n"),)?;
}
queue!(
updates,
style::SetForegroundColor(Color::Green),
style::Print(&self.command),
style::Print("\n"),
style::ResetColor
)?;
// Add the summary if available
if let Some(summary) = &self.summary {
queue!(
updates,
style::Print(CONTINUATION_LINE),
style::Print("\n"),
style::Print(PURPOSE_ARROW),
style::SetForegroundColor(Color::Blue),
style::Print("Purpose: "),
style::ResetColor,
style::Print(summary),
style::Print("\n"),
)?;
}
queue!(updates, style::Print("\n"))?;
Ok(())
}
pub async fn validate(&mut self, _ctx: &Context) -> Result<()> {
// TODO: probably some small amount of PATH checking
Ok(())
}
}
pub struct CommandResult {
pub exit_status: Option<i32>,
/// Truncated stdout
pub stdout: String,
/// Truncated stderr
pub stderr: String,
}
/// Run a bash command.
/// # Arguments
/// * `max_result_size` - max size of output streams, truncating if required
/// * `updates` - output stream to push informational messages about the progress
/// # Returns
/// A [`CommandResult`]
pub async fn run_command<W: Write>(
command: &str,
max_result_size: usize,
mut updates: Option<W>,
) -> Result<CommandResult> {
// We need to maintain a handle on stderr and stdout, but pipe it to the terminal as well
let mut child = tokio::process::Command::new("bash")
.arg("-c")
.arg(command)
.stdin(Stdio::inherit())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.wrap_err_with(|| format!("Unable to spawn command '{}'", command))?;
let stdout_final: String;
let stderr_final: String;
let exit_status: ExitStatus;
// Buffered output vs all-at-once
if let Some(u) = updates.as_mut() {
let stdout = child.stdout.take().unwrap();
let stdout = tokio::io::BufReader::new(stdout);
let mut stdout = stdout.lines();
let stderr = child.stderr.take().unwrap();
let stderr = tokio::io::BufReader::new(stderr);
let mut stderr = stderr.lines();
const LINE_COUNT: usize = 1024;
let mut stdout_buf = VecDeque::with_capacity(LINE_COUNT);
let mut stderr_buf = VecDeque::with_capacity(LINE_COUNT);
let mut stdout_done = false;
let mut stderr_done = false;
exit_status = loop {
select! {
biased;
line = stdout.next_line(), if !stdout_done => match line {
Ok(Some(line)) => {
writeln!(u, "{line}")?;
if stdout_buf.len() >= LINE_COUNT {
stdout_buf.pop_front();
}
stdout_buf.push_back(line);
},
Ok(None) => stdout_done = true,
Err(err) => error!(%err, "Failed to read stdout of child process"),
},
line = stderr.next_line(), if !stderr_done => match line {
Ok(Some(line)) => {
writeln!(u, "{line}")?;
if stderr_buf.len() >= LINE_COUNT {
stderr_buf.pop_front();
}
stderr_buf.push_back(line);
},
Ok(None) => stderr_done = true,
Err(err) => error!(%err, "Failed to read stderr of child process"),
},
exit_status = child.wait() => {
break exit_status;
},
};
}
.wrap_err_with(|| format!("No exit status for '{}'", command))?;
u.flush()?;
stdout_final = stdout_buf.into_iter().collect::<Vec<_>>().join("\n");
stderr_final = stderr_buf.into_iter().collect::<Vec<_>>().join("\n");
} else {
// Take output all at once since we are not reporting anything in real time
//
// NOTE: If we don't split this logic, then any writes to stdout while calling
// this function concurrently may cause the piped child output to be ignored
let output = child
.wait_with_output()
.await
.wrap_err_with(|| format!("No exit status for '{}'", command))?;
exit_status = output.status;
stdout_final = from_utf8(&output.stdout).unwrap_or_default().to_string();
stderr_final = from_utf8(&output.stderr).unwrap_or_default().to_string();
}
Ok(CommandResult {
exit_status: exit_status.code(),
stdout: format!(
"{}{}",
truncate_safe(&stdout_final, max_result_size),
if stdout_final.len() > max_result_size {
" ... truncated"
} else {
""
}
),
stderr: format!(
"{}{}",
truncate_safe(&stderr_final, max_result_size),
if stderr_final.len() > max_result_size {
" ... truncated"
} else {
""
}
),
})
}
#[cfg(test)]
mod tests {
use super::*;
#[ignore = "todo: fix failing on musl for some reason"]
#[tokio::test]
async fn test_execute_bash_tool() {
let mut stdout = std::io::stdout();
// Verifying stdout
let v = serde_json::json!({
"command": "echo Hello, world!",
});
let out = serde_json::from_value::<ExecuteBash>(v)
.unwrap()
.invoke(&mut stdout)
.await
.unwrap();
if let OutputKind::Json(json) = out.output {
assert_eq!(json.get("exit_status").unwrap(), &0.to_string());
assert_eq!(json.get("stdout").unwrap(), "Hello, world!");
assert_eq!(json.get("stderr").unwrap(), "");
} else {
panic!("Expected JSON output");
}
// Verifying stderr
let v = serde_json::json!({
"command": "echo Hello, world! 1>&2",
});
let out = serde_json::from_value::<ExecuteBash>(v)
.unwrap()
.invoke(&mut stdout)
.await
.unwrap();
if let OutputKind::Json(json) = out.output {
assert_eq!(json.get("exit_status").unwrap(), &0.to_string());
assert_eq!(json.get("stdout").unwrap(), "");
assert_eq!(json.get("stderr").unwrap(), "Hello, world!");
} else {
panic!("Expected JSON output");
}
// Verifying exit code
let v = serde_json::json!({
"command": "exit 1",
"interactive": false
});
let out = serde_json::from_value::<ExecuteBash>(v)
.unwrap()
.invoke(&mut stdout)
.await
.unwrap();
if let OutputKind::Json(json) = out.output {
assert_eq!(json.get("exit_status").unwrap(), &1.to_string());
assert_eq!(json.get("stdout").unwrap(), "");
assert_eq!(json.get("stderr").unwrap(), "");
} else {
panic!("Expected JSON output");
}
}
#[test]
fn test_requires_acceptance_for_readonly_commands() {
let cmds = &[
// Safe commands
("ls ~", false),
("ls -al ~", false),
("pwd", false),
("echo 'Hello, world!'", false),
("which aws", false),
// Potentially dangerous readonly commands
("echo hi > myimportantfile", true),
("ls -al >myimportantfile", true),
("echo hi 2> myimportantfile", true),
("echo hi >> myimportantfile", true),
("echo $(rm myimportantfile)", true),
("echo `rm myimportantfile`", true),
("echo hello && rm myimportantfile", true),
("echo hello&&rm myimportantfile", true),
("ls nonexistantpath || rm myimportantfile", true),
("echo myimportantfile | xargs rm", true),
("echo myimportantfile|args rm", true),
("echo <(rm myimportantfile)", true),
("cat <<< 'some string here' > myimportantfile", true),
("echo '\n#!/usr/bin/env bash\necho hello\n' > myscript.sh", true),
("cat <<EOF > myimportantfile\nhello world\nEOF", true),
// Safe piped commands
("find . -name '*.rs' | grep main", false),
("ls -la | grep .git", false),
("cat file.txt | grep pattern | head -n 5", false),
// Unsafe piped commands
("find . -name '*.rs' | rm", true),
("ls -la | grep .git | rm -rf", true),
("echo hello | sudo rm -rf /", true),
// `find` command arguments
("find important-dir/ -exec rm {} \\;", true),
("find . -name '*.c' -execdir gcc -o '{}.out' '{}' \\;", true),
("find important-dir/ -delete", true),
("find important-dir/ -name '*.txt'", false),
];
for (cmd, expected) in cmds {
let tool = serde_json::from_value::<ExecuteBash>(serde_json::json!({
"command": cmd,
}))
.unwrap();
assert_eq!(
tool.requires_acceptance(),
*expected,
"expected command: `{}` to have requires_acceptance: `{}`",
cmd,
expected
);
}
}
}