Skip to content

Commit ecad092

Browse files
authored
feat: inherit PYTHONSAFEPATH env var from outer process (#2076)
By default, PYTHONSAFEPATH is enabled to help prevent imports being found where they shouldn't be. However, this behavior can't be disabled, which makes it harder to use a py_binary when the non-safe path behavior is explicitly desired. To fix, the bootstrap now respects the caller environment's PYTHONSAFEPATH environment variable, if set. This allows the callers to set `PYTHONSAFEPATH=` (empty string) to override the default behavior that enables it. Fixes #2060
1 parent 990a053 commit ecad092

File tree

5 files changed

+73
-2
lines changed

5 files changed

+73
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ A brief description of the categories of changes:
3939
containing ">" sign
4040

4141
### Added
42+
* (rules) `PYTHONSAFEPATH` is inherited from the calling environment to allow
43+
disabling it (Requires {obj}`--bootstrap_impl=script`)
44+
([#2060](https://github.com/bazelbuild/rules_python/issues/2060)).
4245
* (gazelle) Added `python_generation_mode_per_package_require_test_entry_point`
4346
in order to better accommodate users who use a custom macro,
4447
[`pytest-bazel`][pytest_bazel], [rules_python_pytest] or `rules_py`

python/private/stage1_bootstrap_template.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,13 @@ declare -a interpreter_args
105105
# Don't prepend a potentially unsafe path to sys.path
106106
# See: https://docs.python.org/3.11/using/cmdline.html#envvar-PYTHONSAFEPATH
107107
# NOTE: Only works for 3.11+
108-
interpreter_env+=("PYTHONSAFEPATH=1")
108+
# We inherit the value from the outer environment in case the user wants to
109+
# opt-out of using PYTHONSAFEPATH.
110+
# Because empty means false and non-empty means true, we have to distinguish
111+
# between "defined and empty" and "not defined at all".
112+
if [[ -z "${PYTHONSAFEPATH+x}" ]]; then
113+
interpreter_env+=("PYTHONSAFEPATH=${PYTHONSAFEPATH+1}")
114+
fi
109115

110116
if [[ "$IS_ZIPFILE" == "1" ]]; then
111117
interpreter_args+=("-XRULES_PYTHON_ZIP_DIR=$zip_dir")

tests/base_rules/BUILD.bazel

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,11 @@ sh_py_run_test(
5151
sh_src = "run_binary_zip_no_test.sh",
5252
target_compatible_with = _SUPPORTS_BOOTSTRAP_SCRIPT,
5353
)
54+
55+
sh_py_run_test(
56+
name = "inherit_pythonsafepath_env_test",
57+
bootstrap_impl = "script",
58+
py_src = "bin.py",
59+
sh_src = "inherit_pythonsafepath_env_test.sh",
60+
target_compatible_with = _SUPPORTS_BOOTSTRAP_SCRIPT,
61+
)

tests/base_rules/bin.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
#
14+
15+
import os
1516
import sys
1617

1718
print("Hello")
1819
print(
1920
"RULES_PYTHON_ZIP_DIR:{}".format(sys._xoptions.get("RULES_PYTHON_ZIP_DIR", "UNSET"))
2021
)
22+
print("PYTHONSAFEPATH:", os.environ.get("PYTHONSAFEPATH", "UNSET") or "EMPTY")
23+
print("sys.flags.safe_path:", sys.flags.safe_path)
2124
print("file:", __file__)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Copyright 2024 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# --- begin runfiles.bash initialization v3 ---
16+
# Copy-pasted from the Bazel Bash runfiles library v3.
17+
set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash
18+
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
19+
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
20+
source "$0.runfiles/$f" 2>/dev/null || \
21+
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
22+
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
23+
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
24+
# --- end runfiles.bash initialization v3 ---
25+
set +e
26+
27+
bin=$(rlocation $BIN_RLOCATION)
28+
if [[ -z "$bin" ]]; then
29+
echo "Unable to locate test binary: $BIN_RLOCATION"
30+
exit 1
31+
fi
32+
33+
34+
function expect_match() {
35+
local expected_pattern=$1
36+
local actual=$2
37+
if ! (echo "$actual" | grep "$expected_pattern" ) >/dev/null; then
38+
echo "expected output to match: $expected_pattern"
39+
echo "but got:\n$actual"
40+
return 1
41+
fi
42+
}
43+
44+
45+
actual=$(PYTHONSAFEPATH= $bin 2>&1)
46+
expect_match "sys.flags.safe_path: False" "$actual"
47+
expect_match "PYTHONSAFEPATH: EMPTY" "$actual"
48+
49+
actual=$(PYTHONSAFEPATH=OUTER $bin 2>&1)
50+
expect_match "sys.flags.safe_path: True" "$actual"
51+
expect_match "PYTHONSAFEPATH: OUTER" "$actual"

0 commit comments

Comments
 (0)