# CVE-2023-41039: Sandbox escape via various forms of "format" in RestrictedPython

> Attackers can escape the RestrictedPython sandbox to read sensitive objects via format string manipulation, requiring an upgrade to secure versions.

- **Severity:** High 8.3 (`CVSS:3.1/AV:N/AC:L/PR:H/UI:R/S:C/C:H/I:H/A:L`)
- **Exploited in the wild:** Not known
- **Login needed:** An administrator
- **Attackable:** Over the internet
- **Victim action needed:** A user has to click or open something
- **Fix available:** Yes
- **Published:** 30 Aug 2023
- **Updated:** 1 Oct 2024
- **Also known as:** DEBIAN-CVE-2023-41039, GHSA-xjw2-6jm9-rf67, PYSEC-2023-159, UBUNTU-CVE-2023-41039

## What is CVE-2023-41039?

CVE-2023-41039 is a high-severity vulnerability in zopefoundation RestrictedPython, published on 30 Aug 2023. An attacker needs administrator access to exploit it, and only if a user opens a file or follows a link.

### Official description

RestrictedPython is a restricted execution environment for Python to run untrusted code. Python's "format" functionality allows someone controlling the format string to "read" all objects accessible through recursive attribute lookup and subscription from objects he can access. This can lead to critical information disclosure. With `RestrictedPython`, the format functionality is available via the `format` and `format_map` methods of `str` (and `unicode`) (accessed either via the class or its instances) and via `string.Formatter`. All known versions of `RestrictedPython` are vulnerable. This issue has been addressed in commit `4134aedcff1` which has been included in the 5.4 and 6.2 releases. Users are advised to upgrade. There are no known workarounds for this vulnerability.

Weakness: CWE-74 (Improper Neutralization of Special Elements in Output Used by a Downstream Component)

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

5 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.4 |
| `restrictedpython` | PyPI | From 6.0 | 6.2, 5.4 |
| `restrictedpython` | PyPI | From 6.0 | 6.2 |
| `RestrictedPython` | PyPI | All versions | 5.4 |
| `RestrictedPython` | PyPI | From 6.0 | 6.2 |

Products: Zope Restrictedpython.

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

Upgrade restrictedpython to a fixed version for your release line: 5.4 or 6.2.

## Is CVE-2023-41039 being exploited?

There is no known exploitation of CVE-2023-41039 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-37271](https://insights.sbomify.com/CVE-2023-37271/): RestrictedPython vulnerable to arbitrary code execution via stack frame sandbox escape (high 8.4)

## Fix prompt for AI coding agents

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

What it is: CVE-2023-41039 is a high-severity vulnerability in zopefoundation RestrictedPython, published on 30 Aug 2023. An attacker needs administrator access to exploit it, and only if a user opens a file or follows a link.
Exploitation: There is no known exploitation of CVE-2023-41039 in the wild and no public exploit code.

Affected packages and fixed versions:
- restrictedpython (PyPI): affected all versions; fixed in 5.4
- restrictedpython (PyPI): affected from 6.0; fixed in 6.2, 5.4
- restrictedpython (PyPI): affected from 6.0; fixed in 6.2
- RestrictedPython (PyPI): affected all versions; fixed in 5.4
- RestrictedPython (PyPI): affected from 6.0; fixed in 6.2

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-41039/ (Markdown: https://insights.sbomify.com/CVE-2023-41039/index.md)
```

## Sources

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

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