Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 159 lines (145 sloc) 5.334 kB
f12596d create interpreter.py
Victor Stinner authored
1 from __future__ import with_statement
2 from code import InteractiveConsole
c81c4e5 @haypo Drop the interpreter feature
authored
3 from sandbox import Sandbox, SandboxConfig, HAVE_PYPY
759a663 @haypo fix interpreter.py if readline module is missing
authored
4 try:
5 import readline
6 except ImportError:
7 pass
99c462a add command line options to execfile.py and interpreter.py
Victor Stinner authored
8 from optparse import OptionParser
aafb31a interpreter.py: display the version
Victor Stinner authored
9 from sandbox.version import VERSION
65b7be7 interpreter.py: create --quiet option
Victor Stinner authored
10 import sys
5894e4b Disable utf_32_be for Python 2.5
Victor Stinner authored
11 from sys import version_info
f12596d create interpreter.py
Victor Stinner authored
12
a1e3463 Fix interpreter.py for unicode on Python 2.5
Victor Stinner authored
13 def getEncoding():
14 encoding = sys.stdin.encoding
15 if encoding:
16 return encoding
17 # sys.stdin.encoding is None if stdin is not a tty
18
19 # Emulate PYTHONIOENCODING on Python < 2.6
20 import os
21 env = os.getenv('PYTHONIOENCODING')
22 if env:
23 return env.split(':',1)[0]
24
25 # Fallback to locales
26 import locale
27 return locale.getpreferredencoding()
28
29 ENCODING = getEncoding()
30
50e1fe2 interpreter.py: Backport Python 2.6 fix for input encoding
Victor Stinner authored
31 class SandboxConsole(InteractiveConsole):
32 # Backport Python 2.6 fix for input encoding
33 def raw_input(self, prompt):
34 line = InteractiveConsole.raw_input(self, prompt)
a1e3463 Fix interpreter.py for unicode on Python 2.5
Victor Stinner authored
35 if not isinstance(line, unicode):
36 line = line.decode(ENCODING)
50e1fe2 interpreter.py: Backport Python 2.6 fix for input encoding
Victor Stinner authored
37 return line
38
6722778 interpreter.py: convert the code to a class
Victor Stinner authored
39 class SandboxedInterpeter:
40 def __init__(self):
a4cd8be interpreter: define a displayhook to get the magical '_' variable
Victor Stinner authored
41 self.sandbox_locals = None
c81c4e5 @haypo Drop the interpreter feature
authored
42 self.options = self.parseOptions()
43 self.config = self.createConfig()
65b7be7 interpreter.py: create --quiet option
Victor Stinner authored
44 self.stdout = sys.stdout
a4cd8be interpreter: define a displayhook to get the magical '_' variable
Victor Stinner authored
45
6722778 interpreter.py: convert the code to a class
Victor Stinner authored
46 def parseOptions(self):
47 parser = OptionParser(usage="%prog [options]")
426207d @haypo Reintroduce --timeout command line option
authored
48 SandboxConfig.createOptparseOptions(parser, default_timeout=None)
c81c4e5 @haypo Drop the interpreter feature
authored
49 parser.add_option("--debug",
50 help="Debug mode",
51 action="store_true", default=False)
4e20402 Add timeout attribute (default: 5 seconds)
Victor Stinner authored
52 parser.add_option("--verbose", "-v",
53 help="Verbose mode",
6722778 interpreter.py: convert the code to a class
Victor Stinner authored
54 action="store_true", default=False)
65b7be7 interpreter.py: create --quiet option
Victor Stinner authored
55 parser.add_option("--quiet", "-q",
56 help="Quiet mode",
57 action="store_true", default=False)
6722778 interpreter.py: convert the code to a class
Victor Stinner authored
58 options, argv = parser.parse_args()
59 if argv:
60 parser.print_help()
61 exit(1)
65b7be7 interpreter.py: create --quiet option
Victor Stinner authored
62 if options.quiet:
63 options.verbose = False
c81c4e5 @haypo Drop the interpreter feature
authored
64 return options
65b7be7 interpreter.py: create --quiet option
Victor Stinner authored
65
c81c4e5 @haypo Drop the interpreter feature
authored
66 def createConfig(self):
67 config = SandboxConfig.fromOptparseOptions(self.options)
68 config.enable('traceback')
69 config.enable('stdin')
70 config.enable('stdout')
71 config.enable('stderr')
72 config.enable('exit')
73 config.enable('site')
74 config.enable('encodings')
75 config._builtins_whitelist.add('compile')
76 config.allowModuleSourceCode('code')
77 config.allowModule('sys',
78 'api_version', 'version', 'hexversion')
79 config.allowSafeModule('sys', 'version_info')
80 if HAVE_PYPY:
81 config.enable('unicodedata')
82 config.allowModule('os', 'write', 'waitpid')
83 config.allowSafeModule('pyrepl', 'input')
84 config.allowModule('pyrepl.keymap', 'compile_keymap', 'parse_keys')
85 if self.options.debug:
86 config.allowModule('sys', '_getframe')
87 config.allowSafeModule('_sandbox', '_test_crash')
88 config.allowModuleSourceCode('sandbox')
89 if not config.cpython_restricted:
90 config.allowPath(__file__)
91 return config
6722778 interpreter.py: convert the code to a class
Victor Stinner authored
92
93 def dumpConfig(self):
c81c4e5 @haypo Drop the interpreter feature
authored
94 if self.options.debug:
6722778 interpreter.py: convert the code to a class
Victor Stinner authored
95 from pprint import pprint
96 print "Sandbox config:"
97 pprint(self.config.__dict__)
98 else:
a8cdc6f interact.py: use Sandbox.call()
Victor Stinner authored
99 features = ', '.join(sorted(self.config.features))
100 print "Enabled features: %s" % features
2c8d19f interpreter.py: fix config dump
Victor Stinner authored
101 if self.config.cpython_restricted:
102 print "CPython restricted mode enabled."
b0bd2ec @haypo interpreter: write subprocess in the config dump
authored
103 if self.config.use_subprocess:
c81c4e5 @haypo Drop the interpreter feature
authored
104 text = "Run untrusted code in a subprocess"
105 if self.options.debug:
106 from os import getpid
107 text += ": pid=%s" % getpid()
108 print(text)
1ee3c35 help feature is no more actived by interpreter feature
Victor Stinner authored
109 if 'help' not in self.config.features:
110 print "(use --features=help to enable the help function)"
6722778 interpreter.py: convert the code to a class
Victor Stinner authored
111 print
112
a4cd8be interpreter: define a displayhook to get the magical '_' variable
Victor Stinner authored
113 def displayhook(self, result):
114 if result is None:
115 return
116 self.sandbox_locals['_'] = result
65b7be7 interpreter.py: create --quiet option
Victor Stinner authored
117 text = repr(result)
118 if not self.options.quiet:
119 print(text)
120 else:
121 self.stdout.write(text)
a4cd8be interpreter: define a displayhook to get the magical '_' variable
Victor Stinner authored
122
a8cdc6f interact.py: use Sandbox.call()
Victor Stinner authored
123 def interact(self):
50e1fe2 interpreter.py: Backport Python 2.6 fix for input encoding
Victor Stinner authored
124 console = SandboxConsole()
a4cd8be interpreter: define a displayhook to get the magical '_' variable
Victor Stinner authored
125 self.sandbox_locals = console.locals
65b7be7 interpreter.py: create --quiet option
Victor Stinner authored
126 if not self.options.quiet:
127 banner = "Try to break the sandbox!"
128 else:
129 banner = ''
130 console.interact(banner)
a8cdc6f interact.py: use Sandbox.call()
Victor Stinner authored
131
6722778 interpreter.py: convert the code to a class
Victor Stinner authored
132 def main(self):
65b7be7 interpreter.py: create --quiet option
Victor Stinner authored
133 if not self.options.quiet:
134 print("pysandbox %s" % VERSION)
135 self.dumpConfig()
7d8aa48 (Re)Create "help" feature, enabled by "interpreter"
Victor Stinner authored
136 if 'help' in self.config.features:
4e20402 Add timeout attribute (default: 5 seconds)
Victor Stinner authored
137 # Import pydoc here because it uses a lot of modules
7d8aa48 (Re)Create "help" feature, enabled by "interpreter"
Victor Stinner authored
138 # blocked by the sandbox
139 import pydoc
0389a72 Improve usage of CPython restricted mode
Victor Stinner authored
140 if self.config.cpython_restricted:
141 # Import is blocked in restricted mode, so preload modules
871cd93 interpreter.py: preload also ENCODING code
Victor Stinner authored
142 import codecs
0389a72 Improve usage of CPython restricted mode
Victor Stinner authored
143 import encodings
144 import encodings.utf_8
145 import encodings.utf_16_be
5894e4b Disable utf_32_be for Python 2.5
Victor Stinner authored
146 if version_info >= (2, 6):
147 import encodings.utf_32_be
0389a72 Improve usage of CPython restricted mode
Victor Stinner authored
148 if sys.stdout.encoding:
149 codecs.lookup(sys.stdout.encoding)
871cd93 interpreter.py: preload also ENCODING code
Victor Stinner authored
150 codecs.lookup(ENCODING)
4ed0787 interpreter.py: change the prompt
Victor Stinner authored
151 sys.ps1 = '\nsandbox>>> '
152 sys.ps2 = '.......... '
a4cd8be interpreter: define a displayhook to get the magical '_' variable
Victor Stinner authored
153 sys.displayhook = self.displayhook
a8cdc6f interact.py: use Sandbox.call()
Victor Stinner authored
154 sandbox = Sandbox(self.config)
155 sandbox.call(self.interact)
6722778 interpreter.py: convert the code to a class
Victor Stinner authored
156
157 if __name__ == "__main__":
158 SandboxedInterpeter().main()
Something went wrong with that request. Please try again.