I know that eval and exec are quite dangerous things, but is it possible to completely defend against possible problems? (in the case of exec - it is unlikely that everything is too problematic there, but I have hope for the safe execution of eval), here is what i have: eval (s, {'__builtins __': {}}) but I know that, for example, this can be circumvented in several ways, for example: s = """ (lambda fc=( lambda n: [ c for c in ().__class__.__bases__[0].__subclasses__() if c.__name__ == n ][0] ): fc("function")( fc("code")( 0,0,0,0,"KABOOM",(),(),(),"","",0,"" ),{} )() )() """ for brevity I will omit the details of what is happening In the example above, we used a list of all subclasses of the object class to create objects of the code and function classes. In exactly the same way, you can get (and instantiate) any class that exists in the program at the time you called eval (). Here is another example of what can be done: s = """ [ c for c in ().__class__.__bases__[0].__subclasses__() if c.__name__ == "Quitter" ][0](0)() """ When I did research on this topic, I came across the protected eval execution mode in Python, which is another attempt to overcome this problem: >>> eval("(lambda:0).func_code", {'__builtins__':{}}) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<string>", line 1, in <module> RuntimeError: function attributes not accessible in restricted mode In short, it works as follows: if __builtins__ inside eval are different from the “official” ones, eval goes into protected mode, which denies access to some dangerous properties, such as func_code for functions. in principle, you can get around the restriction using this code (here we get the "official" __builtins__): s = """[ c for c in ().__class__.__base__.__subclasses__() if c.__name__ == 'catch_warnings' ][0]()._module.__builtins__ """ my question is: is it possible to avoid the problems with eval to the end? Continue reading...