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:
parent
3fa69f17ac
commit
971c8a988d
|
|
@ -44,7 +44,7 @@ def main():
|
||||||
print("usage: elevate -- <cmd>", file=sys.stderr)
|
print("usage: elevate -- <cmd>", file=sys.stderr)
|
||||||
sys.exit(1)
|
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)
|
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||||
try:
|
try:
|
||||||
|
|
|
||||||
|
|
@ -345,6 +345,20 @@ def handle(conn):
|
||||||
pass
|
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):
|
def _handle_inner(conn):
|
||||||
data = b""
|
data = b""
|
||||||
while chunk := conn.recv(65536):
|
while chunk := conn.recv(65536):
|
||||||
|
|
@ -360,6 +374,12 @@ def _handle_inner(conn):
|
||||||
|
|
||||||
cmd = req.get("cmd", "")
|
cmd = req.get("cmd", "")
|
||||||
cwd = req.get("cwd", os.getcwd())
|
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", "")
|
token = os.environ.get("SUPERPOWERD_SESSION_TOKEN", "")
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue