sandbox.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. import os
  2. import sys
  3. import tempfile
  4. import operator
  5. import functools
  6. import itertools
  7. import re
  8. import contextlib
  9. import pickle
  10. import six
  11. from six.moves import builtins, map
  12. import pkg_resources
  13. if sys.platform.startswith('java'):
  14. import org.python.modules.posix.PosixModule as _os
  15. else:
  16. _os = sys.modules[os.name]
  17. try:
  18. _file = file
  19. except NameError:
  20. _file = None
  21. _open = open
  22. from distutils.errors import DistutilsError
  23. from pkg_resources import working_set
  24. __all__ = [
  25. "AbstractSandbox", "DirectorySandbox", "SandboxViolation", "run_setup",
  26. ]
  27. def _execfile(filename, globals, locals=None):
  28. """
  29. Python 3 implementation of execfile.
  30. """
  31. mode = 'rb'
  32. with open(filename, mode) as stream:
  33. script = stream.read()
  34. # compile() function in Python 2.6 and 3.1 requires LF line endings.
  35. if sys.version_info[:2] < (2, 7) or sys.version_info[:2] >= (3, 0) and sys.version_info[:2] < (3, 2):
  36. script = script.replace(b'\r\n', b'\n')
  37. script = script.replace(b'\r', b'\n')
  38. if locals is None:
  39. locals = globals
  40. code = compile(script, filename, 'exec')
  41. exec(code, globals, locals)
  42. @contextlib.contextmanager
  43. def save_argv(repl=None):
  44. saved = sys.argv[:]
  45. if repl is not None:
  46. sys.argv[:] = repl
  47. try:
  48. yield saved
  49. finally:
  50. sys.argv[:] = saved
  51. @contextlib.contextmanager
  52. def save_path():
  53. saved = sys.path[:]
  54. try:
  55. yield saved
  56. finally:
  57. sys.path[:] = saved
  58. @contextlib.contextmanager
  59. def override_temp(replacement):
  60. """
  61. Monkey-patch tempfile.tempdir with replacement, ensuring it exists
  62. """
  63. if not os.path.isdir(replacement):
  64. os.makedirs(replacement)
  65. saved = tempfile.tempdir
  66. tempfile.tempdir = replacement
  67. try:
  68. yield
  69. finally:
  70. tempfile.tempdir = saved
  71. @contextlib.contextmanager
  72. def pushd(target):
  73. saved = os.getcwd()
  74. os.chdir(target)
  75. try:
  76. yield saved
  77. finally:
  78. os.chdir(saved)
  79. class UnpickleableException(Exception):
  80. """
  81. An exception representing another Exception that could not be pickled.
  82. """
  83. @staticmethod
  84. def dump(type, exc):
  85. """
  86. Always return a dumped (pickled) type and exc. If exc can't be pickled,
  87. wrap it in UnpickleableException first.
  88. """
  89. try:
  90. return pickle.dumps(type), pickle.dumps(exc)
  91. except Exception:
  92. # get UnpickleableException inside the sandbox
  93. from setuptools.sandbox import UnpickleableException as cls
  94. return cls.dump(cls, cls(repr(exc)))
  95. class ExceptionSaver:
  96. """
  97. A Context Manager that will save an exception, serialized, and restore it
  98. later.
  99. """
  100. def __enter__(self):
  101. return self
  102. def __exit__(self, type, exc, tb):
  103. if not exc:
  104. return
  105. # dump the exception
  106. self._saved = UnpickleableException.dump(type, exc)
  107. self._tb = tb
  108. # suppress the exception
  109. return True
  110. def resume(self):
  111. "restore and re-raise any exception"
  112. if '_saved' not in vars(self):
  113. return
  114. type, exc = map(pickle.loads, self._saved)
  115. six.reraise(type, exc, self._tb)
  116. @contextlib.contextmanager
  117. def save_modules():
  118. """
  119. Context in which imported modules are saved.
  120. Translates exceptions internal to the context into the equivalent exception
  121. outside the context.
  122. """
  123. saved = sys.modules.copy()
  124. with ExceptionSaver() as saved_exc:
  125. yield saved
  126. sys.modules.update(saved)
  127. # remove any modules imported since
  128. del_modules = (
  129. mod_name for mod_name in sys.modules
  130. if mod_name not in saved
  131. # exclude any encodings modules. See #285
  132. and not mod_name.startswith('encodings.')
  133. )
  134. _clear_modules(del_modules)
  135. saved_exc.resume()
  136. def _clear_modules(module_names):
  137. for mod_name in list(module_names):
  138. del sys.modules[mod_name]
  139. @contextlib.contextmanager
  140. def save_pkg_resources_state():
  141. saved = pkg_resources.__getstate__()
  142. try:
  143. yield saved
  144. finally:
  145. pkg_resources.__setstate__(saved)
  146. @contextlib.contextmanager
  147. def setup_context(setup_dir):
  148. temp_dir = os.path.join(setup_dir, 'temp')
  149. with save_pkg_resources_state():
  150. with save_modules():
  151. hide_setuptools()
  152. with save_path():
  153. with save_argv():
  154. with override_temp(temp_dir):
  155. with pushd(setup_dir):
  156. # ensure setuptools commands are available
  157. __import__('setuptools')
  158. yield
  159. def _needs_hiding(mod_name):
  160. """
  161. >>> _needs_hiding('setuptools')
  162. True
  163. >>> _needs_hiding('pkg_resources')
  164. True
  165. >>> _needs_hiding('setuptools_plugin')
  166. False
  167. >>> _needs_hiding('setuptools.__init__')
  168. True
  169. >>> _needs_hiding('distutils')
  170. True
  171. >>> _needs_hiding('os')
  172. False
  173. >>> _needs_hiding('Cython')
  174. True
  175. """
  176. pattern = re.compile(r'(setuptools|pkg_resources|distutils|Cython)(\.|$)')
  177. return bool(pattern.match(mod_name))
  178. def hide_setuptools():
  179. """
  180. Remove references to setuptools' modules from sys.modules to allow the
  181. invocation to import the most appropriate setuptools. This technique is
  182. necessary to avoid issues such as #315 where setuptools upgrading itself
  183. would fail to find a function declared in the metadata.
  184. """
  185. modules = filter(_needs_hiding, sys.modules)
  186. _clear_modules(modules)
  187. def run_setup(setup_script, args):
  188. """Run a distutils setup script, sandboxed in its directory"""
  189. setup_dir = os.path.abspath(os.path.dirname(setup_script))
  190. with setup_context(setup_dir):
  191. try:
  192. sys.argv[:] = [setup_script] + list(args)
  193. sys.path.insert(0, setup_dir)
  194. # reset to include setup dir, w/clean callback list
  195. working_set.__init__()
  196. working_set.callbacks.append(lambda dist: dist.activate())
  197. # __file__ should be a byte string on Python 2 (#712)
  198. dunder_file = (
  199. setup_script
  200. if isinstance(setup_script, str) else
  201. setup_script.encode(sys.getfilesystemencoding())
  202. )
  203. def runner():
  204. ns = dict(__file__=dunder_file, __name__='__main__')
  205. _execfile(setup_script, ns)
  206. DirectorySandbox(setup_dir).run(runner)
  207. except SystemExit as v:
  208. if v.args and v.args[0]:
  209. raise
  210. # Normal exit, just return
  211. class AbstractSandbox:
  212. """Wrap 'os' module and 'open()' builtin for virtualizing setup scripts"""
  213. _active = False
  214. def __init__(self):
  215. self._attrs = [
  216. name for name in dir(_os)
  217. if not name.startswith('_') and hasattr(self, name)
  218. ]
  219. def _copy(self, source):
  220. for name in self._attrs:
  221. setattr(os, name, getattr(source, name))
  222. def run(self, func):
  223. """Run 'func' under os sandboxing"""
  224. try:
  225. self._copy(self)
  226. if _file:
  227. builtins.file = self._file
  228. builtins.open = self._open
  229. self._active = True
  230. return func()
  231. finally:
  232. self._active = False
  233. if _file:
  234. builtins.file = _file
  235. builtins.open = _open
  236. self._copy(_os)
  237. def _mk_dual_path_wrapper(name):
  238. original = getattr(_os, name)
  239. def wrap(self, src, dst, *args, **kw):
  240. if self._active:
  241. src, dst = self._remap_pair(name, src, dst, *args, **kw)
  242. return original(src, dst, *args, **kw)
  243. return wrap
  244. for name in ["rename", "link", "symlink"]:
  245. if hasattr(_os, name):
  246. locals()[name] = _mk_dual_path_wrapper(name)
  247. def _mk_single_path_wrapper(name, original=None):
  248. original = original or getattr(_os, name)
  249. def wrap(self, path, *args, **kw):
  250. if self._active:
  251. path = self._remap_input(name, path, *args, **kw)
  252. return original(path, *args, **kw)
  253. return wrap
  254. if _file:
  255. _file = _mk_single_path_wrapper('file', _file)
  256. _open = _mk_single_path_wrapper('open', _open)
  257. for name in [
  258. "stat", "listdir", "chdir", "open", "chmod", "chown", "mkdir",
  259. "remove", "unlink", "rmdir", "utime", "lchown", "chroot", "lstat",
  260. "startfile", "mkfifo", "mknod", "pathconf", "access"
  261. ]:
  262. if hasattr(_os, name):
  263. locals()[name] = _mk_single_path_wrapper(name)
  264. def _mk_single_with_return(name):
  265. original = getattr(_os, name)
  266. def wrap(self, path, *args, **kw):
  267. if self._active:
  268. path = self._remap_input(name, path, *args, **kw)
  269. return self._remap_output(name, original(path, *args, **kw))
  270. return original(path, *args, **kw)
  271. return wrap
  272. for name in ['readlink', 'tempnam']:
  273. if hasattr(_os, name):
  274. locals()[name] = _mk_single_with_return(name)
  275. def _mk_query(name):
  276. original = getattr(_os, name)
  277. def wrap(self, *args, **kw):
  278. retval = original(*args, **kw)
  279. if self._active:
  280. return self._remap_output(name, retval)
  281. return retval
  282. return wrap
  283. for name in ['getcwd', 'tmpnam']:
  284. if hasattr(_os, name):
  285. locals()[name] = _mk_query(name)
  286. def _validate_path(self, path):
  287. """Called to remap or validate any path, whether input or output"""
  288. return path
  289. def _remap_input(self, operation, path, *args, **kw):
  290. """Called for path inputs"""
  291. return self._validate_path(path)
  292. def _remap_output(self, operation, path):
  293. """Called for path outputs"""
  294. return self._validate_path(path)
  295. def _remap_pair(self, operation, src, dst, *args, **kw):
  296. """Called for path pairs like rename, link, and symlink operations"""
  297. return (
  298. self._remap_input(operation + '-from', src, *args, **kw),
  299. self._remap_input(operation + '-to', dst, *args, **kw)
  300. )
  301. if hasattr(os, 'devnull'):
  302. _EXCEPTIONS = [os.devnull,]
  303. else:
  304. _EXCEPTIONS = []
  305. class DirectorySandbox(AbstractSandbox):
  306. """Restrict operations to a single subdirectory - pseudo-chroot"""
  307. write_ops = dict.fromkeys([
  308. "open", "chmod", "chown", "mkdir", "remove", "unlink", "rmdir",
  309. "utime", "lchown", "chroot", "mkfifo", "mknod", "tempnam",
  310. ])
  311. _exception_patterns = [
  312. # Allow lib2to3 to attempt to save a pickled grammar object (#121)
  313. r'.*lib2to3.*\.pickle$',
  314. ]
  315. "exempt writing to paths that match the pattern"
  316. def __init__(self, sandbox, exceptions=_EXCEPTIONS):
  317. self._sandbox = os.path.normcase(os.path.realpath(sandbox))
  318. self._prefix = os.path.join(self._sandbox, '')
  319. self._exceptions = [
  320. os.path.normcase(os.path.realpath(path))
  321. for path in exceptions
  322. ]
  323. AbstractSandbox.__init__(self)
  324. def _violation(self, operation, *args, **kw):
  325. from setuptools.sandbox import SandboxViolation
  326. raise SandboxViolation(operation, args, kw)
  327. if _file:
  328. def _file(self, path, mode='r', *args, **kw):
  329. if mode not in ('r', 'rt', 'rb', 'rU', 'U') and not self._ok(path):
  330. self._violation("file", path, mode, *args, **kw)
  331. return _file(path, mode, *args, **kw)
  332. def _open(self, path, mode='r', *args, **kw):
  333. if mode not in ('r', 'rt', 'rb', 'rU', 'U') and not self._ok(path):
  334. self._violation("open", path, mode, *args, **kw)
  335. return _open(path, mode, *args, **kw)
  336. def tmpnam(self):
  337. self._violation("tmpnam")
  338. def _ok(self, path):
  339. active = self._active
  340. try:
  341. self._active = False
  342. realpath = os.path.normcase(os.path.realpath(path))
  343. return (
  344. self._exempted(realpath)
  345. or realpath == self._sandbox
  346. or realpath.startswith(self._prefix)
  347. )
  348. finally:
  349. self._active = active
  350. def _exempted(self, filepath):
  351. start_matches = (
  352. filepath.startswith(exception)
  353. for exception in self._exceptions
  354. )
  355. pattern_matches = (
  356. re.match(pattern, filepath)
  357. for pattern in self._exception_patterns
  358. )
  359. candidates = itertools.chain(start_matches, pattern_matches)
  360. return any(candidates)
  361. def _remap_input(self, operation, path, *args, **kw):
  362. """Called for path inputs"""
  363. if operation in self.write_ops and not self._ok(path):
  364. self._violation(operation, os.path.realpath(path), *args, **kw)
  365. return path
  366. def _remap_pair(self, operation, src, dst, *args, **kw):
  367. """Called for path pairs like rename, link, and symlink operations"""
  368. if not self._ok(src) or not self._ok(dst):
  369. self._violation(operation, src, dst, *args, **kw)
  370. return (src, dst)
  371. def open(self, file, flags, mode=0o777, *args, **kw):
  372. """Called for low-level os.open()"""
  373. if flags & WRITE_FLAGS and not self._ok(file):
  374. self._violation("os.open", file, flags, mode, *args, **kw)
  375. return _os.open(file, flags, mode, *args, **kw)
  376. WRITE_FLAGS = functools.reduce(
  377. operator.or_, [getattr(_os, a, 0) for a in
  378. "O_WRONLY O_RDWR O_APPEND O_CREAT O_TRUNC O_TEMPORARY".split()]
  379. )
  380. class SandboxViolation(DistutilsError):
  381. """A setup script attempted to modify the filesystem outside the sandbox"""
  382. def __str__(self):
  383. return """SandboxViolation: %s%r %s
  384. The package setup script has attempted to modify files on your system
  385. that are not within the EasyInstall build area, and has been aborted.
  386. This package cannot be safely installed by EasyInstall, and may not
  387. support alternate installation locations even if you run its setup
  388. script by hand. Please inform the package's author and the EasyInstall
  389. maintainers to find out if a fix or workaround is available.""" % self.args
  390. #