Summary
SandboxJS prior to version 0.8.26 contains a sandbox escape where async, generator, and async-generator function constructors are not properly isolated from untrusted code.
Attackers running code inside the sandbox can obtain the native AsyncFunction constructor via .constructor and create new functions that execute in the host global scope, leading to full sandbox bypass and remote code execution.
Affected context
This vulnerability affects applications that:
- Embed SandboxJS to execute untrusted or partially trusted JavaScript
- Rely on SandboxJS's
SandboxFunctionto isolate arbitrary code - Allow creation and use of async functions, generator functions, or async generator functions inside the sandbox
The issue manifests where sandboxed code can access function constructors indirectly through .constructor properties, allowing it to cross the sandbox boundary.
Exploit preconditions
- Attack surface: Any API surface that passes attacker-controlled or untrusted JavaScript into SandboxJS for evaluation or execution.
- Required access: Ability to inject or influence JavaScript code that runs inside the SandboxJS environment (e.g., user-provided scripts, plugins, templates, or configuration).
- Trigger point: Sandboxed code creates or uses async / generator functions and then accesses their
.constructorproperty, obtaining the native host constructor. - Why exploitation is possible: The sandbox only replaces the global
Functionconstructor withSandboxFunctionbut fails to remapAsyncFunction,GeneratorFunction, andAsyncGeneratorFunction. Through property access resolution inexecutor.ts, the real host constructor is exposed, and any function constructed with it executes in the unsandboxed global scope.
Why this happens
The underlying problem is incomplete isolation of function constructors, mapping to CWE-94: Improper Control of Generation of Code ('Code Injection') and CWE-693: Protection Mechanism Failure.
SandboxJS attempts to secure dynamic code execution by substituting the global Function with SandboxFunction via a lookup map. However, it overlooks related constructors (AsyncFunction, GeneratorFunction, AsyncGeneratorFunction) that are not globals but are accessible via .constructor on function instances. The executor's property access logic dutifully returns these native constructors because they are absent from the safe-replacement map, breaking the sandbox model.
This design flaw means that user-controlled code can still generate new functions using genuine engine constructors that run in the real global context, bypassing all sandbox controls and enabling arbitrary code execution on the host.
Potential impact
- Execution of arbitrary JavaScript in the host environment outside the sandbox.
- Complete bypass of security and resource restrictions enforced by SandboxJS.
- Access to sensitive data, process environment variables, and in-process secrets.
- Invocation of host APIs, including filesystem, network, and database layers exposed by the embedding application.
- Pivoting to broader system compromise depending on host privileges and integrations.
- Abuse of the application as a platform for further attacks (e.g., SSRF, data exfiltration).
General mitigation guidance
Effective mitigation focuses on hardening the sandbox boundary and reducing reliance on in-process sandboxes for strict isolation:
- Comprehensive constructor isolation: Ensure that all function-like constructors (
Function,AsyncFunction,GeneratorFunction,AsyncGeneratorFunction, and any engine-specific variants) are wrapped or replaced with sandbox-aware versions. Apply this consistently in every property resolution path, not only for global properties. - Strict property access mediation: Centralize and harden property access logic so that traversing object graphs (e.g.,
.constructor,__proto__,constructor.prototype) cannot leak host-native constructors or powerful built-ins back into sandboxed code. - Minimize exposed host capabilities: Treat anything reachable from sandboxed objects as part of the attack surface. Avoid exposing unnecessary objects, methods, or references that might lead back to the host environment.
- Privilege and trust reduction: Do not assume that the sandbox is perfect. Run the embedding process or sandbox executor with the least privileges possible (restricted filesystem access, minimal network, and scoped environment variables).
- Defense-in-depth isolation: For highly untrusted code, prefer process-level or VM/container isolation over purely in-process JavaScript sandboxes. Use OS-level boundaries and inter-process communication to contain code execution.
- Secure-by-default configuration: Ship the sandbox with the safest defaults—restricted globals, hardened constructors, and documented limitations—so that developers must opt into additional capabilities rather than starting from an overly permissive baseline.
How teams detect issues like this
Detection strategies can focus on both code-level patterns and runtime behavior:
- Static analysis and code scanning:
- Search for sandbox implementations that override
Functionbut do not handleAsyncFunction,GeneratorFunction, andAsyncGeneratorFunction. - Flag property access mediators (
executor, proxies, traps) that directly return host constructors without validation against a whitelist. - Detect code that allows untrusted scripts to access
.constructor,__proto__, orprototypeon function objects without constraint. - Code review heuristics:
- During security review, treat any JavaScript sandbox as a critical component and verify the treatment of all function constructors and reflective capabilities.
- Review any logic that maps or proxies native objects into the sandbox; ensure the mapping is complete and includes non-global but discoverable constructors.
- Runtime monitoring:
- Log and monitor usage of dynamic function generation (e.g.,
Function, async function constructors) and unexpected calls originating from sandboxed execution paths. - Use application-level auditing to spot sandboxed scripts that appear to open network connections, access files, or read sensitive configuration unexpectedly.
- Automated testing:
- Incorporate security regression tests that attempt
.constructor-based escapes and other prototype chain traversal techniques against the sandbox. - Leverage fuzzing or property-based tests to explore unusual property access chains from within the sandbox.

