Fixed a security issue with temporary files on the filesystem cache on UNIX.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 701e9ad · Browse files · 2 files·+32 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+32 added−2 removed
- other:+32 added−2 removed
Provenance
- Commit
acb672b- Tree
0d7c3a1- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
701e9ad
2 files changed+32 added−2 removed
modified:jinja2/bccache.py+27 added-2 removed
13 unmodified lines14 :copyright: (c) 2010 by the Jinja Team.15 :license: BSD.16 """17 from os import path, listdir18+import os19 import sys20+import errno21 import marshal22 import tempfile23 import fnmatch24 from hashlib import sha1165 unmodified lines190 """A bytecode cache that stores bytecode on the filesystem. It accepts191 two arguments: The directory where the cache items are stored and a192 pattern string that is used to build the filename.193 192- If no directory is specified the system temporary items folder is used.194+ If no directory is specified a default cache directory is selected. On195+ Windows the user's temp directory is used, on UNIX systems a directory196+ is created for the user in the system temp directory.197 198 The pattern can be used to have multiple separate caches operate on the199 same directory. The default pattern is ``'__jinja2_%s.cache'``. ``%s``200 is replaced with the cache key.4 unmodified lines205 """206 207 def __init__(self, directory=None, pattern='__jinja2_%s.cache'):208 if directory is None:205- directory = tempfile.gettempdir()209+ directory = self._get_default_cache_dir()210 self.directory = directory211 self.pattern = pattern212 213+ def _get_default_cache_dir(self):214+ tmpdir = tempfile.gettempdir()215+216+ # On windows the temporary directory is used specific unless217+ # explicitly forced otherwise. We can just use that.218+ if os.name == 'n':219+ return tmpdir220+ if not hasattr(os, 'getuid'):221+ raise RuntimeError('Cannot determine safe temp directory. You '222+ 'need to explicitly provide one.')223+224+ dirname = '_jinja2-cache-%d' % os.getuid()225+ actual_dir = os.path.join(tmpdir, dirname)226+ try:227+ os.mkdir(actual_dir, 0700)228+ except OSError as e:229+ if e.errno != errno.EEXIST:230+ raise231+232+ return actual_dir233+234 def _get_cache_filename(self, bucket):235 return path.join(self.directory, self.pattern % bucket.key)236 237 def load_bytecode(self, bucket):99 unmodified linesmodified:CHANGES+5 added
5 unmodified lines6 (bugfix release, release date to be decided)7 8 - Prefix loader was not forwarding the locals properly to9 inner loaders. This is now fixed.10+- Security issue: Changed the default folder for the filesystem cache to be11+ user specific and read and write protected on UNIX systems. See `Debian bug12+ 734747`_ for more information.13 14+.. _Debian bug 734747: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=73474715+16 Version 2.7.117 -------------18 (bugfix release, released on August 7th 2013)19 308 unmodified lines