From e0e6f73e7b1b62da2ef023daef179babd43f41f1 Mon Sep 17 00:00:00 2001 From: Kurt McKee Date: Tue, 10 Nov 2026 15:04:56 -0600 Subject: [PATCH] Fix potential process-wide umask mixups --- ...9_kurtmckee_fix_potential_umask_mixups.rst | 17 ++ src/globus_sdk/token_storage/base.py | 47 ++++++ src/globus_sdk/token_storage/json.py | 21 +-- src/globus_sdk/token_storage/legacy/base.py | 47 ++++++ .../token_storage/legacy/file_adapters.py | 12 +- .../token_storage/legacy/sqlite_adapter.py | 11 +- src/globus_sdk/token_storage/sqlite.py | 9 +- tests/common/overlapping_writes.py | 151 ++++++++++++++++++ .../test_file_storage_umask_invariants.py | 106 ++++++++++++ .../tokenstorage/v1/test_simplejson_file.py | 70 +++++++- .../functional/tokenstorage/v1/test_sqlite.py | 13 ++ .../tokenstorage/v2/test_json_tokenstorage.py | 74 ++++++++- .../v2/test_sqlite_tokenstorage.py | 13 ++ 13 files changed, 555 insertions(+), 36 deletions(-) create mode 100644 changelog.d/20261110_150429_kurtmckee_fix_potential_umask_mixups.rst create mode 100644 tests/common/overlapping_writes.py create mode 100644 tests/functional/tokenstorage/test_file_storage_umask_invariants.py diff --git a/changelog.d/20261110_150429_kurtmckee_fix_potential_umask_mixups.rst b/changelog.d/20261110_150429_kurtmckee_fix_potential_umask_mixups.rst new file mode 100644 index 000000000..5a37cda5c --- /dev/null +++ b/changelog.d/20261110_150429_kurtmckee_fix_potential_umask_mixups.rst @@ -0,0 +1,17 @@ +Deprecated +---------- + +- ``FileTokenStorage.user_only_umask()`` and ``FileAdapter.user_only_umask()`` + are deprecated and will be removed in version 5. (:pr:`NUMBER`) + + They change the umask of the whole process, + which is not safe when other threads are running. + +Fixed +----- + +- All file-based token storage classes create files + with mode ``0o600`` set directly, and do not change the process umask. (:pr:`NUMBER`) + + This avoids a thread safety issue that could cause I/O in other threads + to create files with an unexpected umask. diff --git a/src/globus_sdk/token_storage/base.py b/src/globus_sdk/token_storage/base.py index 88b28a635..9fa7cc395 100644 --- a/src/globus_sdk/token_storage/base.py +++ b/src/globus_sdk/token_storage/base.py @@ -200,6 +200,37 @@ def file_exists(self) -> bool: """ return os.path.exists(self.filepath) + @staticmethod + def _owner_only_opener(path: str, flags: int) -> int: + """ + An ``opener`` for the builtin ``open()``. + + A new file will be created with the mode ``0o600``. + An existing file will keep its existing mode. + """ + + try: + # Open the file with a 0o600 mode set. (See below for a corner case.) + # The O_CREAT | O_EXCL flags will trigger `FileExistsError` + # if the file already exists. + fd = os.open(path, flags | os.O_CREAT | os.O_EXCL, 0o600) + except FileExistsError: + # The file already exists. Open and return the descriptor immediately + # to avoid falling through to the code below that changes the file mode. + # Note: a dangling symlink will reach this line and raise an exception. + return os.open(path, flags & ~os.O_CREAT) + + # Corner case: the 0o600 mode, set above, may be further restricted + # by the process-level umask. Therefore, set the permissions exactly. + try: + os.fchmod(fd, 0o600) + except (AttributeError, OSError): + # `os.fchmod` doesn't exist on all platforms. + # In addition, some filesystems do not support changing modes. + pass + + return fd + @contextlib.contextmanager def user_only_umask(self) -> t.Iterator[None]: """ @@ -213,7 +244,23 @@ def user_only_umask(self) -> t.Iterator[None]: On Windows, this has no effect. To control the permissions on files used for token storage, use ``%LOCALAPPDATA%`` or ``%APPDATA%``. These directories should only be accessible to the current user. + + .. warning:: + + This method is deprecated and will be removed in version 5. + + The umask of the entire Python process is changed, so it is not safe + to use this method while other threads create files or use this method. """ + + # In a context manager, the frames above this one are contextlib's + # ``__enter__`` and then the caller's ``with`` statement. + globus_sdk.exc.warn_deprecated( + "'user_only_umask' is deprecated and will be removed in version 5. " + "It changes the umask of the whole process, " + "which is not safe when other threads are running.", + stacklevel=4, + ) old_umask = os.umask(0o177) try: yield diff --git a/src/globus_sdk/token_storage/json.py b/src/globus_sdk/token_storage/json.py index 0d559ab1e..be75686bf 100644 --- a/src/globus_sdk/token_storage/json.py +++ b/src/globus_sdk/token_storage/json.py @@ -130,8 +130,7 @@ def store_token_data_by_resource_server( being written to the file at ``self.filepath``. Under the assumption that this may be running on a system with multiple - local users, this sets the umask such that only the owner of the - resulting file can read or write it. + local users, a newly created file can be read and written only by its owner. :param token_data_by_resource_server: A mapping of resource servers to token data. @@ -149,10 +148,11 @@ def store_token_data_by_resource_server( # update globus-sdk version to_write["globus-sdk.version"] = __version__ - # write the file, denying rwx to Group and World, exec to User - with self.user_only_umask(): - with open(self.filepath, "w", encoding="utf-8") as f: - json.dump(to_write, f) + # write the file (a newly created file is owner read/write only) + with open( + self.filepath, "w", encoding="utf-8", opener=self._owner_only_opener + ) as f: + json.dump(to_write, f) def get_token_data_by_resource_server(self) -> dict[str, TokenStorageData]: """ @@ -180,9 +180,10 @@ def remove_token_data(self, resource_server: str) -> bool: # pop the token data out if it exists popped = to_write["data"].get(self.namespace, {}).pop(resource_server, None) - # overwrite the file, denying rwx to Group and World, exec to User - with self.user_only_umask(): - with open(self.filepath, "w", encoding="utf-8") as f: - json.dump(to_write, f) + # overwrite the file (a newly created file is owner read/write only) + with open( + self.filepath, "w", encoding="utf-8", opener=self._owner_only_opener + ) as f: + json.dump(to_write, f) return popped is not None diff --git a/src/globus_sdk/token_storage/legacy/base.py b/src/globus_sdk/token_storage/legacy/base.py index 57d615709..76a2fe006 100644 --- a/src/globus_sdk/token_storage/legacy/base.py +++ b/src/globus_sdk/token_storage/legacy/base.py @@ -55,6 +55,37 @@ def file_exists(self) -> bool: """ return os.path.exists(self.filename) + @staticmethod + def _owner_only_opener(path: str, flags: int) -> int: + """ + An ``opener`` for the builtin ``open()``. + + A new file will be created with the mode ``0o600``. + An existing file will keep its existing mode. + """ + + try: + # Open the file with a 0o600 mode set. (See below for a corner case.) + # The O_CREAT | O_EXCL flags will trigger `FileExistsError` + # if the file already exists. + fd = os.open(path, flags | os.O_CREAT | os.O_EXCL, 0o600) + except FileExistsError: + # The file already exists. Open and return the descriptor immediately + # to avoid falling through to the code below that changes the file mode. + # Note: a dangling symlink will reach this line and raise an exception. + return os.open(path, flags & ~os.O_CREAT) + + # Corner case: the 0o600 mode, set above, may be further restricted + # by the process-level umask. Therefore, set the permissions exactly. + try: + os.fchmod(fd, 0o600) + except (AttributeError, OSError): + # `os.fchmod` doesn't exist on all platforms. + # In addition, some filesystems do not support changing modes. + pass + + return fd + @contextlib.contextmanager def user_only_umask(self) -> t.Iterator[None]: """ @@ -68,7 +99,23 @@ def user_only_umask(self) -> t.Iterator[None]: On Windows, this has no effect. To control the permissions on files used for token storage, use ``%LOCALAPPDATA%`` or ``%APPDATA%``. These directories should only be accessible to the current user. + + .. warning:: + + This method is deprecated and will be removed in version 5. + + The umask of the entire Python process is changed, so it is not safe + to use this method while other threads create files or use this method. """ + + # In a context manager, the frames above this one are contextlib's + # ``__enter__`` and then the caller's ``with`` statement. + globus_sdk.exc.warn_deprecated( + "'user_only_umask' is deprecated and will be removed in version 5. " + "It changes the umask of the whole process, " + "which is not safe when other threads are running.", + stacklevel=4, + ) old_umask = os.umask(0o177) try: yield diff --git a/src/globus_sdk/token_storage/legacy/file_adapters.py b/src/globus_sdk/token_storage/legacy/file_adapters.py index f0113dfd1..cb9a1cf01 100644 --- a/src/globus_sdk/token_storage/legacy/file_adapters.py +++ b/src/globus_sdk/token_storage/legacy/file_adapters.py @@ -114,8 +114,7 @@ def store(self, token_response: globus_sdk.OAuthTokenResponse) -> None: which was in use. Under the assumption that this may be running on a system with multiple - local users, this sets the umask such that only the owner of the - resulting file can read or write it. + local users, a newly created file can be read and written only by its owner. :param token_response: The token data received from the refresh """ @@ -130,10 +129,11 @@ def store(self, token_response: globus_sdk.OAuthTokenResponse) -> None: # newly received tokens to_write["by_rs"].update(token_response.by_resource_server) - # deny rwx to Group and World, exec to User - with self.user_only_umask(): - with open(self.filename, "w", encoding="utf-8") as f: - json.dump(to_write, f) + # Newly-created files have owner-only read/write permissions. + with open( + self.filename, "w", encoding="utf-8", opener=self._owner_only_opener + ) as f: + json.dump(to_write, f) def get_by_resource_server(self) -> dict[str, t.Any]: """ diff --git a/src/globus_sdk/token_storage/legacy/sqlite_adapter.py b/src/globus_sdk/token_storage/legacy/sqlite_adapter.py index f0a53075e..946d91ac7 100644 --- a/src/globus_sdk/token_storage/legacy/sqlite_adapter.py +++ b/src/globus_sdk/token_storage/legacy/sqlite_adapter.py @@ -61,12 +61,11 @@ def _init_and_connect( init_tables = self._is_memory_db() or not self.file_exists() connect_params = connect_params or {} if init_tables and not self._is_memory_db(): # real file needs to be created - with self.user_only_umask(): - conn: sqlite3.Connection = sqlite3.connect( - self.dbname, **connect_params - ) - else: - conn = sqlite3.connect(self.dbname, **connect_params) + # Create the file with owner-only permissions before SQLite opens it. + # "Append" mode prevents an existing file from being truncated. + with open(self.dbname, "ab", opener=self._owner_only_opener): + pass + conn: sqlite3.Connection = sqlite3.connect(self.dbname, **connect_params) if init_tables: conn.executescript(""" CREATE TABLE config_storage ( diff --git a/src/globus_sdk/token_storage/sqlite.py b/src/globus_sdk/token_storage/sqlite.py index d7fae0536..2a717c901 100644 --- a/src/globus_sdk/token_storage/sqlite.py +++ b/src/globus_sdk/token_storage/sqlite.py @@ -51,10 +51,11 @@ def _init_and_connect( ) -> sqlite3.Connection: connect_params = connect_params or {} if not self.file_exists(): - with self.user_only_umask(): - conn: sqlite3.Connection = sqlite3.connect( - self.filepath, **connect_params - ) + # Create the file with owner-only permissions before SQLite opens it. + # "Append" mode prevents an existing file from being truncated. + with open(self.filepath, "ab", opener=self._owner_only_opener): + pass + conn: sqlite3.Connection = sqlite3.connect(self.filepath, **connect_params) conn.executescript(textwrap.dedent(""" CREATE TABLE token_storage ( namespace VARCHAR NOT NULL, diff --git a/tests/common/overlapping_writes.py b/tests/common/overlapping_writes.py new file mode 100644 index 000000000..d69f6dd86 --- /dev/null +++ b/tests/common/overlapping_writes.py @@ -0,0 +1,151 @@ +""" +Helpers for tests which need two writers to overlap in one precise order, +such as tests of how writes interact with process-wide state (like the umask). + +The order is enforced with events rather than sleeps, so it is the same on every run, +and it does not depend on how the code under test is implemented. +""" + +from __future__ import annotations + +import contextlib +import os +import sqlite3 +import threading +import typing as t + + +def current_umask() -> int: + """Read the process umask. (`os.umask` can only be read by setting it.)""" + current = os.umask(0) + os.umask(current) + return current + + +@contextlib.contextmanager +def process_umask(value: int) -> t.Iterator[None]: + """Set the process umask for the duration of a block, then restore it.""" + original = os.umask(value) + try: + yield + finally: + os.umask(original) + + +class OverlappingWrites: + """ + Run two writes in separate threads, in this order:: + + first starts -> second starts -> first finishes -> second finishes + + Each write must call its checkpoint (``first_checkpoint`` or ``second_checkpoint``) + at a point where the code under test is mid-write. + The first write waits at its checkpoint until the second is also at its checkpoint. + The second write waits at its checkpoint until the first has completely finished. + + If the code under test serializes the writes, the second can never reach its + checkpoint while the first waits, so the first simply times out and carries on. + The order of events is then different, but nothing deadlocks. + + Usage + ----- + + An ``OverlappingWrites`` object is used once, and its lifecycle wraps + around the code under test: build the writes with its checkpoints, + then hand them to ``run()``, which starts and orders the threads. + + .. code-block:: python + + controller = OverlappingWrites() + action1 = build_action1(callback=controller.first_checkpoint) + action2 = build_action2(callback=controller.second_checkpoint) + controller.run(action1, action2) + + # now assert on the state left behind by the overlapping writes + + ``run()`` waits for both threads, raises the first exception that either raised, + and fails if either write never reached its checkpoint. + """ + + def __init__(self, timeout: float = 0.2) -> None: + self._timeout = timeout + self._first_started = threading.Event() + self._second_started = threading.Event() + self._first_finished = threading.Event() + + def first_checkpoint(self) -> None: + self._first_started.set() + self._second_started.wait(self._timeout) + + def second_checkpoint(self) -> None: + self._second_started.set() + self._first_finished.wait(self._timeout) + + def run(self, first: t.Callable[[], t.Any], second: t.Callable[[], t.Any]) -> None: + errors: list[Exception] = [] + + def run_first() -> None: + try: + first() + except Exception as e: + errors.append(e) + finally: + self._first_finished.set() + + def run_second() -> None: + try: + second() + except Exception as e: + errors.append(e) + + first_thread = threading.Thread(target=run_first) + second_thread = threading.Thread(target=run_second) + + first_thread.start() + assert self._first_started.wait( + 5 + ), "the first write never reached its checkpoint" + second_thread.start() + first_thread.join(5) + second_thread.join(5) + + assert not (first_thread.is_alive() or second_thread.is_alive()) + assert ( + self._second_started.is_set() + ), "the second write never reached its checkpoint" + if errors: + raise errors[0] + + +class CheckpointDict(dict[str, t.Any]): + """ + A dict which calls a checkpoint when it is iterated. + + When this is stored as token data, ``json.dump`` iterates it while writing, which + is after the file has been opened. + """ + + def __init__( + self, checkpoint: t.Callable[[], None], data: dict[str, t.Any] + ) -> None: + super().__init__(data) + self._checkpoint = checkpoint + + def items(self) -> t.Any: + self._checkpoint() + return super().items() + + +def sqlite_factory_with_checkpoint( + checkpoint: t.Callable[[], None], +) -> t.Callable[..., sqlite3.Connection]: + """ + A ``factory`` for ``sqlite3.connect()`` which calls a checkpoint just before the + database file is opened (and therefore just before it is created). + """ + + def factory(*args: t.Any, **kwargs: t.Any) -> sqlite3.Connection: + checkpoint() + return sqlite3.Connection(*args, **kwargs) + + return factory diff --git a/tests/functional/tokenstorage/test_file_storage_umask_invariants.py b/tests/functional/tokenstorage/test_file_storage_umask_invariants.py new file mode 100644 index 000000000..42c1193d1 --- /dev/null +++ b/tests/functional/tokenstorage/test_file_storage_umask_invariants.py @@ -0,0 +1,106 @@ +""" +Invariants which hold for every file-based token storage, legacy and current. + +The process umask is shared by every thread, so changing it, even temporarily, +can change the permissions of files which other threads are creating. +File-based storages must therefore never leave the umask changed, which they could +do if two writes overlapped and each tried to restore a different "original" umask. +""" + +import os +import time +from unittest import mock + +import pytest + +from globus_sdk.token_storage import JSONTokenStorage, SQLiteTokenStorage +from globus_sdk.token_storage.legacy import SimpleJSONFileAdapter, SQLiteAdapter +from tests.common.overlapping_writes import ( + CheckpointDict, + OverlappingWrites, + current_umask, + process_umask, + sqlite_factory_with_checkpoint, +) + +IS_WINDOWS = os.name == "nt" + + +@pytest.fixture( + params=[SQLiteAdapter, SQLiteTokenStorage, JSONTokenStorage, SimpleJSONFileAdapter] +) +def overlapping_file_writes(request, tmp_path): + """ + Return a function which runs two writes, to two different files, so that they + overlap in the order described by ``OverlappingWrites``. + + For each storage class, "a write" must call the checkpoint + while the class is in the middle of creating its file. + How that is arranged depends on how the class writes its file. + """ + cls = request.param + mock_token_data = { + "access_token": "access_token_1", + "expires_at_seconds": int(time.time()) + 3600, + "refresh_token": "refresh_token_1", + "resource_server": "resource_server_1", + "scope": "scope1", + "token_type": "bearer", + } + + if cls in (SQLiteAdapter, SQLiteTokenStorage): + + def write_file(filename_stem, checkpoint): + # The checkpoint is called by the `factory` just before the database + # file is opened (and therefore created). + obj = cls( + tmp_path / f"{filename_stem}.db", + connect_params={ + "factory": sqlite_factory_with_checkpoint(checkpoint), + # adapters are created in worker threads but closed in teardown + "check_same_thread": False, + }, + ) + request.addfinalizer(obj.close) + + elif cls is JSONTokenStorage: + + def write_file(filename_stem, checkpoint): + # The checkpoint is called when `json.dump()` iterates the token data, + # which is after the file has been opened. + slow_token_data = mock.Mock( + to_dict=lambda: CheckpointDict(checkpoint, mock_token_data) + ) + cls(tmp_path / f"{filename_stem}.json").store_token_data_by_resource_server( + {"resource_server_1": slow_token_data} + ) + + elif cls is SimpleJSONFileAdapter: + + def write_file(filename_stem, checkpoint): + # As above: the checkpoint is called when `json.dump()` iterates + # the token data, which is after the file has been opened. + response = mock.Mock() + response.by_resource_server = { + "resource_server_1": CheckpointDict(checkpoint, mock_token_data) + } + cls(tmp_path / f"{filename_stem}.json").store(response) + + else: + raise NotImplementedError(cls) + + def run(): + overlap_controller = OverlappingWrites() + overlap_controller.run( + lambda: write_file("first", overlap_controller.first_checkpoint), + lambda: write_file("second", overlap_controller.second_checkpoint), + ) + + return run + + +@pytest.mark.xfail(IS_WINDOWS, reason="cannot set umask perms on Windows") +def test_overlapping_writes_leave_the_process_umask_alone(overlapping_file_writes): + with process_umask(0o022): + overlapping_file_writes() + assert current_umask() == 0o022 diff --git a/tests/functional/tokenstorage/v1/test_simplejson_file.py b/tests/functional/tokenstorage/v1/test_simplejson_file.py index 253d3fe0b..fd735fb5c 100644 --- a/tests/functional/tokenstorage/v1/test_simplejson_file.py +++ b/tests/functional/tokenstorage/v1/test_simplejson_file.py @@ -1,10 +1,12 @@ import os +import stat import pytest -from globus_sdk import __version__ +from globus_sdk import __version__, exc from globus_sdk.token_storage.legacy import SimpleJSONFileAdapter from tests.common import fast_json +from tests.common.overlapping_writes import current_umask, process_umask IS_WINDOWS = os.name == "nt" @@ -70,7 +72,65 @@ def test_store_perms(json_file, mock_response): assert not adapter.file_exists() adapter.store(mock_response) - # mode|0600 should be 0600 -- meaning that those are the maximal - # permissions given - st_mode = json_file.stat().st_mode & 0o777 # & 777 to remove extra bits - assert st_mode | 0o600 == 0o600 + assert stat.S_IMODE(json_file.stat().st_mode) == 0o600 + + +@pytest.mark.xfail(IS_WINDOWS, reason="cannot set umask perms on Windows") +def test_user_only_umask_is_deprecated_but_still_works(json_file): + adapter = SimpleJSONFileAdapter(json_file) + with process_umask(0o022): + with pytest.warns(exc.RemovedInV5Warning, match="user_only_umask") as record: + with adapter.user_only_umask(): + assert current_umask() == 0o177 + assert current_umask() == 0o022 + # the warning points at the caller, not at the SDK or at contextlib + assert record[0].filename == __file__ + + +@pytest.mark.xfail(IS_WINDOWS, reason="cannot set umask perms on Windows") +def test_store_perms_with_a_umask_that_denies_the_owner(json_file, mock_response): + adapter = SimpleJSONFileAdapter(json_file) + with process_umask(0o277): + adapter.store(mock_response) + # the owner can still write to the file + adapter.store(mock_response) + assert stat.S_IMODE(json_file.stat().st_mode) == 0o600 + + +@pytest.mark.xfail(IS_WINDOWS, reason="cannot set umask perms on Windows") +def test_store_does_not_change_the_perms_of_an_existing_file(json_file, mock_response): + adapter = SimpleJSONFileAdapter(json_file) + adapter.store(mock_response) + json_file.chmod(0o640) + adapter.store(mock_response) + assert stat.S_IMODE(json_file.stat().st_mode) == 0o640 + + +def test_owner_only_opener_in_append_mode_keeps_existing_data(json_file): + for data in ("one", "two"): + with open(json_file, "a", opener=SimpleJSONFileAdapter._owner_only_opener) as f: + f.write(data) + assert json_file.read_text() == "onetwo" + + +@pytest.mark.xfail(IS_WINDOWS, reason="cannot set umask perms on Windows") +@pytest.mark.parametrize("fchmod_is", ("missing", "failing")) +def test_owner_only_opener_works_when_fchmod_is_unavailable( + json_file, monkeypatch, fchmod_is +): + # Windows (before Python 3.13) has no `os.fchmod`, + # and some filesystems do not support changing modes + if fchmod_is == "missing": + monkeypatch.delattr(os, "fchmod") + else: + + def fchmod(fd, mode): + raise OSError("chmod is not supported") + + monkeypatch.setattr(os, "fchmod", fchmod) + + with process_umask(0o022): + with open(json_file, "w", opener=SimpleJSONFileAdapter._owner_only_opener) as f: + f.write("data") + assert stat.S_IMODE(json_file.stat().st_mode) == 0o600 + assert json_file.read_text() == "data" diff --git a/tests/functional/tokenstorage/v1/test_sqlite.py b/tests/functional/tokenstorage/v1/test_sqlite.py index ebcaca67e..f77693f0f 100644 --- a/tests/functional/tokenstorage/v1/test_sqlite.py +++ b/tests/functional/tokenstorage/v1/test_sqlite.py @@ -1,6 +1,12 @@ +import os +import stat + import pytest from globus_sdk.token_storage.legacy import SQLiteAdapter +from tests.common.overlapping_writes import process_umask + +IS_WINDOWS = os.name == "nt" @pytest.fixture @@ -195,3 +201,10 @@ def test_iter_namespaces(mock_response, db_file, make_adapter): "bar", "baz", } + + +@pytest.mark.xfail(IS_WINDOWS, reason="cannot set umask perms on Windows") +def test_created_file_perms_with_a_umask_that_denies_the_owner(db_file, make_adapter): + with process_umask(0o277): + make_adapter(db_file) + assert stat.S_IMODE(db_file.stat().st_mode) == 0o600 diff --git a/tests/functional/tokenstorage/v2/test_json_tokenstorage.py b/tests/functional/tokenstorage/v2/test_json_tokenstorage.py index 7f09bd3cc..cce035c81 100644 --- a/tests/functional/tokenstorage/v2/test_json_tokenstorage.py +++ b/tests/functional/tokenstorage/v2/test_json_tokenstorage.py @@ -1,11 +1,13 @@ import os +import stat import pytest -from globus_sdk import __version__ +from globus_sdk import __version__, exc from globus_sdk.token_storage import JSONTokenStorage from globus_sdk.token_storage.legacy import SimpleJSONFileAdapter from tests.common import fast_json +from tests.common.overlapping_writes import current_umask, process_umask IS_WINDOWS = os.name == "nt" @@ -84,10 +86,7 @@ def test_store_perms(json_file, mock_response): assert not adapter.file_exists() adapter.store_token_response(mock_response) - # mode|0600 should be 0600 -- meaning that those are the maximal - # permissions given - st_mode = json_file.stat().st_mode & 0o777 # & 777 to remove extra bits - assert st_mode | 0o600 == 0o600 + assert stat.S_IMODE(json_file.stat().st_mode) == 0o600 def test_migrate_from_v1_adapter(json_file, mock_response): @@ -105,3 +104,68 @@ def test_migrate_from_v1_adapter(json_file, mock_response): new_adapter.store_token_response(mock_response) data = fast_json.loads(json_file.read_text()) assert data["format_version"] == "2.0" + + +@pytest.mark.xfail(IS_WINDOWS, reason="cannot set umask perms on Windows") +def test_user_only_umask_is_deprecated_but_still_works(json_file): + adapter = JSONTokenStorage(json_file) + with process_umask(0o022): + with pytest.warns(exc.RemovedInV5Warning, match="user_only_umask") as record: + with adapter.user_only_umask(): + assert current_umask() == 0o177 + assert current_umask() == 0o022 + # the warning points at the caller, not at the SDK or at contextlib + assert record[0].filename == __file__ + + +@pytest.mark.xfail(IS_WINDOWS, reason="cannot set umask perms on Windows") +def test_store_perms_with_a_umask_that_denies_the_owner( + json_file, mock_token_data_by_resource_server +): + adapter = JSONTokenStorage(json_file) + with process_umask(0o277): + adapter.store_token_data_by_resource_server(mock_token_data_by_resource_server) + # the owner can still write to the file + adapter.store_token_data_by_resource_server(mock_token_data_by_resource_server) + assert stat.S_IMODE(json_file.stat().st_mode) == 0o600 + + +@pytest.mark.xfail(IS_WINDOWS, reason="cannot set umask perms on Windows") +def test_store_does_not_change_the_perms_of_an_existing_file( + json_file, mock_token_data_by_resource_server +): + adapter = JSONTokenStorage(json_file) + adapter.store_token_data_by_resource_server(mock_token_data_by_resource_server) + json_file.chmod(0o640) + adapter.store_token_data_by_resource_server(mock_token_data_by_resource_server) + assert stat.S_IMODE(json_file.stat().st_mode) == 0o640 + + +def test_owner_only_opener_in_append_mode_keeps_existing_data(json_file): + for data in ("one", "two"): + with open(json_file, "a", opener=JSONTokenStorage._owner_only_opener) as f: + f.write(data) + assert json_file.read_text() == "onetwo" + + +@pytest.mark.xfail(IS_WINDOWS, reason="cannot set umask perms on Windows") +@pytest.mark.parametrize("fchmod_is", ("missing", "failing")) +def test_owner_only_opener_works_when_fchmod_is_unavailable( + json_file, monkeypatch, fchmod_is +): + # Windows (before Python 3.13) has no `os.fchmod`, + # and some filesystems do not support changing modes + if fchmod_is == "missing": + monkeypatch.delattr(os, "fchmod") + else: + + def fchmod(fd, mode): + raise OSError("chmod is not supported") + + monkeypatch.setattr(os, "fchmod", fchmod) + + with process_umask(0o022): + with open(json_file, "w", opener=JSONTokenStorage._owner_only_opener) as f: + f.write("data") + assert stat.S_IMODE(json_file.stat().st_mode) == 0o600 + assert json_file.read_text() == "data" diff --git a/tests/functional/tokenstorage/v2/test_sqlite_tokenstorage.py b/tests/functional/tokenstorage/v2/test_sqlite_tokenstorage.py index 21c505efb..4b3e867d9 100644 --- a/tests/functional/tokenstorage/v2/test_sqlite_tokenstorage.py +++ b/tests/functional/tokenstorage/v2/test_sqlite_tokenstorage.py @@ -1,8 +1,14 @@ +import os +import stat + import pytest from globus_sdk import exc from globus_sdk.token_storage import SQLiteTokenStorage from globus_sdk.token_storage.legacy import SQLiteAdapter +from tests.common.overlapping_writes import process_umask + +IS_WINDOWS = os.name == "nt" @pytest.fixture @@ -130,3 +136,10 @@ def test_migrate_from_v1_adapter(mock_response, db_file, make_adapter): assert ( new_adapter.get_token_data("resource_server_2").access_token == "access_token_2" ) + + +@pytest.mark.xfail(IS_WINDOWS, reason="cannot set umask perms on Windows") +def test_created_file_perms_with_a_umask_that_denies_the_owner(db_file, make_adapter): + with process_umask(0o277): + make_adapter(db_file) + assert stat.S_IMODE(db_file.stat().st_mode) == 0o600