Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{rexec} --- |
Fred Drake | ffbe687 | 1999-04-22 21:23:22 +0000 | [diff] [blame] | 2 | Restricted execution framework} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | |
Fred Drake | ffbe687 | 1999-04-22 21:23:22 +0000 | [diff] [blame] | 4 | \declaremodule{standard}{rexec} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 5 | \modulesynopsis{Basic restricted execution framework.} |
| 6 | |
Fred Drake | a891230 | 1998-03-14 07:08:02 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | be0a8a6 | 1996-09-10 17:37:05 +0000 | [diff] [blame] | 8 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 9 | This module contains the \class{RExec} class, which supports |
Guido van Rossum | 7b0c9d8 | 1998-05-08 13:27:38 +0000 | [diff] [blame] | 10 | \method{r_eval()}, \method{r_execfile()}, \method{r_exec()}, and |
Fred Drake | a891230 | 1998-03-14 07:08:02 +0000 | [diff] [blame] | 11 | \method{r_import()} methods, which are restricted versions of the standard |
Guido van Rossum | 7b0c9d8 | 1998-05-08 13:27:38 +0000 | [diff] [blame] | 12 | Python functions \method{eval()}, \method{execfile()} and |
| 13 | the \keyword{exec} and \keyword{import} statements. |
Guido van Rossum | f73f79b | 1996-10-24 22:14:06 +0000 | [diff] [blame] | 14 | Code executed in this restricted environment will |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 15 | only have access to modules and functions that are deemed safe; you |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 16 | can subclass \class{RExec} to add or remove capabilities as desired. |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 17 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 18 | \emph{Note:} The \class{RExec} class can prevent code from performing |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 19 | unsafe operations like reading or writing disk files, or using TCP/IP |
| 20 | sockets. However, it does not protect against code using extremely |
Fred Drake | 8ee679f | 2001-07-14 02:50:55 +0000 | [diff] [blame] | 21 | large amounts of memory or processor time. |
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 | \begin{classdesc}{RExec}{\optional{hooks\optional{, verbose}}} |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 24 | Returns an instance of the \class{RExec} class. |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 25 | |
Fred Drake | a891230 | 1998-03-14 07:08:02 +0000 | [diff] [blame] | 26 | \var{hooks} is an instance of the \class{RHooks} class or a subclass of it. |
| 27 | 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] | 28 | instantiated. |
Fred Drake | ffbe687 | 1999-04-22 21:23:22 +0000 | [diff] [blame] | 29 | Whenever the \module{rexec} module searches for a module (even a |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 30 | built-in one) or reads a module's code, it doesn't actually go out to |
| 31 | the file system itself. Rather, it calls methods of an \class{RHooks} |
| 32 | instance that was passed to or created by its constructor. (Actually, |
| 33 | the \class{RExec} object doesn't make these calls --- they are made by |
| 34 | a module loader object that's part of the \class{RExec} object. This |
Fred Drake | 907e76b | 2001-07-06 20:30:11 +0000 | [diff] [blame] | 35 | allows another level of flexibility, which can be useful when changing |
| 36 | the mechanics of \keyword{import} within the restricted environment.) |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 37 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 38 | By providing an alternate \class{RHooks} object, we can control the |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 39 | file system accesses made to import a module, without changing the |
| 40 | actual algorithm that controls the order in which those accesses are |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 41 | made. For instance, we could substitute an \class{RHooks} object that |
| 42 | passes all filesystem requests to a file server elsewhere, via some |
| 43 | 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] | 44 | importing applets from a URL for a directory. |
| 45 | |
Guido van Rossum | f73f79b | 1996-10-24 22:14:06 +0000 | [diff] [blame] | 46 | 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] | 47 | standard output. |
Fred Drake | a891230 | 1998-03-14 07:08:02 +0000 | [diff] [blame] | 48 | \end{classdesc} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 49 | |
Fred Drake | 307cb05 | 2001-06-22 18:21:53 +0000 | [diff] [blame] | 50 | It is important to be aware that code running in a restricted |
| 51 | environment can still call the \function{sys.exit()} function. To |
| 52 | disallow restricted code from exiting the interpreter, always protect |
| 53 | calls that cause restricted code to run with a |
| 54 | \keyword{try}/\keyword{except} statement that catches the |
| 55 | \exception{SystemExit} exception. Removing the \function{sys.exit()} |
| 56 | function from the restricted environment is not sufficient --- the |
| 57 | restricted code could still use \code{raise SystemExit}. Removing |
| 58 | \exception{SystemExit} is not a reasonable option; some library code |
| 59 | makes use of this and would break were it not available. |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 60 | |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 61 | |
Fred Drake | 307cb05 | 2001-06-22 18:21:53 +0000 | [diff] [blame] | 62 | \begin{seealso} |
| 63 | \seetitle[http://grail.sourceforge.net/]{Grail Home Page}{Grail is a |
| 64 | Web browser written entirely in Python. It uses the |
| 65 | \module{rexec} module as a foundation for supporting |
| 66 | Python applets, and can be used as an example usage of |
| 67 | this module.} |
| 68 | \end{seealso} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 69 | |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 70 | |
Fred Drake | 307cb05 | 2001-06-22 18:21:53 +0000 | [diff] [blame] | 71 | \subsection{RExec Objects \label{rexec-objects}} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 72 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 73 | \class{RExec} instances support the following methods: |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 74 | |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 75 | \begin{methoddesc}{r_eval}{code} |
Guido van Rossum | f73f79b | 1996-10-24 22:14:06 +0000 | [diff] [blame] | 76 | \var{code} must either be a string containing a Python expression, or |
| 77 | a compiled code object, which will be evaluated in the restricted |
Fred Drake | a891230 | 1998-03-14 07:08:02 +0000 | [diff] [blame] | 78 | environment's \module{__main__} module. The value of the expression or |
Guido van Rossum | f73f79b | 1996-10-24 22:14:06 +0000 | [diff] [blame] | 79 | code object will be returned. |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 80 | \end{methoddesc} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 81 | |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 82 | \begin{methoddesc}{r_exec}{code} |
Guido van Rossum | f73f79b | 1996-10-24 22:14:06 +0000 | [diff] [blame] | 83 | \var{code} must either be a string containing one or more lines of |
| 84 | 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] | 85 | restricted environment's \module{__main__} module. |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 86 | \end{methoddesc} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 87 | |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 88 | \begin{methoddesc}{r_execfile}{filename} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 89 | Execute the Python code contained in the file \var{filename} in the |
Fred Drake | a891230 | 1998-03-14 07:08:02 +0000 | [diff] [blame] | 90 | restricted environment's \module{__main__} module. |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 91 | \end{methoddesc} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 92 | |
Fred Drake | a891230 | 1998-03-14 07:08:02 +0000 | [diff] [blame] | 93 | Methods whose names begin with \samp{s_} are similar to the functions |
| 94 | beginning with \samp{r_}, but the code will be granted access to |
Fred Drake | 71f894a | 1998-02-23 14:37:40 +0000 | [diff] [blame] | 95 | restricted versions of the standard I/O streams \code{sys.stdin}, |
Fred Drake | a891230 | 1998-03-14 07:08:02 +0000 | [diff] [blame] | 96 | \code{sys.stderr}, and \code{sys.stdout}. |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 97 | |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 98 | \begin{methoddesc}{s_eval}{code} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 99 | \var{code} must be a string containing a Python expression, which will |
| 100 | be evaluated in the restricted environment. |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 101 | \end{methoddesc} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 102 | |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 103 | \begin{methoddesc}{s_exec}{code} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 104 | \var{code} must be a string containing one or more lines of Python code, |
| 105 | which will be executed in the restricted environment. |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 106 | \end{methoddesc} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 107 | |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 108 | \begin{methoddesc}{s_execfile}{code} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 109 | Execute the Python code contained in the file \var{filename} in the |
| 110 | restricted environment. |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 111 | \end{methoddesc} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 112 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 113 | \class{RExec} objects must also support various methods which will be |
Guido van Rossum | f73f79b | 1996-10-24 22:14:06 +0000 | [diff] [blame] | 114 | implicitly called by code executing in the restricted environment. |
| 115 | Overriding these methods in a subclass is used to change the policies |
| 116 | enforced by a restricted environment. |
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_import}{modulename\optional{, globals\optional{, |
| 119 | locals\optional{, fromlist}}}} |
Fred Drake | a891230 | 1998-03-14 07:08:02 +0000 | [diff] [blame] | 120 | Import the module \var{modulename}, raising an \exception{ImportError} |
Guido van Rossum | f73f79b | 1996-10-24 22:14:06 +0000 | [diff] [blame] | 121 | exception if the module is considered unsafe. |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 122 | \end{methoddesc} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 123 | |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 124 | \begin{methoddesc}{r_open}{filename\optional{, mode\optional{, bufsize}}} |
Fred Drake | a891230 | 1998-03-14 07:08:02 +0000 | [diff] [blame] | 125 | Method called when \function{open()} is called in the restricted |
| 126 | environment. The arguments are identical to those of \function{open()}, |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 127 | and a file object (or a class instance compatible with file objects) |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 128 | should be returned. \class{RExec}'s default behaviour is allow opening |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 129 | 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] | 130 | the example below for an implementation of a less restrictive |
Fred Drake | a891230 | 1998-03-14 07:08:02 +0000 | [diff] [blame] | 131 | \method{r_open()}. |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 132 | \end{methoddesc} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 133 | |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 134 | \begin{methoddesc}{r_reload}{module} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 135 | Reload the module object \var{module}, re-parsing and re-initializing it. |
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}{r_unload}{module} |
Fred Drake | 907e76b | 2001-07-06 20:30:11 +0000 | [diff] [blame] | 139 | Unload the module object \var{module} (remove it from the |
Guido van Rossum | f73f79b | 1996-10-24 22:14:06 +0000 | [diff] [blame] | 140 | restricted environment's \code{sys.modules} dictionary). |
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 | |
Guido van Rossum | f73f79b | 1996-10-24 22:14:06 +0000 | [diff] [blame] | 143 | And their equivalents with access to restricted standard I/O streams: |
| 144 | |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 145 | \begin{methoddesc}{s_import}{modulename\optional{, globals\optional{, |
| 146 | locals\optional{, fromlist}}}} |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 147 | Import the module \var{modulename}, raising an \exception{ImportError} |
Guido van Rossum | f73f79b | 1996-10-24 22:14:06 +0000 | [diff] [blame] | 148 | exception if the module is considered unsafe. |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 149 | \end{methoddesc} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 150 | |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 151 | \begin{methoddesc}{s_reload}{module} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 152 | Reload the module object \var{module}, re-parsing and re-initializing it. |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 153 | \end{methoddesc} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 154 | |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 155 | \begin{methoddesc}{s_unload}{module} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 156 | Unload the module object \var{module}. |
| 157 | % XXX what are the semantics of this? |
Fred Drake | da70ee1 | 1998-04-02 18:51:30 +0000 | [diff] [blame] | 158 | \end{methoddesc} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 159 | |
Fred Drake | 307cb05 | 2001-06-22 18:21:53 +0000 | [diff] [blame] | 160 | |
| 161 | \subsection{Defining restricted environments \label{rexec-extension}} |
| 162 | |
| 163 | The \class{RExec} class has the following class attributes, which are |
| 164 | used by the \method{__init__()} method. Changing them on an existing |
| 165 | instance won't have any effect; instead, create a subclass of |
| 166 | \class{RExec} and assign them new values in the class definition. |
| 167 | Instances of the new class will then use those new values. All these |
| 168 | attributes are tuples of strings. |
| 169 | |
| 170 | \begin{memberdesc}{nok_builtin_names} |
| 171 | Contains the names of built-in functions which will \emph{not} be |
| 172 | available to programs running in the restricted environment. The |
| 173 | value for \class{RExec} is \code{('open', 'reload', '__import__')}. |
| 174 | (This gives the exceptions, because by far the majority of built-in |
| 175 | functions are harmless. A subclass that wants to override this |
| 176 | variable should probably start with the value from the base class and |
| 177 | concatenate additional forbidden functions --- when new dangerous |
| 178 | built-in functions are added to Python, they will also be added to |
| 179 | this module.) |
| 180 | \end{memberdesc} |
| 181 | |
| 182 | \begin{memberdesc}{ok_builtin_modules} |
| 183 | Contains the names of built-in modules which can be safely imported. |
| 184 | The value for \class{RExec} is \code{('audioop', 'array', 'binascii', |
| 185 | 'cmath', 'errno', 'imageop', 'marshal', 'math', 'md5', 'operator', |
| 186 | 'parser', 'regex', 'rotor', 'select', 'sha', '_sre', 'strop', |
| 187 | 'struct', 'time')}. A similar remark about overriding this variable |
| 188 | applies --- use the value from the base class as a starting point. |
| 189 | \end{memberdesc} |
| 190 | |
| 191 | \begin{memberdesc}{ok_path} |
| 192 | Contains the directories which will be searched when an \keyword{import} |
| 193 | is performed in the restricted environment. |
| 194 | The value for \class{RExec} is the same as \code{sys.path} (at the time |
| 195 | the module is loaded) for unrestricted code. |
| 196 | \end{memberdesc} |
| 197 | |
| 198 | \begin{memberdesc}{ok_posix_names} |
| 199 | % Should this be called ok_os_names? |
| 200 | Contains the names of the functions in the \refmodule{os} module which will be |
| 201 | available to programs running in the restricted environment. The |
| 202 | value for \class{RExec} is \code{('error', 'fstat', 'listdir', |
| 203 | 'lstat', 'readlink', 'stat', 'times', 'uname', 'getpid', 'getppid', |
| 204 | 'getcwd', 'getuid', 'getgid', 'geteuid', 'getegid')}. |
| 205 | \end{memberdesc} |
| 206 | |
| 207 | \begin{memberdesc}{ok_sys_names} |
| 208 | Contains the names of the functions and variables in the \refmodule{sys} |
| 209 | module which will be available to programs running in the restricted |
| 210 | environment. The value for \class{RExec} is \code{('ps1', 'ps2', |
| 211 | 'copyright', 'version', 'platform', 'exit', 'maxint')}. |
| 212 | \end{memberdesc} |
| 213 | |
| 214 | |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 215 | \subsection{An example} |
| 216 | |
| 217 | 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] | 218 | standard \class{RExec} class. For example, if we're willing to allow |
| 219 | files in \file{/tmp} to be written, we can subclass the \class{RExec} |
| 220 | class: |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 221 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 222 | \begin{verbatim} |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 223 | class TmpWriterRExec(rexec.RExec): |
| 224 | def r_open(self, file, mode='r', buf=-1): |
Guido van Rossum | f73f79b | 1996-10-24 22:14:06 +0000 | [diff] [blame] | 225 | if mode in ('r', 'rb'): |
| 226 | pass |
| 227 | elif mode in ('w', 'wb', 'a', 'ab'): |
| 228 | # check filename : must begin with /tmp/ |
| 229 | if file[:5]!='/tmp/': |
| 230 | raise IOError, "can't write outside /tmp" |
| 231 | elif (string.find(file, '/../') >= 0 or |
| 232 | file[:3] == '../' or file[-3:] == '/..'): |
| 233 | raise IOError, "'..' in filename forbidden" |
| 234 | else: raise IOError, "Illegal open() mode" |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 235 | return open(file, mode, buf) |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 236 | \end{verbatim} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 237 | % |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 238 | Notice that the above code will occasionally forbid a perfectly valid |
| 239 | filename; for example, code in the restricted environment won't be |
| 240 | 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] | 241 | \method{r_open()} method would have to simplify the filename to |
Guido van Rossum | 095538d | 1996-10-22 01:11:19 +0000 | [diff] [blame] | 242 | \file{/tmp/bar}, which would require splitting apart the filename and |
| 243 | performing various operations on it. In cases where security is at |
| 244 | stake, it may be preferable to write simple code which is sometimes |
| 245 | overly restrictive, instead of more general code that is also more |
| 246 | complex and may harbor a subtle security hole. |