Skip to content

Conversation

@fabi200123
Copy link
Contributor

Fix UnicodeDecodeError with non-UTF-8 filenames in SSH operations

This PR fixes a critical bug where migrations fail with UnicodeDecodeError
when source systems contain files or directories with non-UTF-8 characters in
their names.

The fix centralizes UTF-8 decoding with error handling in exec_ssh_cmd(),
which now returns decoded strings with errors='replace' instead of raw bytes.
Invalid UTF-8 sequences are replaced with the character, allowing migrations
to continue while logging warnings for monitoring.

Changes:

  • Modified exec_ssh_cmd() to return strings with safe decoding
  • Added SFTP fallback in list_ssh_dir() for encoding errors
  • Removed all .decode() calls on command execution results (15 locations)
  • Updated test expectations to match new return type
  • Preserved SFTP file reading operations (still return bytes)

Impact:

  • Prevents migration failures on systems with non-UTF-8 filenames
  • No breaking changes to external APIs
  • Minimal performance overhead
  • Backward compatible (except test mocks)

repos_list = self._exec_cmd_chroot(
"zypper repos -u | awk -F '|' '/^\s[0-9]+/ {print $2 $7}'"
).decode()
)
Copy link
Contributor

Choose a reason for hiding this comment

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

NIT: can you please put this paranthesis on the end of the previous line?

LOG.warning(
"SFTP listdir failed due to non-UTF-8 filename, "
"falling back to shell command. Error: %s", str(ex))
output = exec_ssh_cmd(ssh, "ls -1 %s" % remote_path, get_pty=True)
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd prefer to also avoid permission issues, so please add a sudo to the command.

try:
sftp = ssh.open_sftp()
return sftp.listdir(remote_path)
except UnicodeDecodeError as ex:
Copy link
Contributor

Choose a reason for hiding this comment

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

As mentioned below, I'd like to also avoid permission issues, not just decode errors. Catch Exception here, and then attempt the sudo ls -1 command. Make sure to warn about the exception. Use get_exception_details() instead of str(ex) as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants