Add server shell access
This commit is contained in:
21
app.py
21
app.py
@@ -538,13 +538,22 @@ def api_terminal_exec():
|
|||||||
return jsonify({"error": "No command provided"}), 400
|
return jsonify({"error": "No command provided"}), 400
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Use shell execution via sh()
|
# HOST ESCAPE: execute command on the host by mounting host / and using chroot
|
||||||
# sh() uses subprocess.check_output with shell=False by default (list of strings)
|
# We use a tiny alpine container to bridge to the host.
|
||||||
# However, to support pipe/redirection for the user, we should allow shell=True-like behavior
|
# This requires the flask container to have docker socket access (which it does).
|
||||||
# Let's wrap it in ['sh', '-c', command]
|
host_cmd = [
|
||||||
output = sh(["sh", "-c", command])
|
DOCKER, "run", "--rm",
|
||||||
|
"-v", "/:/host",
|
||||||
|
"alpine", "chroot", "/host", "sh", "-c", command
|
||||||
|
]
|
||||||
|
|
||||||
|
output = sh(host_cmd)
|
||||||
return jsonify({"output": output, "status": "success"})
|
return jsonify({"output": output, "status": "success"})
|
||||||
except subprocess.CalledProcessError as e:
|
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:
|
except Exception as e:
|
||||||
return jsonify({"output": str(e), "error": True, "status": "error"})
|
return jsonify({"output": str(e), "error": True, "status": "error"})
|
||||||
|
|||||||
Reference in New Issue
Block a user