Repository navigation
Keep check environment values out of launcher arguments #1355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b9abb58
acca83c
a2baa35
e833fd1
4ee5654
5120c98
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,7 +16,7 @@ | |
| import tempfile | ||
| import threading | ||
| import time | ||
| from collections.abc import Callable | ||
| from collections.abc import Callable, Sequence | ||
| from contextlib import ExitStack, contextmanager | ||
| from dataclasses import asdict, replace | ||
| from os.path import commonpath | ||
|
|
@@ -288,7 +288,7 @@ def _execution_settings(config: Config, base: str) -> dict: | |
| settings["runner_executable"] = _runner_identity(config) | ||
| settings["environment"] = _proc.build_env(config.env_allowlist) | ||
| else: | ||
| settings["environment"] = _sandbox_environment(config) | ||
| settings["environment"] = check_environment(config) | ||
| return settings | ||
|
|
||
|
|
||
|
|
@@ -372,12 +372,9 @@ def _preflight_alternates(config: Config, worktree: Path, prefix: list[str]) -> | |
| remedy = (f"Git object alternate {alternate!r} is not readable in the check sandbox. " | ||
| "Create a standalone clone without --shared/--reference, then run " | ||
| "loopzero start there and rerun loopzero check.") | ||
| command = ("/bin/sh", "-c", 'test -d "$1" && test -r "$1" && test -x "$1"', "loopzero-alternate", alternate) | ||
| try: | ||
| result = _proc.run( | ||
| [*prefix, "/bin/sh", "-c", 'test -d "$1" && test -r "$1" && test -x "$1"', | ||
| "loopzero-alternate", alternate], | ||
| cwd=worktree, env_allowlist=config.env_allowlist, timeout=GIT_TIMEOUT, | ||
| ) | ||
| result = _run_bwrap(config, worktree, prefix, command, timeout=GIT_TIMEOUT) | ||
| except _proc.ProcTimeout: | ||
| raise SandboxUnavailable(f"Git alternate visibility probe timed out. {remedy}") from None | ||
| if result.exit_code != 0: | ||
|
|
@@ -451,16 +448,12 @@ def _run_one( | |
| ) -> CheckResult: | ||
| try: | ||
| limits = config.limits | ||
| done = _proc.run( | ||
| [*prefix, "prlimit", | ||
| f"--as={_cap_limit(limits.memory_mb * 1024 * 1024, resource.RLIMIT_AS)}", | ||
| f"--fsize={_cap_limit(limits.file_mb * 1024 * 1024, resource.RLIMIT_FSIZE)}", | ||
| "--", *CHECK_SHELL, command], | ||
| cwd=worktree, | ||
| env_allowlist=config.env_allowlist, | ||
| timeout=timeout, | ||
| merge_output=True, | ||
| argv = ( | ||
| "prlimit", f"--as={_cap_limit(limits.memory_mb * 1024 * 1024, resource.RLIMIT_AS)}", | ||
| f"--fsize={_cap_limit(limits.file_mb * 1024 * 1024, resource.RLIMIT_FSIZE)}", | ||
| "--", *CHECK_SHELL, command, | ||
| ) | ||
| done = _run_bwrap(config, worktree, prefix, argv, timeout=timeout, merge_output=True) | ||
| except _proc.ProcTimeout as exc: | ||
| return CheckResult(command, TIMEOUT_EXIT, timeout, | ||
| _check_tail(exc.stdout + f"\n[loopzero] timed out after {timeout:g}s")) | ||
|
|
@@ -470,9 +463,7 @@ def _run_one( | |
| def probe(config: Config, worktree: Path, prefix: list[str]) -> None: | ||
| """Raise `SandboxUnavailable` unless bwrap can build the exact sandbox we will use.""" | ||
| try: | ||
| probe = _proc.run( | ||
| [*prefix, "true"], cwd=worktree, env_allowlist=config.env_allowlist, timeout=GIT_TIMEOUT | ||
| ) | ||
| probe = _run_bwrap(config, worktree, prefix, ("true",), timeout=GIT_TIMEOUT) | ||
| except _proc.ToolMissing as exc: | ||
| raise SandboxUnavailable( | ||
| f"bwrap binary missing while starting the namespace probe: {exc}\n" | ||
|
|
@@ -488,13 +479,27 @@ def probe(config: Config, worktree: Path, prefix: list[str]) -> None: | |
| raise SandboxUnavailable(f"bwrap namespace creation failed: exit {probe.exit_code}{remedy}") | ||
|
|
||
|
|
||
| def _sandbox_environment(config: Config) -> dict[str, str]: | ||
| return { | ||
| **SANDBOX_ENV, | ||
| **_proc.build_env(config.env_allowlist), | ||
| **dict(config.env), | ||
| "HOME": SANDBOX_HOME, | ||
| } | ||
| def check_environment(config: Config) -> dict[str, str]: | ||
| return SANDBOX_ENV | _proc.build_env(config.env_allowlist, dict(config.env)) | {"HOME": SANDBOX_HOME} | ||
|
|
||
|
|
||
| def _run_bwrap( | ||
| config: Config, worktree: Path, prefix: list[str], command: Sequence[str], | ||
| *, timeout: float, merge_output: bool = False, | ||
| ) -> _proc.Completed: | ||
| # Apply settings inside bwrap without exposing the option stream in argv. | ||
| with tempfile.TemporaryFile() as options: | ||
| for name, value in check_environment(config).items(): | ||
| if "\0" in value: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Null-byte check covers values but not names
|
||
| raise ValueError("embedded null byte in check environment") | ||
| options.write(os.fsencode(f"--setenv\0{name}\0{value}\0")) | ||
| options.seek(0) | ||
| fd = options.fileno() | ||
| return _proc.run( | ||
| [*prefix[:-1], "--args", str(fd), "--", *command], | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: _run_bwrap silently relies on the prefix ending in "--"
|
||
| cwd=worktree, env_allowlist=("PATH",) if "PATH" in config.env_allowlist else (), | ||
| timeout=timeout, merge_output=merge_output, pass_fds=(fd,), | ||
| ) | ||
|
|
||
|
|
||
| def bwrap_argv( | ||
|
|
@@ -508,11 +513,9 @@ def bwrap_argv( | |
| clearenv: bool = True, | ||
| writable_binds: tuple[tuple[Path, Path], ...] = (), | ||
| ) -> list[str]: | ||
| """Build the bwrap command line up to and including the `--` separator. | ||
| """Build mounts and environment options before the command separator. | ||
|
|
||
| With ``clearenv=False`` the caller's (already filtered) environment is inherited | ||
| instead of cleared, so secrets never appear as ``--setenv`` arguments. | ||
| ``writable_binds`` overlays specific paths inside the otherwise private HOME. | ||
| Reviewers inherit their filtered environment and apply sandbox defaults. | ||
| """ | ||
| argv = [ | ||
| "bwrap", "--die-with-parent", "--new-session", "--unshare-all", | ||
|
|
@@ -552,11 +555,10 @@ def bwrap_argv( | |
| for entry, source in (scratch or {}).items(): | ||
| argv += ["--bind", str(source), str(worktree / entry)] | ||
| argv += ["--chdir", str(worktree), *(["--clearenv"] if clearenv else [])] | ||
| for key, value in _sandbox_environment(config).items(): | ||
| if key != "HOME": | ||
| if not clearenv: | ||
| for key, value in check_environment(config).items(): | ||
| argv += ["--setenv", key, value] | ||
| argv += ["--setenv", "HOME", SANDBOX_HOME, "--"] | ||
| return argv | ||
| return [*argv, "--"] | ||
|
|
||
|
|
||
| def _git(config: Config, worktree: Path, *args: str) -> str: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Reviewer namespace probe now differs from the real reviewer launch
runners._sandbox_prefixcallssandbox.probewith a reviewer prefix built withclearenv=False, which already carries--setenvarguments. Becauseprobenow goes through_run_bwrap, the reviewer probe also gets an--argsstream built fromcheck_environment(review_config), which adds check defaults such as UV_CACHE_DIR. Its host launcher also gets only PATH, while the real reviewer launch gets BASE_ENV. Runningtruemakes this harmless today, but it undercuts the docstring's claim that the probe tests 'the exact sandbox we will use', and reviewers now set environment through two channels. Consider givingprobea flag, or letting the reviewer path call_proc.rundirectly as before.