473,569 Members | 2,436 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

injecting functions into a python sandbox within a python program

I have a large Python 2.5 program that I want my users to be able to
"extend" using a Python script. However, I want their script to run in a
sandbox within the overall program so that they only have access to the
rest of the program via a single simple interface. Note that this is not
meant to be a real anti-hacker type security sandbox - just "help them
to avoid shooting themselves in the foot" type security.

So I created a single object that has the interface that I want them to
access. I call their script via "exec" passing the single interface
object in the "globals" parameter to exec. It (conceptually) looks like
this:

i = Interface()
glob = { 'i': i }
exec script in glob

Then they can call i.whatever() from within their script. This all works
fine.

Now, what I want to do is provide some "helper" functions for them to
use in the script. These functions still only access the rest of the
program via 'i'. They just wrap some of the interface functions to make
life easier for the user. My current solution is to prepend these
functions onto the start of the script. I.e.

helperFuncs = """
def f1(): i.whatever()
"""

exec helperFuncs + "\n" + script.read() in glob

This works but doesn't seem very elegant.

I've tried defining the helper funcions in my caller and passing them
through the globals i.e.

def f1(): i.whatever()
glob = { 'i': i, 'f1': f1 }
exec script in glob

The problem here is that the functions don't have access to the global
variable 'i'. I need to use that object instance since it has other
functionality that is required to interface to the rest of the program.

I'm sure that this is simple to get around for a Python expert (which I
am not!). Does anybody have any ideas? Any alternate approach?

Thanks in advance for any assistance,
Graham
Jan 9 '07 #1
2 2105
Graham Menhennitt a écrit :
I have a large Python 2.5 program that I want my users to be able to
"extend" using a Python script. However, I want their script to run in a
sandbox within the overall program so that they only have access to the
rest of the program via a single simple interface. Note that this is not
meant to be a real anti-hacker type security sandbox - just "help them
to avoid shooting themselves in the foot" type security.

So I created a single object that has the interface that I want them to
access. I call their script via "exec" passing the single interface
object in the "globals" parameter to exec. It (conceptually) looks like
this:

i = Interface()
glob = { 'i': i }
exec script in glob

Then they can call i.whatever() from within their script. This all works
fine.

Now, what I want to do is provide some "helper" functions for them to
use in the script. These functions still only access the rest of the
program via 'i'. They just wrap some of the interface functions to make
life easier for the user. My current solution is to prepend these
functions onto the start of the script. I.e.

helperFuncs = """
def f1(): i.whatever()
"""

exec helperFuncs + "\n" + script.read() in glob

This works but doesn't seem very elegant.
Indeed.
If all your helper functions are really methods of the Interface
instance, you may try this instead (NB : not tested):

glob = {
'i': i,
'f1': i.whatever,
}
exec script in glob

HTH
Jan 9 '07 #2
Bruno Desthuilliers wrote:
If all your helper functions are really methods of the Interface
instance, you may try this instead (NB : not tested):

glob = {
'i': i,
'f1': i.whatever,
}
exec script in glob
Bruno,

Thanks for replying.

Some of the functions are as simple as that so I can do as you
described. Some are more complicated so it doesn't work. For now I'll
stick with the prepending.

Thanks,
Graham
Jan 11 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3328
by: hokiegal99 | last post by:
A few questions about the following code. How would I "wrap" this in a function, and do I need to? Also, how can I make the code smart enough to realize that when a file has 2 or more bad charcters in it, that the code needs to run until all bad characters are gone? For example, if a file has the name "<bad*mac\file" the program has to run...
99
5851
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less important changes, so in this thread I would like to focus on that issue alone. I have edited the Proposed Syntax example below to take out the...
4
2617
by: Edmond Rusjan | last post by:
Hi All, I'd like to use Python-2.3.4 on OSF1 V4.0, but have trouble installing. With a plain "./configure; make" build, I cannot import socket. If I uncomment the socketmodule in Modules/Setup, the build fails. Using gcc 3.4.2. Had no problem on OSF1 V5.1, Linux and Sun. Have not been able to find reference to a similar problem in...
13
3995
by: Rolf Magnus | last post by:
Hi, I would like to embed a python interpreter within a program, but since that program would be able to automatically download scripts from the internet, I'd like to run those in a restricted environment, which basically means that I want to allow only a specific set of modules to be used by the scripts, so that it wouldn't be possible for...
7
12476
by: Rune Strand | last post by:
What would it take to create a Firefox extension that enables Python as a script language in the browser - just like Javascript? Is it at all possible? Are the hundred good reasons not to bother? I once made an application that used MozPython. It was fun and very fast compared to the Mod_Python I eventually replaced it with. I had to,...
31
2359
by: Fredrik Tolf | last post by:
Hi List! I was thinking about secure Python code execution, and I'd really appreciate some comments from those who know Python better than I do. I was thinking that maybe it could be possible to load and run untrusted Python code, simply by loading it in a module with a modified version of __builtins__. Without any reachable function that...
1
1907
by: elcron | last post by:
Hi, I'm using python 2.5 on Vista and writing a Genetic Program in Python. I was wondering is there a way to run a script from within another one but in sandbox environment? If there is can it automatically kill the script if it runs too long? I know you can use the exec() function but it can override variables in the current script and could get...
1
2622
by: Adem24 | last post by:
How would one program a sandbox? Let's say the server and all clients have the same architecture, and environment (for example 32bit x86-Linux environment). The server shall offer sandbox services to clients. A remote client can get a sandbox (an empty directory space) on the server, upload his precompiled executable program, start it, and...
4
1138
by: Peter Waller | last post by:
Dear Pythoners, I know this will probably be perceived as 'evil voodoo', and fair enough: it probably is. I guess it is unpythonic. ... but I want to know how to do it anyway - mostly for my own interest. Consider the following snippet of code:
0
7703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7618
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8138
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7679
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6287
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5223
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3657
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2117
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.