Skip to content

Commit b61f9d3

Browse files
oharboeclaude
andcommitted
bazel-hermetic: read builtin for signature; indirect env lookup
Two style/robustness findings from the latest Gemini pass: - Read the bazel signature with `read -r -n 2` instead of a `head` subprocess; the redirection still follows the symlink via open(), so no head/realpath/readlink is involved. Verified it reads the ELF magic through a symlink and matches "#!" for a shebang wrapper. - Preserve environment via pure-bash indirect expansion: `${!var+x}` tests whether the named variable is set (set -u-safe) and `${!var}` reads it — no eval, no subprocess. Full stub-bazel harness still green: build/repo_env, bug-case exit, info passthrough, and the non-bazelisk-wrapper fallback. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Signed-off-by: Øyvind Harboe <oyvind.harboe@zylin.com>
1 parent 5f9a5d5 commit b61f9d3

1 file changed

Lines changed: 14 additions & 12 deletions

File tree

etc/bazel-hermetic

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,12 @@ if [[ -z ${BAZEL} ]]; then
9191
fi
9292
# Wrapper detection: the bazelisk launcher by name, or any script shebang
9393
# (npm/shell wrappers). Native binaries (ELF on Linux, Mach-O on macOS)
94-
# are symlinked directly. Read via redirection: open() follows the
95-
# symlink for us, so no realpath/readlink -f is needed (neither is
96-
# reliably present on macOS).
97-
if [[ ${BAZEL} == *bazelisk* || $(head -c 2 < "${BAZEL}") == '#!' ]]; then
94+
# are symlinked directly. Read the first two bytes with the `read`
95+
# builtin (no head subprocess); the redirection's open() follows the
96+
# symlink for us, so no realpath/readlink -f is needed either.
97+
sig=""
98+
read -r -n 2 sig < "${BAZEL}" 2>/dev/null || true
99+
if [[ ${BAZEL} == *bazelisk* || ${sig} == '#!' ]]; then
98100
version=$(BAZELISK_SKIP_WRAPPER=1 "${BAZEL}" --version | awk '{print $2}')
99101
os=$(uname -s | tr '[:upper:]' '[:lower:]')
100102
arch=$(uname -m | sed 's/aarch64/arm64/')
@@ -186,14 +188,14 @@ for var in http_proxy https_proxy no_proxy HTTP_PROXY HTTPS_PROXY NO_PROXY \
186188
SSH_AUTH_SOCK SSL_CERT_FILE SSL_CERT_DIR XDG_CACHE_HOME TMPDIR \
187189
DISPLAY WAYLAND_DISPLAY XAUTHORITY \
188190
BAZELISK_HOME BAZELISK_BASE_URL BAZELISK_GITHUB_TOKEN GITHUB_TOKEN; do
189-
# Pure-bash lookup via eval: no subprocess (vs printenv, which also
190-
# silently drops everything if it isn't on PATH), and no ${!var}/-v,
191-
# which are unreliable on the bash 3.2 macOS ships. The names come from
192-
# the fixed list above, never from input, so the eval is not an
193-
# injection surface. ${var:-} keeps set -u quiet when the var is unset.
194-
eval "value=\${${var}:-}"
195-
if [[ -n ${value} ]]; then
196-
preserved_env+=("${var}=${value}")
191+
# Pure-bash indirect lookup, no subprocess and no eval: ${!var+x} tests
192+
# whether the variable named by $var is set (set -u-safe), then ${!var}
193+
# reads its value.
194+
if [[ -n ${!var+x} ]]; then
195+
value=${!var}
196+
if [[ -n ${value} ]]; then
197+
preserved_env+=("${var}=${value}")
198+
fi
197199
fi
198200
done
199201

0 commit comments

Comments
 (0)