Skip to content

fix(cloudlab): quote ssh arguments in on_nodes (SC2068) - #103

Open
randomizedcoder wants to merge 1 commit into
PlatformLab:mainfrom
randomizedcoder:fix/shell-unquoted-args
Open

randomizedcoder wants to merge 1 commit into
PlatformLab:mainfrom
randomizedcoder:fix/shell-unquoted-args

Conversation

@randomizedcoder

Copy link
Copy Markdown

Bug: unquoted $@ forwarded to ssh in on_nodes (SC2068)

cloudlab/bin/on_nodes runs a caller-supplied command on a range of cluster nodes, forwarding the command and its arguments with a bare, unquoted $@:

for ((i = $first ; i <= $last; i++)); do
    node=node$i
    ...
    ssh -4 $node $@
done

An unquoted $@ is subject to word splitting and glob expansion in on_nodes itself, before ssh ever sees the arguments. So:

  • an argument containing spaces ("grep foo bar") is split into several arguments;
  • an argument containing a glob metacharacter ('*.c') is expanded against the local filesystem;
  • an empty-string argument is dropped entirely.

shellcheck reports this as SC2068 (error).

Fix

Quote both the node and the forwarded arguments so word boundaries reach ssh intact:

-    ssh -4 $node $@
+    ssh -4 "$node" "$@"

Verification (linter gate + bats regression)

Before:  shellcheck --include=SC2068 cloudlab/bin/on_nodes
         line 28: SC2068 (error): Double quote array expansions ...
After:   (clean, exit 0)

A bats scaffold is included as executable documentation (no new CI is wired). It stubs ssh on PATH to record the exact argv it receives, then asserts argument boundaries survive. Run against the old bare-$@ script, three cases fail; after the fix all pass:

$ nix shell nixpkgs#bats -c bats cloudlab/tests/on_nodes.bats
 ✓ usage error when fewer than 3 args
 ✓ simple command is forwarded verbatim
 ✓ argument containing spaces stays a single argument      # RED before fix
 ✓ argument with a glob char is not expanded by on_nodes   # RED before fix
 ✓ empty-string argument is preserved as one argument      # RED before fix

The pre-existing SC2004 style note on the arithmetic loop ($first/$last inside (( ))) is a separate, style-level check and is left untouched to keep this diff focused on the SC2068 defect.

on_nodes forwarded the remote command with a bare, unquoted $@:

    ssh -4 $node $@

An unquoted $@ undergoes word splitting and glob expansion in the caller
before ssh sees it, so any argument that contains spaces is split into
several arguments, an argument containing a glob metacharacter is
expanded against the local filesystem, and an empty-string argument is
dropped entirely. shellcheck reports this as SC2068 (error).

Quote both the node and the forwarded arguments so word boundaries reach
ssh intact:

    ssh -4 "$node" "$@"

Also add the missing newline at end of file.

Gate: `shellcheck --include=SC2068 cloudlab/bin/on_nodes` reports the
SC2068 error before and is clean after. A bats scaffold
(cloudlab/tests/on_nodes.bats) stubs ssh on PATH and asserts argument
boundaries survive: 3 of its cases (spaces / glob char / empty arg) fail
against the old bare-$@ script and all pass after the fix; no new CI is
wired. The pre-existing SC2004 style note on the arithmetic loop is left
untouched as out of scope.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant