Skip to content

security: delete ShellEscape and migrate every caller to real single-quoting - #702

Merged
scttfrdmn merged 2 commits into
mainfrom
fix/680-delete-shellescape
Oct 5, 2026
Merged

scttfrdmn merged 2 commits into
mainfrom
fix/680-delete-shellescape

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Fixes #680. ShellEscape is deleted, not deprecated, and every caller migrated.

Deleted because the name was the trap. It was strconv.Quote — Go/C escaping inside double quotes, where a POSIX shell still expands $VAR, $(…) and backticks — while being the obvious thing to reach for when you want "the escape function". Leaving a deprecated footgun with a better name than the safe function keeps the trap baited.

The storage user-data was the worst of it, and I measured it

Mount points, mount options, filesystem DNS names and device names all went through shellEscape. Rendering an ordinary EFS config produced three distinct defects:

mkdir -p "/efs$(id -u)"                    ← substitution RUNS at boot
echo "export EFS_MOUNT="/efs""             ← nested quotes; correct only by accident
echo "export EFS_MOUNT="/my efs""          ← export EFS_MOUNT=/my, then `efs` as a command

The third needs nothing exotic. A mount point with a space is legal, and it corrupts /etc/profile.d/efs.sh for every login on the instance — $EFS_MOUNT is wrong and a stray efs runs each time. The second is the one I'd have missed by eye: it is syntactically valid and happens to produce the right string for simple paths, because bash concatenates "export EFS_MOUNT=" + /efs + "".

The fstab lines were separate: they interpolated the mount point raw into a double-quoted echo, so a substitution executed there too. Those are now single-quoted as a whole line, since fstab wants literal text and nothing in it should expand. The attached-volume lines use printf with the path quoted instead, because $SPAWN_DEV must still expand — the whole line can't be single-quoted.

(One thing I did not fix: fstab needs \040 for a space in a path, so a spaced mount point is still wrong in fstab regardless of quoting. That is a pre-existing and separate concern from shell expansion; flagging rather than silently half-fixing.)

spawn config set executed values instead of storing them

The key and value come straight from argv and the result is executed on the instance, so spawn config set k '$(id)' ran id there. The caller already owns the box, so this is self-inflicted rather than a privilege boundary — but it also meant a value containing $ could not be stored at all, which is a plain correctness bug.

A passing test was certifying the hole

This is the part that convinced me a gate was needed rather than a comment.

TestShellEscapeAttackPatterns fed in "; rm -rf /", "$(whoami)", "`curl evil.com`" and asserted:

if !strings.HasPrefix(escaped, "\"") {
    t.Errorf("ShellEscape() did not quote attack pattern: ...")
}

It required the output to start with a double quote — the exact property that makes it unsafe — and passed. A test named for attack patterns, reading as a security control, blessing the vulnerability.

I kept its input list and now run each value through a real shell, requiring a byte-for-byte round-trip:

cmd := exec.Command("bash", "-c", "printf '%s' "+ShellQuote(p))
cmd.Env = []string{"PATH=/usr/bin:/bin", "HOME=/should-not-appear", "IFS= "}
// out must equal p exactly

All 17 patterns pass. Checking the string shape is what let the old version pass while the behaviour was wrong; checking the shell's behaviour can't.

The gate fails the build on any func ShellEscape( or strconv.Quote in pkg/security, and is verified to fire when I reintroduce the function.

Two more found during the audit

  • The MPI template registered an unused escaper. --mpi-command is passed through strconv.Quote, so any command with arguments becomes one argv word (and $ still expands) #660 moved --mpi-command out of the template entirely, leaving shellEscape registered with zero uses — an escaper sitting there for the next person to reach for. Removed.
  • The pipeline lambda interpolated a stage command twice: escaped into STAGE_CMD='%s', and raw into echo "Running stage command: %s". Inside those double quotes a $(…) executed — and executed again when the stage actually ran, so a substitution with side effects happened twice. The echo now prints $STAGE_CMD, and I moved the assignment above the echo that reads it (my first version printed empty).

Not consolidated, deliberately

There are still two other correct single-quoting implementations: plugin.shellQuoteValue and taskproto.shQuote. Both are right, and both are in packages that would gain a dependency on pkg/security to share one. I left them and noted it rather than widening this diff — the unsafe one is gone, which was the point.

Gates

gofmt -l empty across cmd/ pkg/ lambda/, go vet ./..., go test -short ./... (whole module), the lambda module builds and vets, make check-docs, changelog gate, semgrep → 0 findings.

Three pre-existing tests asserted the old double-quoted renderings. I updated them to require single quotes rather than relaxing them — the values must still appear, now in quoting that protects them.

--no-dns and --dns were classified as sweep gaps. They are not: a sweep
row gets spawn:dns-name only if the param file sets dns:/dns_name:, and
spored skips registration without that tag — so sweeps never register
DNS and there is nothing for --no-dns to disable.

Found while verifying #549, which is fully fixed: --no-dns is wired,
dns.enabled is read as a tri-state *bool (nil = inherit, so an absent
key cannot disable DNS), and six pkg/config tests plus a cmd-level guard
test cover every case the report raised.
…quoting

ShellEscape was strconv.Quote — Go/C escaping inside DOUBLE quotes,
where a POSIX shell still expands $VAR, $(...) and backticks — while
being named as though it were the safe choice. Deleted rather than
deprecated: the name was the trap.

The storage user-data was the worst of it. For an ordinary EFS mount
point it produced mkdir -p "/efs$(id -u)" (substitution runs), nested
quotes that were correct only by accident, and `/my efs` rendering as
export EFS_MOUNT=/my — leaving `efs` to run as a command on every
login. The fstab lines took the mount point raw into a double-quoted
echo; they are now single-quoted whole, since fstab wants literal text.

spawn config set executed values rather than storing them: the key and
value come from argv and the result runs on the instance.

Also removes the dead shellEscape template registration in the MPI
user-data, and stops the pipeline lambda interpolating a stage command
twice — once escaped into STAGE_CMD and once RAW into an echo, where a
substitution executed and then executed again for real.

A gate fails the build on any func ShellEscape( or strconv.Quote in
pkg/security. It earns a gate because a PASSING test named
TestShellEscapeAttackPatterns fed in $(whoami) and asserted only that
the output started with a double quote — the exact property that made it
unsafe. Its inputs are kept and now round-tripped through a real shell.

Fixes #680
@scttfrdmn
scttfrdmn merged commit 3d2191c into main Oct 5, 2026
6 checks passed
@scttfrdmn
scttfrdmn deleted the fix/680-delete-shellescape branch October 5, 2026 01:31
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.

security.ShellEscape is not shell-safe (it is strconv.Quote): audit and migrate the remaining call sites

1 participant