/

CVE-2023-54345

Severity:

HIGH RISK

(8.8)

Code Injection / Sandbox Escape in ERPNext Frappe

CVE-2023-54345

Analyzed

Vulnerability type

Category:

Code Injection / Sandbox Escape

CWE:

CWE-94

Impact:

Remote Code Execution by Privileged Authenticated Users

Severity:

HIGH RISK

(8.8)

CVSS version:

3.1

Catch this CVE while code is being written

Detect vulnerable code directly in pull requests, so CVEs are fixed before merge.

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-script endpoint.
  • 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 like os and functions such as os.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 like gi_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 of os, 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.

Sources

SourceLink
erpnext.orgerpnext.org
frappeframework.comfrappeframework.com/docs/v13/user/en/desk/scripting/server-script
gist.github.comgist.github.com/lebr0nli/c2fc617390451f0e5a4c31c87d8720b6
github.comgithub.com/frappe/frappe/
github.comgithub.com/frappe/frappe/blob/v13.4.0/frappe/utils/safe_exec.py#L42
ur4ndom.devur4ndom.dev/posts/2023-07-02-uiuctf-rattler-read/
exploit-db.comexploit-db.com/exploits/51580
vulncheck.comvulncheck.com/advisories/frappe-framework-erpnext-remote-code-execution

Frequently asked

questions

Yes, but only by authenticated users with sufficient ERPNext privileges. An attacker who can log in as a System Manager (or equivalent) and access the server script functionality can trigger the vulnerability remotely through the web interface or API.

The attacker must have a valid ERPNext account with the System Manager role or any role that grants permission to create or edit server scripts via /app/server-script. Without such privileges, the vulnerable code path is not reachable under normal configurations.

The safest pattern is to avoid executing dynamically supplied code altogether where possible. When scripting is absolutely required, use a hardened sandbox that:

  • Completely blocks frame and object introspection,
  • Exposes only a minimal, well-defined API surface,
  • Denies access to powerful modules and builtins by construction rather than by blacklist.

Prevention relies on designing sandboxed execution with a "default deny" mindset: remove access to meta-programming primitives, avoid exposing raw Python objects, keep scripting features as simple and limited as possible, and subject any changes to sandbox or scripting functionality to dedicated security review and automated checks. Continuous monitoring of script usage and strict role separation for script authors further reduce the risk.

Find out what's already
exploitable in your codebase.