Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions news/fix-symop-eval.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* <news item>

**Changed:**

* <news item>

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* <news item>

**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``.
36 changes: 35 additions & 1 deletion src/diffpy/structure/parsers/p_cif.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
Expand Down
108 changes: 108 additions & 0 deletions tests/test_p_cif.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""Unit tests for diffpy.structure.parsers.p_cif module."""

import unittest
from pathlib import Path

import numpy
import pytest
Expand Down Expand Up @@ -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__":
Expand Down
Loading