Skip to content

fix: handle file names that aren't valid UTF-8 - #94

Merged
shcheklein merged 2 commits into
mainfrom
fix/non-utf8-names
Nov 29, 2026
Merged

shcheklein merged 2 commits into
mainfrom
fix/non-utf8-names

Conversation

@shcheklein

Copy link
Copy Markdown
Collaborator

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 was path_encoding=None, which makes every path bytes. str paths then stop working, and only some methods accept bytes (#84).

This sets asyncssh's path_errors to "surrogateescape" by default. Such names come back as str with surrogate escapes (b"name-\xff" becomes "name-\udcff"), the way os.listdir returns them, and encode back to the same bytes when passed in. Names that are valid UTF-8 decode as before, and a path_errors passed in sftp_client_kwargs still wins.

cp_file and checksum now 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, so these names raised UnicodeEncodeError.
  • With 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) new default
ls, find SFTPBadMessage work
info, exists, cat_file, open, walk, glob — work
cp_file, mv, checksum, pipe_file, makedirs, put, rm — work, and the server-side names keep their original bytes

The 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

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>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Comment thread sshfs/spec.py Resolved
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>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

There are no active review issues for the non-UTF-8 path-handling changes.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@shcheklein
shcheklein merged commit 5afc52a into main Nov 29, 2026
11 checks passed
@shcheklein
shcheklein deleted the fix/non-utf8-names branch November 29, 2026 17:39
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.

Bytes paths (path_encoding=None) only work in some methods

2 participants