Created
November 10, 2025 09:02
-
-
Save vmikk/08212e23459d62da40b2cc0c5e177e0b to your computer and use it in GitHub Desktop.
Run shell commands in a lightweight sandbox (using `bubblewrap`)
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
| sbx() { | |
| : ' | |
| sbx - run a command in a minimal bubblewrap sandbox | |
| Overview: | |
| - Only the current working directory has read-write permissions and allows persistent changes | |
| - Host filesystem mounted read-only | |
| - /proc and /dev are also sandboxed | |
| - Existing PATH/env are kept, so system + conda tools work normally | |
| bwrap flags explanation: | |
| --unshare-all Unshare all namespaces (mount, pid, ipc, uts, cgroup, user, etc.) | |
| --share-net Keep host network (remove if you also want network isolation) | |
| --new-session Start a new session; helps avoid TTY injection tricks | |
| --die-with-parent Kill the sandbox when the calling shell exits | |
| --ro-bind / / Mount host / as read-only root inside the sandbox | |
| --dev /dev Create a minimal /dev inside the sandbox | |
| --proc /proc Mount a fresh /proc | |
| --tmpfs /tmp Use an in-memory /tmp (no writes to host /tmp) | |
| --bind $PWD $PWD Bind current directory read-write (real changes allowed here) | |
| --chdir $PWD Start the command in the current directory | |
| Usage: | |
| sbx <command> [args...] | |
| sbx ./script.sh | |
| sbx python run.py --flag | |
| sbx bash # to run custom commands; when finished, run `exit` | |
| NB! | |
| sbx echo 111 > ../111.txt # this will bypass the sandbox! | |
| To install bubblewrap, use: | |
| Debian/Ubuntu/Mint: sudo apt install bubblewrap | |
| Fedora: sudo dnf install bubblewrap | |
| ' | |
| if [ "$#" -eq 0 ]; then | |
| echo "Usage: sbx <command> [args...]" >&2 | |
| return 1 | |
| fi | |
| local CURRENTDIR="$PWD" | |
| bwrap \ | |
| --unshare-all --share-net \ | |
| --new-session \ | |
| --die-with-parent \ | |
| --ro-bind / / \ | |
| --dev /dev \ | |
| --proc /proc \ | |
| --tmpfs /tmp \ | |
| --bind "$CURRENTDIR" "$CURRENTDIR" \ | |
| --chdir "$CURRENTDIR" \ | |
| "$@" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment