Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions googleapiclient/discovery_cache/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@

from __future__ import absolute_import

import getpass
import logging
import os
import stat
import tempfile

LOGGER = logging.getLogger(__name__)

Expand All @@ -26,6 +29,57 @@
os.path.dirname(os.path.realpath(__file__)), "documents"
)

_CACHE_SUBDIR_PREFIX = "google-api-python-client-discovery-cache-"


def _get_cache_directory():
"""Return a private, per-user directory for file based discovery caches.

The file based discovery cache was historically written to a fixed,
world-predictable file name directly in the shared system temporary
directory (for example ``/tmp`` on Linux, which is writable by every
local user). Any local user could pre-create, replace, read, or symlink
that file and poison the cached discovery document. Because a discovery
document controls ``rootUrl``, a poisoned entry redirects every later
authenticated request, including the caller's OAuth credentials, to an
attacker chosen host.

Keep the cache inside a per-user directory created with ``0o700`` so
other local users cannot access it, and refuse to use the directory when
it is not a regular directory owned by the current user with no access
for group or others.
"""
if hasattr(os, "getuid"):
suffix = str(os.getuid())
else:
try:
raw = getpass.getuser()
except Exception:
raw = ""
suffix = "".join(c for c in raw if c.isalnum() or c in "._-") or "default"

cache_dir = os.path.join(tempfile.gettempdir(), _CACHE_SUBDIR_PREFIX + suffix)
os.makedirs(cache_dir, exist_ok=True)

if hasattr(os, "getuid"):
try:
os.chmod(cache_dir, 0o700)
except OSError:
pass
info = os.lstat(cache_dir)
if (
not stat.S_ISDIR(info.st_mode)
or info.st_uid != os.getuid()
or info.st_mode & 0o077
):
raise OSError(
"Refusing to use discovery cache directory %r: it must be a "
"directory owned by the current user with no group or other "
"access" % cache_dir
)
Comment on lines +62 to +79

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security-high high

Calling os.chmod before verifying that the path is a real directory (and not a symlink) introduces a security vulnerability. If an attacker pre-creates the cache directory path as a symlink pointing to a sensitive directory owned by the current user, os.chmod will follow the symlink and modify the permissions of that target directory.

To prevent this, we should:

  1. Create the directory with restrictive permissions (mode=0o700) from the start in os.makedirs.
  2. Perform the os.lstat check before calling os.chmod to ensure the path is a real directory owned by the current user.
  3. Only call os.chmod if the directory exists and has incorrect permissions, and then re-verify.

Additionally, we should use f-strings for string interpolation per the general rules.

    os.makedirs(cache_dir, mode=0o700, exist_ok=True)

    if hasattr(os, "getuid"):
        info = os.lstat(cache_dir)
        if not stat.S_ISDIR(info.st_mode) or info.st_uid != os.getuid():
            raise OSError(
                f"Refusing to use discovery cache directory {cache_dir!r}: it must be a "
                "directory owned by the current user with no group or other "
                "access"
            )
        if info.st_mode & 0o077:
            try:
                os.chmod(cache_dir, 0o700)
            except OSError:
                pass
            info = os.lstat(cache_dir)
            if info.st_mode & 0o077:
                raise OSError(
                    f"Refusing to use discovery cache directory {cache_dir!r}: it must be a "
                    "directory owned by the current user with no group or other "
                    "access"
                )
References
  1. Prefer using f-strings for string interpolation in Python.


return cache_dir


def autodetect():
"""Detects an appropriate cache module and returns it.
Expand Down
5 changes: 2 additions & 3 deletions googleapiclient/discovery_cache/file_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import json
import logging
import os
import tempfile

try:
from oauth2client.contrib.locked_file import LockedFile
Expand All @@ -41,7 +40,7 @@
)

from . import base
from ..discovery_cache import DISCOVERY_DOC_MAX_AGE
from ..discovery_cache import DISCOVERY_DOC_MAX_AGE, _get_cache_directory

LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -85,7 +84,7 @@ def __init__(self, max_age):
max_age: Cache expiration in seconds.
"""
self._max_age = max_age
self._file = os.path.join(tempfile.gettempdir(), FILENAME)
self._file = os.path.join(_get_cache_directory(), FILENAME)
f = LockedFile(self._file, "a+", "r")
try:
f.open_and_lock()
Expand Down
35 changes: 34 additions & 1 deletion tests/test_discovery_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,50 @@
"""Discovery document cache tests."""

import datetime
import os
import stat
import tempfile
import unittest
from unittest import mock

from googleapiclient.discovery_cache import DISCOVERY_DOC_MAX_AGE
from googleapiclient.discovery_cache import (
_CACHE_SUBDIR_PREFIX,
DISCOVERY_DOC_MAX_AGE,
_get_cache_directory,
)

try:
from googleapiclient.discovery_cache.file_cache import Cache as FileCache
except ImportError:
FileCache = None


class CacheDirectoryTest(unittest.TestCase):
def test_cache_directory_is_private_and_contained(self):
with tempfile.TemporaryDirectory() as tmp:
with mock.patch("tempfile.gettempdir", return_value=tmp):
cache_dir = _get_cache_directory()
# The cache lives in a dedicated subdirectory, not directly under
# the shared temporary directory with a predictable file name.
self.assertEqual(os.path.dirname(cache_dir), tmp)
self.assertTrue(os.path.isdir(cache_dir))
if hasattr(os, "getuid"):
info = os.stat(cache_dir)
self.assertEqual(stat.S_IMODE(info.st_mode), 0o700)
self.assertEqual(info.st_uid, os.getuid())

@unittest.skipUnless(hasattr(os, "getuid"), "POSIX permission check")
def test_rejects_symlinked_cache_directory(self):
with tempfile.TemporaryDirectory() as tmp:
target = os.path.join(tmp, "attacker")
os.mkdir(target)
link = os.path.join(tmp, _CACHE_SUBDIR_PREFIX + str(os.getuid()))
os.symlink(target, link)
with mock.patch("tempfile.gettempdir", return_value=tmp):
with self.assertRaises(OSError):
_get_cache_directory()


@unittest.skipIf(FileCache is None, "FileCache unavailable.")
class FileCacheTest(unittest.TestCase):
@mock.patch(
Expand Down
Loading