CVE-2023-41039: RestrictedPython Sandbox Escape Vulnerability

HIGH
Advisory Identifier: GHSA-xjw2-6jm9-rf67
CVSS 8.3
CWE-74
Published
Advisory Summary

CVE-2023-41039 is a HIGH (CVSS 8.3) sandbox escape vulnerability (CWE-74: Improper Neutralization of Special Elements in Output Used by a Downstream Component ('Injection')) affecting RestrictedPython libraries in the PyPI ecosystem. The issue involves sandbox escape via various forms of "format".. Remediation is available by upgrading affected packages to version 5.4, 6.2.

CVE-2023-41039 at a Glance

CVE Identifier CVE-2023-41039
GHSA Identifier GHSA-xjw2-6jm9-rf67
Severity HIGH
CVSS v3.1 Base Score 8.3
CWE Classification CWE-74: Improper Neutralization of Special Elements in Output Used by a Downstream Component ('Injection')
Affected Product RestrictedPython
Package Ecosystem PyPI
Published Date
Last Updated

Security Impact

CVSS: 8.3

The security impact metrics below are parsed directly from the CVSS v3.1 vector string (CVSS:3.1/AV:N/AC:L/PR:H/UI:R/S:C/C:H/I:H/A:L).

Attack Vector Network (AV:N)
Attack Complexity Low (AC:L)
Privileges Required High (PR:H)
User Interaction Required (UI:R)
Scope Changed (S:C)
Confidentiality High (C:H)
Integrity High (I:H)
Availability Low (A:L)

How to Fix CVE-2023-41039

To remediate this vulnerability, upgrade all affected RestrictedPython dependencies to a patched release. Both direct and transitive dependencies should be checked, as frameworks or security libraries may pull in vulnerable versions transitively.

RestrictedPython fixed: 5.4
pip install RestrictedPython>=5.4
RestrictedPython fixed: 6.2
pip install RestrictedPython>=6.2
Verify Direct and Transitive Dependencies

Run the following command in your project directory to verify that all resolved dependencies use the patched release:

pip show RestrictedPython

How to Check if You Are Affected

To determine whether your Python environment is affected by CVE-2023-41039, inspect your installed packages using pip:

pip show RestrictedPython

Analyze the terminal output to locate any version of RestrictedPython in your dependency tree. If any resolved version matches the vulnerable ranges listed in the table below (for example, versions earlier than 5.4), your application is affected.

Note: The pip show RestrictedPython command checks currently installed packages in your active Python environment.

Technical Details

Vulnerability Mechanism & Technical Analysis

This vulnerability is classified under CWE-74: Improper Neutralization of Special Elements in Output Used by a Downstream Component ('Injection'), where the product constructs all or part of a command, data structure, or record using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify how it is parsed or interpreted when it is sent to a downstream component. In RestrictedPython, software state validation or input boundaries are circumvented during processing. Review the official source advisory statement below for specific affected components, classes, and reproduction details.

Source Advisory Statement

### Impact 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. ### Patches The issue will be fixed in 5.4 and 6.2. ### Workarounds There are no workarounds to fix the issue without upgrading. ### References * https://docs.python.org/3/library/stdtypes.html#str.format_map * http://lucumr.pocoo.org/2016/12/29/careful-with-str-format/ * https://www.exploit-db.com/exploits/51580 ### For more information If you have any questions or comments about this advisory: * Open an issue in the [RestrictedPython issue tracker](https://github.com/zopefoundation/RestrictedPython/issues) * Email us at [security@plone.org](mailto:security@plone.org) ### Credits Thanks for analysing and reporting the go to: * Abhishek Govindarasu * Ankush Menat * Ward Theunisse

CVE-2023-41039 Affected RestrictedPython Versions

2 component(s)
Fixed Versions:
5.46.2
Product / Package Ecosystem Affected Versions Fixed Version
RestrictedPython
PyPI
< 5.4
5.4
RestrictedPython
PyPI
≥ 6.0
< 6.2
6.2

References

Frequently Asked Questions

What is CVE-2023-41039?
CVE-2023-41039 is a security vulnerability in RestrictedPython involving Sandbox escape via various forms of "format". under CWE-74: Improper Neutralization of Special Elements in Output Used by a Downstream Component ('Injection').
Which RestrictedPython versions are affected?
Affected versions include releases prior to 5.4, 6.2 across affected packages: RestrictedPython.
What version fixes CVE-2023-41039?
Upgrading to 5.4, 6.2 (or the latest patched release for your respective branch) resolves the vulnerability.
Is RestrictedPython affected?
Yes, RestrictedPython is affected in releases prior to 5.4, 6.2. Upgrade your dependency to 5.4, 6.2 or later.
What is the CVSS score of CVE-2023-41039?
CVE-2023-41039 has a CVSS v3.1 base score of 8.3 (HIGH) with vector string CVSS:3.1/AV:N/AC:L/PR:H/UI:R/S:C/C:H/I:H/A:L.
What CWE is associated with CVE-2023-41039?
CVE-2023-41039 is classified under CWE-74: Improper Neutralization of Special Elements in Output Used by a Downstream Component ('Injection') (The product constructs all or part of a command, data structure, or record using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify how it is parsed or interpreted when it is sent to a downstream component.).
How can I check PyPI dependencies for CVE-2023-41039?
Run the dependency tree command: 'pip show RestrictedPython'. This command lists direct and transitive dependencies in your build configuration so you can verify if a vulnerable version is resolved.
Machine-readable OSV JSON OSV v1.5 Schema
Normalized schema output
{
  "schema_version" : "1.4.0",
  "id" : "GHSA-xjw2-6jm9-rf67",
  "modified" : "2023-08-30T20:47:58Z",
  "published" : "2023-08-30T20:47:58Z",
  "aliases" : [ "CVE-2023-41039" ],
  "summary" : "Sandbox escape via various forms of \"format\".",
  "details" : "### Impact\nPython'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.\nWith `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`.\nAll known versions of `RestrictedPython` are vulnerable. \n\n### Patches\nThe issue will be fixed in 5.4 and 6.2.\n\n### Workarounds\nThere are no workarounds to fix the issue without upgrading.\n\n### References\n* https://docs.python.org/3/library/stdtypes.html#str.format_map\n* http://lucumr.pocoo.org/2016/12/29/careful-with-str-format/\n* https://www.exploit-db.com/exploits/51580\n\n### For more information\n\nIf you have any questions or comments about this advisory:\n\n* Open an issue in the [RestrictedPython issue tracker](https://github.com/zopefoundation/RestrictedPython/issues)\n* Email us at [security@plone.org](mailto:security@plone.org)\n\n### Credits\n\nThanks for analysing and reporting the go to:\n\n* Abhishek Govindarasu\n* Ankush Menat\n* Ward Theunisse\n\n",
  "severity" : [ {
    "type" : "CVSS_V3",
    "score" : "CVSS:3.1/AV:N/AC:L/PR:H/UI:R/S:C/C:H/I:H/A:L"
  } ],
  "affected" : [ {
    "package" : {
      "ecosystem" : "PyPI",
      "name" : "RestrictedPython"
    },
    "ranges" : [ {
      "type" : "ECOSYSTEM",
      "events" : [ {
        "introduced" : "0"
      }, {
        "fixed" : "5.4"
      } ]
    } ],
    "database_specific" : {
      "last_known_affected_version_range" : "<= 5.3"
    }
  }, {
    "package" : {
      "ecosystem" : "PyPI",
      "name" : "RestrictedPython"
    },
    "ranges" : [ {
      "type" : "ECOSYSTEM",
      "events" : [ {
        "introduced" : "6.0"
      }, {
        "fixed" : "6.2"
      } ]
    } ],
    "database_specific" : {
      "last_known_affected_version_range" : "<= 6.1"
    }
  } ],
  "references" : [ {
    "type" : "WEB",
    "url" : "https://github.com/zopefoundation/RestrictedPython/security/advisories/GHSA-xjw2-6jm9-rf67"
  }, {
    "type" : "ADVISORY",
    "url" : "https://nvd.nist.gov/vuln/detail/CVE-2023-41039"
  }, {
    "type" : "WEB",
    "url" : "https://github.com/zopefoundation/RestrictedPython/commit/4134aedcff17c977da7717693ed89ce56d54c120"
  }, {
    "type" : "PACKAGE",
    "url" : "https://github.com/zopefoundation/RestrictedPython"
  } ],
  "database_specific" : {
    "cwe_ids" : [ "CWE-74" ],
    "severity" : "HIGH",
    "github_reviewed" : true,
    "github_reviewed_at" : "2023-08-30T20:47:58Z",
    "nvd_published_at" : "2023-08-30T18:15:09Z"
  }
}