Repository navigation
fix: handle file names that aren't valid UTF-8 - #94
Merged
Merged
Conversation
asyncssh decodes SFTP names as strict UTF-8 by default, so listing a
directory with a name that isn't valid UTF-8 failed with
SFTPBadMessage("Unable to decode name"). path_errors now defaults to
"surrogateescape": such names come back as str with surrogate escapes,
the way os.listdir returns them, and encode back to the same bytes.
Names that are valid UTF-8 decode as before, and a path_errors passed in
sftp_client_kwargs still wins.
cp_file and checksum send their commands as bytes, encoded with the
same path_encoding and path_errors as the SFTP client. asyncssh encodes
a str command as strict UTF-8, which failed for these names and named a
different file when path_encoding isn't UTF-8. Their output is decoded
with errors="replace", because failing to decode it strictly closes
the SSH connection.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
🟡 Changes recommended
Encoding pathnames after shell quoting can introduce unescaped syntax in cp_file and checksum, allowing crafted filenames to execute unintended remote commands.
1 open issue
What changed in this PR
This PR makes non-UTF-8 remote filenames usable as string paths in SSHFileSystem, reducing reliance on its limited bytes-path support.
Changes:
- Defaults SFTP filename handling to
surrogateescape, while preserving explicit overrides. - Encodes copy and checksum commands using the configured path encoding and tolerates undecodable output.
- Documents filename behavior and adds regression tests for non-UTF-8 names and encoding options.
| File | Description |
|---|---|
tests/test_sshfs.py |
Adds coverage for encoding options, non-UTF-8 shell operations, and connection survival after errors. |
sshfs/spec.py |
Updates encoding defaults, shell-command handling, and filename documentation. |
Review effort: Balanced
The command was quoted as str and then encoded with path_encoding. Some encodings, such as HZ and ISO-2022-JP, encode non-ASCII characters as ASCII bytes that include a quote, so a name could end the quoted argument and run the rest as a command. Each path is now encoded first and the bytes are quoted, with the program name and shell syntax kept as ASCII bytes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

asyncssh decodes SFTP names as strict UTF-8 by default, so listing a directory that contains a name that isn't valid UTF-8 fails with
SFTPBadMessage: Unable to decode name(#39). Until now the only workaround waspath_encoding=None, which makes every path bytes. str paths then stop working, and only some methods accept bytes (#84).This sets asyncssh's
path_errorsto"surrogateescape"by default. Such names come back as str with surrogate escapes (b"name-\xff"becomes"name-\udcff"), the wayos.listdirreturns them, and encode back to the same bytes when passed in. Names that are valid UTF-8 decode as before, and apath_errorspassed insftp_client_kwargsstill wins.cp_fileandchecksumnow send their commands as bytes, encoded with the samepath_encodingandpath_errorsas the SFTP client:UnicodeEncodeError.path_encoding="latin-1",checksum("café")sent the UTF-8 bytes and named a different file.Their output is decoded with
errors="replace": the output can include such a name, and failing to decode it strictly closes the SSH connection.Checked against OpenSSH 10.0 in Docker, with names
b"name-\xff",b"dir-\xff/inner-\xfe"and"café":path_errors="strict"(old default)ls,findSFTPBadMessageinfo,exists,cat_file,open,walk,globcp_file,mv,checksum,pipe_file,makedirs,put,rmThe mock server encodes SFTP names as strict UTF-8, so the tests can only cover the shell commands. The tests with an existing non-UTF-8 name run on Linux only, because macOS rejects such names.
Supersedes #86 and closes #84: bytes paths are no longer needed for these servers.
🤖 Generated with Claude Code