Skip to content

Commit 6f74eff

Browse files
committed
ENH: Use $SANDBOX_RO_BIND environment variable
1 parent 3abe87e commit 6f74eff

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ You can run `sandbox-run bash` to spawn **interactive shell inside the sandbox**
125125
#### Environment variables
126126

127127
* `BWRAP_ARGS=`– Extra arguments passed to `bwrap` process; space or line-delimited (if arguments such as paths themselves contain spaces).
128+
* `SANDBOX_RO_BIND=`– List of additional path glob expressions to mount read-only inside the sandbox.
128129
* `VERBOSE=`– Print full `exec bwrap` command line right before execution.
129130

130131

sandbox-run

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ format_args () {
3434
formatted_cmdline="$(format_args "$@")"
3535
warn () { echo "sandbox-run: $*" >&2; }
3636

37+
lf='
38+
'
39+
split_args_by_lf () { printf '%s' "$1" | case "$1" in *$lf*) cat ;; *) tr ' ' '\n' ;; esac; }
40+
3741
# RO-bind select paths
3842
paths='
3943
/etc/resolv.conf
@@ -51,14 +55,13 @@ paths='
5155
/usr
5256
'
5357

54-
lf='
55-
'
56-
split_args_by_lf () { printf '%s' "$1" | case "$1" in *$lf*) cat ;; *) tr ' ' '\n' ;; esac; }
5758
# Support BWRAP_ARGS passed to the process as well as via .env file
5859
prev_BWRAP_ARGS="${BWRAP_ARGS:-}"
5960
# Init env from dotenv file
6061
# shellcheck disable=SC2046
6162
[ ! -e "$cwd/.env" ] || { . "$cwd/.env"; export $(grep -Pzo '(?m)^\w*(?==)' "$cwd/.env" | tr '\0' '\n'); }
63+
paths="$(split_args_by_lf "${SANDBOX_RO_BIND:-}" | tr ',' '\n')
64+
$paths" # Add paths from SANDBOX_RO_BIND
6265
IFS="$lf" # Split args only on newline
6366
# shellcheck disable=SC2046
6467
set -- $(split_args_by_lf "${BWRAP_ARGS:-}") $(split_args_by_lf "${prev_BWRAP_ARGS:-}") "$bin" "$@"
@@ -82,7 +85,7 @@ for var in $(env -0 |
8285
paste -z -s -d '|'))$" |
8386
paste -z -s -d '|'))=" |
8487
grep -Ezv -e '^(_|LS_COLORS|PS1)=' |
85-
grep -Ezv -e '^(BWRAP_ARGS)=' |
88+
grep -Ezv -e '^(BWRAP_ARGS|SANDBOX_RO_BIND)=' |
8689
tr '\0' '\037'); do
8790
set -- --setenv "${var%%=*}" "${var#*=}" "$@"
8891
done

tests/test-ro-bind-env-var.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/sh
2+
set -eu
3+
4+
. "${0%/*}/_init.sh"
5+
6+
SANDBOX_RO_BIND='/etc/shad*,/etc/motd*' \
7+
sandbox-run sh -c 'test -f /etc/shadow; test -f /etc/motd'

0 commit comments

Comments
 (0)