Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Forbid Lambda expressions in ImageMath.eval() #5963

Closed
Closed
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
5 changes: 3 additions & 2 deletions Tests/test_imagemath.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ def test_ops():
assert pixel(ImageMath.eval("float(B)**33", images)) == "F 8589934592.0"


def test_prevent_exec():
@pytest.mark.parametrize("expression", ("exec('pass')", "(lambda: None)()"))
def test_prevent_exec(expression):
with pytest.raises(ValueError):
ImageMath.eval("exec('pass')")
ImageMath.eval(expression)


def test_logical():
Expand Down
3 changes: 3 additions & 0 deletions src/PIL/ImageMath.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ def eval(expression, _dict={}, **kw):
for name in code.co_names:
if name not in args and name != "abs":
raise ValueError(f"'{name}' not allowed")
for const in code.co_consts:
if getattr(const, "co_name", None) == "<lambda>":
raise ValueError("Lambda expressions are not allowed")

out = builtins.eval(expression, {"__builtins": {"abs": abs}}, args)
try:
Expand Down