Summary
RestrictedPython could allow restricted code to escape its sandbox when a custom import policy or globals exposed the standard library string module, string.Formatter, or related formatter objects. Because formatter methods performed attribute and item traversal outside RestrictedPython's safer access controls, an attacker may reach host functions, builtins, file access, or code execution primitives.
Affected context
The vulnerability occurs in RestrictedPython environments that expose the standard library string module, a string.Formatter class or instance, or a Formatter subclass to restricted code. It requires a deployment configuration in which custom import policies or globals make these live formatter objects available, allowing their internal traversal behavior to bypass the sandbox's usual attribute-access protections.
Exploit preconditions
- Attack surface: Restricted Python code executed within a host application
- Required access: Ability to submit or influence code processed by the RestrictedPython environment
- Configuration requirement: A custom import policy or global namespace exposes the standard library
stringmodule or a Formatter-related object - Trigger point: Invocation of formatter functionality that performs field, attribute, or item traversal
- Why exploitation is possible: Relevant formatter methods access objects internally without routing traversal through RestrictedPython's safer attribute-access controls
Why this happens
The underlying issue is a protection-mechanism failure caused by trusted formatter objects crossing the sandbox boundary. The format, get_field, get_value, and vformat methods performed attribute and item traversal internally, bypassing the restricted access mediation expected by the sandbox. This behavior maps primarily to CWE-693: Protection Mechanism Failure, with related concerns involving unsafe reflection, unintended information exposure, and uncontrolled resource or object access.
Potential impact
- Restricted code may access host functions or builtins
- An attacker could read files or other sensitive host data
- Host-side code execution may occur
- Application confidentiality, integrity, and availability could be affected
- A compromised process could be used for further local or network activity
General mitigation guidance
Mitigation generally focuses on preserving a strict trust boundary between restricted code and host objects.
Common defensive patterns include:
- Avoiding exposure of the standard library
stringmodule and Formatter-related objects to restricted code - Applying explicit allowlists to imported modules, globals, classes, and object capabilities
- Routing all attribute and item traversal through sandbox-aware access controls
- Prefering narrowly scoped data-only interfaces over live host objects
- Running sandboxed workloads with least privilege, isolation, and restricted filesystem and network access
- Treating sandboxed code as potentially hostile and limiting the value of any host capabilities it can reach
How teams detect issues like this
Detection can include:
- Static analysis for exposure of
string.Formatter, Formatter instances, or Formatter subclasses in restricted globals - Code review checks for custom import policies that expose broad standard-library modules
- Searches for formatter methods that perform object traversal without sandbox authorization checks
- Policy enforcement that blocks live host objects and unrestricted module imports from sandbox namespaces
- Runtime monitoring for unexpected file access, process activity, or use of builtins by restricted workloads
- Automated security review tools can help identify unsafe capability exposure and missing access mediation

