-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reject known problematic env vars (#308)
We expose all env vars by default to subprocesses to allow for customisation of package manager behaviour - such as custom indexes, authentication and requirements file env var interpolation. (An allow-list approach wouldn't work with all use-cases, plus wouldn't help the app at run-time.) To improve the error UX (particularly during initial buildpack bootstrap, where failures would otherwise look like a problem with the buildpack and not the user inputs), the buildpack now rejects known problematic env vars that may break the build / the app. The list of env vars was based on the env vars this buildpack sets, plus an audit of: - https://docs.python.org/3/using/cmdline.html#environment-variables - https://pip.pypa.io/en/stable/cli/pip/#general-options - https://pip.pypa.io/en/stable/cli/pip_install/#options This also unblocks #265. GUS-W-17454486.
- Loading branch information
Showing
6 changed files
with
97 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use libcnb::Env; | ||
|
||
// We expose all env vars by default to subprocesses to allow for customisation of package manager | ||
// behaviour (such as custom indexes, authentication and requirements file env var interpolation). | ||
// As such, we have to block known problematic env vars that may break the build / the app. | ||
// This list was based on the env vars this buildpack sets, plus an audit of: | ||
// https://docs.python.org/3/using/cmdline.html#environment-variables | ||
// https://pip.pypa.io/en/stable/cli/pip/#general-options | ||
// https://pip.pypa.io/en/stable/cli/pip_install/#options | ||
const FORBIDDEN_ENV_VARS: [&str; 12] = [ | ||
"PIP_CACHE_DIR", | ||
"PIP_PREFIX", | ||
"PIP_PYTHON", | ||
"PIP_ROOT", | ||
"PIP_TARGET", | ||
"PIP_USER", | ||
"PYTHONHOME", | ||
"PYTHONINSPECT", | ||
"PYTHONNOUSERSITE", | ||
"PYTHONPLATLIBDIR", | ||
"PYTHONUSERBASE", | ||
"VIRTUAL_ENV", | ||
]; | ||
|
||
pub(crate) fn check_environment(env: &Env) -> Result<(), ChecksError> { | ||
if let Some(&name) = FORBIDDEN_ENV_VARS | ||
.iter() | ||
.find(|&name| env.contains_key(name)) | ||
{ | ||
return Err(ChecksError::ForbiddenEnvVar(name.to_string())); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Errors due to one of the environment checks failing. | ||
#[derive(Debug)] | ||
pub(crate) enum ChecksError { | ||
ForbiddenEnvVar(String), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use crate::tests::default_build_config; | ||
use indoc::indoc; | ||
use libcnb_test::{assert_contains, PackResult, TestRunner}; | ||
|
||
#[test] | ||
#[ignore = "integration test"] | ||
fn checks_reject_pythonhome_env_var() { | ||
let mut config = default_build_config("tests/fixtures/pyproject_toml_only"); | ||
config.env("PYTHONHOME", "/invalid"); | ||
config.expected_pack_result(PackResult::Failure); | ||
|
||
TestRunner::default().build(config, |context| { | ||
assert_contains!( | ||
context.pack_stderr, | ||
indoc! {" | ||
[Error: Unsafe environment variable found] | ||
The environment variable 'PYTHONHOME' is set, however, it can | ||
cause problems with the build so we do not allow using it. | ||
You must unset that environment variable. If you didn't set it | ||
yourself, check that it wasn't set by an earlier buildpack. | ||
"} | ||
); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters