From 803595cf6ef864154ca967de60ab9a431228bceb Mon Sep 17 00:00:00 2001 From: hikmetba-bit Date: Thu, 17 Sep 2026 20:32:36 +0300 Subject: [PATCH] Respect the process umask when creating the cache directory Cache.__init__ created the cache directory with os.makedirs(directory, 0o755), which hard-codes the permission bits instead of letting the OS apply the umask like it does for any other newly created directory. Since umask can only clear bits (mode & ~umask), passing an explicit 0o755 caps the directory at rwxr-xr-x no matter what umask the caller has configured (e.g. umask 000 expecting world-writable directories). Drop the explicit mode so os.makedirs() uses its default (0o777), which the OS then masks with the umask as usual. Fixes #332 Co-Authored-By: Claude Sonnet 5 --- diskcache/core.py | 6 +++++- tests/test_core.py | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/diskcache/core.py b/diskcache/core.py index 7a3d23b..2f14f1b 100644 --- a/diskcache/core.py +++ b/diskcache/core.py @@ -444,7 +444,11 @@ def __init__(self, directory=None, timeout=60, disk=Disk, **settings): if not op.isdir(directory): try: - os.makedirs(directory, 0o755) + # Let the OS apply the umask to the default mode rather than + # hard-coding 0o755, so the cache directory's permissions + # honor the caller's umask like any other newly created + # directory would. + os.makedirs(directory) except OSError as error: if error.errno != errno.EEXIST: raise EnvironmentError( diff --git a/tests/test_core.py b/tests/test_core.py index 788afef..6bdc95f 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -144,6 +144,23 @@ def test_init_makedirs(): raise +def test_init_makedirs_respects_umask(): + cache_dir = tempfile.mkdtemp() + shutil.rmtree(cache_dir) + makedirs = mock.Mock(wraps=os.makedirs) + + try: + with mock.patch('os.makedirs', makedirs): + cache = dc.Cache(cache_dir) + cache.close() + finally: + shutil.rmtree(cache_dir, ignore_errors=True) + + # No explicit mode should be passed, so the OS applies the umask to the + # default mode like it does for any other new directory. + makedirs.assert_called_once_with(cache_dir) + + def test_pragma_error(cache): local = mock.Mock() con = mock.Mock()