Skip to main content

Untrusted code sandboxing

Retool runs untrusted code that builders and AI agents write, and isolates each run in its own sandbox using NsJail or gVisor.

ServiceRunsSandbox
JS executorBackend functionsNsJail
Agent sandboxThe app builder's AI agent and app previews.gVisor
Code executorworkflows 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 adds clone, mount, umount2, pivot_root, and sethostname to 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 a hostPath volume.
  • Capabilities: SETUID and SETGID. The container starts as root and uses them only to switch to a non-root user before the service starts, so policies that require runAsNonRoot block it.
  • AppArmor profile: Unconfined by 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.allowedDomains to limit sandboxes to specific domains.
  • Set rr.agentSandbox.networkPolicy.enabled to true to 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, plus ptrace, which gVisor uses to intercept the sandboxed code's syscalls, and the io_uring syscalls for asynchronous I/O.
  • User: 1001, with no capabilities and a read-only root filesystem.
  • Device: /dev/net/tun, which pods request through the smarter-devices/net_tun resource.
  • AppArmor profile: Unconfined by 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.json to the kubelet's seccomp directory, /var/lib/kubelet/seccomp, through a hostPath volume.
  • Device plugin: Registers /dev/net/tun with the kubelet. It uses hostPath volumes for /var/lib/kubelet/device-plugins, /dev, and /sys, and the system-node-critical priority class. If another process already runs this plugin on your nodes, set rr.agentSandbox.sandboxNetwork.deployDaemonSet to false.
  • 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.json profile as the JS executor, installed the same way.
  • Capabilities: NET_ADMIN, NET_RAW, SETUID, and SETGID. The container starts as root, uses NET_ADMIN and NET_RAW to add its iptables rules, then uses SETUID and SETGID to switch to a non-root user. Policies that require runAsNonRoot block it.
  • User namespace: hostUsers: false, so root in the container is an unprivileged user on the node, and procMount: Unmasked. The container runtime and kernel must support user namespaces for pods.
  • AppArmor profile: Unconfined by 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​

Warning

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.