diff --git a/b2sdk/_internal/account_info/sqlite_account_info.py b/b2sdk/_internal/account_info/sqlite_account_info.py index c618dcd9..16c2a57e 100644 --- a/b2sdk/_internal/account_info/sqlite_account_info.py +++ b/b2sdk/_internal/account_info/sqlite_account_info.py @@ -75,6 +75,8 @@ def __init__(self, file_name=None, last_upgrade_to_run=None, profile: str | None :param int last_upgrade_to_run: For testing only, override the auto-update on the db. """ self.thread_local = threading.local() + self._connections = set() + self._connections_lock = threading.Lock() self.filename = self.get_user_account_info_path(file_name=file_name, profile=profile) logger.debug('%s file path to use: %s', self.__class__.__name__, self.filename) @@ -109,7 +111,7 @@ def _get_xdg_config_path(cls) -> str | None: @classmethod def get_user_account_info_path(cls, file_name: str | None = None, profile: str | None = None): - if profile and not B2_ACCOUNT_INFO_PROFILE_NAME_REGEXP.match(profile): + if profile and not B2_ACCOUNT_INFO_PROFILE_NAME_REGEXP.fullmatch(profile): raise ValueError(f'Invalid profile name: {profile}') profile_file = B2_ACCOUNT_INFO_PROFILE_FILE.format(profile=profile) if profile else None @@ -130,7 +132,7 @@ def get_user_account_info_path(cls, file_name: str | None = None, profile: str | elif profile and os.path.exists(profile_file): user_account_info_path = profile_file elif xdg_config_path: - os.makedirs(os.path.join(xdg_config_path, 'b2'), mode=0o755, exist_ok=True) + os.makedirs(os.path.join(xdg_config_path, 'b2'), mode=0o700, exist_ok=True) file_name = f'db-{profile}.sqlite' if profile else 'account_info' user_account_info_path = os.path.join(xdg_config_path, 'b2', file_name) @@ -203,17 +205,17 @@ def _validate_database(self, last_upgrade_to_run=None): raise CorruptAccountInfo(self.filename) def _get_connection(self): - """ - Connections to sqlite cannot be shared across threads. - """ - try: - return self.thread_local.connection - except AttributeError: - self.thread_local.connection = self._connect() - return self.thread_local.connection + """Return a connection owned by the calling thread, reopening it after close().""" + with self._connections_lock: + connection = getattr(self.thread_local, 'connection', None) + if connection not in self._connections: + connection = self._connect() + self._connections.add(connection) + self.thread_local.connection = connection + return connection def _connect(self): - return sqlite3.connect(self.filename, isolation_level='EXCLUSIVE') + return sqlite3.connect(self.filename, isolation_level='EXCLUSIVE', check_same_thread=False) @contextmanager def _temp_connection(self): @@ -226,12 +228,13 @@ def _temp_connection(self): conn.close() def close(self): - connection = getattr(self.thread_local, 'connection', None) - if connection is None: - return - - connection.close() - del self.thread_local.connection + """Close cached connections after callers have finished using them.""" + with self._connections_lock: + for connection in self._connections: + connection.close() + self._connections.clear() + if hasattr(self.thread_local, 'connection'): + del self.thread_local.connection def _create_database(self, last_upgrade_to_run): """ diff --git a/changelog.d/+sqlite-account-info-safety.fixed.md b/changelog.d/+sqlite-account-info-safety.fixed.md new file mode 100644 index 00000000..b156489a --- /dev/null +++ b/changelog.d/+sqlite-account-info-safety.fixed.md @@ -0,0 +1 @@ +Protect new account configuration directories, validate profile names, and close cached SQLite connections across threads. diff --git a/test/unit/account_info/test_regression_sqlite.py b/test/unit/account_info/test_regression_sqlite.py index eb48454a..8185c775 100644 --- a/test/unit/account_info/test_regression_sqlite.py +++ b/test/unit/account_info/test_regression_sqlite.py @@ -29,7 +29,7 @@ SqliteAccountInfo, ) -from test.known_defect import DefectStillPresent, expect, known_defect +from test.known_defect import known_defect pytestmark = [pytest.mark.apiver(from_ver=2)] @@ -59,11 +59,6 @@ def test_c47_existing_legacy_profile_database_is_used_with_xdg(self, home, monke class TestThreadConnections: - @known_defect( - 'C37', - "close() closes only the calling thread's database connection", - raises=DefectStillPresent, - ) def test_c37_close_closes_connections_of_other_threads(self, tmp_path): """C37: close() must close the database connections opened by other threads, too.""" info = SqliteAccountInfo(file_name=str(tmp_path / 'acct.sqlite')) @@ -86,4 +81,4 @@ def worker(): closed_by_main.set() thread.join(timeout=10) assert 'closed' in result, 'the worker thread did not finish its check' - expect(result['closed'], 'connection of another thread is still open after close()') + assert result['closed'], 'connection of another thread is still open after close()' diff --git a/test/unit/account_info/test_sqlite_low_audit.py b/test/unit/account_info/test_sqlite_low_audit.py new file mode 100644 index 00000000..58a745c4 --- /dev/null +++ b/test/unit/account_info/test_sqlite_low_audit.py @@ -0,0 +1,98 @@ +###################################################################### +# +# File: test/unit/account_info/test_sqlite_low_audit.py +# +# Copyright 2026 Backblaze Inc. All Rights Reserved. +# +# License https://www.backblaze.com/using_b2_code.html +# +###################################################################### +import os +import stat +import threading + +import pytest +from apiver_deps import SqliteAccountInfo + + +@pytest.mark.skipif(os.name == 'nt', reason='POSIX directory permissions') +def test_c58_new_config_directory_is_private(tmp_path, monkeypatch): + monkeypatch.setenv('HOME', str(tmp_path)) + monkeypatch.setenv('XDG_CONFIG_HOME', str(tmp_path / 'config')) + monkeypatch.delenv('B2_ACCOUNT_INFO', raising=False) + SqliteAccountInfo.get_user_account_info_path(profile='test') + mode = stat.S_IMODE((tmp_path / 'config' / 'b2').stat().st_mode) + assert mode == 0o700 + + +def test_c23_profile_rejects_trailing_path(tmp_path, monkeypatch): + monkeypatch.setenv('HOME', str(tmp_path)) + monkeypatch.setenv('XDG_CONFIG_HOME', str(tmp_path / 'config')) + monkeypatch.delenv('B2_ACCOUNT_INFO', raising=False) + with pytest.raises(ValueError, match='Invalid profile name'): + SqliteAccountInfo.get_user_account_info_path(profile='ok/../../x') + assert not (tmp_path / 'config').exists() + + +def test_c37_worker_reopens_after_another_thread_closes(tmp_path): + info = SqliteAccountInfo(file_name=str(tmp_path / 'account.sqlite')) + opened, closed = threading.Event(), threading.Event() + result = [] + + def worker(): + conn = info._get_connection() + opened.set() + if closed.wait(10): + try: + new_conn = info._get_connection() + result.append((new_conn is not conn, new_conn.execute('select 1').fetchone())) + except Exception as exc: + result.append(exc) + + thread = threading.Thread(target=worker) + thread.start() + try: + assert opened.wait(10) + info.close() + finally: + closed.set() + thread.join(10) + assert not thread.is_alive() + assert result == [(True, (1,))] + info.close() + + +def test_c37_closes_all_worker_connections_and_is_idempotent(tmp_path): + info = SqliteAccountInfo(file_name=str(tmp_path / 'account.sqlite')) + connections = [] + ready = threading.Barrier(4) + release = threading.Event() + outcomes = [] + + def worker(): + conn = info._get_connection() + connections.append(conn) + ready.wait(timeout=10) + if release.wait(10): + try: + conn.execute('select 1') + except Exception as exc: + outcomes.append(type(exc).__name__) + else: + outcomes.append('open') + + threads = [threading.Thread(target=worker) for _ in range(3)] + for thread in threads: + thread.start() + try: + ready.wait(timeout=10) + info.close() + info.close() + finally: + release.set() + for thread in threads: + thread.join(10) + assert all(not thread.is_alive() for thread in threads) + assert len({id(conn) for conn in connections}) == 3 + assert outcomes == ['ProgrammingError'] * 3 + info.close()