Skip to content
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
5 changes: 4 additions & 1 deletion src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ async fn run_script(
/// Execute a full input line. `proc` is the shell's process — cd modifies it.
/// Returns (exit_code, should_exit).
pub async fn execute(os: Arc<dyn Kernel>, proc: &mut Process, input: &str) -> (i32, bool) {
let result = if input.contains('\n') {
let result = if proc.max_input > 0 && input.len() > proc.max_input {
proc.err_msg("strands-shell: input too large");
(1, false)
} else if input.contains('\n') {
execute_sourced(os.clone(), proc, input).await
} else {
execute_with_reader(os.clone(), proc, input, &mut |_| None).await
Expand Down
39 changes: 33 additions & 6 deletions tests/shell_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4252,6 +4252,26 @@ fn builder_max_input() {
}));
}

#[test]
fn max_input_applies_to_whole_multiline_input() {
let (rt, local) = rt();
rt.block_on(local.run_until(async {
let mut shell = Shell::builder().max_input(16).build().unwrap();
// Each line is under the limit, but the whole input is not
let out = shell.run("echo aaaa\necho bbbb\necho cccc").await;
assert_eq!(
(out.status, out.stdout.as_str(), out.stderr.as_str()),
(1, "", "strands-shell: input too large\n")
);
// Multiline input within the limit still runs
let out = shell.run("echo a\necho b").await;
assert_eq!(
(out.status, out.stdout.as_str(), out.stderr.as_str()),
(0, "a\nb\n", "")
);
}));
}

#[test]
fn builder_max_file_size() {
let (rt, local) = rt();
Expand Down Expand Up @@ -4427,17 +4447,24 @@ fn max_output_truncates_subst() {
fn max_input_in_subst() {
let (rt, local) = rt();
rt.block_on(local.run_until(async {
// The top-level command is `cat <<EOF`, which is under the limit.
// The heredoc body reaches capture_output, which checks the limit again.
// The sourced script's first command is `cat <<EOF`, which is under
// the limit. The heredoc body reaches capture_output, which checks
// the limit again.
let mut shell = Shell::builder().max_input(12).build().unwrap();
let out = shell.run("cat <<EOF\n$(echo short)\nEOF").await;
shell
.write_file("/tmp/s", b"cat <<EOF\n$(echo short)\nEOF\n")
.await
.unwrap();
shell
.write_file("/tmp/l", b"cat <<EOF\n$(echo aaaaaaaaaaaaaaaaaaaa)\nEOF\n")
.await
.unwrap();
let out = shell.run(". /tmp/s").await;
assert_eq!(
(out.status, out.stdout.as_str(), out.stderr.as_str()),
(0, "short\n", "")
);
let out = shell
.run("cat <<EOF\n$(echo aaaaaaaaaaaaaaaaaaaa)\nEOF")
.await;
let out = shell.run(". /tmp/l").await;
assert_eq!(out.stdout, "\n");
assert_eq!(out.stderr, "strands-shell: input too large\n");
}));
Expand Down
Loading