rsnarsna
feat(alli-backend): Wave-1 standardization, standalone sys.env node & vault crypto, and DEC-014 security hardening
8628c98 Download runtime_environment/env_injection.py from Rsnarsna/auto: direct link, hf CLI and curl.
- Browser
- Download file 8.85 kB
-
https://huggingface.co/spaces/Rsnarsna/auto/resolve/main/runtime_environment/env_injection.py
- Command line
-
hf download hf://spaces/Rsnarsna/auto/runtime_environment/env_injection.py
-
curl -L -o env_injection.py https://huggingface.co/spaces/Rsnarsna/auto/resolve/main/runtime_environment/env_injection.py
8.85 kB
| """ | |
| ALLI Runtime Environment - Secret Injection, Egress Redaction & Ephemeral Env-File Isolation | |
| Provides request/run-scoped ContextVar propagation, base64/hex/URL-aware secret redaction, | |
| and private host --env-file lifecycle outside container bind mounts (/app). | |
| """ | |
| import base64 | |
| from contextlib import contextmanager | |
| from contextvars import ContextVar | |
| from dataclasses import dataclass, field | |
| import os | |
| import tempfile | |
| import time | |
| from typing import Any, Dict, Iterable, Iterator, List, Optional, Set, Tuple | |
| from urllib.parse import quote as url_quote | |
| from language_core.workspace_env_contracts import ( | |
| RESERVED_SENSITIVE_VALUE_MIN_LEN, | |
| validate_env_key, | |
| validate_env_value, | |
| ) | |
| REDACTED_MARKER = "[REDACTED_ENV_SECRET]" | |
| class RunEnvContext: | |
| """Isolated per-run environment state scoped cleanly via ContextVar.""" | |
| org_id: Optional[str] = None | |
| role: Optional[str] = None | |
| is_viewer: bool = False | |
| vars: Dict[str, str] = field(default_factory=dict) | |
| written_vars: Dict[str, str] = field(default_factory=dict) | |
| secret_values: Tuple[str, ...] = field(default_factory=tuple) | |
| def get_effective_vars(self) -> Dict[str, str]: | |
| merged = dict(self.vars) | |
| merged.update(self.written_vars) | |
| return merged | |
| def read_var(self, key: str) -> Optional[str]: | |
| if key in self.written_vars: | |
| return self.written_vars[key] | |
| return self.vars.get(key) | |
| def write_var(self, key: str, value: str) -> None: | |
| self.written_vars[key] = value | |
| self._register_secret_variant(value) | |
| def _register_secret_variant(self, raw_val: str) -> None: | |
| if not raw_val or len(raw_val) < RESERVED_SENSITIVE_VALUE_MIN_LEN: | |
| return | |
| variants = set(self.secret_values) | |
| variants.update(_expand_secret_encodings(raw_val)) | |
| self.secret_values = tuple(sorted(variants, key=len, reverse=True)) | |
| _ACTIVE_RUN_ENV: ContextVar[Optional[RunEnvContext]] = ContextVar("alli_active_run_env", default=None) | |
| def _expand_secret_encodings(secret: str) -> Set[str]: | |
| """Generate raw, base64, hex, and URL-encoded variants of a secret for egress scrubbing.""" | |
| out: Set[str] = set() | |
| s = str(secret or "") | |
| if len(s) < RESERVED_SENSITIVE_VALUE_MIN_LEN: | |
| return out | |
| out.add(s) | |
| try: | |
| raw_bytes = s.encode("utf-8") | |
| out.add(base64.b64encode(raw_bytes).decode("ascii")) | |
| out.add(base64.urlsafe_b64encode(raw_bytes).decode("ascii")) | |
| out.add(raw_bytes.hex()) | |
| quoted = url_quote(s, safe="") | |
| if quoted != s: | |
| out.add(quoted) | |
| except Exception: | |
| pass | |
| return {v for v in out if len(v) >= RESERVED_SENSITIVE_VALUE_MIN_LEN} | |
| def get_active_run_env() -> Optional[RunEnvContext]: | |
| """Return the active RunEnvContext for the current execution context, if any.""" | |
| return _ACTIVE_RUN_ENV.get() | |
| def activate_run_env(ctx: Optional[RunEnvContext]) -> Iterator[Optional[RunEnvContext]]: | |
| """Bind a RunEnvContext cleanly to the current execution ContextVar.""" | |
| token = _ACTIVE_RUN_ENV.set(ctx) | |
| try: | |
| yield ctx | |
| finally: | |
| _ACTIVE_RUN_ENV.reset(token) | |
| def graph_accesses_workspace_env(graph: Optional[Dict[str, Any]]) -> bool: | |
| """Return True if any workflow node attempts to read or write workspace env variables.""" | |
| if not isinstance(graph, dict): | |
| return False | |
| for node in graph.get("nodes") or []: | |
| if not isinstance(node, dict): | |
| continue | |
| data = node.get("data") if isinstance(node.get("data"), dict) else {} | |
| raw_type = "" if node.get("type") == "studioNode" else node.get("type") | |
| ntype = str(node.get("node_type") or raw_type or data.get("node_type") or data.get("nodeType") or "").lower() | |
| if "env_get" in ntype or "env_set" in ntype or ntype in ("alli.sys.env", "sys.env"): | |
| return True | |
| return False | |
| def build_run_env_context( | |
| org_id: Optional[str] = None, | |
| *, | |
| role: Optional[str] = None, | |
| explicit_env: Optional[Dict[str, str]] = None, | |
| extra_secrets: Iterable[str] = (), | |
| ) -> RunEnvContext: | |
| """Build sanitized, value-redacted RunEnvContext from explicit map or workspace DB.""" | |
| norm_role = str(role or "").lower().strip() | |
| is_viewer = norm_role == "viewer" | |
| loaded: Dict[str, str] = {} | |
| variants: Set[str] = set() | |
| if not is_viewer: | |
| secret_candidates: List[str] = [] | |
| if explicit_env: | |
| source_map = explicit_env | |
| secret_candidates = list(explicit_env.values()) | |
| elif org_id: | |
| from database.mongo import workflow_repo | |
| source_map, secret_candidates = workflow_repo.load_decrypted_workspace_env_entries(org_id) | |
| else: | |
| source_map = {} | |
| for raw_k, raw_v in source_map.items(): | |
| ok_k, clean_k, _ = validate_env_key(raw_k) | |
| ok_v, clean_v, _ = validate_env_value(raw_v) | |
| if ok_k and ok_v: | |
| loaded[clean_k] = clean_v | |
| for val in secret_candidates: | |
| variants.update(_expand_secret_encodings(val)) | |
| for s in extra_secrets: | |
| variants.update(_expand_secret_encodings(str(s))) | |
| return RunEnvContext( | |
| org_id=org_id, | |
| role=norm_role or None, | |
| is_viewer=is_viewer, | |
| vars=loaded, | |
| secret_values=tuple(sorted(variants, key=len, reverse=True)), | |
| ) | |
| def redact_secrets_from_text(text: Any, ctx: Optional[RunEnvContext] = None) -> str: | |
| """Replace active secret values and their encoded variants in stdout/stderr/logs.""" | |
| if text is None: | |
| return "" | |
| s = str(text) | |
| active = ctx or get_active_run_env() | |
| if not active or not active.secret_values: | |
| return s | |
| for token in active.secret_values: | |
| if token and token in s: | |
| s = s.replace(token, REDACTED_MARKER) | |
| return s | |
| def redact_secrets_from_obj(obj: Any, ctx: Optional[RunEnvContext] = None) -> Any: | |
| """Recursively scrub active secret variants across dicts/lists/strings.""" | |
| active = ctx or get_active_run_env() | |
| if not active or not active.secret_values: | |
| return obj | |
| if isinstance(obj, str): | |
| return redact_secrets_from_text(obj, active) | |
| if isinstance(obj, list): | |
| return [redact_secrets_from_obj(item, active) for item in obj] | |
| if isinstance(obj, tuple): | |
| return tuple(redact_secrets_from_obj(item, active) for item in obj) | |
| if isinstance(obj, dict): | |
| return {k: redact_secrets_from_obj(v, active) for k, v in obj.items()} | |
| return obj | |
| def _get_private_env_dir() -> str: | |
| base = os.path.join(tempfile.gettempdir(), "alli_ephemeral_env") | |
| os.makedirs(base, exist_ok=True) | |
| if os.name != "nt": | |
| try: os.chmod(base, 0o700) | |
| except OSError: pass | |
| return base | |
| def sweep_orphan_env_files(max_age_seconds: int = 300) -> None: | |
| """Remove stale ephemeral Docker --env-file artifacts left by interrupted runs.""" | |
| cutoff = time.time() - max_age_seconds | |
| try: | |
| for entry in os.scandir(_get_private_env_dir()): | |
| if entry.is_file() and entry.name.endswith(".env") and entry.stat().st_mtime < cutoff: | |
| try: os.remove(entry.path) | |
| except OSError: pass | |
| except OSError: | |
| pass | |
| def create_ephemeral_docker_env_file(env_vars: Dict[str, str]) -> Optional[str]: | |
| """Write validated KEY=VALUE pairs to an external private --env-file outside /app.""" | |
| if not env_vars: | |
| return None | |
| sweep_orphan_env_files() | |
| env_dir, lines = _get_private_env_dir(), [] | |
| for raw_k, raw_v in env_vars.items(): | |
| ok_k, clean_k, err_k = validate_env_key(raw_k) | |
| ok_v, clean_v, err_v = validate_env_value(raw_v) | |
| if not ok_k or not ok_v: | |
| raise ValueError(err_k or err_v) | |
| lines.append(f"{clean_k}={clean_v}") | |
| fd, path = tempfile.mkstemp(prefix="alli_env_", suffix=".env", dir=env_dir) | |
| try: | |
| with os.fdopen(fd, "w", encoding="utf-8", newline="\n") as handle: | |
| handle.write("\n".join(lines) + "\n") | |
| if os.name != "nt": os.chmod(path, 0o600) | |
| return path | |
| except Exception: | |
| remove_ephemeral_env_file(path) | |
| raise | |
| def remove_ephemeral_env_file(path: Optional[str]) -> None: | |
| """Safely remove an ephemeral --env-file on completion or failure.""" | |
| try: | |
| if path and os.path.exists(path): | |
| os.remove(path) | |
| except OSError: | |
| pass | |
| def resolve_runner_workspace_env(workspace_env: Optional[Dict[str, str]] = None) -> Dict[str, str]: | |
| """Resolve effective validated workspace env map for Local/Docker runners.""" | |
| if workspace_env is not None: | |
| return dict(workspace_env) | |
| active = get_active_run_env() | |
| if active and not active.is_viewer: | |
| return active.get_effective_vars() | |
| return {} | |