stdout in a restricted environment

Michael P. Reilly arcege at shore.net
Thu Apr 22 08:51:56 EDT 1999


Chris AtLee <catlee at globalserve.net> wrote:
: I'm trying to make a program that will enable users to connect to a server
: running a python interpreter and be able to program in a restricted
: environment in that interpreter.  The rexec object has methods called s_exec
: and s_eval which are supposed to use secure forms of the standard I/O
: streams, but I can't redefine them to be something else (specifically, an
: object that sends the response back to the client)  Any pointers on how to
: go about redirecting the standard I/O streams from a restricted environment?

I would suggest making a subclass of RExec that redefines the
make_delegate_files method.

  def __init__(self, socket, hooks = None, verbose = 0):
    RExec.__init__(self, hooks=hooks, verbose=verbose)
    self._data_socket = socket

  def make_delegate_files(self):
    reader = self._data_socket.makefile('r')
    writer = self._data_socket.makefile('w')
    s = self.modules['sys']
    self.delegate_stdin = FileDelegate(s, 'stdin')
    self.delegate_stdout = FileDelegate(s, 'stdout')
    self.delegate_stderr = FileDelegate(s, 'stderr')
    self.restricted_stdin = FileWrapper(reader)
    self.restricted_stdout = FileWrapper(writer)
    self.restricted_stderr = FileWrapper(writer)

Granted, I haven't tried this, but it looks correct. :)

Good luck.

  -Arcege





More information about the Python-list mailing list