github-actions[bot] commented on code in PR #41748:
URL: https://github.com/apache/beam/pull/41748#discussion_r4731794852


##########
sdks/python/apache_beam/io/localfilesystem.py:
##########
@@ -221,19 +226,66 @@ def copy(self, source_file_names, destination_file_names):
         "be equal in length")
     assert len(source_file_names) == len(destination_file_names), err_msg
 
+    def _absolute_path(path):
+      path = Path(os.fsdecode(path))
+      if path.drive and not path.root:
+        # Expand the drive anchor without collapsing symlink-sensitive '..'.
+        path = Path(os.path.abspath(path.drive + '.'), *path.parts[1:])
+      return path.absolute()
+
+    def _ancestors(path):
+      pending = [_absolute_path(path)]
+      seen_links = set()
+      while pending:
+        current = pending.pop()
+        yield from current.parents
+        for entry in (current, *current.parents):
+          try:
+            target = entry.readlink()
+          except OSError as error:
+            # Windows 4390 means the path is not a reparse point.
+            if (error.errno in (errno.EINVAL, errno.ENOENT, errno.ENOTDIR) or
+                getattr(error, 'winerror', None) == 4390):
+              continue
+            raise
+          link = (str(entry.parent.resolve()), entry.name)
+          if link not in seen_links:
+            seen_links.add(link)
+            pending.append(_absolute_path(entry.parent / target))
+
     def _copy_path(source, destination):
       """Recursively copy the file tree from the source to the destination
       """
       try:
-        if os.path.exists(destination):
-          if os.path.samefile(source, destination):
-            raise shutil.SameFileError(
-                '%r and %r are the same file' % (source, destination))
-          if os.path.isdir(destination):
+        source_is_directory = os.path.isdir(source)
+        destination_is_directory = os.path.isdir(destination)
+        destination_exists = os.path.exists(destination)
+        if destination_exists and os.path.samefile(source, destination):
+          raise shutil.SameFileError(
+              '%r and %r are the same file' % (source, destination))
+        if source_is_directory or destination_is_directory:
+          # Include entries and intermediate link targets needed by the copy.
+          source_in_destination = destination_is_directory and any(
+              parent.exists() and os.path.samefile(parent, destination)
+              for parent in _ancestors(source))
+          # Normalize the output location so harmless sibling paths using .. 
work.
+          destination_path = _absolute_path(destination)
+          destination_entry = (
+              destination_path.parent.resolve() / destination_path.name)
+          destination_in_source = source_is_directory and any(
+              parent.exists() and os.path.samefile(parent, source)
+              for output in (destination_entry, destination_path.resolve())
+              for parent in output.parents)
+          if source_in_destination or destination_in_source:
+            raise IOError(
+                'Cannot copy between ancestor and descendant paths: %r, %r' %
+                (source, destination))
+        if destination_exists:
+          if destination_is_directory:

Review Comment:
   **🟡 Medium**: When `source` is a directory (`source_is_directory` is `True`) 
and `destination` is a broken symlink with a symlink cycle (so 
`destination_is_directory` is `False` and `destination_exists` is `False`), 
calling `destination_path.resolve()` on line 284 will raise `RuntimeError: 
Symlink loop from ...` on Python < 3.13 instead of `OSError` (note that 
`test_copy_disjoint_link_cycle_fails_without_mutating_source` currently catches 
`RuntimeError` only because `_copy_path` is wrapped in `except Exception as e:` 
in `copy()`, rather than turning it into an `IOError`).
   
   Additionally, because `destination_in_source` only needs 
`destination_path.resolve()` when `destination` actually resolves to an 
existing directory/file (or you can catch `RuntimeError` or check 
`destination_exists` before resolving `destination_path`), guarding 
`destination_path.resolve()` with `destination_exists` avoids unnecessary 
resolution and `RuntimeError` on broken/cyclic destination symlinks (since 
`destination_entry` already resolves `destination_path.parent`).
   
   ```suggestion
                 for output in ((destination_entry, destination_path.resolve()) 
if destination_exists else (destination_entry,))
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to