From 916194f341c4885af74fdb38f4553891734bf63b Mon Sep 17 00:00:00 2001 From: "Simon J.L. Billinge" Date: Thu, 19 Nov 2026 21:39:51 -0800 Subject: [PATCH] fix: p_cif.py get_symop no longer evals CIF symmetry operations, to stop code execution when loading a CIF, by parsing translations as numbers The translation part of each symmetry operation was passed to eval, so a crafted CIF file could run arbitrary Python code on load. It is now matched against a strict pattern of signed integers, decimals and fractions, and anything else raises StructureFormatError. closes #204 Co-Authored-By: Claude Fable 5.2 --- news/fix-symop-eval.rst | 23 ++++++ src/diffpy/structure/parsers/p_cif.py | 36 ++++++++- tests/test_p_cif.py | 108 ++++++++++++++++++++++++++ 3 files changed, 166 insertions(+), 1 deletion(-) create mode 100644 news/fix-symop-eval.rst diff --git a/news/fix-symop-eval.rst b/news/fix-symop-eval.rst new file mode 100644 index 00000000..5180aef7 --- /dev/null +++ b/news/fix-symop-eval.rst @@ -0,0 +1,23 @@ +**Added:** + +* + +**Changed:** + +* + +**Deprecated:** + +* + +**Removed:** + +* + +**Fixed:** + +* + +**Security:** + +* Fixed arbitrary code execution when reading a CIF file by parsing the translation part of symmetry operations in ``get_symop`` as numbers instead of passing it to ``eval``. diff --git a/src/diffpy/structure/parsers/p_cif.py b/src/diffpy/structure/parsers/p_cif.py index 652fb34c..227665c8 100644 --- a/src/diffpy/structure/parsers/p_cif.py +++ b/src/diffpy/structure/parsers/p_cif.py @@ -888,6 +888,35 @@ def leading_float(s, d=0.0): symvec["+z"] = symvec["z"] +_symop_number = r"(?:\d+\.?\d*|\.\d+)" +_symop_term = rf"{_symop_number}(?:/{_symop_number})?" +_rx_symop_translation = re.compile(rf"[+-]?{_symop_term}(?:[+-]{_symop_term})*") +_rx_symop_term = re.compile(rf"([+-]?)({_symop_number})(?:/({_symop_number}))?") + + +def _get_symop_translation(tpart, s): + # Sum the numeric terms in tpart without evaluating it as Python code. + # Only signed integers, decimals and fractions such as 1/2 are accepted. + emsg = ( + f"Cannot read the symmetry operation {s!r} because {tpart!r} is not a number or a fraction. " + "Please correct the symmetry operations in the CIF file so that each one " + "looks like 'x,1/2-y,1/2+z' and try again." + ) + if not tpart: + return 0.0 + if not _rx_symop_translation.fullmatch(tpart): + raise StructureFormatError(emsg) + translation = 0.0 + for sign, numerator, denominator in _rx_symop_term.findall(tpart): + value = float(numerator) + if denominator: + if float(denominator) == 0: + raise StructureFormatError(emsg) + value /= float(denominator) + translation += -value if sign == "-" else value + return translation + + @deprecated(getSymOp_deprecation_msg) def getSymOp(s): """This function has been deprecated and will be removed in version @@ -910,6 +939,11 @@ def get_symop(s): ------- SymOp Instance of `SymOp`. + + Raises + ------ + StructureFormatError + The translation part of the formula is not a number or a fraction. """ from diffpy.structure.spacegroups import SymOp @@ -922,7 +956,7 @@ def get_symop(s): for Rpart in eqparts[1::2]: R[i, :] += symvec[Rpart.lower()] for tpart in eqparts[::2]: - t[i] += eval("1.0*%s+0" % tpart) + t[i] += _get_symop_translation(tpart, s) t -= numpy.floor(t) rv = SymOp(R, t) return rv diff --git a/tests/test_p_cif.py b/tests/test_p_cif.py index 5784ecfd..8f2b900f 100644 --- a/tests/test_p_cif.py +++ b/tests/test_p_cif.py @@ -15,6 +15,7 @@ """Unit tests for diffpy.structure.parsers.p_cif module.""" import unittest +from pathlib import Path import numpy import pytest @@ -496,6 +497,113 @@ def test_get_parser(self): # End of class TestP_cif + +# Test that get_symop reads the rotation and translation of a symmetry +# operation formula. +# C1: fraction before and after the coordinate, expect the fractions as +# translations +# C2: mixed coordinates in one component, expect off-diagonal rotation +# C3: decimal translations with and without leading zero, expect the same +# values as floats +# C4: blanks, upper case and explicit plus signs, expect them to be ignored +# C5: negative translations, expect them wrapped into the unit cell +# C6: several numeric terms in one component, expect their sum +@pytest.mark.parametrize( + "symop_string, expected_rotation, expected_translation", + [ + pytest.param( + "x,1/2-y,1/2+z", + [[1, 0, 0], [0, -1, 0], [0, 0, 1]], + [0, 0.5, 0.5], + id="fractions", + ), + pytest.param( + "-x,-x+y,z+1/2", + [[-1, 0, 0], [-1, 1, 0], [0, 0, 1]], + [0, 0, 0.5], + id="mixed-coordinates", + ), + pytest.param( + "x+0.25,y+.5,z+1.", + [[1, 0, 0], [0, 1, 0], [0, 0, 1]], + [0.25, 0.5, 0], + id="decimals", + ), + pytest.param( + " +X, +1/4 + Y, -Z ", + [[1, 0, 0], [0, 1, 0], [0, 0, -1]], + [0, 0.25, 0], + id="blanks-case-plus-signs", + ), + pytest.param( + "x-1/4,-3/4+y,z", + [[1, 0, 0], [0, 1, 0], [0, 0, 1]], + [0.75, 0.25, 0], + id="negative-translations", + ), + pytest.param( + "1/2+x+1/4,y,z", + [[1, 0, 0], [0, 1, 0], [0, 0, 1]], + [0.75, 0, 0], + id="several-numeric-terms", + ), + ], +) +def test_get_symop_values(symop_string, expected_rotation, expected_translation): + actual_symop = get_symop(symop_string) + numpy.testing.assert_allclose(actual_symop.R, expected_rotation) + numpy.testing.assert_allclose(actual_symop.t, expected_translation) + + +# Test that get_symop rejects translations that are not plain numbers +# instead of evaluating them as Python code. +# C1: Python call in a component, expect StructureFormatError and no +# code execution +# C2: arithmetic that is not a sum of numbers, expect StructureFormatError +# C3: letters other than the coordinates, expect StructureFormatError +# C4: fraction with zero denominator, expect StructureFormatError +# The code avoids blanks, commas and the letters of the coordinates so +# that it would be executed if the formula were evaluated. +symop_code = "__import__('builtins').__dict__.update(_cif_pwned=1)" + + +@pytest.mark.parametrize( + "symop_string", + [ + pytest.param(f"x,y,{symop_code}", id="python-call"), + pytest.param("x,2**8+y,z", id="non-sum-arithmetic"), + pytest.param("x,y,z+a", id="unknown-letter"), + pytest.param("x,y+1/0,z", id="zero-denominator"), + ], +) +def test_get_symop_bad(symop_string, monkeypatch): + import builtins + + monkeypatch.delattr(builtins, "_cif_pwned", raising=False) + try: + with pytest.raises(StructureFormatError, match="Cannot read the symmetry operation"): + get_symop(symop_string) + assert not hasattr(builtins, "_cif_pwned") + finally: + vars(builtins).pop("_cif_pwned", None) + + +def test_parse_cif_does_not_execute_symop(datafile, tmp_path): + """Check that code in a CIF symmetry operation is never executed.""" + import builtins + + cif_text = Path(datafile("graphite.cif")).read_text() + assert "'x,y,z'" in cif_text + bad_cif = tmp_path / "bad.cif" + bad_cif.write_text(cif_text.replace("'x,y,z'", f'"x,y,z+{symop_code}"', 1)) + try: + with pytest.raises(StructureFormatError): + get_parser("cif").parse_file(str(bad_cif)) + assert not hasattr(builtins, "_cif_pwned") + finally: + vars(builtins).pop("_cif_pwned", None) + + # ---------------------------------------------------------------------------- if __name__ == "__main__":