views:

70

answers:

2

Is there a way to limit the abilities of python scripts running under an embedded interpretor? Specifically I wish to prevent the scripts from doing things like the following:

  • Importing python extension modules (ie .pyd modules), except those specifically allowed by the application.
  • Manipulating processes in any way (ie starting new processes, or terminating the application).
  • Any kind of networking.
  • Manipulating the file system (eg creating, modifying and deleting files).
+2  A: 

No. There's no easy way to prevent those things on CPython. Your options are:

  1. Edit CPython source code and remove things you don't want - provide mocking methods for all those things. Very error-prone and hard to do. This is the approach of Google's App Engine.
  2. Use Restricted Python. However, with it you can't prevent your user from exhausting the memory available or running infinite eat-all-cpu loops.
  3. Use another python implementation. PyPy has a sandbox mode you can use. Jython runs under java and I guess java can be sandboxed.
nosklo
Well I'm not to worried about scripts crashing/freezing the application, more when a hostile plug-in say downloads and executes a file from the internet, changes system settings, etc. I guess I could edit the CPython implementation, for some things I guess just not compiling the module to start with will be suitable, and if I guess I could hook the module loader to check a whitelist of safe pyd modules.Is there any info around somewhere on simply removing modules/objects from the python compile?
Fire Lancer
A: 

Maybe this can be helpful. You have an example provided on how to work with the ast.

Geo
"ACCESS DENIED", I really hate colleges internet restrictions...will check this later when I get home.
Fire Lancer