Embedding Python in Python

Benjamin Niemann b.niemann at betternet.de
Thu Aug 19 05:27:31 EDT 2004


Well it seems that this is impossible to do with the current Python. But 
  it is a feature that would be important for certain applications. 
Actually I've been searching for this, too - and only found 
abandoned/deprecated modules.

If you want to use the current Python interpreter to execute the code, 
you'd have to remove many language features, because they could provide 
a backdoor for malicous code. This could be done by defining a grammar 
for a subset of Python (perhaps with some semantic checks), and verify 
that the code satisfies the grammar before you feed it into eval(). This 
could either be easy (resulting in a small subset of Python that is 
probably too small for real use...), or difficult (resulting in a usable 
subset, but with a large amount of complex grammar rules - with at least 
one rule that introduces a security leak...).

A good solution has to be implemented in the Python interpreter. Are 
there any plans for future versions of Python? I've seen the phrase 
"security initiative" on this list. Was that a "there is a ..." or 
"there should be a ..."? I couldn't find anything on the web (but didn't 
search very deep).

My first idea:

- extend the C-API (alternative to Py_Initialize??) for embedding Python 
to provide a 'stripped down' interpreter: no builtins with sideeffects 
(like open()...), ...
I don't know anything about Pythons internals or embedding Python, so I 
can say, if this is easy or possible at all.

- communication of the embedded script to the outside world (file or 
network I/O...) must be provided by the hosting application that is 
responsible for enforcing the desired security limitations.

- wrap it into a Python module. Then you can start the isolated embedded 
Python from 'real' Python code.

The interesting (and most difficult) thing is, which part of Pythons 
standard library relies on "dangerous" features. This could drastically 
reduce the usability of this approach (until you build your own 'secure' 
library).
Using this model, the secure interpreter is running in the same process 
context as the unsecure host. A bug in python could result in unchecked 
access to resources of the host. For higher security a separate process 
should be started.



More information about the Python-list mailing list