Env
provide.foundation.process.env
¶
TODO: Add module docstring.
Functions¶
is_sensitive_env_var
¶
mask_sensitive_env_vars
¶
Mask sensitive environment variables for safe logging.
This function creates a copy of the environment with sensitive values replaced by "[MASKED]" for safe display in logs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
Mapping[str, str]
|
Environment dictionary to mask |
required |
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
Environment dictionary with sensitive values masked |
Examples:
>>> env = {"PATH": "/usr/bin", "AWS_SECRET_KEY": "secret123"}
>>> masked = mask_sensitive_env_vars(env)
>>> masked["PATH"]
'/usr/bin'
>>> masked["AWS_SECRET_KEY"]
'[MASKED]'
Source code in provide/foundation/process/env.py
prepare_subprocess_environment
¶
prepare_subprocess_environment(
caller_overrides: Mapping[str, str] | None = None,
scrub: bool = DEFAULT_ENV_SCRUBBING_ENABLED,
allowlist: set[str] | None = None,
) -> dict[str, str]
Prepare environment for subprocess execution with scrubbing.
This function creates a minimal, safe environment for subprocess execution by combining allowlisted system variables with caller-provided overrides.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
caller_overrides
|
Mapping[str, str] | None
|
Environment variables provided by caller (always included) |
None
|
scrub
|
bool
|
Whether to scrub the base environment (default: True) |
DEFAULT_ENV_SCRUBBING_ENABLED
|
allowlist
|
set[str] | None
|
Custom allowlist (defaults to SAFE_ENV_ALLOWLIST) |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
Environment dictionary for subprocess |
Security Note
- If scrub=True: Only allowlisted system vars + caller overrides included
- If scrub=False: Full os.environ + caller overrides (NOT RECOMMENDED)
- Caller overrides always included (caller is trusted)
- PROVIDE_TELEMETRY_DISABLED always added to prevent recursive logging
Source code in provide/foundation/process/env.py
scrub_environment
¶
scrub_environment(
env: Mapping[str, str],
allowlist: set[str] | None = None,
enabled: bool = DEFAULT_ENV_SCRUBBING_ENABLED,
) -> dict[str, str]
Scrub environment to only include allowlisted variables.
This function filters the environment to only include safe, non-sensitive variables from a curated allowlist. This prevents credential leakage when environment variables are logged or stored.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
Mapping[str, str]
|
Environment dictionary to scrub |
required |
allowlist
|
set[str] | None
|
Set of allowed variable names (defaults to SAFE_ENV_ALLOWLIST) |
None
|
enabled
|
bool
|
Whether scrubbing is enabled (default: True) |
DEFAULT_ENV_SCRUBBING_ENABLED
|
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
Scrubbed environment dictionary containing only allowlisted variables |
Examples:
>>> import os
>>> scrubbed = scrub_environment(os.environ)
>>> "PATH" in scrubbed # Safe variable included
True
>>> "AWS_SECRET_ACCESS_KEY" in scrubbed # Secret excluded
False