Add server shell access

This commit is contained in:
Peter Stockings
2025-12-24 11:11:53 +11:00
parent 4f34696c72
commit e5d650c8a4

21
app.py
View File

@@ -538,13 +538,22 @@ def api_terminal_exec():
return jsonify({"error": "No command provided"}), 400
try:
# Use shell execution via sh()
# sh() uses subprocess.check_output with shell=False by default (list of strings)
# However, to support pipe/redirection for the user, we should allow shell=True-like behavior
# Let's wrap it in ['sh', '-c', command]
output = sh(["sh", "-c", command])
# HOST ESCAPE: execute command on the host by mounting host / and using chroot
# We use a tiny alpine container to bridge to the host.
# This requires the flask container to have docker socket access (which it does).
host_cmd = [
DOCKER, "run", "--rm",
"-v", "/:/host",
"alpine", "chroot", "/host", "sh", "-c", command
]
output = sh(host_cmd)
return jsonify({"output": output, "status": "success"})
except subprocess.CalledProcessError as e:
return jsonify({"output": e.output or str(e), "error": True, "status": "error"})
# Cast output to string as it might be bytes
out = e.output
if hasattr(out, "decode"):
out = out.decode("utf-8", errors="replace")
return jsonify({"output": out or str(e), "error": True, "status": "error"})
except Exception as e:
return jsonify({"output": str(e), "error": True, "status": "error"})