Why should exec() and eval() be avoided?

Question:

I’ve seen this multiple times in multiple places, but never have found a satisfying explanation as to why this should be the case.

So, hopefully, one will be presented here. Why should we (at least, generally) not use exec() and eval()?

EDIT: I see that people are assuming that this question pertains to web servers – it doesn’t. I can see why an unsanitized string being passed to exec could be bad. Is it bad in non-web-applications?

Asked By: Isaac

||

Answers:

Allowing these function in a context where they might run user input is a security issue, and sanitizers that actually work are hard to write.

Don’t try to do the following on your computer:

s = "import shutil; shutil.rmtree('/nonexisting')"
eval(s)

Now assume somebody can control s from a web application, for example.

Answered By: Chris

Same reason you shouldn’t login as root: it’s too easy to shoot yourself in the foot.

Answered By: Azeem.Butt

Reason #1: One security flaw (ie. programming errors… and we can’t claim those can be avoided) and you’ve just given the user access to the shell of the server.

Answered By: Rushyo

There are often clearer, more direct ways to get the same effect. If you build a complex string and pass it to exec, the code is difficult to follow, and difficult to test.

Example: I wrote code that read in string keys and values and set corresponding fields in an object. It looked like this:

for key, val in values:
    fieldName = valueToFieldName[key]
    fieldType = fieldNameToType[fieldName]
    if fieldType is int:
        s = 'object.%s = int(%s)' % (fieldName, fieldType) 
    #Many clauses like this...

exec(s)

That code isn’t too terrible for simple cases, but as new types cropped up it got more and more complex. When there were bugs they always triggered on the call to exec, so stack traces didn’t help me find them. Eventually I switched to a slightly longer, less clever version that set each field explicitly.

The first rule of code clarity is that each line of your code should be easy to understand by looking only at the lines near it. This is why goto and global variables are discouraged. exec and eval make it easy to break this rule badly.

Answered By: RossFabricant

Try this in the interactive interpreter and see what happens:

>>> import sys
>>> eval('{"name" : %s}' % ("sys.exit(1)"))

Of course, this is a corner case, but it can be tricky to prevent things like this.

Answered By: Jason Baker

Security aside, eval and exec are often marked as undesirable because of the complexity they induce. When you see a eval call you often don’t know what’s really going on behind it, because it acts on data that’s usually in a variable. This makes code harder to read.

Invoking the full power of the interpreter is a heavy weapon that should be only reserved for very tricky cases. In most cases, however, it’s best avoided and simpler tools should be employed.

That said, like all generalizations, be wary of this one. In some cases, exec and eval can be valuable. But you must have a very good reason to use them. See this post for one acceptable use.

Answered By: Eli Bendersky

eval() and exec() can promote lazy programming. More importantly it indicates the code being executed may not have been written at design time therefore not tested. In other words, how do you test dynamically generated code? Especially across browsers.

Answered By: Square Rig Master

I have used eval() in the past (and still do from time-to-time) for massaging data during quick and dirty operations. It is part of the toolkit that can be used for getting a job done, but should NEVER be used for anything you plan to use in production such as any command-line tools or scripts, because of all the reasons mentioned in the other answers.

You cannot trust your users–ever–to do the right thing. In most cases they will, but you have to expect them to do all of the things you never thought of and find all of the bugs you never expected. This is precisely where eval() goes from being a tool to a liability.

A perfect example of this would be using Django, when constructing a QuerySet. The parameters passed to a query accepts keyword arguments, that look something like this:

results = Foo.objects.filter(whatever__contains='pizza')

If you’re programmatically assigning arguments, you might think to do something like this:

results = eval("Foo.objects.filter(%s__%s=%s)" % (field, matcher, value))

But there is always a better way that doesn’t use eval(), which is passing a dictionary by reference:

results = Foo.objects.filter( **{'%s__%s' % (field, matcher): value} ) 

By doing it this way, it’s not only faster performance-wise, but also safer and more Pythonic.

Moral of the story?

Use of eval() is ok for small tasks, tests, and truly temporary things, but bad for permanent usage because there is almost certainly always a better way to do it!

Answered By: jathanism

When you need exec and eval, yeah, you really do need them.

But, the majority of the in-the-wild usage of these functions (and the similar constructs in other scripting languages) is totally inappropriate and could be replaced with other simpler constructs that are faster, more secure and have fewer bugs.

You can, with proper escaping and filtering, use exec and eval safely. But the kind of coder who goes straight for exec/eval to solve a problem (because they don’t understand the other facilities the language makes available) isn’t the kind of coder that’s going to be able to get that processing right; it’s going to be someone who doesn’t understand string processing and just blindly concatenates substrings, resulting in fragile insecure code.

It’s the Lure Of Strings. Throwing string segments around looks easy and fools naïve coders into thinking they understand what they’re doing. But experience shows the results are almost always wrong in some corner (or not-so-corner) case, often with potential security implications. This is why we say eval is evil. This is why we say regex-for-HTML is evil. This is why we push SQL parameterisation. Yes, you can get all these things right with manual string processing… but unless you already understand why we say those things, chances are you won’t.

Answered By: bobince

In contrast to what most answers are saying here, exec is actually part of the recipe for building super-complete decorators in Python, as you can duplicate everything about the decorated function exactly, producing the same signature for the purposes of documentation and such. It’s key to the functionality of the widely used decorator module (http://pypi.python.org/pypi/decorator/). Other cases where exec/eval are essential is when constructing any kind of “interpreted Python” type of application, such as a Python-parsed template language (like Mako or Jinja).

So it’s not like the presence of these functions are an immediate sign of an “insecure” application or library. Using them in the naive javascripty way to evaluate incoming JSON or something, yes that’s very insecure. But as always, its all in the way you use it and these are very essential functions.

Answered By: zzzeek
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.