How to run untrusted code from customers' apps in an agent environment
In short
Run untrusted code as a non-root user in a short-lived environment of its own, with all Linux capabilities dropped, privilege escalation blocked, a read-only root file system, memory, CPU and process limits, no route to internal networks, and a sandboxed runtime.
Untrusted code is any code your platform runs that you did not write and review. For a vendor whose AI agent tests other companies' apps, that is most of what runs: the customer's front end in the agent's browser, scripts or test files they upload, packages the agent installs, and code the model writes after reading pages it does not control. This guide shows the settings that keep such code from reaching your platform or another customer.
Why do agent platforms run so much untrusted code?
Agent platforms run untrusted code because the agent's job is to execute what the customer's app and the model produce. The Kubernetes documentation names the same situation for other software: "In some applications, like a Content Management System (CMS), customers may be allowed the ability to upload and execute untrusted scripts or code", and warns that in a shared environment unpatched flaws "can be exploited by attackers for container breakouts and remote code execution that allow access to host resources" (Kubernetes multi-tenancy, read 2026-11-26).
Plan as if every run will try to:
- Gain root inside its environment.
- Escape to the host or the cluster.
- Reach internal services, metadata endpoints or other runs over the network.
- Exhaust memory, CPU, processes or disk so other customers' runs slow down or fail.
- Leave something behind for the next run.
Which settings stop each of those?
Each goal above has a matching setting. The table pairs them with the Docker option and the Kubernetes field that apply it.
Table: Hardening settings for an agent run, with the Docker and Kubernetes forms
| Goal of the code | Setting | Docker | Kubernetes |
|---|---|---|---|
| Gain root | Run as a fixed non-root user | --user 10001:10001 | runAsNonRoot: true, runAsUser: 10001 |
| Gain root | Block privilege escalation | --security-opt=no-new-privileges | allowPrivilegeEscalation: false |
| Escape | Drop all Linux capabilities | --cap-drop ALL | capabilities: { drop: ["ALL"] } |
| Escape | Keep the runtime's default system call profile | (on by default; never disable it) | seccompProfile: { type: RuntimeDefault } |
| Escape | Sandboxed runtime per run | runtime option of your engine | runtimeClassName |
| Change the image | Read-only root file system, scratch space in memory | --read-only --tmpfs /tmp | readOnlyRootFilesystem: true plus an emptyDir |
| Exhaust resources | Memory, CPU and process limits | --memory, --cpus, --pids-limit | resources.limits |
| Reach the network | Per-run network with default deny | --network <per-run network> | NetworkPolicy |
| Survive the run | Remove the environment at exit | --rm | restartPolicy: Never, delete the pod |
The Docker options are from the docker container run reference and the OWASP Docker Security Cheat Sheet, both read 2026-11-26. OWASP's rule on the default profile is short: "do not disable default security profile!"
Step 1: How do I start a hardened container for one run?
Start each run from a pinned image with every option above set on the command line. This example uses illustrative values; size the limits for your own workload:
docker network create --internal run-7f3c-net
docker run --rm \
--name run-7f3c \
--user 10001:10001 \
--cap-drop ALL \
--security-opt=no-new-privileges \
--read-only \
--tmpfs /tmp \
--memory 4g \
--cpus 2 \
--pids-limit 512 \
--network run-7f3c-net \
registry.example/agent-runner@sha256:<digest>
--internal creates a network whose containers "may communicate between each other, but not with any other network", as Docker's network create reference puts it (read 2026-11-26). The same page notes that the gateway address and the host can still reach containers on it, so keep host services off that address. Attach your egress proxy to the network when the run needs the customer's app. Never mount the container engine's socket into a run: OWASP's Rule #1 is "Do not expose the Docker daemon socket (even to the containers)", and it notes that mounting it read-only does not make the API read-only.
Step 2: How do I express the same settings in Kubernetes?
Express them in the pod's security context and resource limits, then enforce the Restricted Pod Security Standard on the namespace so a pod without them is refused. The Restricted profile is "Heavily restricted policy, following current Pod hardening best practices"; it requires non-root, allowPrivilegeEscalation: false, dropping ALL capabilities with only NET_BIND_SERVICE allowed back, and a system call profile of RuntimeDefault or Localhost (Kubernetes Pod Security Standards, read 2026-11-26).
apiVersion: v1
kind: Namespace
metadata:
name: tenant-acme
labels:
pod-security.kubernetes.io/enforce: restricted
---
apiVersion: v1
kind: Pod
metadata:
name: run-7f3c
namespace: tenant-acme
spec:
runtimeClassName: sandboxed
restartPolicy: Never
automountServiceAccountToken: false
securityContext:
runAsNonRoot: true
runAsUser: 10001
seccompProfile:
type: RuntimeDefault
containers:
- name: agent
image: registry.example/agent-runner@sha256:<digest>
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop: ["ALL"]
resources:
limits:
memory: 4Gi
cpu: "2"
volumeMounts:
- name: scratch
mountPath: /tmp
volumes:
- name: scratch
emptyDir:
sizeLimit: 2Gi
automountServiceAccountToken: false opts the pod out of mounting its service account's API credentials, which Kubernetes otherwise mounts by default (Kubernetes service accounts, read 2026-11-26). The values are an example, not a recommendation.
Step 3: Why add a sandboxed runtime on top?
Add a sandboxed runtime because every setting above still leaves the host kernel shared. The Kubernetes documentation explains that containers "are processes running on a shared kernel; they mount file systems like /sys and /proc from the underlying host", and that a sandbox "helps to insulate the host from container escapes" (Kubernetes multi-tenancy, read 2026-11-26). NIST makes the same point in control SC-39, Process Isolation: "Maintain a separate execution domain for each executing system process" (NIST SP 800-53 Rev. 5, read 2026-11-26).
Our article on tenant isolation compares the boundaries.
Step 4: What if the engine itself must run as root?
If the container engine runs as root, remap users so that root inside a container is an ordinary user on the host. Docker's guide puts it this way: "For containers whose processes must run as the root user within the container, you can re-map this user to a less-privileged user on the Docker host" (Docker user namespace remapping, read 2026-11-26). The Linux manual explains the mechanism: "a process can have a normal unprivileged user ID outside a user namespace while at the same time having a user ID of 0" inside it (user_namespaces(7), read 2026-11-26). OWASP's Rule #11 goes further and recommends running the engine rootless.
How do I check a run is locked down?
Check from inside a run, with the same image and settings production uses:
idprints a non-zero user ID.grep CapEff /proc/self/statusshows an all-zero mask, meaning no effective capabilities.grep NoNewPrivs /proc/self/statusprintsNoNewPrivs: 1, the no_new_privs bit (proc_pid_status(5), read 2026-11-26).- Writing to
/usrfails; writing to/tmpworks. - A fork loop stops at the process limit, and a memory hog is killed at its limit without affecting other runs.
- Connections to internal addresses and the cloud metadata address fail.
- After the run ends, nothing it wrote appears in the next run.
What we read for this article
- Kubernetes: Multi-tenancy, read 2026-11-26
- Kubernetes: Pod Security Standards, read 2026-11-26
- Docker: docker container run reference, read 2026-11-26
- Docker: docker network create reference, read 2026-11-26
- Kubernetes: Configure Service Accounts for Pods, read 2026-11-26
- proc_pid_status(5), Linux manual page, read 2026-11-26
- Docker: Isolate containers with a user namespace, read 2026-11-26
- OWASP Docker Security Cheat Sheet, read 2026-11-26
- user_namespaces(7), Linux manual page, read 2026-11-26
- NIST SP 800-53 Rev. 5, Security and Privacy Controls (control SC-39), read 2026-11-26
Questions readers ask
What counts as untrusted code for an AI testing agent?
Anything you did not write and review. That includes the customer's app and its JavaScript, scripts or test files the customer uploads, packages the agent installs, and code the model writes after reading the app.
Is running as a non-root user inside a container enough?
No. It removes one easy path to the host, but the container still shares the host kernel. Combine it with dropped capabilities, no privilege escalation, resource limits, network rules and, for untrusted code, a sandboxed runtime.
What does Kubernetes' Restricted profile require?
Pods must run as non-root, set allowPrivilegeEscalation to false, drop ALL capabilities (only NET_BIND_SERVICE may be added back), set a RuntimeDefault or Localhost system call profile, and use only listed volume types.
Should the agent's environment be reused between runs?
Not between customers, and preferably not between runs. Create the environment from a pinned image at the start of the run and destroy it at the end, so nothing one run plants survives into the next.
Yours,
SecureAgentSandbox editors
Revised , 5 minutes to read