You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Crashes on filenames that are not UTF-8 (listdir, maybe others) #546
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.
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)
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).
@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.
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.
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.
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.
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.
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...
#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.
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.
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.
@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.
Activity
pkaczynski commentedon Nov 2, 2015
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)
Probable fix of paramiko#546 converting (or not) to unicode of msg.ge…
pkaczynski commentedon Nov 2, 2015
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.
pkaczynski commentedon Nov 3, 2015
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).
Russell-Jones commentedon Nov 12, 2015
@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 == Truewhist it is false on Python 3, where str is more like Python 2's unicode.fabiobatalha commentedon Apr 20, 2016
Any news on this bug?
RepoFarmer commentedon Jul 1, 2016
Still seems to be an issue here in version 2.0.1.
29 remaining items
keyroii commentedon Aug 25, 2020
Can we get some speed behind this. This is still a much awaited functionality which is still missing since 2015!
hakaishi commentedon Aug 25, 2020
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.
keyroii commentedon Aug 27, 2020
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.
todd-pettit commentedon Sep 29, 2020
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.
psimons2 commentedon Apr 28, 2021
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.
hakaishi commentedon Apr 28, 2021
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...
griseau commentedon Jun 1, 2021
@bitprophet any update on this ? This is still an annoying issue ...
JavierLopezT commentedon Dec 20, 2021
Same problem here
bitprophet commentedon Mar 4, 2022
#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.
hakaishi commentedon Mar 4, 2022
I missed adding the necessary changes for the docu. I plan add them this weekend.
hakaishi commentedon Mar 8, 2022
I just added necessary demo/docu changes.
cheers
hakaishi commentedon Mar 14, 2022
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
Inndy commentedon Jul 1, 2022
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_sftpMessage.__init__SFTPClient.__init__SFTPClient.from_transportTransport.open_sftp_clientSince 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.
hakaishi commentedon Jul 22, 2022
@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.