Untrusted code sandboxing
Learn how self-hosted Retool sandboxes human and AI-generated code, and what each sandboxed service needs from your Kubernetes cluster.
Retool runs untrusted code that builders and AI agents write, and isolates each run in its own sandbox using NsJail or gVisor.
| Service | Runs | Sandbox |
|---|---|---|
| JS executor | Backend functions | NsJail |
| Agent sandbox | The app builder's AI agent and app previews. | gVisor |
| Code executor | workflows and custom API authentication. | NsJail |
Creating these sandboxes needs settings that Retool's other services don't, such as custom seccomp profiles, additional Linux capabilities, and files on each node. The Retool Helm chart configures them, but your cluster and its policies must allow them. Each service's section lists what it needs. To run the services on a cluster that restricts these settings, refer to Run sandboxed services on a restricted cluster.
JS executor
The js-executor service runs each function in its own NsJail sandbox. NsJail uses Linux namespaces to give the function its own processes, filesystem, and network, and limits the memory, CPU, and time each run can use.
Functions have no network access of their own and can only reach the Retool backend to query resources.
The JS executor's pods need the following from your cluster:
- Seccomp profile:
nsjail-seccomp.json. It addsclone,mount,umount2,pivot_root, andsethostnameto Docker's default profile so NsJail can create namespaces and set up the sandbox's filesystem. An init container copies it to the kubelet's seccomp directory,/var/lib/kubelet/seccomp, through ahostPathvolume. - Capabilities:
SETUIDandSETGID. The container starts as root and uses them only to switch to a non-root user before the service starts, so policies that requirerunAsNonRootblock it. - AppArmor profile:
Unconfinedby default. Retool recommends loading its own profile on platforms that support AppArmor.
Agent sandbox
The agent sandbox runs each app builder session in its own pod, isolated with gVisor. gVisor is a user-space kernel that handles the sandboxed code's syscalls itself, so untrusted code never reaches the host kernel.
Sandboxes have no direct route to the internet. Outbound traffic goes through the agent sandbox proxy, which blocks private, loopback, and link-local addresses, including cloud metadata endpoints. To restrict outbound traffic further:
- Set
rr.agentSandbox.proxy.allowedDomainsto limit sandboxes to specific domains. - Set
rr.agentSandbox.networkPolicy.enabledtotrueto also enforce the blocked ranges with Kubernetes network policies. This requires a network plugin that supports network policies. If the app builder preview uses app hostnames, also let the ingress controller reach the sandbox proxy.
Sandbox pods need the following from your cluster:
- Seccomp profile:
gvisor-seccomp.json. It adds the syscalls gVisor needs to create namespaces and set up the sandbox's filesystem to Docker's default profile, plusptrace, which gVisor uses to intercept the sandboxed code's syscalls, and theio_uringsyscalls for asynchronous I/O. - User:
1001, with no capabilities and a read-only root filesystem. - Device:
/dev/net/tun, which pods request through thesmarter-devices/net_tunresource. - AppArmor profile:
Unconfinedby default. Retool recommends loading its own profile on platforms that support AppArmor.
The chart also runs the following DaemonSets on the nodes that run sandbox pods. They use the host network by default, which rr.agentSandbox.hostNetwork controls.
- Seccomp installer: Copies
gvisor-seccomp.jsonto the kubelet's seccomp directory,/var/lib/kubelet/seccomp, through ahostPathvolume. - Device plugin: Registers
/dev/net/tunwith the kubelet. It useshostPathvolumes for/var/lib/kubelet/device-plugins,/dev, and/sys, and thesystem-node-criticalpriority class. If another process already runs this plugin on your nodes, setrr.agentSandbox.sandboxNetwork.deployDaemonSettofalse. - Image prepuller: Pulls the sandbox image ahead of sessions. Its only host access is
hostNetwork: true.
The agent sandbox's controller and proxy pods need none of these settings.
Code executor
The code executor also runs code in NsJail sandboxes. It adds iptables rules that block sandboxed code from reaching link-local addresses, including cloud metadata endpoints, and the 192.168.0.0/16 range.
NsJail needs either a custom seccomp profile or a privileged container, so the code executor can run in one of the following modes.
Seccomp profile mode
Retool recommends this mode. It gives the container only the permissions NsJail needs, and requires Kubernetes 1.33 or later. To use it, set codeExecutor.useSeccompProfile to true. For the full steps, refer to Run the code executor with a seccomp profile.
The code executor's pods need the following from your cluster:
- Seccomp profile: The same
nsjail-seccomp.jsonprofile as the JS executor, installed the same way. - Capabilities:
NET_ADMIN,NET_RAW,SETUID, andSETGID. The container starts as root, usesNET_ADMINandNET_RAWto add its iptables rules, then usesSETUIDandSETGIDto switch to a non-root user. Policies that requirerunAsNonRootblock it. - User namespace:
hostUsers: false, so root in the container is an unprivileged user on the node, andprocMount: Unmasked. The container runtime and kernel must support user namespaces for pods. - AppArmor profile:
Unconfinedby default. Retool recommends loading its own profile on platforms that support AppArmor.
Privileged mode
This is the default. The container runs with privileged: true, which gives it every capability and access to the node's devices, much more than NsJail needs. Use this mode if your cluster runs a Kubernetes version earlier than 1.33.
Without NsJail sandboxing
Without NsJail, code runs directly in the code executor container, without a sandbox or network rules. It can read the container's files and environment. Only use this mode if you trust every user who can edit workflows or create resources.
Only use this mode if your cluster allows neither a seccomp profile nor a privileged container. The ALLOW_UNSAFE_CODE_EXECUTION environment variable controls it.
AppArmor profiles
Retool recommends loading its own AppArmor profiles instead of using Unconfined, as an extra layer of protection around the sandboxes. NsJail and gVisor create user namespaces, which some Linux distributions restrict with AppArmor. For example, Ubuntu 24.04 turns on kernel.apparmor_restrict_unprivileged_userns by default, so sandboxes can't start there with Unconfined.
To load Retool's profiles, set codeExecutor.appArmorProfileInstaller for the JS executor and the code executor, and rr.agentSandbox.appArmorProfileInstaller for the agent sandbox. Use ubuntu for Ubuntu or Debian nodes, or cos for GKE Container-Optimized OS nodes.
Each installer runs a DaemonSet with a privileged container, hostPID: true, and hostPath volumes for /etc/apparmor.d and /run/retool/apparmor. It must run on every node that runs the pods it applies to. Those pods also mount /run/retool/apparmor to wait for the profile to load.
Platforms
The JS executor and agent sandbox can't run on platforms that manage the nodes for you, such as AWS Fargate or GKE Autopilot. These platforms don't allow the node changes the sandboxes need, such as installing custom seccomp profiles. Run them on nodes you manage instead, such as Amazon EKS or Google GKE. For other platform-specific settings, refer to Deploy Retool on Kubernetes with Helm.