|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# Run a command without the two side effects a unit test run should not have: |
| 5 | +# |
| 6 | +# - network reach beyond loopback |
| 7 | +# - writes outside the directories the run owns |
| 8 | +# |
| 9 | +# Written for `go test -exec`: |
| 10 | +# |
| 11 | +# go test -exec "$PWD/.github/scripts/ci/sandbox-exec.sh" ./... |
| 12 | +# |
| 13 | +# Loopback stays reachable so tests can stand up httptest servers, and writes |
| 14 | +# stay open in the temp dir, the Go caches and Terragrunt's user cache. |
| 15 | +# |
| 16 | +# How much of that holds depends on the platform: |
| 17 | +# |
| 18 | +# - macOS confines both through one seatbelt profile. |
| 19 | +# - Linux confines the network alone, through a network namespace. |
| 20 | +# |
| 21 | +# The --check flag confirms the sandbox is working properly. |
| 22 | + |
| 23 | +CURDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 24 | +PROFILE="$CURDIR/sandbox.sb" |
| 25 | + |
| 26 | +run_sandboxed() { |
| 27 | + case "$(uname -s)" in |
| 28 | + Darwin) |
| 29 | + # sandbox-exec(1) has been deprecated since macOS 10.8, but it is still |
| 30 | + # the only thing on macOS that applies a profile to an arbitrary command, |
| 31 | + # and this never ships anywhere but a contributor's machine. Restrictions |
| 32 | + # reach spawned processes because a child inherits its parent's sandbox, |
| 33 | + # per sandbox(7). |
| 34 | + exec /usr/bin/sandbox-exec \ |
| 35 | + -D "TMP=$(cd "${TMPDIR:-/tmp}" && pwd -P)" \ |
| 36 | + -D "GOCACHE=$(go env GOCACHE)" \ |
| 37 | + -D "GOMODCACHE=$(go env GOMODCACHE)" \ |
| 38 | + -D "TGCACHE=$HOME/Library/Caches/terragrunt" \ |
| 39 | + -f "$PROFILE" \ |
| 40 | + "$@" |
| 41 | + ;; |
| 42 | + Linux) |
| 43 | + # A fresh network namespace starts with `lo` down, so bring it up before |
| 44 | + # handing over to the real command. See network_namespaces(7) for what |
| 45 | + # the namespace covers and user_namespaces(7) for what --map-root-user |
| 46 | + # buys an unprivileged caller. |
| 47 | + exec unshare --map-root-user --net -- \ |
| 48 | + sh -c 'ip link set lo up && exec "$@"' sh "$@" |
| 49 | + ;; |
| 50 | + *) |
| 51 | + echo "sandbox-exec.sh: no sandbox mechanism for $(uname -s)" >&2 |
| 52 | + exit 1 |
| 53 | + ;; |
| 54 | + esac |
| 55 | +} |
| 56 | + |
| 57 | +check_egress() { |
| 58 | + local script="$1" |
| 59 | + |
| 60 | + if ! command -v curl >/dev/null 2>&1; then |
| 61 | + echo "sandbox-exec.sh: --check needs curl" >&2 |
| 62 | + exit 1 |
| 63 | + fi |
| 64 | + |
| 65 | + if "$script" curl --silent --show-error --max-time 15 --output /dev/null https://example.com; then |
| 66 | + echo "sandbox-exec.sh: check failed, https://example.com is still reachable inside the sandbox" >&2 |
| 67 | + exit 1 |
| 68 | + fi |
| 69 | + |
| 70 | + echo "sandbox-exec.sh: egress is blocked" |
| 71 | +} |
| 72 | + |
| 73 | +check_writes() { |
| 74 | + local script="$1" |
| 75 | + |
| 76 | + local allowed |
| 77 | + allowed="$(mktemp)" |
| 78 | + |
| 79 | + if ! "$script" touch "$allowed"; then |
| 80 | + rm -f "$allowed" |
| 81 | + echo "sandbox-exec.sh: check failed, the temp dir is not writable inside the sandbox" >&2 |
| 82 | + exit 1 |
| 83 | + fi |
| 84 | + |
| 85 | + rm -f "$allowed" |
| 86 | + |
| 87 | + local refused="$CURDIR/.sandbox-check" |
| 88 | + |
| 89 | + if "$script" touch "$refused" 2>/dev/null; then |
| 90 | + rm -f "$refused" |
| 91 | + echo "sandbox-exec.sh: check failed, the source tree is still writable inside the sandbox" >&2 |
| 92 | + exit 1 |
| 93 | + fi |
| 94 | + |
| 95 | + echo "sandbox-exec.sh: writes are confined to the temp dir and the caches" |
| 96 | +} |
| 97 | + |
| 98 | +run_check() { |
| 99 | + local script="${BASH_SOURCE[0]}" |
| 100 | + |
| 101 | + if ! "$script" true; then |
| 102 | + echo "sandbox-exec.sh: check failed, the sandbox cannot run a command at all" >&2 |
| 103 | + exit 1 |
| 104 | + fi |
| 105 | + |
| 106 | + check_egress "$script" |
| 107 | + |
| 108 | + if [[ "$(uname -s)" != "Darwin" ]]; then |
| 109 | + echo "sandbox-exec.sh: writes are NOT confined on $(uname -s)" |
| 110 | + return |
| 111 | + fi |
| 112 | + |
| 113 | + check_writes "$script" |
| 114 | +} |
| 115 | + |
| 116 | +if [[ "${1:-}" == "--check" ]]; then |
| 117 | + run_check |
| 118 | + exit 0 |
| 119 | +fi |
| 120 | + |
| 121 | +run_sandboxed "$@" |
0 commit comments