description:Use when a command or file content needs to pass through multiple nested shell layers (e.g. local shell -> ssh -> a jail/container exec wrapper -> su -> the target interpreter). Avoids silent corruption of $, quotes, and special characters.
---
# Remote Shell Quoting Safety
## The problem
Content containing `$`, quotes, or other shell-special characters gets
progressively mangled the more shell layers it passes through (local
shell → `ssh` → a container/jail exec wrapper like `bastille cmd` →
`su -c` → the actual target interpreter). Each layer's own quoting rules
interact, and by 3+ layers deep it becomes extremely hard to reason about
correctly — and failures are often **silent**: no error, just corrupted
content (e.g. `$_SERVER['KEY']` silently becoming `['KEY']`, `$` and the
variable name simply eaten).
**Confirmed real incident**: a `wp-config.php` snippet containing
`$_SERVER['HTTP_X_FORWARDED_PROTO']` passed through
`ssh → bastille cmd → su -m www -c '...'` came out the other side as
bare `["HTTP_X_FORWARDED_PROTO"]` — no error at the point of corruption,
the resulting fatal PHP error only surfaced two steps later at a
completely different command, making the actual cause much harder to
trace back.
## The fix: base64, not nested quoting
Don't try to escape correctly through N layers. Instead:
1. Write the target file's **complete final content** locally (a real
file, not a shell variable).
2. Base64-encode it locally: `base64 -w0 localfile > /tmp/payload.b64`
(or pipe directly).
3.`cat`/pipe the base64 text through the SSH/exec layers (base64 output
is alphanumeric-only — nothing for any shell layer to misinterpret,
regardless of how many layers it passes through).
4. Decode back to the real file **on the far side, in ONE final step**: