Note that there are some explanatory texts on larger screens.

plurals
  1. POHow safe is expression evaluation using eval?
    primarykey
    data
    text
    <p>I am building a website where I have a need that user should be able to evaluate some expression based from the value in DB tables, instead of using tools like pyparsing etc, I am thinking of using python itself, and have come up with a solution which is sufficient for my purpose. I am basically using eval to evaluate the expression and passing globals dict with empty <code>__builtins__</code> so that nothing can be accessed and a locals dict for values from DB, if user will need some functions I can pass those too e.g.</p> <pre><code>import datetime def today(): return datetime.datetime.now() expression = """ first_name.lower() == "anurag" and today().year == 2010 """ print eval(expression, {'__builtins__':{}}, {'first_name':'Anurag', 'today':today}) </code></pre> <p>So my question is how safe it would be , I have three criteria</p> <ol> <li>Can user access current state of my program or table etc someshow?</li> <li>Can user have access to os level calls?</li> <li>Can user halt my system by looping or using much memory e.g. by doing range(10*8), in some cases he can e.g 100**1000 etc so 3 is not so much of a problem. i may check such op with tokenize and anyway I will be using GAE so it is not not much of concern.</li> </ol> <p><strong>Edit</strong>: IMO this is not the duplicate of <a href="https://stackoverflow.com/questions/661084/security-of-pythons-eval-on-untrusted-strings">Q:661084</a> because where it ends this one starts, I want to know even with <code>__builtins__</code> blocked, can user do bad things?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload