net-ssh / net-sftp

Pure Ruby implementation of an SFTP (protocols 1-6) client.
http://net-ssh.github.io/
MIT License
287 stars 131 forks source link

does not take UTF-8 more byte character length into account when writing data to a file #133

Open msproact opened 2 years ago

msproact commented 2 years ago

I have:

        begin
            # Does not seem to correctly set permissions on open
            # https://stackoverflow.com/questions/7713850/unable-to-create-file-with-correct-permissions-using-netssh-library
            filehandle = @sftp.open!(newauthkeyfile, 'w', :permissions => 0600)
            @sftp.fsetstat!(filehandle, {:permissions => 0600})
            # Does not take a permissions argument this way
            @sftp.file.open(newauthkeyfile , "w" ) do | file |
                authkeys=["Dozentenschlüssel", "1234567890"]
                authkeys.each do | line |
                    puts line
                    file.puts line
                    wantedsize += line.length + 1
                end
            end
            @sftp.loop
        rescue Net::SFTP::StatusException => exception
            puts    exception.message
            puts "ERROR: Can't open authorized_keys for writing! Skipped."
            return
        end

(source from https://github.com/proact-de/distkeys with some minor adaptions made during debugging this)

which should produce:

Dozentenschlüssel
1234567890

But it produces:

root@debiantemplate:~# cat .ssh/authorized_keys
Dozentenschlüsse
1234567890

As hexdump:

root@debiantemplate:~# hd .ssh/authorized_keys
00000000  44 6f 7a 65 6e 74 65 6e  73 63 68 6c c3 bc 73 73  |Dozentenschl..ss|
00000010  65 0a 31 32 33 34 35 36  37 38 39 30 0a           |e.1234567890.|
0000001d

Just using "puts" to terminal produces the correct result. I bet Net:SFTP overloads the method and it has to do with the SFTP specific implementation.

If you use "Dozentenschlüüssel" instead then two bytes are missing.

I did not check whether the setting permissions issue I worked around in my code is still there or not.