Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python, safe, sandbox [duplicate]

Tags:

python

sandbox

I'd like to make a website where people could upload their Python scripts. Of course I'd like to execute those scripts. Those scripts should do some interesting work. The problem is that people could upload scripts that could harm my server and I'd like to prevent that. What is the option to run arbitrary scripts without harming my system - actually without seeing my system at all? Thank you

like image 932
maliperica Avatar asked Sep 10 '10 22:09

maliperica


2 Answers

"Can't be done."

Running arbitrary (untrusted) scripts and staying safe is a contradiction. You should go as far as using custom kernels, jails, vms, the like.

You can look at how http://codepad.org/about does it, it's a lot of work.

like image 80
supakeen Avatar answered Oct 20 '22 15:10

supakeen


I dont know in earlier versions, in Python 3 you can create functions with access to a custom scope through types.FunctionType.

def f():
  return __builtins__

f() # this will work because it has access to __builtins__
scope = {}
sandboxed = FunctionType(f.__code__,scope)
sandboxed()  # will throw NameError, builtins is not defined

the returned function has only access to whatever you supplied in the scope dictionary. I wonder if still there are hacks around this.

like image 30
andres Avatar answered Oct 20 '22 15:10

andres