Skip to content

Resolve symlinks in check_contained and normalize local paths in rsync / ReferenceFileSystem.get - #2522

Open
liuc99 wants to merge 1 commit into
fsspec:masterfrom
liuc99:fix-path-containment
Open

liuc99 wants to merge 1 commit into
fsspec:masterfrom
liuc99:fix-path-containment

Conversation

@liuc99

@liuc99 liuc99 commented Nov 26, 2026

Copy link
Copy Markdown

Summary

Follow-up improvements to destination path containment checks (#2299, #2300):

  1. Symlink-aware containment for paths with .. (fsspec/utils.py):

    • Keep the lexical os.path.abspath check in check_contained for all paths.
    • When a destination path contains .. segments below root (or does not share a literal prefix with root), lazily verify os.path.realpath(path) against os.path.realpath(root) as well.
    • Preserves existing behavior for paths without .. (including user-configured symlinks and avoiding per-path lstat overhead on large bulk copies) as well as benign in-tree .. segments.
  2. Anchored path mapping and protocol normalization in generic.rsync (fsspec/generic.py):

    • Replace str.replace(..., 1) with anchored prefix extraction and joining that handles both root_marker == "/" (memory://) and root_marker == "" (dir://, reference://, zip://) bare scheme roots.
    • Validate relative .. segment depth directly on extracted relative names across both local and non-local backends.
    • Resolve wrapped local destinations (LocalFileSystem, DirFileSystem, AsyncFileSystemWrapper, ChainedFileSystem) to stripped OS paths before calling check_contained, rather than passing file:// URLs to os.path.abspath / os.path.realpath.
    • Build to_delete only when delete_missing is enabled.
  3. Normalize local destination paths in ReferenceFileSystem.get (fsspec/implementations/reference.py):

    • Strip local protocol prefixes and expand ~ via LocalFileSystem._strip_protocol before other_paths and check_contained so the checked path matches the path passed to pipe_file.

Tests

  • fsspec/tests/test_utils.py:
    • test_check_contained_resolves_symlinks_with_dotdot (POSIX)
    • test_check_contained_preserves_outward_symlink_without_dotdot
    • test_get_resolves_symlinks_in_containment (POSIX)
  • fsspec/tests/test_generic.py:
    • test_rsync_rejects_names_escaping_memory_destination
    • test_rsync_passes_local_paths_to_check_contained
    • test_rsync_resolves_symlinks_in_containment (POSIX)
    • test_rsync_wrapped_local_dirfs_containment (POSIX)
    • test_rsync_wrapped_local_async_wrapper_containment (POSIX)
    • test_rsync_bare_root_dirfs
  • fsspec/implementations/tests/test_reference.py:
    • test_get_file_url_destination_containment
    • test_get_file_url_resolves_symlinks_in_containment (POSIX)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant