Skip to content

fix(exec): keep a quoted empty word as an argument - #332

Merged
pgrayy merged 5 commits into
strands-agents:mainfrom
pgrayy:feature/pgrayy/empty-word
Nov 20, 2026
Merged

pgrayy merged 5 commits into
strands-agents:mainfrom
pgrayy:feature/pgrayy/empty-word

Conversation

@pgrayy

@pgrayy pgrayy commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

Description

A double-quoted word that expands to nothing is dropped, so a command sees shifted arguments. f "" x passes one argument, not two, and "$unset" disappears. A test such as [ "$p" != "" ] reaches test as [ /a != ], which nvm hits when it loads.

In expand_part_split, a double-quoted part now makes a field even when it comes out empty. "", "$unset" and "$1" with no arguments each pass one empty argument, as in bash. "$@" with no arguments still makes no field, because it is the one quoted form that may expand to nothing.

Keeping the word means commands now get "" as an operand, and VfsKernel read an empty path as the cwd. rm -rf "" would then act on the cwd, and touch "", mv "" and chmod "" would change it. vfs::absolute now refuses an empty path with NotFound, as POSIX does, and its error names the operand as ''. Every VfsKernel method gets that rule from the one helper. The Kernel trait now states the rule, so a custom kernel knows to follow it, and the test MemKernel resolves through vfs::absolute too. Three commands follow bash on top of it: cd stays put when the directory it chose is empty, whether from "", $HOME or $OLDPWD; mkdir -p "" fails; and ln -s "" l fails, as symlink(2) does.

The same rule reaches the embedding API: Shell::list_files("") and the MCP list_dir tool with "" now fail with NotFound, where they listed the cwd.

vfs::absolute now returns io::Result<String>. Box will call it once strands-agents/box#534 merges, and it will handle the error when it next moves its pin.

The last commit fixes a panic in test. parse_primary checked for two more words but read three, so [ a != ] ended the script. It is now false and the script goes on. bash exits 2 with a message there, where Shell exits 1. That gap applies to every test error, so it is left for its own change.

The reproduction in #299 now prints what bash prints:

$ strands-shell -c 'f(){ echo $#; }; f "" x; f '"''"' x; x=""; f "$x" y; printf "[%s]" "" x; echo'
2
2
2
[][x]

Related Issues

Fixes #299. Fixes #322. Refs #321: the panic is fixed, but test still exits 1 where bash exits 2.

Documentation PR

None.

Type of Change

Bug fix

Testing

  • cargo test --lib: 86 passed

  • cargo test --test vfs_unit: 98 passed. absolute_against_cwd now also checks that "" is NotFound.

  • cargo test --test custom_kernel: 15 passed

  • cargo test --test shell_integration: 2032 passed, including [ a != ], with main merged in.

  • cargo test --test differential: passed. Two known-difference markers come out, in quote_empty_args and case_glob_and_fallthrough, and two cases are new: empty_command_name and empty_path, which runs rm, touch, mkdir, mv, ln, rmdir, chmod, test -e and cd on "". All four fail on main.

  • cargo fmt is clean, and clippy reports nothing in the changed lines.

  • I ran the relevant test suites for the bindings I touched (cargo test --workspace --all-targets, pytest tests/python, npm test)

  • If I touched Rust, I ran cargo fmt and cargo clippy

Checklist

  • I have read the CONTRIBUTING document
  • I have reviewed and understand every line of code in this PR, including any generated by AI tools, and I can explain why it works
  • My change is focused and reasonably small; I have split unrelated work into separate PRs
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

vfs::absolute read "" as the cwd, so a command given an empty operand
acted on the cwd: rm -rf "" walked it and, joining "" with each child
name, removed top-level paths named like its children. An empty path is
now ENOENT, as in POSIX, in the one helper both kernels use. cd "" stays
a no-op, as in bash, mkdir -p "" fails, and an empty symlink target is
refused, as symlink(2) does.
A double-quoted part now makes a field even when it comes out empty, so
"" and "$unset" pass an empty argument, as in bash. "$@" with no
arguments still makes no field. A quoted empty command name now fails
with 127 instead of running the next word.

Fixes strands-agents#299
parse_primary checked for two more words but read three, so [ a != ]
panicked and ended the script. It now needs all three, so the test is
false and the script goes on.
@github-actions github-actions Bot added the bug Something isn't working label Nov 19, 2026
@pgrayy

pgrayy commented Nov 19, 2026

Copy link
Copy Markdown
Member Author

@strandly-the-agent Please review this PR

@strandly-the-agent strandly-the-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes requested: one blocker, and it only affects custom kernels. On the default VfsKernel this is correct and safe: the #299 repro matches bash, rm -rf "" leaves the cwd alone, and [ a != ] no longer panics. But a kernel passed to Shell::with_kernel now receives "" for "$unset", and on the repo's own MemKernel that makes rm -rf "$unset" delete the cwd and a root-level directory, where base fails safely.

Item Result
🔴 rm -rf "$unset" on a custom Kernel Deletes files; base prints missing operand (inline, src/exec.rs)
🟡 cd with an empty HOME or OLDPWD Fails; base and bash return 0 (inline, tested fix)
🟡 Error text for an empty operand mv: no such file or directory names no operand (inline, tested fix)
Tests cargo test --workspace --all-targets passes on 3abe869: 2,683 passed, 0 failed
What I verified
  • Reviewed head 3abe869 against base 55d7816, debug builds on Linux aarch64, compared with bash 5.2.15.
  • The #299 repro prints 1 2 2 [x] on base and 2 2 2 [][x] on head.
  • cargo test --workspace --all-targets on head exits 0 with 2,683 passed. The differential suite counts as one test and covers 121 cases. I did not run the Python or Node suites, cargo fmt or clippy.
  • "All four fail on main" holds: with head's differential cases on base, 4 cases fail, and test_binary_op_missing_right panics at src/builtins/test.rs:97.
  • I re-ran each inline finding myself on head, base and bash. Both suggested fixes were applied to a copy of head, where the lib, custom_kernel, differential, shell_integration and vfs_unit suites still pass.
  • From the adversarial pass, which I spot-checked but did not re-run in full:
    • Expansion: about 22,700 word forms and 12,900 random multi-word cases on bash, base and head. No word where base matched bash and head does not.
    • Empty paths on VfsKernel: 254 commands with "", none changed the cwd.
    • test: 8,000 random expressions. Head never panics; base panics on 333.
  • One adversarial probe (rm -rf ""/) was run under real bash in my sandbox by mistake. GNU rm refused it, and I checked that the sandbox and both worktrees were intact. No result here depends on it.
Questions (1 blocking, 3 non-blocking)
  1. Blocking: should Shell refuse an empty path before it reaches the kernel, or is each kernel expected to? See the inline comment on src/exec.rs.
  2. The description says Box is the only other caller of vfs::absolute. Box main does not call it yet: it has a private copy (crates/shell/src/resolve.rs:21) that maps "" to the cwd, and only the open box#534 switches to vfs::absolute. So Box gets the compile error only if box#534 merges before its pin moves past this PR. Is that order certain? I read the Box source and did not run it.
  3. Shell::list_files("") and the MCP list_dir tool with "" go from listing the cwd to NotFound. Is that intended? If so it is worth a line in the description. The API pass ran both; I did not re-run them.
  4. Could the description add Fixes #322? #321's panic is fixed here, but it also asks for status 2, so Refs #321 fits better. Does this PR supersede #320?
Appendix: non-blocking (3)
  • Coverage: empty_path.sh does not exercise rmdir, chmod, ls or readlink. The adversarial pass reverted the guard in list_dir, remove_file, remove_dir, set_permissions, rename (destination) and read_link in a scratch copy: all four suites still passed, and rmdir "" removed the cwd. Adding rmdir "$e" and chmod 600 "$e" to the case would pin them. I did not re-run the mutant.
  • Pre-existing, filed #335: cat, tee, mkdir, rmdir, chmod, cp, sed -i and wc stop at the first operand that fails. This PR makes it easier to hit: with LOG unset, echo data | tee "$LOG" | cat prints nothing on head, where base and bash print data.
  • Pre-existing, filed #334: [ "" -lt 3 ] is true. With this PR unset n; [ "$n" -lt 3 ] goes from false to true; bash reports an error and is false.

Comment thread src/exec.rs Resolved
Comment thread src/builtins/cd.rs Resolved
Comment thread src/vfs.rs Resolved
The Kernel trait now states that an empty path names no file, and the
test MemKernel resolves through vfs::absolute, so a custom kernel has
the rule to follow and the repo's own example follows it. cd maps an
empty directory to `.` after choosing it, so an empty HOME or OLDPWD
stays put, as in bash. The empty-path error names its operand as '',
and empty_path covers rmdir and chmod.
@pgrayy

pgrayy commented Nov 20, 2026

Copy link
Copy Markdown
Member Author

Thanks. All three inline points are addressed in ef2024c, with main merged in (f8783fc). On the questions:

  1. Box: right, Box main still has its own copy. box#534 switches it to vfs::absolute, and Box moves its pin past this PR only after that, so the pin bump has to handle the new Result. The description now says so.
  2. Shell::list_files("") and MCP list_dir with "": intended, since both are an empty path. The description now notes it.
  3. Added Fixes #322 and Refs #321. This PR has the same word fix as fix: preserve literal empty double-quoted arguments #320, plus the empty-path rule that keeps it from making rm -rf "$unset" act on the cwd. I'll leave it to the maintainers and @ikenyal how to settle the two.

Coverage: empty_path now also runs rmdir and chmod. #334 and #335 stay separate.

--lib 86, vfs_unit 98, custom_kernel 15, differential and shell_integration 2032 pass, and fmt, cargo doc -D warnings and clippy on the changed lines are clean.

@pgrayy

pgrayy commented Nov 20, 2026

Copy link
Copy Markdown
Member Author

@strandly-the-agent Please review this PR

@strandly-the-agent strandly-the-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approve. All three findings are fixed on f8783fc, and I found nothing new in the delta or the merge of main.

Previous finding Status
🔴 rm -rf "$unset" on a custom Kernel Fixed in ef2024c. My repro on MemKernel now exits 0 and removes nothing, as bash does
🟡 cd with an empty HOME or OLDPWD Fixed. Both return 0 and stay put, as in bash
🟡 Error text for an empty operand Fixed. mv "$S" x prints mv: no such file or directory: ''
Tests cargo test --workspace --all-targets passes on f8783fc: 2,698 passed, 0 failed. CI Gate is green

The blocker is closed by making each kernel own the rule, which the Kernel trait now states. A third-party kernel that still maps "" to the cwd is not protected by Shell; I take that as the decided trade-off, not an open item.

What I verified
  • Reviewed 3abe869..ef2024c (7 files) and the merge f8783fc. Debug builds on Linux aarch64, head f8783fc against main @ 2a1495e and bash 5.2.15.
  • Blocker: the test from my earlier inline comment, appended to tests/custom_kernel.rs in a copy of f8783fc, passes. rm returns 0, and the cwd and /data/db.txt are kept.
  • cd: HOME=; cd, OLDPWD=; cd -, cd "" and set -e; HOME=; cd match bash. cd "" keeps PWD and sets OLDPWD as bash does.
  • Empty paths on VfsKernel: rm -rf, rmdir, mv and chmod on "$unset" leave the cwd alone with bash's statuses.
  • empty_path.sh run under bash reproduces the committed .expected stdout exactly, including the new rmdir=1 and chmod=1.
  • Merge of main: 22 [[ ]] forms from #325 with "", "$unset", "$1" and "$@" give the same status on head, main and bash.
  • cargo test --workspace --all-targets on f8783fc exits 0. I did not run the Python or Node suites, cargo fmt or clippy locally; CI ran the Python and Node suites and they pass.

@pgrayy
pgrayy enabled auto-merge (squash) November 20, 2026 16:14
@pgrayy
pgrayy merged commit 754a534 into strands-agents:main Nov 20, 2026
34 of 36 checks passed
@pgrayy
pgrayy deleted the feature/pgrayy/empty-word branch November 20, 2026 16:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

3 participants