Oops -- Lib/Test should be Lib/test, of course!
[python/dscho.git] / Doc / lib / librexec.tex
blobbaddece119a65e0ca1a7434fe7b70dc6fd1260a2
1 \section{Standard Module \module{rexec}}
2 \label{module-rexec}
3 \stmodindex{rexec}
6 This module contains the \class{RExec} class, which supports
7 \method{r_exec()}, \method{r_eval()}, \method{r_execfile()}, and
8 \method{r_import()} methods, which are restricted versions of the standard
9 Python functions \method{exec()}, \method{eval()}, \method{execfile()}, and
10 the \keyword{import} statement.
11 Code executed in this restricted environment will
12 only have access to modules and functions that are deemed safe; you
13 can subclass \class{RExec} to add or remove capabilities as desired.
15 \emph{Note:} The \class{RExec} class can prevent code from performing
16 unsafe operations like reading or writing disk files, or using TCP/IP
17 sockets. However, it does not protect against code using extremely
18 large amounts of memory or CPU time.
20 \begin{classdesc}{RExec}{\optional{hooks\optional{, verbose}}}
21 Returns an instance of the \class{RExec} class.
23 \var{hooks} is an instance of the \class{RHooks} class or a subclass of it.
24 If it is omitted or \code{None}, the default \class{RHooks} class is
25 instantiated.
26 Whenever the \module{RExec} module searches for a module (even a
27 built-in one) or reads a module's code, it doesn't actually go out to
28 the file system itself. Rather, it calls methods of an \class{RHooks}
29 instance that was passed to or created by its constructor. (Actually,
30 the \class{RExec} object doesn't make these calls --- they are made by
31 a module loader object that's part of the \class{RExec} object. This
32 allows another level of flexibility, e.g. using packages.)
34 By providing an alternate \class{RHooks} object, we can control the
35 file system accesses made to import a module, without changing the
36 actual algorithm that controls the order in which those accesses are
37 made. For instance, we could substitute an \class{RHooks} object that
38 passes all filesystem requests to a file server elsewhere, via some
39 RPC mechanism such as ILU. Grail's applet loader uses this to support
40 importing applets from a URL for a directory.
42 If \var{verbose} is true, additional debugging output may be sent to
43 standard output.
44 \end{classdesc}
46 The \class{RExec} class has the following class attributes, which are
47 used by the \method{__init__()} method. Changing them on an existing
48 instance won't have any effect; instead, create a subclass of
49 \class{RExec} and assign them new values in the class definition.
50 Instances of the new class will then use those new values. All these
51 attributes are tuples of strings.
53 \begin{memberdesc}{nok_builtin_names}
54 Contains the names of built-in functions which will \emph{not} be
55 available to programs running in the restricted environment. The
56 value for \class{RExec} is \code{('open',} \code{'reload',}
57 \code{'__import__')}. (This gives the exceptions, because by far the
58 majority of built-in functions are harmless. A subclass that wants to
59 override this variable should probably start with the value from the
60 base class and concatenate additional forbidden functions --- when new
61 dangerous built-in functions are added to Python, they will also be
62 added to this module.)
63 \end{memberdesc}
65 \begin{memberdesc}{ok_builtin_modules}
66 Contains the names of built-in modules which can be safely imported.
67 The value for \class{RExec} is \code{('audioop',} \code{'array',}
68 \code{'binascii',} \code{'cmath',} \code{'errno',} \code{'imageop',}
69 \code{'marshal',} \code{'math',} \code{'md5',} \code{'operator',}
70 \code{'parser',} \code{'regex',} \code{'rotor',} \code{'select',}
71 \code{'strop',} \code{'struct',} \code{'time')}. A similar remark
72 about overriding this variable applies --- use the value from the base
73 class as a starting point.
74 \end{memberdesc}
76 \begin{memberdesc}{ok_path}
77 Contains the directories which will be searched when an \keyword{import}
78 is performed in the restricted environment.
79 The value for \class{RExec} is the same as \code{sys.path} (at the time
80 the module is loaded) for unrestricted code.
81 \end{memberdesc}
83 \begin{memberdesc}{ok_posix_names}
84 % Should this be called ok_os_names?
85 Contains the names of the functions in the \module{os} module which will be
86 available to programs running in the restricted environment. The
87 value for \class{RExec} is \code{('error',} \code{'fstat',}
88 \code{'listdir',} \code{'lstat',} \code{'readlink',} \code{'stat',}
89 \code{'times',} \code{'uname',} \code{'getpid',} \code{'getppid',}
90 \code{'getcwd',} \code{'getuid',} \code{'getgid',} \code{'geteuid',}
91 \code{'getegid')}.
92 \end{memberdesc}
94 \begin{memberdesc}{ok_sys_names}
95 Contains the names of the functions and variables in the \module{sys}
96 module which will be available to programs running in the restricted
97 environment. The value for \class{RExec} is \code{('ps1',}
98 \code{'ps2',} \code{'copyright',} \code{'version',} \code{'platform',}
99 \code{'exit',} \code{'maxint')}.
100 \end{memberdesc}
103 \class{RExec} instances support the following methods:
105 \begin{methoddesc}{r_eval}{code}
106 \var{code} must either be a string containing a Python expression, or
107 a compiled code object, which will be evaluated in the restricted
108 environment's \module{__main__} module. The value of the expression or
109 code object will be returned.
110 \end{methoddesc}
112 \begin{methoddesc}{r_exec}{code}
113 \var{code} must either be a string containing one or more lines of
114 Python code, or a compiled code object, which will be executed in the
115 restricted environment's \module{__main__} module.
116 \end{methoddesc}
118 \begin{methoddesc}{r_execfile}{filename}
119 Execute the Python code contained in the file \var{filename} in the
120 restricted environment's \module{__main__} module.
121 \end{methoddesc}
123 Methods whose names begin with \samp{s_} are similar to the functions
124 beginning with \samp{r_}, but the code will be granted access to
125 restricted versions of the standard I/O streams \code{sys.stdin},
126 \code{sys.stderr}, and \code{sys.stdout}.
128 \begin{methoddesc}{s_eval}{code}
129 \var{code} must be a string containing a Python expression, which will
130 be evaluated in the restricted environment.
131 \end{methoddesc}
133 \begin{methoddesc}{s_exec}{code}
134 \var{code} must be a string containing one or more lines of Python code,
135 which will be executed in the restricted environment.
136 \end{methoddesc}
138 \begin{methoddesc}{s_execfile}{code}
139 Execute the Python code contained in the file \var{filename} in the
140 restricted environment.
141 \end{methoddesc}
143 \class{RExec} objects must also support various methods which will be
144 implicitly called by code executing in the restricted environment.
145 Overriding these methods in a subclass is used to change the policies
146 enforced by a restricted environment.
148 \begin{methoddesc}{r_import}{modulename\optional{, globals\optional{,
149 locals\optional{, fromlist}}}}
150 Import the module \var{modulename}, raising an \exception{ImportError}
151 exception if the module is considered unsafe.
152 \end{methoddesc}
154 \begin{methoddesc}{r_open}{filename\optional{, mode\optional{, bufsize}}}
155 Method called when \function{open()} is called in the restricted
156 environment. The arguments are identical to those of \function{open()},
157 and a file object (or a class instance compatible with file objects)
158 should be returned. \class{RExec}'s default behaviour is allow opening
159 any file for reading, but forbidding any attempt to write a file. See
160 the example below for an implementation of a less restrictive
161 \method{r_open()}.
162 \end{methoddesc}
164 \begin{methoddesc}{r_reload}{module}
165 Reload the module object \var{module}, re-parsing and re-initializing it.
166 \end{methoddesc}
168 \begin{methoddesc}{r_unload}{module}
169 Unload the module object \var{module} (i.e., remove it from the
170 restricted environment's \code{sys.modules} dictionary).
171 \end{methoddesc}
173 And their equivalents with access to restricted standard I/O streams:
175 \begin{methoddesc}{s_import}{modulename\optional{, globals\optional{,
176 locals\optional{, fromlist}}}}
177 Import the module \var{modulename}, raising an \exception{ImportError}
178 exception if the module is considered unsafe.
179 \end{methoddesc}
181 \begin{methoddesc}{s_reload}{module}
182 Reload the module object \var{module}, re-parsing and re-initializing it.
183 \end{methoddesc}
185 \begin{methoddesc}{s_unload}{module}
186 Unload the module object \var{module}.
187 % XXX what are the semantics of this?
188 \end{methoddesc}
190 \subsection{An example}
192 Let us say that we want a slightly more relaxed policy than the
193 standard \class{RExec} class. For example, if we're willing to allow
194 files in \file{/tmp} to be written, we can subclass the \class{RExec}
195 class:
197 \begin{verbatim}
198 class TmpWriterRExec(rexec.RExec):
199 def r_open(self, file, mode='r', buf=-1):
200 if mode in ('r', 'rb'):
201 pass
202 elif mode in ('w', 'wb', 'a', 'ab'):
203 # check filename : must begin with /tmp/
204 if file[:5]!='/tmp/':
205 raise IOError, "can't write outside /tmp"
206 elif (string.find(file, '/../') >= 0 or
207 file[:3] == '../' or file[-3:] == '/..'):
208 raise IOError, "'..' in filename forbidden"
209 else: raise IOError, "Illegal open() mode"
210 return open(file, mode, buf)
211 \end{verbatim}
213 Notice that the above code will occasionally forbid a perfectly valid
214 filename; for example, code in the restricted environment won't be
215 able to open a file called \file{/tmp/foo/../bar}. To fix this, the
216 \method{r_open()} method would have to simplify the filename to
217 \file{/tmp/bar}, which would require splitting apart the filename and
218 performing various operations on it. In cases where security is at
219 stake, it may be preferable to write simple code which is sometimes
220 overly restrictive, instead of more general code that is also more
221 complex and may harbor a subtle security hole.