Skip to content

Crashes on filenames that are not UTF-8 (listdir, maybe others) #546

Open

Description

In Obnam, my backup program, I use Paramiko to access files over SFTP. Mostly, Obnam users use this to access the backup repository, but some use it to access their live data. Live data files have all sorts of nasty filenames, and someone has just reported problems with non-UTF-8 filenames.

To reproduce this with Obnam 1.9 on Debian 8 (jessie):

$ mkdir data
$ touch data/"$(printf '\xff')"
$ obnam backup --no-default-config -r repo sftp://localhost$(pwd)/data
00h00m00s 1 files 0 B scanned: scanning for files in sftp://localhost/tmp/x/dataTraceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/cliapp/app.py", line 190, in _run
    self.process_args(args)
...
  File "/usr/lib/python2.7/dist-packages/obnamlib/plugins/sftp_plugin.py", line 455, in listdir2
    attrs = self.sftp.listdir_attr(pathname)
  File "/usr/lib/python2.7/dist-packages/paramiko/sftp_client.py", line 208, in listdir_attr
    filename = msg.get_text()
  File "/usr/lib/python2.7/dist-packages/paramiko/message.py", line 199, in get_text
    return u(self.get_bytes(self.get_size()))
  File "/usr/lib/python2.7/dist-packages/paramiko/py3compat.py", line 51, in u
    return s.decode(encoding)
  File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 0: invalid start byte

What Obnam does is to get the list of files using listdir_attr, and that one fails, if there's a non-UTF8 filename on the remote side.

Activity

  1. pkaczynski commented on Nov 2, 2015

    @pkaczynski

    One should have an option to select encoding of the server, for example I have a serverthet returns list of files in cp1250. I get exatly the same error in get_text. There should be encoding passed to u() function (from py3compat)

  2. added a commit that references this issue on Nov 2, 2015
  3. pkaczynski commented on Nov 2, 2015

    @pkaczynski

    Yes, the thing is there is no way to get the list as byte strings. listdir tries to convert all entries to utf-8, no questions asked.

  4. pkaczynski commented on Nov 3, 2015

    @pkaczynski

    Yes, it's paramiko. However, the client is unusable, since listdir uses get_text() for getting names of files (and folders) in a path instead of get_str(). If the server serves them in encoding other than utf-8, listdir crashes.

    I think the solution should be trying to convert to ascii, then to utf-8. If that fails, listdir should return plain bytes it received from server. There is even a function written in sftp_client.py - _to_unicode. Please review my commit to this file in my fork of this repo (I did other changes too, which were wrong, so sorry).

  5. Russell-Jones commented on Nov 12, 2015

    @Russell-Jones

    @larswirzenius I agree that bytes should be available, but that should be an opt-in, not the default. Two similar cases in stdlib that I can think of are the functions in the os module such as getcwdb(), environb() and getenvb() and open(..., "r")/open(..., "rb"). It should be possible to specify either an encoding or that bytes should be returned. One possibility would be having listdir() and listdirb(). It would also be useful to add an "encoding" keyword parameter to SFTPClient.from_transport() and other relevant methods for non-utf8 encodings (utf8 should be the default, as it will work in most cases). Note that on Python 2 str is bytes == True whist it is false on Python 3, where str is more like Python 2's unicode.

  6. fabiobatalha commented on Apr 20, 2016

    @fabiobatalha

    Any news on this bug?

  7. RepoFarmer commented on Jul 1, 2016

    @RepoFarmer

    Still seems to be an issue here in version 2.0.1.

  8. 29 remaining items

  9. keyroii commented on Aug 25, 2020

    @keyroii

    Can we get some speed behind this. This is still a much awaited functionality which is still missing since 2015!

  10. hakaishi commented on Aug 25, 2020

    @hakaishi

    Just try my fork, it will work for all encodings.
    Not as beautiful as it could be, but it works flawlessly.
    I already created a PR, but there has been no response of any kind.
    I covered stat(), open(), utime(), listdir(), listdir_attr() and many more.

  11. keyroii commented on Aug 27, 2020

    @keyroii

    Just try my fork, it will work for all encodings.
    Not as beautiful as it could be, but it works flawlessly.
    I already created a PR, but there has been no response of any kind.
    I covered stat(), open(), utime(), listdir(), listdir_attr() and many more.

    For now I will be using your fork, thanks for creating a fix for this problem. I hope this will get merged soon since now I will be using the original paramiko and the forked one under another name at the same time.

  12. todd-pettit commented on Sep 29, 2020

    @todd-pettit

    I'm experiencing this problem right now. Migrating files from an on prem windows SFTP server to a cloud Linux based SFTP server. Every special character (damn all who name folders and files with special characters) blows up my script. Cant change the folder names because it will break existing automated processes for us and our clients. This issue might not crop up often, but it is making this library unusable for me.

  13. psimons2 commented on Apr 28, 2021

    @psimons2

    Why don't you just use surrogateescape in py3compat.b() and u() and it will at least work for any filenames in python 3.
    Like s.encode(encoding, 'surrogateescape') and s.decode(encoding, 'surrogateescape').
    I currently monkey patch b() and u() to get it to work with mixed encodings.

  14. hakaishi commented on Apr 28, 2021

    @hakaishi

    What ever the solution, the authors keep ignoring them all. I already gave up on paramiko and went over to ssh2-python. It's much faster as well. But not as easy to use though.
    This issue here is quite a huge drawback, I wonder why nothing is done. Maybe they want to completely re-write something including a solution...

  15. griseau commented on Jun 1, 2021

    @griseau

    @bitprophet any update on this ? This is still an annoying issue ...

  16. added this to the p1 milestone on Jun 4, 2021
  17. JavierLopezT commented on Dec 20, 2021

    @JavierLopezT

    Same problem here

  18. bitprophet commented on Mar 4, 2022

    @bitprophet
    Member

    #1995 is the latest from @hakaishi on this - I haven't had time to read closely but offhand it looks like a decent solution to this (which has been in a milestone for a long time 😢 ). I have other even more pressing feature overhauls to do around here (auth in particular is in an awful place) but this is the sort of thing I still want to tackle in subsequent feature upgrades.

  19. hakaishi commented on Mar 4, 2022

    @hakaishi

    I missed adding the necessary changes for the docu. I plan add them this weekend.

  20. hakaishi commented on Mar 8, 2022

    @hakaishi

    I just added necessary demo/docu changes.
    cheers

  21. hakaishi commented on Mar 14, 2022

    @hakaishi

    New update. I simplified a lot of code by moving the encoding/errors parameters to the SFTPClient constructor and a static class method.

    In other levels of the API nothing changed. A merit is that most of the SFTPClient API will be as it was until now, thus reducing confusion and incompatibilities. The parameters are optional, which will allow full backward compatibility.

    Cheers

  22. Inndy commented on Jul 1, 2022

    @Inndy

    Hello, I've submit a PR to solve this issue last year but have not received any comment or review.
    My approach is to add optional argument to following constructor/function:

    • SSHClient.open_sftp
    • Message.__init__
    • SFTPClient.__init__
    • SFTPClient.from_transport
    • Transport.open_sftp_client

    Since the argument is optional and set to UTF-8 by default, these patches will not affect original API.
    Please have a look at #1914 if you have ran into same problem when dealing with non-UTF8 encoding SFTP server.

  23. hakaishi commented on Jul 22, 2022

    @hakaishi

    @Inndy Would you mind if I merge some of your changes into my PR? We do almost the same thing, but I seem to have missed two or three spots. Your changes on the other hand leave a lot of other API untouched where the encoding would be needed too.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions