From 42fc3aaa71411daed60544e3b40179c1038bed5d Mon Sep 17 00:00:00 2001
From: Remi Rampin <remi@rampin.org>
Date: Tue, 6 Oct 2026 16:48:45 -0400
Subject: [PATCH] Catch erroneous server paths when downloading

---
 scp.py | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

Index: scp-0.13.2/scp.py
===================================================================
--- scp-0.13.2.orig/scp.py
+++ scp-0.13.2/scp.py
@@ -214,7 +214,7 @@ class SCPClient(object):
         if not isinstance(remote_path, (list, tuple)):
             remote_path = [remote_path]
         remote_path = [self.sanitize(asbytes(r)) for r in remote_path]
-        self._recv_dir = local_path or os.getcwd()
+        self._recv_dir = os.path.realpath(local_path or os.getcwd())
         self._depth = 0
         self._rename = (len(remote_path) == 1 and
                         not os.path.isdir(os.path.abspath(local_path)))
@@ -415,10 +415,14 @@ class SCPClient(object):
                 name = parts[2].decode('utf-8')
                 assert not os.path.isabs(name)
                 path = os.path.join(asunicode_win(self._recv_dir), name)
+                if hasattr(os.path, 'commonpath'):
+                    assert os.path.commonpath([os.path.realpath(path), asunicode_win(self._recv_dir)]) == asunicode_win(self._recv_dir)
             else:
                 name = parts[2]
                 assert not os.path.isabs(name)
                 path = os.path.join(asbytes(self._recv_dir), name)
+                if hasattr(os.path, 'commonpath'):
+                    assert os.path.commonpath([os.path.realpath(path), asbytes(self._recv_dir)]) == asbytes(self._recv_dir)
         except:
             chan.send('\x01')
             chan.close()
@@ -495,7 +499,7 @@ class SCPClient(object):
                 raise SCPException('%s: Not a directory' % path)
             self._dirtimes[path] = (self._utime)
             self._utime = None
-            self._recv_dir = path
+            self._recv_dir = os.path.realpath(path)
         except (OSError, SCPException) as e:
             self.channel.send(b'\x01' + asbytes(str(e)))
             raise
