Skip to content
This repository was archived by the owner on Dec 5, 2022. It is now read-only.

Commit 4c5e3db

Browse files
edmorleydryan
authored andcommitted
Reduce the number of env vars exposed to subprocess (heroku#1011)
The following env vars are no longer exposed to subprocesses run by the buildpack (such as the `bin/pre_compile` and `bin/post_compile` hooks): * `BPLOG_PREFIX` * `CACHED_PYTHON_STACK` * `DEFAULT_PYTHON_STACK` * `DEFAULT_PYTHON_VERSION` * `LATEST_27` * `LATEST_34` * `LATEST_35` * `LATEST_36` * `LATEST_37` * `LATEST_38` * `PIP_UPDATE` * `PY27` * `PY34` * `PY35` * `PY36` * `PY37` * `PYPY_27` * `PYPY_36` * `RECOMMENDED_PYTHON_VERSION` * `WARNINGS_LOG` There were previously no tests at all for the pre/post-compile hooks, so I've added some now. Fixes heroku#1010.
1 parent e6b2e0f commit 4c5e3db

File tree

7 files changed

+61
-18
lines changed

7 files changed

+61
-18
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
# Master
44

5+
- Reduce the number of environment variables exposed to `bin/{pre,post}_compile` and other subprocesses (#1011)
56

67
# 173 (2020-07-21)
78

bin/compile

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
set -eo pipefail
1717

1818
# Boostrap the Buildpack Standard Library.
19-
export BPLOG_PREFIX="buildpack.python"
19+
# Disable unused env var warning since shellcheck doesn't know about the stdlib.
20+
# shellcheck disable=2034
21+
BPLOG_PREFIX="buildpack.python"
2022
export BUILDPACK_LOG_FILE=${BUILDPACK_LOG_FILE:-/dev/null}
2123

2224
[ "$BUILDPACK_XTRACE" ] && set -o xtrace
@@ -84,16 +86,12 @@ if [[ -f "$BUILD_DIR/Pipfile" ]]; then
8486
PIP_UPDATE="9.0.2"
8587
fi
8688

87-
export DEFAULT_PYTHON_STACK PIP_UPDATE
88-
export PY38 PY37 PY36 PY35 PY27 PY34
89-
9089
# Common Problem Warnings:
9190
# This section creates a temporary file in which to stick the output of `pip install`.
9291
# The `warnings` subscript then greps through this for common problems and guides
9392
# the user towards resolution of known issues.
9493
WARNINGS_LOG=$(mktemp)
95-
export WARNINGS_LOG
96-
export RECOMMENDED_PYTHON_VERSION=$DEFAULT_PYTHON_VERSION
94+
RECOMMENDED_PYTHON_VERSION=$DEFAULT_PYTHON_VERSION
9795

9896
# The buildpack ships with a few executable tools (e.g. pip-grep, etc).
9997
# This installs them into the path, so we can execute them directly.
@@ -217,8 +215,6 @@ else
217215
CACHED_PYTHON_STACK=$STACK
218216
fi
219217

220-
export CACHED_PYTHON_STACK
221-
222218
# Pipenv Python version support.
223219
# Detect the version of Python requested from a Pipfile (e.g. python_version or python_full_version).
224220
# Convert it to a runtime.txt file.

bin/default_pythons

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#!/usr/bin/env bash
22

3+
# Disable unused env var warning, since shellcheck doesn't take into account
4+
# that this file is sourced. We don't want to use export since it exposes
5+
# the env vars to subprocesses.
6+
# shellcheck disable=2034
7+
38
DEFAULT_PYTHON_VERSION="python-3.6.11"
49
LATEST_38="python-3.8.5"
510
LATEST_37="python-3.7.8"
@@ -9,13 +14,3 @@ LATEST_34="python-3.4.10"
914
LATEST_27="python-2.7.18"
1015
PYPY_36="pypy3.6-7.3.1"
1116
PYPY_27="pypy2.7-7.3.1"
12-
13-
export DEFAULT_PYTHON_VERSION \
14-
LATEST_38 \
15-
LATEST_37 \
16-
LATEST_36 \
17-
LATEST_35 \
18-
LATEST_34 \
19-
LATEST_27 \
20-
PYPY_36 \
21-
PYPY_27
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
set -euo pipefail
2+
3+
echo "post_compile ran!"
4+
echo "post_compile env: $(printenv | cut -d '=' -f 1 | sort | xargs)."
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
set -euo pipefail
2+
3+
echo "pre_compile ran!"
4+
echo "pre_compile env: $(printenv | cut -d '=' -f 1 | sort | xargs)."

test/fixtures/hooks/requirements.txt

Whitespace-only changes.

test/run-features

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,49 @@ testDontWarnOldDjango() {
8686
assertCapturedSuccess
8787
}
8888

89+
testHooks() {
90+
# Test that the hooks are called correctly, and that the environment contains
91+
# the app's config vars but no unexpected env vars from the buildpack.
92+
local env_dir="$(mktmpdir)"
93+
echo 'test' > "${env_dir}/SOME_APP_CONFIG_VAR"
94+
local expected_env_vars=(
95+
_
96+
BIN_DIR
97+
BUILD_DIR
98+
BUILDPACK_LOG_FILE
99+
CACHE_DIR
100+
C_INCLUDE_PATH
101+
CPLUS_INCLUDE_PATH
102+
ENV_DIR
103+
EXPORT_PATH
104+
HOME
105+
LANG
106+
LD_LIBRARY_PATH
107+
LIBRARY_PATH
108+
OLDPWD
109+
PATH
110+
PKG_CONFIG_PATH
111+
PROFILE_PATH
112+
PWD
113+
PYTHONUNBUFFERED
114+
SHLVL
115+
SOME_APP_CONFIG_VAR
116+
STACK
117+
VENDOR_URL
118+
)
119+
if [[ "${STACK}" == "cedar-14" || "${STACK}" == "heroku-16" ]]; then
120+
# Remove "OLDPWD" from expected_env_vars since for bash <4.4 it's not exported to subshells:
121+
# https://github.com/heroku/heroku-buildpack-python/pull/1011#issuecomment-665117835
122+
read -ra expected_env_vars <<< "${expected_env_vars[@]/OLDPWD/}"
123+
fi
124+
compile 'hooks' '' "${env_dir}"
125+
assertCaptured "pre_compile ran!"
126+
assertCaptured "pre_compile env: ${expected_env_vars[*]}."
127+
assertCaptured "post_compile ran!"
128+
assertCaptured "post_compile env: ${expected_env_vars[*]}."
129+
assertCapturedSuccess
130+
}
131+
89132
pushd $(dirname 0) >/dev/null
90133
popd >/dev/null
91134

0 commit comments

Comments
 (0)