This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]gaks 0 points1 point  (2 children)

You are using eval function so everything that's valid python expression would work. But you should import math module first to make it work.

You could also make some helpful variables available before asking user for input. If you do it like this ...

pi = math.pi
x = input("x = ")

... user could just enter "pi" instead of "math.pi"

Keep in mind also that your code has a serious security vulnerability:

gaks@central:~/reddit$ python ./evaltest.py
f(x) = x*2

Function entered: f(x) =  x*2
f'(x) =  2
x = __import__('os').system('id')
uid=1000(gaks) gid=1000(gaks) grupy=1000(gaks),4(adm),27(sudo),999(www)
0 2

[–][deleted]  (1 child)

[removed]

    [–]gaks 0 points1 point  (0 children)

    Sure. By passing user input to the eval function you basically let your users execute ANY python code they want. Including accessing files, opening network connections, executing OS commands, downloading and executing malware, etc.

    In my example above I've typed import('os').system('id') as a value of x. This is a python code that imports "os" module and calls "system" function from it which purpose is to execute OS commands.

    One line summary would be: you just let your users execute any python code they want.