From fe1bbcbff5de13bf7f73fa4c2f91bd7a30cd483a Mon Sep 17 00:00:00 2001 From: Olzhas Arystanov Date: Tue, 10 Jun 2025 00:50:58 +0500 Subject: [PATCH 1/3] Fix bucket name mapping in `RawSimulator.authorize_account` --- b2sdk/_internal/raw_simulator.py | 8 ++++++++ changelog.d/+raw_simulator_bucket_name_mapping.fixed.md | 1 + 2 files changed, 9 insertions(+) create mode 100644 changelog.d/+raw_simulator_bucket_name_mapping.fixed.md diff --git a/b2sdk/_internal/raw_simulator.py b/b2sdk/_internal/raw_simulator.py index aa757fcd3..291366422 100644 --- a/b2sdk/_internal/raw_simulator.py +++ b/b2sdk/_internal/raw_simulator.py @@ -1404,6 +1404,14 @@ def authorize_account(self, realm_url, application_key_id, application_key): allowed = key_sim.get_allowed() + buckets = allowed.get('buckets') + if buckets: + for item in buckets: + try: + item['name'] = self.bucket_id_to_bucket[item['id']].bucket_name + except KeyError: + item['name'] = None + return dict( accountId=key_sim.account_id, authorizationToken=auth_token, diff --git a/changelog.d/+raw_simulator_bucket_name_mapping.fixed.md b/changelog.d/+raw_simulator_bucket_name_mapping.fixed.md new file mode 100644 index 000000000..144420e10 --- /dev/null +++ b/changelog.d/+raw_simulator_bucket_name_mapping.fixed.md @@ -0,0 +1 @@ +Fix bucket name mapping in `RawSimulator.authorize_account()`. From ac31d3ff5d7c82e636a9c2aa142b45909ecbac36 Mon Sep 17 00:00:00 2001 From: Olzhas Arystanov Date: Tue, 10 Jun 2025 00:58:01 +0500 Subject: [PATCH 2/3] Fix broken `StubAccountInfo.list_bucket_names_ids` --- b2sdk/_internal/account_info/stub.py | 2 +- changelog.d/+stub_account_info_list_bucket_ids.fixed.md | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 changelog.d/+stub_account_info_list_bucket_ids.fixed.md diff --git a/b2sdk/_internal/account_info/stub.py b/b2sdk/_internal/account_info/stub.py index c052ed3ec..107336389 100644 --- a/b2sdk/_internal/account_info/stub.py +++ b/b2sdk/_internal/account_info/stub.py @@ -77,7 +77,7 @@ def get_bucket_name_or_none_from_bucket_id(self, bucket_id: str) -> str | None: return None def list_bucket_names_ids(self) -> list[tuple[str, str]]: - return list((bucket.bucket_name, bucket.id_) for bucket in self.buckets.values()) + return list((bucket.name, bucket.id_) for bucket in self.buckets.values()) def save_bucket(self, bucket): self.buckets[bucket.id_] = bucket diff --git a/changelog.d/+stub_account_info_list_bucket_ids.fixed.md b/changelog.d/+stub_account_info_list_bucket_ids.fixed.md new file mode 100644 index 000000000..44bdd296b --- /dev/null +++ b/changelog.d/+stub_account_info_list_bucket_ids.fixed.md @@ -0,0 +1 @@ +Fix broken `StubAccountInfo.list_bucket_names_ids()`. \ No newline at end of file From cdd4d5babdee8eea590f433ff58b1d749a640237 Mon Sep 17 00:00:00 2001 From: Olzhas Arystanov Date: Tue, 10 Jun 2025 01:23:26 +0500 Subject: [PATCH 3/3] Fix incorrect path check in `LocalFolder.make_full_path` --- b2sdk/_internal/scan/folder.py | 6 +-- .../+local_folder_make_full_path.fixed.md | 1 + test/unit/scan/test_folder.py | 46 +++++++++++++++++++ 3 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 changelog.d/+local_folder_make_full_path.fixed.md create mode 100644 test/unit/scan/test_folder.py diff --git a/b2sdk/_internal/scan/folder.py b/b2sdk/_internal/scan/folder.py index 62d4500d5..38dda43f7 100644 --- a/b2sdk/_internal/scan/folder.py +++ b/b2sdk/_internal/scan/folder.py @@ -180,11 +180,11 @@ def make_full_path(self, file_name): # Generate the full path to the file full_path = os.path.normpath(os.path.join(self.root, file_name)) - # Get the common prefix between the new full_path and self.root - common_prefix = os.path.commonprefix([full_path, self.root]) + # Get the common path between the new full_path and self.root + common_path = os.path.commonpath([full_path, self.root]) # Ensure the new full_path is inside the self.root directory - if common_prefix != self.root: + if common_path != self.root: raise UnsupportedFilename('illegal file name', full_path) return full_path diff --git a/changelog.d/+local_folder_make_full_path.fixed.md b/changelog.d/+local_folder_make_full_path.fixed.md new file mode 100644 index 000000000..9daaecc17 --- /dev/null +++ b/changelog.d/+local_folder_make_full_path.fixed.md @@ -0,0 +1 @@ +Fix incorrect path check in `LocalFolder.make_full_path`. diff --git a/test/unit/scan/test_folder.py b/test/unit/scan/test_folder.py new file mode 100644 index 000000000..ecfeb9b78 --- /dev/null +++ b/test/unit/scan/test_folder.py @@ -0,0 +1,46 @@ +###################################################################### +# +# File: test/unit/scan/test_folder.py +# +# Copyright 2025 Backblaze Inc. All Rights Reserved. +# +# License https://www.backblaze.com/using_b2_code.html +# +###################################################################### +from __future__ import annotations + +import platform +from pathlib import Path + +import pytest + +from b2sdk._internal.scan.exception import UnsupportedFilename +from b2sdk._internal.scan.folder import LocalFolder + + +@pytest.mark.skipif( + platform.system() == 'Windows', + reason="Windows doesn't allow / or \\ in filenames", +) +class TestFolder: + @pytest.fixture + def root_path(self, tmp_path: Path): + return tmp_path / 'dir' + + @pytest.fixture + def folder(self, root_path: Path): + return LocalFolder(str(root_path)) + + @pytest.mark.parametrize('file_path_str', ['dir/foo.txt', 'dir/foo/bar.txt', 'foo.txt']) + def test_make_full_path(self, folder: LocalFolder, root_path: Path, file_path_str: str): + file_path = root_path / file_path_str + assert folder.make_full_path(str(file_path)) == str(root_path / file_path_str) + + @pytest.mark.parametrize('file_path_str', ['invalid/test.txt', 'dirinvalid.txt']) + def test_make_full_path_invalid_prefix( + self, folder: LocalFolder, tmp_path: Path, file_path_str: str + ): + file_path = tmp_path / file_path_str + + with pytest.raises(UnsupportedFilename): + folder.make_full_path(str(file_path))