<p>I'm looking for a "safe" eval function, to implement spreadsheet-like calculations (using numpy/scipy).</p>

<p>The functionality to do this (the <a href="http://docs.python.org/lib/module-rexec.html" rel="nofollow noreferrer">rexec module</a>) has been removed from Python since 2.3 due to apparently unfixable security problems. There are several third-party hacks out there that purport to do this - the most thought-out solution that I have found is 
<a href="http://code.activestate.com/recipes/496746/" rel="nofollow noreferrer">this Python Cookbok recipe</a>, "safe_eval". </p>

<p>Am I reasonably safe if I use this (or something similar), to protect from malicious code, or am I stuck with writing my own parser? Does anyone know of any better alternatives?</p>

<p>EDIT: I just discovered <a href="http://pypi.python.org/pypi/RestrictedPython" rel="nofollow noreferrer">RestrictedPython</a>, which is part of Zope. Any opinions on this are  welcome.</p>

[{'answer_id': 28436, 'author': 'pix0r', 'author_id': 72, 'author_profile': 'https://Stackoverflow.com/users/72', 'pm_score': 2, 'selected': False, 'text': "<p>Writing your own parser could be fun!  It might be a better option because people are expecting to use the familiar spreadsheet syntax (Excel, etc) and not Python when they're entering formulas.  I'm not familiar with safe_eval but I would imagine that anything like this certainly has the potential for exploitation.</p>\n"}, {'answer_id': 29390, 'author': 'dwestbrook', 'author_id': 3119, 'author_profile': 'https://Stackoverflow.com/users/3119', 'pm_score': 1, 'selected': False, 'text': "<p>Although that code looks quite secure, I've always held the opinion that any sufficiently motivated person could break it given adequate time. I do think it will take quite a bit of determination to get through that, but I'm relatively sure it could be done.</p>\n"}, {'answer_id': 31964, 'author': 'John Douthat', 'author_id': 2774, 'author_profile': 'https://Stackoverflow.com/users/2774', 'pm_score': 0, 'selected': False, 'text': '<p>Daniel,\n<a href="http://jinja.pocoo.org/2/documentation/intro" rel="nofollow noreferrer">Jinja</a> implements a sandboxe environment that may or may not be useful to you. From what I remember, it doesn\'t yet "comprehend" list comprehensions. </p>\n\n<p><a href="http://jinja.pocoo.org/2/documentation/sandbox" rel="nofollow noreferrer">Sanbox info</a> </p>\n'}, {'answer_id': 32028, 'author': 'Aaron Maenpaa', 'author_id': 2603, 'author_profile': 'https://Stackoverflow.com/users/2603', 'pm_score': 3, 'selected': True, 'text': '<p>Depends on your definition of safe I suppose. A lot of the security depends on what you pass in and what you are allowed to pass in the context. For instance, if a file is passed in, I can open arbitrary files:</p>\n\n<pre><code>&gt;&gt;&gt; names[\'f\'] = open(\'foo\', \'w+\')\n&gt;&gt;&gt; safe_eval.safe_eval("baz = type(f)(\'baz\', \'w+\')", names)\n&gt;&gt;&gt; names[\'baz\']\n&lt;open file \'baz\', mode \'w+\' at 0x413da0&gt;\n</code></pre>\n\n<p>Furthermore, the environment is very restricted (you cannot pass in modules), thus, you can\'t simply pass in a module of utility functions like re or random.</p>\n\n<p>On the other hand, you don\'t need to write your own parser, you could just write your own evaluator for the python ast:</p>\n\n<pre><code>&gt;&gt;&gt; import compiler\n&gt;&gt;&gt; ast = compiler.parse("print \'Hello world!\'")\n</code></pre>\n\n<p>That way, hopefully, you could implement safe imports. The other idea is to use Jython or IronPython and take advantage of Java/.Net sandboxing capabilities.</p>\n'}, {'answer_id': 6909802, 'author': 'Philippe F', 'author_id': 13618, 'author_profile': 'https://Stackoverflow.com/users/13618', 'pm_score': 2, 'selected': False, 'text': '<p>If you simply need to write down and read some data structure in Python, and don\'t need the actual capacity of executing custom code, this one is a better fit:\n<a href="http://code.activestate.com/recipes/364469-safe-eval/" rel="nofollow">http://code.activestate.com/recipes/364469-safe-eval/</a></p>\n\n<p>It garantees that no code is executed, only static data structures are evaluated: strings, lists, tuples, dictionnaries.</p>\n'}, {'answer_id': 8765435, 'author': 'user405', 'author_id': 929623, 'author_profile': 'https://Stackoverflow.com/users/929623', 'pm_score': 0, 'selected': False, 'text': '<p>The functionality you want is in the compiler language services, see\n<a href="http://docs.python.org/library/language.html" rel="nofollow">http://docs.python.org/library/language.html</a>\nIf you define your app to accept only expressions, you can compile the input as an expression and get an exception if it is not, e.g. if there are semicolons or statement forms.</p>\n'}]
['https://Stackoverflow.com/questions/28369', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3002/']