From 6cf03e081ceb93e7602e4474dcbf01b6a8d79e20 Mon Sep 17 00:00:00 2001 From: Felipe Fumero Date: Sat, 21 Nov 2026 16:52:57 -0600 Subject: [PATCH] fix: harden local path and filename validation --- b2sdk/_internal/scan/folder.py | 12 ++++--- b2sdk/_internal/utils/__init__.py | 2 ++ b2sdk/_internal/utils/filesystem.py | 8 +++++ changelog.d/+path-validation.fixed.md | 1 + test/unit/scan/test_folder.py | 34 +++++++++++++++++++ test/unit/scan/test_folder_traversal.py | 36 ++------------------- test/unit/scan/test_symlink_device_inode.py | 4 +-- test/unit/utils/test_filename_controls.py | 25 ++++++++++++++ test/unit/utils/test_filesystem.py | 30 +++++++++++++++++ 9 files changed, 113 insertions(+), 39 deletions(-) create mode 100644 changelog.d/+path-validation.fixed.md create mode 100644 test/unit/utils/test_filename_controls.py diff --git a/b2sdk/_internal/scan/folder.py b/b2sdk/_internal/scan/folder.py index 3b01db59..c8260ea8 100644 --- a/b2sdk/_internal/scan/folder.py +++ b/b2sdk/_internal/scan/folder.py @@ -165,11 +165,15 @@ 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 path between the new full_path and self.root - common_path = os.path.commonpath([full_path, self.root]) + # Resolve symlinks in both paths before checking containment. + resolved_root = os.path.realpath(self.root) + resolved_path = os.path.realpath(full_path) + try: + common_path = os.path.commonpath([resolved_path, resolved_root]) + except ValueError: # Paths on different Windows drives. + raise UnsupportedFilename('illegal file name', full_path) from None - # Ensure the new full_path is inside the self.root directory - if common_path != self.root: + if os.path.normcase(common_path) != os.path.normcase(resolved_root): raise UnsupportedFilename('illegal file name', full_path) return full_path diff --git a/b2sdk/_internal/utils/__init__.py b/b2sdk/_internal/utils/__init__.py index d6819864..d3fdc8b4 100644 --- a/b2sdk/_internal/utils/__init__.py +++ b/b2sdk/_internal/utils/__init__.py @@ -253,6 +253,8 @@ def validate_b2_file_name(name): raise ValueError("file names must not contain '\\'") if '//' in name: raise ValueError("file names must not contain '//'") + if any(ord(char) < 32 for char in name): + raise ValueError('file names must not contain ASCII control characters') if chr(127) in name: raise ValueError('file names must not contain DEL') if any(250 < len(segment) for segment in name_utf8.split(b'/')): diff --git a/b2sdk/_internal/utils/filesystem.py b/b2sdk/_internal/utils/filesystem.py index f9a65e26..997639bf 100644 --- a/b2sdk/_internal/utils/filesystem.py +++ b/b2sdk/_internal/utils/filesystem.py @@ -16,6 +16,7 @@ DRIVE_MATCHER = re.compile(r'^([A-Za-z]):([/\\])') ABSOLUTE_PATH_MATCHER = re.compile(r'^(/)|^(\\)') +WINDOWS_DEVICE_NAMES = re.compile(r'^(?:CON|PRN|AUX|NUL|COM[1-9¹²³]|LPT[1-9¹²³])$', re.I) RELATIVE_PATH_MATCHER = re.compile( # "abc" and "xyz" represent anything, including "nothing" r'^(\.\.[/\\])|' # ../abc or ..\abc @@ -63,3 +64,10 @@ def validate_b2_file_name_as_path(file_name: str) -> None: if _IS_WINDOWS and DRIVE_MATCHER.search(file_name): raise ValueError('File names containing Windows drive letters are not supported') + + if _IS_WINDOWS: + if ':' in file_name: + raise ValueError('File names containing Windows stream separators are not supported') + for part in re.split(r'[/\\]', file_name): + if WINDOWS_DEVICE_NAMES.fullmatch(part.split('.')[0].rstrip(' ')): + raise ValueError('File names containing Windows device names are not supported') diff --git a/changelog.d/+path-validation.fixed.md b/changelog.d/+path-validation.fixed.md new file mode 100644 index 00000000..efc4d871 --- /dev/null +++ b/changelog.d/+path-validation.fixed.md @@ -0,0 +1 @@ +Reject local paths that resolve outside the sync root, Windows device names and streams, and ASCII control characters in file names. diff --git a/test/unit/scan/test_folder.py b/test/unit/scan/test_folder.py index ecfeb9b7..d887eab8 100644 --- a/test/unit/scan/test_folder.py +++ b/test/unit/scan/test_folder.py @@ -44,3 +44,37 @@ def test_make_full_path_invalid_prefix( with pytest.raises(UnsupportedFilename): folder.make_full_path(str(file_path)) + + +@pytest.mark.skipif( + platform.system() == 'Windows' and platform.python_implementation() == 'PyPy', + reason='Symlinks not supported on PyPy/Windows', +) +def test_make_full_path_rejects_symlink_outside_root(tmp_path): + root = tmp_path / 'root' + root.mkdir() + outside = tmp_path / 'outside' + outside.mkdir() + (root / 'link').symlink_to(outside, target_is_directory=True) + + with pytest.raises(UnsupportedFilename): + LocalFolder(root).make_full_path('link/new-file.txt') + + assert not (outside / 'new-file.txt').exists() + + +@pytest.mark.skipif( + platform.system() == 'Windows' and platform.python_implementation() == 'PyPy', + reason='Symlinks not supported on PyPy/Windows', +) +def test_make_full_path_accepts_internal_link_and_symlinked_root(tmp_path): + root = tmp_path / 'root' + root.mkdir() + (root / 'inside').mkdir() + (root / 'link').symlink_to(root / 'inside', target_is_directory=True) + root_link = tmp_path / 'root-link' + root_link.symlink_to(root, target_is_directory=True) + + assert LocalFolder(root_link).make_full_path('link/new-file.txt') == str( + root_link / 'link' / 'new-file.txt' + ) diff --git a/test/unit/scan/test_folder_traversal.py b/test/unit/scan/test_folder_traversal.py index 5c601b71..26c3efae 100644 --- a/test/unit/scan/test_folder_traversal.py +++ b/test/unit/scan/test_folder_traversal.py @@ -20,6 +20,7 @@ import pytest +from b2sdk._internal.scan.exception import UnsupportedFilename from b2sdk._internal.scan.folder import LocalFolder from b2sdk._internal.scan.policies import ScanPoliciesManager from b2sdk._internal.scan.report import ProgressReport @@ -287,39 +288,8 @@ def test_folder_with_symlink_to_parent(self, tmp_path): folder = LocalFolder(str(tmp_path / 'parent' / 'child')) - local_paths = folder.all_files(reporter=MagicMock()) - absolute_paths = [path.absolute_path for path in list(local_paths)] - - assert absolute_paths == [ - fix_windows_path_limit(str(tmp_path / 'parent' / 'child' / 'file4.txt')), - fix_windows_path_limit(str(tmp_path / 'parent' / 'child' / 'grandchild' / 'file5.txt')), - fix_windows_path_limit( - str( - tmp_path - / 'parent' - / 'child' - / 'grandchild' - / 'symlink_dir' - / 'child' - / 'file4.txt' - ) - ), - fix_windows_path_limit( - str( - tmp_path - / 'parent' - / 'child' - / 'grandchild' - / 'symlink_dir' - / 'child' - / 'grandchild' - / 'file5.txt' - ) - ), - fix_windows_path_limit( - str(tmp_path / 'parent' / 'child' / 'grandchild' / 'symlink_dir' / 'file3.txt') - ), - ] + with pytest.raises(UnsupportedFilename, match='illegal file name'): + list(folder.all_files(reporter=MagicMock())) @pytest.mark.skipif( platform.system() == 'Windows' and platform.python_implementation() == 'PyPy', diff --git a/test/unit/scan/test_symlink_device_inode.py b/test/unit/scan/test_symlink_device_inode.py index 4638dfd1..5fc30950 100644 --- a/test/unit/scan/test_symlink_device_inode.py +++ b/test/unit/scan/test_symlink_device_inode.py @@ -15,7 +15,7 @@ def test_symlink_targets_with_same_inode_on_different_devices_are_scanned(tmp_path, monkeypatch): first = tmp_path / 'first' - second = tmp_path / 'second' + second = first / 'second' first.mkdir() second.mkdir() (second / 'file.txt').write_text('contents') @@ -37,4 +37,4 @@ def stat_with_device_collision(path, *args, **kwargs): monkeypatch.setattr(Path, 'stat', stat_with_device_collision) files = list(LocalFolder(root).all_files(None)) - assert [path.relative_path for path in files] == ['next/file.txt'] + assert [path.relative_path for path in files] == ['next/file.txt', 'second/file.txt'] diff --git a/test/unit/utils/test_filename_controls.py b/test/unit/utils/test_filename_controls.py new file mode 100644 index 00000000..503a24a9 --- /dev/null +++ b/test/unit/utils/test_filename_controls.py @@ -0,0 +1,25 @@ +###################################################################### +# +# File: test/unit/utils/test_filename_controls.py +# +# Copyright 2026 Backblaze Inc. All Rights Reserved. +# +# License https://www.backblaze.com/using_b2_code.html +# +###################################################################### +import pytest + +from b2sdk._internal.utils import validate_b2_file_name + + +@pytest.mark.parametrize('codepoint', range(32)) +def test_rejects_ascii_control_characters(codepoint): + with pytest.raises(ValueError): + validate_b2_file_name(f'folder/name{chr(codepoint)}.txt') + + +def test_del_is_rejected_and_printable_boundaries_are_allowed(): + with pytest.raises(ValueError): + validate_b2_file_name('file\x7f.txt') + validate_b2_file_name('folder/space and tilde~.txt') + validate_b2_file_name('folder/café.txt') diff --git a/test/unit/utils/test_filesystem.py b/test/unit/utils/test_filesystem.py index 0b97cf3a..831d6694 100644 --- a/test/unit/utils/test_filesystem.py +++ b/test/unit/utils/test_filesystem.py @@ -96,3 +96,33 @@ def test_drive_letter_disallowed_on_windows(self): match=r'drive letters', ): validate_b2_file_name_as_path(r'C:\file.txt') + + +@pytest.mark.parametrize( + 'file_name', + ['CON', 'folder/nul.txt', r'folder\PRN.log', 'aux ', 'dir/COM1.txt', 'lpt³', 'file:stream'], +) +def test_windows_device_names_and_streams_are_rejected(monkeypatch, file_name): + from b2sdk._internal.utils import filesystem + + monkeypatch.setattr(filesystem, '_IS_WINDOWS', True) + with pytest.raises(ValueError): + filesystem.validate_b2_file_name_as_path(file_name) + + +@pytest.mark.parametrize( + 'file_name', ['console.txt', 'com0', 'com10', 'lpt40.logfile', 'folder/file.txt'] +) +def test_windows_regular_names_are_allowed(monkeypatch, file_name): + from b2sdk._internal.utils import filesystem + + monkeypatch.setattr(filesystem, '_IS_WINDOWS', True) + filesystem.validate_b2_file_name_as_path(file_name) + + +@pytest.mark.parametrize('file_name', ['CON', 'folder/nul.txt', 'file:stream']) +def test_unix_allows_windows_device_names_and_stream_characters(monkeypatch, file_name): + from b2sdk._internal.utils import filesystem + + monkeypatch.setattr(filesystem, '_IS_WINDOWS', False) + filesystem.validate_b2_file_name_as_path(file_name)