English 中文(简体)
Possible to sandbox Python configuration file?
原标题:

I m thinking of implementing a configuration file written in Python syntax, not unlike what Django does.

While I ve seen one or two SO questions about the merits of using executable code in configuration files, I m curious whether there is a way to execute the config file code in a "sandbox" to prevent mistakes in the code from locking up the host application.

Because the host application is a programmer s tool, I m not concerned about teaching Python syntax or introducing security holes as mentioned in at least one other SO question. But I am worried about the configuration code branching to Fishkill and wedging the host app. I d much rather that the host app trap those problems and display diagnostic error information.

Has anyone tried this sort of sandboxing for a Python configuration file? And, if so, what techniques proved useful, and what pitfalls cropped up that I should be aware of?

最佳回答

We do this for some of our internal tools

What we do protects us from exception issues and discourages any attempts by the users to get overly creative in the config scripts. However it doesn t protect us from infinite loops or actively malicious third parties.

The core of the approach here is to run the script in a locked down exec.

  1. First we go through the __ builtin __ module and del everything we don t want them to be able to touch, especially __ import __. We actually do this in a context manager which backs the original values up and dels them on the way in and then restores the original values on the way back out.

  2. Next we create an empty dictionary to be the config scripts namespace.

  3. Then we exec the config with the namespace.

  4. The exec is of course wrapped in a try except that will catch anything.

  5. And finally we inspect the namespace to extract the variables we are interested in.

Points to note here:

  1. It might be tempting to prepopulate the namespace with stuff that might be useful to the config script, but you want to be very careful doing that you quickly open up hooks back into the host program.

  2. The config scripts can still create functions and classes so you might get back something that looks like a string for example, but is actually an arbitrary blob of executable code.

Because of these we impose the restriction that our config scripts are expected to produce pure primitive data structures (generally just ints, strings, lists, tuples and None) that we then separately verify.

问题回答

Unfortunately there isn t a lot you can do about this issue with standard Python. When the Python interpreter is running the "configuration code" that code can do whatever it likes including accessing the host program or not returning control. Running the configuration code in a separate process might help but also limits the interaction between the host and config code.

Your best bet would be to check out the PyPy project s sandbox feature. This might be what you need but may also involve quite a bit of work on your part to integrate.

Is there an alternative to rexec for Python sandboxing? also discusses this topic.

You should probably also ask yourself how important this problem actually is to you. I guess that depends on your use case and who s going to be writing the configuration code.





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签