# CVE-2023-37271: RestrictedPython vulnerable to arbitrary code execution via stack frame sandbox escape

> Attackers can escape the RestrictedPython sandbox via stack frame access to execute arbitrary code, affecting deployments allowing untrusted Python code.

- **Severity:** High 8.4 (`CVSS:3.1/AV:N/AC:H/PR:L/UI:N/S:C/C:H/I:H/A:L`)
- **Exploited in the wild:** Not known
- **Login needed:** Any signed-in user
- **Attackable:** Over the internet
- **Victim action needed:** Not needed
- **Fix available:** Yes
- **Published:** 11 Jul 2023
- **Updated:** 7 Nov 2024
- **Also known as:** DEBIAN-CVE-2023-37271, GHSA-wqc8-x2pr-7jqh, PYSEC-2023-118, UBUNTU-CVE-2023-37271

## What is CVE-2023-37271?

CVE-2023-37271 is a high-severity code injection vulnerability in zopefoundation RestrictedPython, published on 11 Jul 2023. Any signed-in user can exploit it over the network, with no action from the victim.

### Official description

RestrictedPython is a tool that helps to define a subset of the Python language which allows users to provide a program input into a trusted environment. RestrictedPython does not check access to stack frames and their attributes. Stack frames are accessible within at least generators and generator expressions, which are allowed inside RestrictedPython. Prior to versions 6.1 and 5.3, an attacker with access to a RestrictedPython environment can write code that gets the current stack frame in a generator and then walk the stack all the way beyond the RestrictedPython invocation boundary, thus breaking out of the restricted sandbox and potentially allowing arbitrary code execution in the Python interpreter. All RestrictedPython deployments that allow untrusted users to write Python code in the RestrictedPython environment are at risk. In terms of Zope and Plone, this would mean deployments where the administrator allows untrusted users to create and/or edit objects of type `Script (Python)`, `DTML Method`, `DTML Document` or `Zope Page Template`. This is a non-default configuration and likely to be extremely rare. The problem has been fixed in versions 6.1 and 5.3.

Weakness: CWE-913 (Improper Control of Dynamically-Managed Code Resources)

## Which versions are affected by CVE-2023-37271?

6 open source packages ship an affected version, including restrictedpython and RestrictedPython. It affects 1 product.


| Package | Ecosystem | Affected | Fixed in |
| --- | --- | --- | --- |
| `restrictedpython` | PyPI | All versions | 5.3 |
| `restrictedpython` | PyPI | All versions | 5.3 |
| `restrictedpython` | PyPI | All versions | 5.3 |
| `restrictedpython` | PyPI | From 6.0a1.dev0 | 6.1 |
| `RestrictedPython` | PyPI | All versions | 5.3 |
| `RestrictedPython` | PyPI | From 6.0a1.dev0 | 6.1 |

Products: Zope Restrictedpython.

## How do I fix CVE-2023-37271?

Upgrade restrictedpython to a fixed version for your release line: 5.3 or 6.1.

## Is CVE-2023-37271 being exploited?

There is no known exploitation of CVE-2023-37271 in the wild and no public exploit code.

## Other vulnerabilities in zopefoundation RestrictedPython

- [CVE-2024-47532](https://insights.sbomify.com/CVE-2024-47532/): RestrictedPython information leakage via `AttributeError.obj` and the `string` module (high 8.7)
- [CVE-2023-41039](https://insights.sbomify.com/CVE-2023-41039/): Sandbox escape via various forms of "format" in RestrictedPython (high 8.3)

## Fix prompt for AI coding agents

```text
Check this codebase for CVE-2023-37271 and fix it if it is affected.

What it is: CVE-2023-37271 is a high-severity code injection vulnerability in zopefoundation RestrictedPython, published on 11 Jul 2023. Any signed-in user can exploit it over the network, with no action from the victim.
Exploitation: There is no known exploitation of CVE-2023-37271 in the wild and no public exploit code.

Affected packages and fixed versions:
- restrictedpython (PyPI): affected all versions; fixed in 5.3
- restrictedpython (PyPI): affected all versions; fixed in 5.3
- restrictedpython (PyPI): affected all versions; fixed in 5.3
- restrictedpython (PyPI): affected from 6.0a1.dev0; fixed in 6.1
- RestrictedPython (PyPI): affected all versions; fixed in 5.3
- RestrictedPython (PyPI): affected from 6.0a1.dev0; fixed in 6.1

Task:
1. Search the dependency manifests and lockfiles (for example package.json, package-lock.json, requirements.txt, poetry.lock, uv.lock, pom.xml, build.gradle, go.mod, Cargo.lock, Gemfile.lock) and any Dockerfiles for the affected packages, including transitive dependencies.
2. If an affected version is in use, upgrade it to the fixed version for its release line with the project's package manager, so the lockfile is regenerated. Prefer the smallest upgrade that fixes it.
3. If there is no fix, or the upgrade would break the build, do not guess: explain the risk and the safest workaround instead.
4. Run the tests and summarise what changed and why.
5. If nothing in the project is affected, say so and change nothing.

Reference: https://insights.sbomify.com/CVE-2023-37271/ (Markdown: https://insights.sbomify.com/CVE-2023-37271/index.md)
```

## Sources

- CVE record: https://www.cve.org/CVERecord?id=CVE-2023-37271
- NVD: https://nvd.nist.gov/vuln/detail/CVE-2023-37271
- OSV: https://osv.dev/vulnerability/CVE-2023-37271

---
From sbomify Insights: https://insights.sbomify.com/CVE-2023-37271/
Summaries are generated and may contain mistakes; the official description above is the record.
