elevate: forward caller environment to elevated commands

The elevate client now includes the full environment in its request.
The adapter wraps the command with 'env -i K=V ... bash -c cmd' so
the elevated command runs with the agent's env rather than the
adapter's startup env.
This commit is contained in:
Levi Neely 2026-07-27 09:49:44 +02:00
parent 3fa69f17ac
commit 971c8a988d
2 changed files with 21 additions and 1 deletions

View File

@ -44,7 +44,7 @@ def main():
print("usage: elevate -- <cmd>", file=sys.stderr)
sys.exit(1)
req = json.dumps({"cmd": sys.argv[2], "cwd": os.getcwd()}).encode() + b"\n"
req = json.dumps({"cmd": sys.argv[2], "cwd": os.getcwd(), "env": dict(os.environ)}).encode() + b"\n"
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
try:

View File

@ -345,6 +345,20 @@ def handle(conn):
pass
def _wrap_with_env(cmd, env):
"""Wrap a command with explicit env setup.
Produces: env -i K1=V1 K2=V2 ... bash -c 'original command'
This ensures the command runs with exactly the caller's environment.
"""
import shlex
parts = ["env", "-i"]
for k, v in sorted(env.items()):
parts.append(f"{k}={v}")
parts.extend(["bash", "-c", cmd])
return " ".join(shlex.quote(p) for p in parts)
def _handle_inner(conn):
data = b""
while chunk := conn.recv(65536):
@ -360,6 +374,12 @@ def _handle_inner(conn):
cmd = req.get("cmd", "")
cwd = req.get("cwd", os.getcwd())
env = req.get("env")
# If the client sent its environment, wrap the command to reproduce it.
# This ensures the elevated command sees the agent's env, not the adapter's.
if env and isinstance(env, dict):
cmd = _wrap_with_env(cmd, env)
token = os.environ.get("SUPERPOWERD_SESSION_TOKEN", "")