Fred Drake | 3a0351c | 1998-04-04 07:23:21 +0000 | [diff] [blame^] | 1 | \section{Standard Module \module{rexec}} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 2 | \label{module-rexec} |
Guido van Rossum | be0a8a6 | 1996-09-10 17:37:05 +0000 | [diff] [blame] | 3 | \stmodindex{rexec} |
Fred Drake | a891230 | 1998-03-14 07:08:02 +0000 | [diff] [blame] | 4 | |
Guido van Rossum | be0a8a6 | 1996-09-10 17:37:05 +0000 | [diff] [blame] | 5 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 6 | This module contains the \class{RExec} class, which supports |
Fred Drake | a891230 | 1998-03-14 07:08:02 +0000 | [diff] [blame] | 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. |
Guido van Rossum | f73f79b | 1996-10-24 22:14:06 +0000 | [diff] [blame] | 11 | Code executed in this restricted environment will |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 12 | only have access to modules and functions that are deemed safe; you |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 13 | can subclass \class{RExec} to add or remove capabilities as desired. |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 14 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 15 | \emph{Note:} The \class{RExec} class can prevent code from performing |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 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. |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 19 | |
Fred Drake | a891230 | 1998-03-14 07:08:02 +0000 | [diff] [blame] | 20 | \begin{classdesc}{RExec}{\optional{hooks\optional{, verbose}}} |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 21 | Returns an instance of the \class{RExec} class. |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 22 | |
Fred Drake | a891230 | 1998-03-14 07:08:02 +0000 | [diff] [blame] | 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 |
Guido van Rossum | f73f79b | 1996-10-24 22:14:06 +0000 | [diff] [blame] | 25 | instantiated. |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 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.) |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 33 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 34 | By providing an alternate \class{RHooks} object, we can control the |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 35 | file system accesses made to import a module, without changing the |
| 36 | actual algorithm that controls the order in which those accesses are |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 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 |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 40 | importing applets from a URL for a directory. |
| 41 | |
Guido van Rossum | f73f79b | 1996-10-24 22:14:06 +0000 | [diff] [blame] | 42 | If \var{verbose} is true, additional debugging output may be sent to |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 43 | standard output. |
Fred Drake | a891230 | 1998-03-14 07:08:02 +0000 | [diff] [blame] | 44 | \end{classdesc} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 45 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 46 | The \class{RExec} class has the following class attributes, which are |
Fred Drake | a891230 | 1998-03-14 07:08:02 +0000 | [diff] [blame] | 47 | used by the \method{__init__()} method. Changing them on an existing |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 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. |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 52 | |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 53 | \begin{memberdesc}{nok_builtin_names} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 54 | Contains the names of built-in functions which will \emph{not} be |
Guido van Rossum | f73f79b | 1996-10-24 22:14:06 +0000 | [diff] [blame] | 55 | available to programs running in the restricted environment. The |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 56 | value for \class{RExec} is \code{('open',} \code{'reload',} |
Guido van Rossum | f73f79b | 1996-10-24 22:14:06 +0000 | [diff] [blame] | 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.) |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 63 | \end{memberdesc} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 64 | |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 65 | \begin{memberdesc}{ok_builtin_modules} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 66 | Contains the names of built-in modules which can be safely imported. |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 67 | The value for \class{RExec} is \code{('audioop',} \code{'array',} |
Guido van Rossum | f73f79b | 1996-10-24 22:14:06 +0000 | [diff] [blame] | 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. |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 74 | \end{memberdesc} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 75 | |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 76 | \begin{memberdesc}{ok_path} |
Fred Drake | a891230 | 1998-03-14 07:08:02 +0000 | [diff] [blame] | 77 | Contains the directories which will be searched when an \keyword{import} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 78 | is performed in the restricted environment. |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 79 | The value for \class{RExec} is the same as \code{sys.path} (at the time |
Guido van Rossum | f73f79b | 1996-10-24 22:14:06 +0000 | [diff] [blame] | 80 | the module is loaded) for unrestricted code. |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 81 | \end{memberdesc} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 82 | |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 83 | \begin{memberdesc}{ok_posix_names} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 84 | % Should this be called ok_os_names? |
Fred Drake | a891230 | 1998-03-14 07:08:02 +0000 | [diff] [blame] | 85 | Contains the names of the functions in the \module{os} module which will be |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 86 | available to programs running in the restricted environment. The |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 87 | value for \class{RExec} is \code{('error',} \code{'fstat',} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 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')}. |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 92 | \end{memberdesc} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 93 | |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 94 | \begin{memberdesc}{ok_sys_names} |
Fred Drake | a891230 | 1998-03-14 07:08:02 +0000 | [diff] [blame] | 95 | Contains the names of the functions and variables in the \module{sys} |
Guido van Rossum | f73f79b | 1996-10-24 22:14:06 +0000 | [diff] [blame] | 96 | module which will be available to programs running in the restricted |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 97 | environment. The value for \class{RExec} is \code{('ps1',} |
Guido van Rossum | f73f79b | 1996-10-24 22:14:06 +0000 | [diff] [blame] | 98 | \code{'ps2',} \code{'copyright',} \code{'version',} \code{'platform',} |
| 99 | \code{'exit',} \code{'maxint')}. |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 100 | \end{memberdesc} |
| 101 | |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 102 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 103 | \class{RExec} instances support the following methods: |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 104 | |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 105 | \begin{methoddesc}{r_eval}{code} |
Guido van Rossum | f73f79b | 1996-10-24 22:14:06 +0000 | [diff] [blame] | 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 |
Fred Drake | a891230 | 1998-03-14 07:08:02 +0000 | [diff] [blame] | 108 | environment's \module{__main__} module. The value of the expression or |
Guido van Rossum | f73f79b | 1996-10-24 22:14:06 +0000 | [diff] [blame] | 109 | code object will be returned. |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 110 | \end{methoddesc} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 111 | |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 112 | \begin{methoddesc}{r_exec}{code} |
Guido van Rossum | f73f79b | 1996-10-24 22:14:06 +0000 | [diff] [blame] | 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 |
Fred Drake | a891230 | 1998-03-14 07:08:02 +0000 | [diff] [blame] | 115 | restricted environment's \module{__main__} module. |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 116 | \end{methoddesc} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 117 | |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 118 | \begin{methoddesc}{r_execfile}{filename} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 119 | Execute the Python code contained in the file \var{filename} in the |
Fred Drake | a891230 | 1998-03-14 07:08:02 +0000 | [diff] [blame] | 120 | restricted environment's \module{__main__} module. |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 121 | \end{methoddesc} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 122 | |
Fred Drake | a891230 | 1998-03-14 07:08:02 +0000 | [diff] [blame] | 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 |
Fred Drake | 71f894a | 1998-02-23 14:37:40 +0000 | [diff] [blame] | 125 | restricted versions of the standard I/O streams \code{sys.stdin}, |
Fred Drake | a891230 | 1998-03-14 07:08:02 +0000 | [diff] [blame] | 126 | \code{sys.stderr}, and \code{sys.stdout}. |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 127 | |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 128 | \begin{methoddesc}{s_eval}{code} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 129 | \var{code} must be a string containing a Python expression, which will |
| 130 | be evaluated in the restricted environment. |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 131 | \end{methoddesc} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 132 | |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 133 | \begin{methoddesc}{s_exec}{code} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 134 | \var{code} must be a string containing one or more lines of Python code, |
| 135 | which will be executed in the restricted environment. |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 136 | \end{methoddesc} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 137 | |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 138 | \begin{methoddesc}{s_execfile}{code} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 139 | Execute the Python code contained in the file \var{filename} in the |
| 140 | restricted environment. |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 141 | \end{methoddesc} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 142 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 143 | \class{RExec} objects must also support various methods which will be |
Guido van Rossum | f73f79b | 1996-10-24 22:14:06 +0000 | [diff] [blame] | 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. |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 147 | |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 148 | \begin{methoddesc}{r_import}{modulename\optional{, globals\optional{, |
| 149 | locals\optional{, fromlist}}}} |
Fred Drake | a891230 | 1998-03-14 07:08:02 +0000 | [diff] [blame] | 150 | Import the module \var{modulename}, raising an \exception{ImportError} |
Guido van Rossum | f73f79b | 1996-10-24 22:14:06 +0000 | [diff] [blame] | 151 | exception if the module is considered unsafe. |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 152 | \end{methoddesc} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 153 | |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 154 | \begin{methoddesc}{r_open}{filename\optional{, mode\optional{, bufsize}}} |
Fred Drake | a891230 | 1998-03-14 07:08:02 +0000 | [diff] [blame] | 155 | Method called when \function{open()} is called in the restricted |
| 156 | environment. The arguments are identical to those of \function{open()}, |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 157 | and a file object (or a class instance compatible with file objects) |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 158 | should be returned. \class{RExec}'s default behaviour is allow opening |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 159 | any file for reading, but forbidding any attempt to write a file. See |
Guido van Rossum | f73f79b | 1996-10-24 22:14:06 +0000 | [diff] [blame] | 160 | the example below for an implementation of a less restrictive |
Fred Drake | a891230 | 1998-03-14 07:08:02 +0000 | [diff] [blame] | 161 | \method{r_open()}. |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 162 | \end{methoddesc} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 163 | |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 164 | \begin{methoddesc}{r_reload}{module} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 165 | Reload the module object \var{module}, re-parsing and re-initializing it. |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 166 | \end{methoddesc} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 167 | |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 168 | \begin{methoddesc}{r_unload}{module} |
Guido van Rossum | f73f79b | 1996-10-24 22:14:06 +0000 | [diff] [blame] | 169 | Unload the module object \var{module} (i.e., remove it from the |
| 170 | restricted environment's \code{sys.modules} dictionary). |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 171 | \end{methoddesc} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 172 | |
Guido van Rossum | f73f79b | 1996-10-24 22:14:06 +0000 | [diff] [blame] | 173 | And their equivalents with access to restricted standard I/O streams: |
| 174 | |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 175 | \begin{methoddesc}{s_import}{modulename\optional{, globals\optional{, |
| 176 | locals\optional{, fromlist}}}} |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 177 | Import the module \var{modulename}, raising an \exception{ImportError} |
Guido van Rossum | f73f79b | 1996-10-24 22:14:06 +0000 | [diff] [blame] | 178 | exception if the module is considered unsafe. |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 179 | \end{methoddesc} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 180 | |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 181 | \begin{methoddesc}{s_reload}{module} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 182 | Reload the module object \var{module}, re-parsing and re-initializing it. |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 183 | \end{methoddesc} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 184 | |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 185 | \begin{methoddesc}{s_unload}{module} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 186 | Unload the module object \var{module}. |
| 187 | % XXX what are the semantics of this? |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 188 | \end{methoddesc} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 189 | |
| 190 | \subsection{An example} |
| 191 | |
| 192 | Let us say that we want a slightly more relaxed policy than the |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 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: |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 196 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 197 | \begin{verbatim} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 198 | class TmpWriterRExec(rexec.RExec): |
| 199 | def r_open(self, file, mode='r', buf=-1): |
Guido van Rossum | f73f79b | 1996-10-24 22:14:06 +0000 | [diff] [blame] | 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" |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 210 | return open(file, mode, buf) |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 211 | \end{verbatim} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 212 | % |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 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 |
Fred Drake | a891230 | 1998-03-14 07:08:02 +0000 | [diff] [blame] | 216 | \method{r_open()} method would have to simplify the filename to |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 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. |