Summary
ERPNext 13.4.0, built on the Frappe Framework, contains a sandbox escape in its RestrictedPython-based server scripting environment that lets privileged users execute arbitrary code on the host.
By abusing frame introspection in server scripts, an authenticated System Manager can traverse the call stack and call functions like os.popen, leading to system command execution despite the intended sandbox.
Affected context
This vulnerability occurs in ERPNext deployments that:
- Use Frappe Framework 13.4.0 (or similar) with server-side scripting enabled
- Rely on RestrictedPython to sandbox user-defined server scripts
- Allow users with the System Manager role to create or modify server scripts via
/app/server-script
The risk is most pronounced in multi-user or multi-tenant ERPNext instances where System Manager accounts are broadly assigned or where those accounts may be phished, misused, or otherwise compromised.
Exploit preconditions
- Attack surface: ERPNext web interface and API, specifically the
/app/server-scriptendpoint. - Required access: Authenticated access with System Manager role or equivalent permission to create or edit server scripts.
- Trigger point: Execution of a crafted server script within the RestrictedPython sandbox.
- Why exploitation is possible: The RestrictedPython sandbox still exposes frame introspection capabilities (such as access to
gi_frame), enabling traversal of the call stack to reach powerful objects and ultimately invoke unrestricted modules likeosand functions such asos.popen.
Why this happens
The underlying bug falls under CWE-94: Improper Control of Generation of Code ('Code Injection'), in the specific context of an incomplete sandbox.
Frappe/ERPNext uses RestrictedPython to limit what server scripts can do, but the sandbox does not fully prevent introspection on generator frames and call stack objects.
By accessing gi_frame and related attributes, a script can escape the restricted namespace, recover privileged builtins and modules, and construct calls to dangerous functions such as os.popen.
In abstract terms, untrusted or semi-trusted script content is executed in an environment that still exposes powerful meta-programming primitives, allowing the attacker to reassemble a path to native code execution despite nominal restrictions.
Potential impact
- Execution of arbitrary system commands under the ERPNext/Frappe process account
- Reading, modifying, or deleting application data managed by ERPNext
- Pivoting to other services or hosts reachable from the ERPNext server
- Installation of backdoors, web shells, or persistence mechanisms on the host
- Abuse of the ERP system to exfiltrate sensitive business or personal data
- Disruption of ERP services, including data corruption or denial of service
General mitigation guidance
Effective mitigation focuses on tightening or eliminating risky sandboxed execution paths and minimizing the power of server-side scripting:
- Harden or replace the sandbox: Use a more restrictive execution environment that prevents frame introspection, access to
__builtins__, or other meta-level APIs; ensure RestrictedPython (or equivalent) is configured to block attributes likegi_frame. - Reduce script privileges: Design server scripts to operate on strictly limited, pre-defined APIs instead of raw Python objects and modules; avoid exposing global objects that can be introspected or climbed from.
- Constrain who can author scripts: Restrict the ability to create or edit server scripts to a minimal set of highly trusted administrators; separate daily operational roles from code-authoring roles.
- Apply least privilege on the host: Run the ERPNext/Frappe service under a non-privileged account, with minimal OS-level permissions and restricted filesystem and network access.
- Implement defense in depth: Combine in-application controls with OS-level controls (such as mandatory access control, process confinement, and command whitelisting) so that even if the sandbox is bypassed, the blast radius is reduced.
How teams detect issues like this
Organizations can detect and monitor for this class of issue and its exploitation with several approaches:
- Static review of sandbox code: Examine the RestrictedPython configuration, policy modules, and any custom wrappers for exposure of introspection attributes (
gi_frame,f_back,__globals__, etc.) and for availability ofos,subprocess, or similar modules. - Application code review: Flag any locations where user-controlled or administrator-controlled script content is executed, and verify that only safe, tightly scoped APIs are exposed.
- Static analysis rules: Configure SAST tools or linters to detect direct or indirect access to frame objects and restricted attributes within sandbox-related code and templates.
- Runtime monitoring and logging: Log creation and modification of server scripts, especially those referencing introspection attributes,
os,subprocess, or shell commands; alert on unusual script activity or high-risk patterns. - Policy enforcement: Use CI/CD pipelines to enforce security gates around sandbox and scripting changes, requiring explicit security review for modifications that touch these areas.

