Zero-Trust Isolation for AI-Generated Web Sandboxes
How we isolate AI-generated React and Vite containers, strip platform secrets at the proxy boundary, and defend against sandbox-to-account pivot chains.
Table of Contents
- →AI-generated frontends are untrusted third-party code: run every build in a rootless, capability-dropped container (
--cap-drop=ALL,uid=1000:1000). - →Block
/.env,/.session_id, and/node_modules/.binat the proxy while allowing Vite's/node_modules/.vite/deps/ESM cache so apps still hydrate. - →Treat
/preview/and/s/Referers as hostile origins in CSRF middleware even when served from the same apex hostname.
Whenever an AI builder lets users generate arbitrary JavaScript, CSS, and server configurations in one prompt, the preview surface stops being harmless static HTML. From a security standpoint, every user-generated frontend is untrusted third-party code executing inside the user's browser.
After an internal security review across DomainFA's Go API and Dockerized builder infrastructure, we hardened three architectural boundaries between untrusted sandboxes and the control plane.
Rootless Capability-Dropped Builder Containers
Even if untrusted generated code or build scripts attempted local escape inside a sandbox container, kernel surface area is constrained at container launch:
docker run -d --name dfb-<project_id> \
--user 1000:1000 \
--cap-drop=ALL \
--security-opt=no-new-privileges:true \
--pids-limit=128 \
--memory=1024m \
--network builder-net \
domainfa-template:latest
Separating ephemeral container runtime from durable source custody on a dedicated per-project data volume allows aggressive memory and process caps without losing built websites during container restarts.
Narrow Dotfile Filtering Without Breaking Vite ESM Hydration
Naively blocking all paths containing a dot segment (/.) stops sensitive .env and .git leaks, but it unintentionally breaks Vite's pre-bundled dependency chunks served under /node_modules/.vite/deps/*.js.
We narrowed the reverse-proxy gate explicitly:
| Request Path Pattern | Edge Decision | Rationale |
|---|---|---|
/.env, /.git/*, /.session_id | 404 Blocked | Prevents secret/session metadata leakage |
/node_modules/.bin/* | 404 Blocked | Blocks binary/CLI inspection |
/.. traversal segments | 404 Blocked | Stops path-normalization escapes |
/node_modules/.vite/deps/* | 200 Allowed | Required for React + Vite ESM client hydration |
Sandbox Referer Firewall & Privacy Headers
Serving previews under domainfa.com/preview/<pid>/ or domainfa.com/s/<slug>/ requires guarding against same-site JavaScript attempting fetch('/api/...') with ambient cookies:
- Control-plane Referer deny: DomainFA's
csrfGuardrejects every non-GET request carrying a cookie wheneverReferermatches/previewor/s/, returning403 Forbiddenbefore reaching any handler. - Unpublished sandbox de-indexing: Private previews emit
X-Robots-Tag: noindex, nofollow, noarchivealongside strictPermissions-Policy: camera=(), microphone=(), geolocation=(), browsing-topics=(). - Publisher anonymity on gallery feeds: Public gallery cards hash internal identifiers into
pub_<16hex>tokens so internal account IDs never enumerate across/api/builder/gallery.
If your engineering team is hardening an AI coding agent, multi-tenant preview proxy, or SaaS control plane and wants a second pair of offensive eyes, reach our DomainFA Security Desk directly or email [email protected].
Why Automated Scanners Miss Business Logic and Authorization Bugs
Why dependency and signature scanners stay green even when state-machine bypasses, sibling-route authorization gaps, and accounting invariant breaks sit in production code.
Securing AI Agent Tool Boundaries and Ephemeral Build Sandboxes
Practical defense-in-depth patterns for AI coding agents and web builders: MCP filesystem confinement, immutable build harnesses, networkless ephemeral sandboxes, and dedicated preview domains.