From 014eea2ebddb63e75453b008d426f9ce9db633f3 Mon Sep 17 00:00:00 2001 From: Ivan Shcheklein Date: Sat, 28 Nov 2026 14:47:33 -0800 Subject: [PATCH 1/2] fix: pass -- before paths in cp, md5sum and md5 Quoting a path for the shell doesn't stop cp, md5sum or md5 from parsing a name that starts with "-" as an option: cp_file("-r", "copy") ran "cp -r copy" and failed with a usage error. GNU and BSD cp and md5sum, and macOS md5, all accept "--" to end the options. Co-Authored-By: Claude Opus 5.5 --- sshfs/spec.py | 4 ++-- tests/test_sshfs.py | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/sshfs/spec.py b/sshfs/spec.py index 2338040..4b9f257 100644 --- a/sshfs/spec.py +++ b/sshfs/spec.py @@ -280,7 +280,7 @@ async def _get_file( async def _cp_file(self, lpath, rpath, **kwargs): lpath = self._strip_protocol(lpath) rpath = self._strip_protocol(rpath) - cmd = f"cp {shlex.quote(lpath)} {shlex.quote(rpath)}" + cmd = f"cp -- {shlex.quote(lpath)} {shlex.quote(rpath)}" await self._execute(cmd) @wrap_exceptions @@ -379,7 +379,7 @@ async def _checksum(self, path): else: raise ValueError(f"{system!r} doesn't support checksum operation") - cmd = f"{command} {shlex.quote(path)}" + cmd = f"{command} -- {shlex.quote(path)}" result = await self._execute(cmd) parts = result.stdout.strip().split() diff --git a/tests/test_sshfs.py b/tests/test_sshfs.py index 494f523..8106a34 100644 --- a/tests/test_sshfs.py +++ b/tests/test_sshfs.py @@ -282,6 +282,18 @@ def test_checksum(fs, remote_dir): assert fs.checksum(remote_dir + "/a.txt") == checksum +def test_shell_commands_with_leading_dash(fs, tmp_path, monkeypatch): + # A relative path is the only way to start a name with "-". The mock + # server resolves it against the current directory. + monkeypatch.chdir(tmp_path) + (tmp_path / "-r").write_bytes(b"data") + + fs.cp_file("-r", "-r-copy") + + assert (tmp_path / "-r-copy").read_bytes() == b"data" + assert fs.checksum("-r") == hashlib.md5(b"data").hexdigest() + + def test_ls(fs, remote_dir, plain_remote_dir): fs.mkdir(remote_dir + "dir/") files = set() From a7c31f8f21060db7482df9e2e7ea668ccf49a121 Mon Sep 17 00:00:00 2001 From: Ivan Shcheklein Date: Sat, 28 Nov 2026 17:12:43 -0800 Subject: [PATCH 2/2] fix: prefix paths starting with - with ./ instead of passing -- md5sum and md5 read stdin for a "-" argument even after "--", so checksum("-") hung, because the command's stdin is never closed. A "./" prefix keeps both "-" and option-like names such as "-r" from being parsed by cp, md5sum and md5. The test is split into a cp_file test and a checksum test, each next to its existing test. The checksum test covers "-" and has a timeout, so a regression fails instead of hanging. Co-Authored-By: Claude Opus 5.5 --- sshfs/spec.py | 12 ++++++++++-- tests/test_sshfs.py | 23 ++++++++++++++++------- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/sshfs/spec.py b/sshfs/spec.py index 4b9f257..d054854 100644 --- a/sshfs/spec.py +++ b/sshfs/spec.py @@ -34,6 +34,14 @@ _DEFAULT_MAX_SESSIONS = 10 +def _quote(path): + """Quote a path for a POSIX shell as a command's file argument.""" + # Without "./", commands read "-r" as an option and "-" as stdin + if path.startswith("-"): + path = "./" + path + return shlex.quote(path) + + class SSHFileSystem(AsyncFileSystem): def __init__( self, @@ -280,7 +288,7 @@ async def _get_file( async def _cp_file(self, lpath, rpath, **kwargs): lpath = self._strip_protocol(lpath) rpath = self._strip_protocol(rpath) - cmd = f"cp -- {shlex.quote(lpath)} {shlex.quote(rpath)}" + cmd = f"cp {_quote(lpath)} {_quote(rpath)}" await self._execute(cmd) @wrap_exceptions @@ -379,7 +387,7 @@ async def _checksum(self, path): else: raise ValueError(f"{system!r} doesn't support checksum operation") - cmd = f"{command} -- {shlex.quote(path)}" + cmd = f"{command} {_quote(path)}" result = await self._execute(cmd) parts = result.stdout.strip().split() diff --git a/tests/test_sshfs.py b/tests/test_sshfs.py index 8106a34..319edde 100644 --- a/tests/test_sshfs.py +++ b/tests/test_sshfs.py @@ -254,6 +254,15 @@ def test_copy(fs, remote_dir): assert strip_keys(initial_info) == strip_keys(secondary_info) +def test_cp_file_leading_dash(fs, tmp_path, monkeypatch): + # A relative path is the only way to start a name with "-". The mock + # server resolves it against the current directory. + monkeypatch.chdir(tmp_path) + (tmp_path / "-r").write_bytes(b"data") + fs.cp_file("-r", "-r-copy") + assert (tmp_path / "-r-copy").read_bytes() == b"data" + + def test_rm(fs, remote_dir): fs.touch(remote_dir + "/a.txt") fs.rm(remote_dir + "/a.txt") @@ -282,16 +291,16 @@ def test_checksum(fs, remote_dir): assert fs.checksum(remote_dir + "/a.txt") == checksum -def test_shell_commands_with_leading_dash(fs, tmp_path, monkeypatch): +@pytest.mark.parametrize("name", ["-", "-r"]) +def test_checksum_leading_dash(fs, tmp_path, monkeypatch, name): # A relative path is the only way to start a name with "-". The mock # server resolves it against the current directory. monkeypatch.chdir(tmp_path) - (tmp_path / "-r").write_bytes(b"data") - - fs.cp_file("-r", "-r-copy") - - assert (tmp_path / "-r-copy").read_bytes() == b"data" - assert fs.checksum("-r") == hashlib.md5(b"data").hexdigest() + (tmp_path / name).write_bytes(b"data") + # md5sum and md5 read stdin when they get "-" or no file, and the + # command's stdin is never closed, so a regression hangs without timeout + checksum = fs.checksum(name, timeout=10) + assert checksum == hashlib.md5(b"data").hexdigest() def test_ls(fs, remote_dir, plain_remote_dir):