Skip to content
Merged
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
12 changes: 8 additions & 4 deletions b2sdk/_internal/scan/folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions b2sdk/_internal/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'/')):
Expand Down
8 changes: 8 additions & 0 deletions b2sdk/_internal/utils/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')
1 change: 1 addition & 0 deletions changelog.d/+path-validation.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Reject local paths that resolve outside the sync root, Windows device names and streams, and ASCII control characters in file names.
34 changes: 34 additions & 0 deletions test/unit/scan/test_folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
)
36 changes: 3 additions & 33 deletions test/unit/scan/test_folder_traversal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions test/unit/scan/test_symlink_device_inode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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']
25 changes: 25 additions & 0 deletions test/unit/utils/test_filename_controls.py
Original file line number Diff line number Diff line change
@@ -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')
30 changes: 30 additions & 0 deletions test/unit/utils/test_filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Loading