Summary
SandboxJS, a JavaScript sandboxing library, allowed untrusted code to obtain arrays containing Function and then reconstruct callable function objects from them.
When this occurs, an attacker can escape the sandbox and execute arbitrary JavaScript code in the host context, fully bypassing intended isolation.
Affected context
This vulnerability affects applications that embed SandboxJS to execute potentially untrusted or user-supplied JavaScript in an isolated environment.
It becomes relevant when:
- SandboxJS versions prior to 0.8.34 are used, and
- The sandboxed code has access to constructs that leak
Functionvia arrays, and - The sandboxed environment exposes utilities such as
Object.fromEntriesor similar helpers.
In such setups, the sandbox boundary can be bypassed, allowing sandboxed code to interact with the host environment in ways the application did not intend.
Exploit preconditions
- Attack surface: JavaScript execution environment where SandboxJS runs untrusted or semi-trusted code.
- Required access: Ability to supply or influence code executed inside the SandboxJS instance (e.g., user scripts, plugins, or configuration-based scripts).
- Trigger point: Code paths that expose or return arrays containing
Function, combined with access toObject.fromEntriesor equivalent mechanisms that can rebuild objects with function-valued properties. - Why exploitation is possible: The sandbox design assumes that certain built-ins and objects are safe to expose, but the presence of
Functionin an array, coupled with object construction helpers, enables reconstruction of executable function objects. This breaks the isolation and maps to arbitrary code injection (CWE-94).
Why this happens
The underlying issue is an incomplete isolation model in the sandbox implementation.
SandboxJS attempts to restrict access to powerful primitives such as the Function constructor, which can compile and execute arbitrary code.
However, the library permits scenarios where arrays containing Function become reachable from inside the sandbox.
When combined with utilities like Object.fromEntries, sandboxed code can create objects with function-valued properties, effectively reintroducing a callable Function into the restricted environment.
This is a manifestation of CWE-94: Improper Control of Generation of Code, where the sandbox fails to fully control access to code generation primitives, allowing untrusted input to reach components that can dynamically create and execute code.
Potential impact
- Execution of arbitrary JavaScript code with the privileges of the host environment, bypassing sandbox restrictions.
- Access to data and APIs that should have remained isolated from untrusted scripts.
- Manipulation of application state, including sensitive in-memory objects and configuration.
- Potential theft of authentication tokens or session information exposed to the host environment.
- Pivoting into broader system compromise if the sandboxed environment had access to powerful host APIs, file I/O, or network capabilities.
General mitigation guidance
Effective mitigation focuses on strengthening the isolation boundaries and eliminating unsafe exposure of code-generation primitives:
- Review all sandboxed environments to ensure that constructors such as
Function,eval, and similar code-generation APIs are inaccessible from sandboxed code, including via indirect references (arrays, prototypes, or helper APIs). - Avoid exposing general-purpose object construction helpers (e.g.,
Object.fromEntries,Object.assign, spread operators applied to host objects) to untrusted scripts when they can be combined with powerful primitives. - Enforce a strict object capability model inside the sandbox: only pass in the minimal set of functions and objects required for the sandboxed code to operate.
- Employ defense-in-depth by running the hosting process with least privilege and separating high-risk script execution from sensitive data and operations.
- Consider layering security controls (e.g., process-level isolation, VM-based sandboxes, or hardened interpreters) so that a logical sandbox escape does not automatically imply full system compromise.
How teams detect issues like this
Detection generally involves a combination of code inspection and automated analysis:
- Static analysis and code review:
- Look for any paths where
Function,eval, or dynamic code construction (e.g.,new Function,setTimeoutwith string arguments) can be reached from sandboxed code. - Examine bridges between host and sandbox for accidental exposure of host objects, arrays, or utilities that may contain or rebuild
Function. - Verify that helper APIs like
Object.fromEntries,Object.assign, or custom factory functions do not accept untrusted keys or values that can produce function-valued properties. - Automated scanning:
- Use SAST or custom linters to flag usage of dynamic code-generation APIs in the context of sandbox implementations.
- Add CI checks that detect the presence of high-risk primitives inside the set of objects and globals exposed to sandboxed code.
- Runtime monitoring and tests:
- Implement security-focused test cases that attempt to obtain
Functionor equivalent capabilities from the sandbox. - Monitor logs for unexpected errors or stack traces indicating that sandboxed code interacted with host-only APIs.

