mirror of
				https://github.com/python-pillow/Pillow.git
				synced 2025-11-04 01:47:47 +03:00 
			
		
		
		
	Merge pull request #6009 from radarhere/eval
Restrict builtins within lambdas for ImageMath.eval
This commit is contained in:
		
						commit
						f84ab3bb8a
					
				| 
						 | 
					@ -52,9 +52,17 @@ def test_ops():
 | 
				
			||||||
    assert pixel(ImageMath.eval("float(B)**33", images)) == "F 8589934592.0"
 | 
					    assert pixel(ImageMath.eval("float(B)**33", images)) == "F 8589934592.0"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def test_prevent_exec():
 | 
					@pytest.mark.parametrize(
 | 
				
			||||||
 | 
					    "expression",
 | 
				
			||||||
 | 
					    (
 | 
				
			||||||
 | 
					        "exec('pass')",
 | 
				
			||||||
 | 
					        "(lambda: exec('pass'))()",
 | 
				
			||||||
 | 
					        "(lambda: (lambda: exec('pass'))())()",
 | 
				
			||||||
 | 
					    ),
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					def test_prevent_exec(expression):
 | 
				
			||||||
    with pytest.raises(ValueError):
 | 
					    with pytest.raises(ValueError):
 | 
				
			||||||
        ImageMath.eval("exec('pass')")
 | 
					        ImageMath.eval(expression)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def test_logical():
 | 
					def test_logical():
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -240,11 +240,18 @@ def eval(expression, _dict={}, **kw):
 | 
				
			||||||
        if hasattr(v, "im"):
 | 
					        if hasattr(v, "im"):
 | 
				
			||||||
            args[k] = _Operand(v)
 | 
					            args[k] = _Operand(v)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    code = compile(expression, "<string>", "eval")
 | 
					    compiled_code = compile(expression, "<string>", "eval")
 | 
				
			||||||
    for name in code.co_names:
 | 
					 | 
				
			||||||
        if name not in args and name != "abs":
 | 
					 | 
				
			||||||
            raise ValueError(f"'{name}' not allowed")
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def scan(code):
 | 
				
			||||||
 | 
					        for const in code.co_consts:
 | 
				
			||||||
 | 
					            if type(const) == type(compiled_code):
 | 
				
			||||||
 | 
					                scan(const)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        for name in code.co_names:
 | 
				
			||||||
 | 
					            if name not in args and name != "abs":
 | 
				
			||||||
 | 
					                raise ValueError(f"'{name}' not allowed")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    scan(compiled_code)
 | 
				
			||||||
    out = builtins.eval(expression, {"__builtins": {"abs": abs}}, args)
 | 
					    out = builtins.eval(expression, {"__builtins": {"abs": abs}}, args)
 | 
				
			||||||
    try:
 | 
					    try:
 | 
				
			||||||
        return out.im
 | 
					        return out.im
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user